Skip to content

Commit 9826fc5

Browse files
akxdu33169
andcommitted
Reuse InitCatalog's guts in UpdateCatalog
Fixes #1139 (since `_init_catalog` creates directories on the way) Co-authored-by: lando <du33169@qq.com>
1 parent 12a14b6 commit 9826fc5

2 files changed

Lines changed: 46 additions & 25 deletions

File tree

babel/messages/frontend.py

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
597612
class 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

674681
class 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:

tests/messages/frontend/test_cli.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,3 +647,22 @@ def test_update_init_missing(cli):
647647
with open(po_file) as infp:
648648
catalog = read_po(infp)
649649
assert len(catalog) == 4 # Catalog was updated
650+
651+
652+
def test_update_init_missing_creates_dest_dir(cli, tmp_path):
653+
template = Catalog()
654+
template.add("xyzzy")
655+
template.add("ferg")
656+
tmpl_file = tmp_path / 'temp.pot'
657+
with tmpl_file.open("wb") as outfp:
658+
write_po(outfp, template)
659+
660+
dest_dir = tmp_path / 'newdir' / 'hierarchy'
661+
assert not dest_dir.exists()
662+
po_file = dest_dir / 'temp.po'
663+
664+
cli.run(['pybabel', 'update', '--init-missing', '-l', 'ja', '-o', po_file, '-i', tmpl_file])
665+
assert dest_dir.exists()
666+
667+
with po_file.open() as infp:
668+
assert len(read_po(infp)) == 2

0 commit comments

Comments
 (0)