@@ -594,6 +594,23 @@ def _get_mappings(self):
594594 return mappings
595595
596596
597+ def _init_catalog (* , input_file , output_file , locale : Locale , width : int ) -> None :
598+ with open (input_file , 'rb' ) as infile :
599+ # Although reading from the catalog template, read_po must be fed
600+ # the locale in order to correctly calculate plurals
601+ catalog = read_po (infile , locale = locale )
602+
603+ catalog .locale = locale
604+ catalog .revision_date = datetime .datetime .now (LOCALTZ )
605+ catalog .fuzzy = False
606+
607+ if dirname := os .path .dirname (output_file ):
608+ os .makedirs (dirname , exist_ok = True )
609+
610+ with open (output_file , 'wb' ) as outfile :
611+ write_po (outfile , catalog , width = width )
612+
613+
597614class InitCatalog (CommandMixin ):
598615 description = 'create a new catalog based on a POT file'
599616 user_options = [
@@ -642,8 +659,6 @@ def finalize_options(self):
642659 lc_messages_path = pathlib .Path (self .output_dir ) / self .locale / "LC_MESSAGES"
643660 self .output_file = str (lc_messages_path / f"{ self .domain } .po" )
644661
645- if not os .path .exists (os .path .dirname (self .output_file )):
646- os .makedirs (os .path .dirname (self .output_file ))
647662 if self .no_wrap and self .width :
648663 raise OptionError ("'--no-wrap' and '--width' are mutually exclusive" )
649664 if not self .no_wrap and not self .width :
@@ -657,18 +672,12 @@ def run(self):
657672 self .output_file ,
658673 self .input_file ,
659674 )
660-
661- with open (self .input_file , 'rb' ) as infile :
662- # Although reading from the catalog template, read_po must be fed
663- # the locale in order to correctly calculate plurals
664- catalog = read_po (infile , locale = self .locale )
665-
666- catalog .locale = self ._locale
667- catalog .revision_date = datetime .datetime .now (LOCALTZ )
668- catalog .fuzzy = False
669-
670- with open (self .output_file , 'wb' ) as outfile :
671- write_po (outfile , catalog , width = self .width )
675+ _init_catalog (
676+ input_file = self .input_file ,
677+ output_file = self .output_file ,
678+ locale = self ._locale ,
679+ width = self .width ,
680+ )
672681
673682
674683class UpdateCatalog (CommandMixin ):
@@ -807,17 +816,12 @@ def run(self):
807816 self .input_file ,
808817 )
809818
810- with open (self .input_file , 'rb' ) as infile :
811- # Although reading from the catalog template, read_po must
812- # be fed the locale in order to correctly calculate plurals
813- catalog = read_po (infile , locale = self .locale )
814-
815- catalog .locale = self ._locale
816- catalog .revision_date = datetime .datetime .now (LOCALTZ )
817- catalog .fuzzy = False
818-
819- with open (filename , 'wb' ) as outfile :
820- write_po (outfile , catalog )
819+ _init_catalog (
820+ input_file = self .input_file ,
821+ output_file = filename ,
822+ locale = self ._locale ,
823+ width = self .width ,
824+ )
821825
822826 self .log .info ('updating catalog %s based on %s' , filename , self .input_file )
823827 with open (filename , 'rb' ) as infile :
0 commit comments