44import os
55import pathlib
66import re
7- from datetime import datetime
7+ from datetime import date , datetime
88from time import sleep
99
1010import pygit2
@@ -83,7 +83,7 @@ def on_mount(self) -> None:
8383 self .back_button = Button ("Atrás" , id = "back" )
8484 self .quit_button = Button ("Salir" , id = "quit" )
8585
86- # Mount them in the form container
86+ # 3) Mount them in the form container
8787 self .form_container .mount (self .form_header )
8888 self .form_container .mount (self .name_input )
8989 self .form_container .mount (self .email_input )
@@ -186,10 +186,10 @@ def load_file_into_form(self, filename: str) -> None:
186186 if not os .path .exists (path_md ):
187187 return
188188 try :
189- with open (path_md , "r" , encoding = "utf-8" ) as fh :
190- content = fh .read ()
189+ with open (path_md , "r" , encoding = "utf-8" ) as fd :
190+ content = fd .read ()
191191 except Exception as e :
192- self .exit (f"Error loading file { filename } : { e } " )
192+ self .exit (f"Error al leer el archivo { filename } : { e } " )
193193 return
194194
195195 self .clear_form ()
@@ -308,6 +308,18 @@ def on_button_pressed(self, event: Button.Pressed) -> None:
308308
309309 def add_social_entry (self ) -> None :
310310 class SocialEntry (Horizontal ):
311+ DEFAULT_CSS = """
312+ SocialEntry Select {
313+ width: 25%;
314+ }
315+ SocialEntry Input {
316+ width: 50%;
317+ }
318+ SocialEntry Button {
319+ width: 25%;
320+ }
321+ """
322+
311323 def __init__ (se , index ):
312324 super ().__init__ ()
313325 se .index = index
@@ -348,7 +360,16 @@ def remove_social_entry(self, index: int) -> None:
348360 found .remove ()
349361
350362 def add_alias_entry (self ) -> None :
351- class AliasEntryRow (Horizontal ):
363+ class AliasEntry (Horizontal ):
364+ DEFAULT_CSS = """
365+ AliasEntry Input {
366+ width: 75%;
367+ }
368+ AliasEntry Button {
369+ width: 25%;
370+ }
371+ """
372+
352373 def __init__ (se , index ):
353374 super ().__init__ ()
354375 se .index = index
@@ -359,7 +380,7 @@ def compose(se) -> ComposeResult:
359380 yield se .alias_input
360381 yield se .delete_btn
361382
362- row = AliasEntryRow (self .alias_index )
383+ row = AliasEntry (self .alias_index )
363384 self .alias_index += 1
364385 self .alias_entries .append (row )
365386 self .alias_container .mount (row )
@@ -403,16 +424,15 @@ def save_member(self) -> None:
403424 md_lines = [
404425 "---" ,
405426 "blogpost: true" ,
406- f"author: { name } " ,
427+ f"date: { date .today ().strftime ("%d %b, %Y" )} " ,
428+ f"author: { get_alias (aliases , name )} " ,
407429 f"location: { city } " ,
408430 "category: members" ,
409431 "language: Español" ,
410432 "image: 1" ,
411433 "excerpt: 1" ,
412434 "---" ,
413435 "" ,
414- "% NOTA: No olvidar poner la fecha de publicación debajo de `blogpost: true`" ,
415- "" ,
416436 f"# { name } " ,
417437 "" ,
418438 f"```{{gravatar}} { email } " ,
@@ -553,18 +573,28 @@ def compute_file_name(aliases: list[str], name: str, email: str) -> str:
553573 return f"{ alias_for_name } -{ sha_hash } .md"
554574
555575
576+ def read_file (file_path : str ) -> str :
577+ with open (file_path , "r" , encoding = "utf-8" ) as fd :
578+ return fd .read ()
579+
580+
581+ def append_file (file_content : str , file_path : str ) -> None :
582+ os .makedirs (os .path .dirname (file_path ), exist_ok = True )
583+ with open (file_path , "a" , encoding = "utf-8" ) as fd :
584+ fd .write (file_content )
585+
586+
556587def write_file (file_content : str , file_path : str ) -> None :
557588 os .makedirs (os .path .dirname (file_path ), exist_ok = True )
558- with open (file_path , "w" , encoding = "utf-8" ) as f :
559- f .write (file_content )
589+ with open (file_path , "w" , encoding = "utf-8" ) as fd :
590+ fd .write (file_content )
560591
561592
562593def commit_and_push (
563594 repo_path : str ,
564595 token : str ,
565596 was_changed : bool ,
566597 name_file : str ,
567- file_path : str ,
568598 name : str ,
569599 email : str ,
570600) -> tuple [
@@ -574,11 +604,7 @@ def commit_and_push(
574604 pygit2 .callbacks .RemoteCallbacks ,
575605]:
576606 repo = pygit2 .repository .Repository (repo_path )
577- rel_path = os .path .relpath (file_path , repo_path )
578- rel_path = pathlib .Path (
579- rel_path
580- ).as_posix () # Force path to POSIX format so Windows backslashes (\) don't break pygit2
581- repo .index .add (rel_path )
607+ repo .index .add_all ()
582608 repo .index .write ()
583609 author_sig = pygit2 .Signature (name or "Unknown" , email or "unknown@email" )
584610 tree_id = repo .index .write_tree ()
@@ -598,17 +624,20 @@ def commit_and_push(
598624 return commit_msg , repo , remote , callbacks
599625
600626
601- def create_pr (
627+ def get_alias (aliases : list [str ], name : str ) -> str :
628+ if aliases :
629+ return aliases [0 ]
630+ return name
631+
632+
633+ def create_member_file (
602634 file_content : str ,
603635 current_file : str | None ,
604636 repo_path : str ,
605- original_repo : Repository ,
606- forked_repo : Repository ,
607- token : str ,
608637 aliases : list [str ],
609638 name : str ,
610639 email : str ,
611- ) -> str :
640+ ) -> tuple [ str , str ] :
612641 # Name the file
613642 name_file = (
614643 current_file
@@ -619,14 +648,41 @@ def create_pr(
619648 # Write file
620649 file_path = os .path .join (repo_path , "blog" , "members" , name_file )
621650 write_file (file_content , file_path )
651+ return name_file , file_path
652+
653+
654+ def write_authors_file (
655+ repo_path : str , aliases : list [str ], name : str , email : str
656+ ):
657+ file_path = os .path .join (repo_path , "AUTHORS" )
658+ contents = read_file (file_path )
659+ file_content = f"\n { name } ({ get_alias (aliases , name )} ) <{ email } >"
660+ if file_content .strip () not in contents :
661+ append_file (file_content , file_path )
662+
663+
664+ def create_pr (
665+ file_content : str ,
666+ current_file : str | None ,
667+ repo_path : str ,
668+ original_repo : Repository ,
669+ forked_repo : Repository ,
670+ token : str ,
671+ aliases : list [str ],
672+ name : str ,
673+ email : str ,
674+ ) -> str :
675+ name_file , file_path = create_member_file (
676+ file_content , current_file , repo_path , aliases , name , email
677+ )
678+ write_authors_file (repo_path , aliases , name , email )
622679
623680 # commit & push
624681 commit_msg , repo , remote , callbacks = commit_and_push (
625682 repo_path ,
626683 token ,
627684 current_file is not None ,
628685 name_file ,
629- file_path ,
630686 name ,
631687 email ,
632688 )
0 commit comments