Skip to content

Commit ff8071e

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 ff8071e

File tree

2 files changed

+48
-25
lines changed

2 files changed

+48
-25
lines changed

babel/messages/frontend.py

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

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

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)