2121class MemberApp (App ):
2222 """Single app that toggles between a file list and a form while connected to a GitHub fork+push flow."""
2323
24- def __init__ (self , token : str , original_repo : Repository ) -> None :
24+ def __init__ (self , repo_path : str ) -> None :
2525 super ().__init__ ()
26- self .token = token
27- self .original_repo = original_repo
26+ self .repo_path = repo_path
2827
2928 def compose (self ) -> ComposeResult :
3029 # Two main containers: self.list_container for the file list, self.form_container for the form.
@@ -35,23 +34,7 @@ def compose(self) -> ComposeResult:
3534 yield self .form_container
3635
3736 def on_mount (self ) -> None :
38- """Perform setup: do fork if needed, set up UI."""
39- self .forked_repo = self .original_repo .create_fork ()
40- self .FORKED_REPO_URL = self .forked_repo .clone_url
41- self .REPO_PATH = user_data_dir (
42- appname = "edit-python-pe" , appauthor = "python.pe"
43- )
44-
45- if not os .path .exists (self .REPO_PATH ):
46- callbacks = pygit2 .callbacks .RemoteCallbacks (
47- credentials = pygit2 .UserPass (self .token , "x-oauth-basic" )
48- )
49- sleep (3 )
50- pygit2 .clone_repository (
51- self .FORKED_REPO_URL , self .REPO_PATH , callbacks = callbacks
52- )
53-
54- # 2) Build the list portion
37+ # 1) Build the list portion
5538 self .list_title = Static ("Archivos en 'blog/members':" )
5639 self .list_view = ListView ()
5740 self .quit_list_button = Button ("Salir" , id = "quit_list" )
@@ -63,13 +46,13 @@ def on_mount(self) -> None:
6346 self .list_container .mount (self .quit_list_button )
6447
6548 md_files = glob .glob (
66- os .path .join (self .REPO_PATH , "blog" , "members" , "*.md" )
49+ os .path .join (self .repo_path , "blog" , "members" , "*.md" )
6750 )
6851 for f in md_files :
6952 basename = os .path .basename (f )
7053 self .list_view .append (ListItem (Static (basename )))
7154
72- # 3 ) Build the form portion, hidden at first
55+ # 2 ) Build the form portion, hidden at first
7356 self .form_header = Static ("Formulario de Miembro" , classes = "header" )
7457 self .name_input = Input (placeholder = "Nombre" )
7558 self .email_input = Input (placeholder = "Correo electrónico" )
@@ -175,7 +158,7 @@ def on_list_view_selected(self, event: ListView.Selected) -> None:
175158 self .show_form ()
176159
177160 def load_file_into_form (self , filename : str ) -> None :
178- path_md = os .path .join (self .REPO_PATH , "blog" , "members" , filename )
161+ path_md = os .path .join (self .repo_path , "blog" , "members" , filename )
179162 if not os .path .exists (path_md ):
180163 return
181164 try :
@@ -494,20 +477,20 @@ def save_member(self) -> None:
494477
495478 # Write file
496479 file_path = os .path .join (
497- self .REPO_PATH , "blog" , "members" , f"{ name_file } .md"
480+ self .repo_path , "blog" , "members" , f"{ name_file } .md"
498481 )
499482 else :
500483 name_file = self .current_file
501484 file_path = os .path .join (
502- self .REPO_PATH , "blog" , "members" , f"{ name_file } "
485+ self .repo_path , "blog" , "members" , f"{ name_file } "
503486 )
504487 os .makedirs (os .path .dirname (file_path ), exist_ok = True )
505488 with open (file_path , "w" , encoding = "utf-8" ) as f :
506489 f .write (md_content )
507490
508491 # commit & push
509- repo = pygit2 .Repository (self .REPO_PATH )
510- rel_path = os .path .relpath (file_path , self .REPO_PATH )
492+ repo = pygit2 .Repository (self .repo_path )
493+ rel_path = os .path .relpath (file_path , self .repo_path )
511494 rel_path = pathlib .Path (rel_path ).as_posix () # Force path to POSIX format so Windows backslashes (\) don't break pygit2
512495 repo .index .add (rel_path )
513496 repo .index .write ()
@@ -592,7 +575,7 @@ async def on_event(self, event: Event) -> None:
592575
593576
594577def get_repo () -> tuple [str , Repository ]:
595- token : str = getpass .getpass (
578+ token = getpass .getpass (
596579 "Por favor ingrese su access token personal de GitHub: "
597580 )
598581 g = Github (token )
@@ -609,9 +592,28 @@ def get_repo() -> tuple[str, Repository]:
609592 exit (1 )
610593
611594
595+ def fork_repo (token : str , original_repo : Repository ) -> str :
596+ forked_repo = original_repo .create_fork ()
597+ forked_repo_url = forked_repo .clone_url
598+ repo_path = user_data_dir (
599+ appname = "edit-python-pe" , appauthor = "python.pe"
600+ )
601+
602+ if not os .path .exists (repo_path ):
603+ callbacks = pygit2 .callbacks .RemoteCallbacks (
604+ credentials = pygit2 .UserPass (token , "x-oauth-basic" )
605+ )
606+ sleep (3 )
607+ pygit2 .clone_repository (
608+ forked_repo_url , repo_path , callbacks = callbacks
609+ )
610+ return repo_path
611+
612+
612613def main () -> None :
613614 token , original_repo = get_repo ()
614- app = MemberApp (token = token , original_repo = original_repo )
615+ repo_path = fork_repo (token , original_repo )
616+ app = MemberApp (repo_path )
615617 app .run ()
616618
617619
0 commit comments