@@ -594,6 +594,21 @@ 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+ os .makedirs (os .path .dirname (output_file ), exist_ok = True )
608+ with open (output_file , 'wb' ) as outfile :
609+ write_po (outfile , catalog , width = width )
610+
611+
597612class InitCatalog (CommandMixin ):
598613 description = 'create a new catalog based on a POT file'
599614 user_options = [
@@ -642,8 +657,6 @@ def finalize_options(self):
642657 lc_messages_path = pathlib .Path (self .output_dir ) / self .locale / "LC_MESSAGES"
643658 self .output_file = str (lc_messages_path / f"{ self .domain } .po" )
644659
645- if not os .path .exists (os .path .dirname (self .output_file )):
646- os .makedirs (os .path .dirname (self .output_file ))
647660 if self .no_wrap and self .width :
648661 raise OptionError ("'--no-wrap' and '--width' are mutually exclusive" )
649662 if not self .no_wrap and not self .width :
@@ -657,18 +670,12 @@ def run(self):
657670 self .output_file ,
658671 self .input_file ,
659672 )
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 )
673+ _init_catalog (
674+ input_file = self .input_file ,
675+ output_file = self .output_file ,
676+ locale = self ._locale ,
677+ width = self .width ,
678+ )
672679
673680
674681class UpdateCatalog (CommandMixin ):
@@ -807,17 +814,12 @@ def run(self):
807814 self .input_file ,
808815 )
809816
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 )
817+ _init_catalog (
818+ input_file = self .input_file ,
819+ output_file = filename ,
820+ locale = self ._locale ,
821+ width = self .width ,
822+ )
821823
822824 self .log .info ('updating catalog %s based on %s' , filename , self .input_file )
823825 with open (filename , 'rb' ) as infile :
0 commit comments