diff --git a/babel/messages/frontend.py b/babel/messages/frontend.py index 6ef62ec4a..f44041c9f 100644 --- a/babel/messages/frontend.py +++ b/babel/messages/frontend.py @@ -594,6 +594,23 @@ def _get_mappings(self): return mappings +def _init_catalog(*, input_file, output_file, locale: Locale, width: int) -> None: + with open(input_file, 'rb') as infile: + # Although reading from the catalog template, read_po must be fed + # the locale in order to correctly calculate plurals + catalog = read_po(infile, locale=locale) + + catalog.locale = locale + catalog.revision_date = datetime.datetime.now(LOCALTZ) + catalog.fuzzy = False + + if dirname := os.path.dirname(output_file): + os.makedirs(dirname, exist_ok=True) + + with open(output_file, 'wb') as outfile: + write_po(outfile, catalog, width=width) + + class InitCatalog(CommandMixin): description = 'create a new catalog based on a POT file' user_options = [ @@ -642,8 +659,6 @@ def finalize_options(self): lc_messages_path = pathlib.Path(self.output_dir) / self.locale / "LC_MESSAGES" self.output_file = str(lc_messages_path / f"{self.domain}.po") - if not os.path.exists(os.path.dirname(self.output_file)): - os.makedirs(os.path.dirname(self.output_file)) if self.no_wrap and self.width: raise OptionError("'--no-wrap' and '--width' are mutually exclusive") if not self.no_wrap and not self.width: @@ -657,18 +672,12 @@ def run(self): self.output_file, self.input_file, ) - - with open(self.input_file, 'rb') as infile: - # Although reading from the catalog template, read_po must be fed - # the locale in order to correctly calculate plurals - catalog = read_po(infile, locale=self.locale) - - catalog.locale = self._locale - catalog.revision_date = datetime.datetime.now(LOCALTZ) - catalog.fuzzy = False - - with open(self.output_file, 'wb') as outfile: - write_po(outfile, catalog, width=self.width) + _init_catalog( + input_file=self.input_file, + output_file=self.output_file, + locale=self._locale, + width=self.width, + ) class UpdateCatalog(CommandMixin): @@ -807,17 +816,12 @@ def run(self): self.input_file, ) - with open(self.input_file, 'rb') as infile: - # Although reading from the catalog template, read_po must - # be fed the locale in order to correctly calculate plurals - catalog = read_po(infile, locale=self.locale) - - catalog.locale = self._locale - catalog.revision_date = datetime.datetime.now(LOCALTZ) - catalog.fuzzy = False - - with open(filename, 'wb') as outfile: - write_po(outfile, catalog) + _init_catalog( + input_file=self.input_file, + output_file=filename, + locale=self._locale, + width=self.width, + ) self.log.info('updating catalog %s based on %s', filename, self.input_file) with open(filename, 'rb') as infile: diff --git a/tests/messages/frontend/test_cli.py b/tests/messages/frontend/test_cli.py index 9eb2c4cd0..200632ec3 100644 --- a/tests/messages/frontend/test_cli.py +++ b/tests/messages/frontend/test_cli.py @@ -647,3 +647,22 @@ def test_update_init_missing(cli): with open(po_file) as infp: catalog = read_po(infp) assert len(catalog) == 4 # Catalog was updated + + +def test_update_init_missing_creates_dest_dir(cli, tmp_path): + template = Catalog() + template.add("xyzzy") + template.add("ferg") + tmpl_file = tmp_path / 'temp.pot' + with tmpl_file.open("wb") as outfp: + write_po(outfp, template) + + dest_dir = tmp_path / 'newdir' / 'hierarchy' + assert not dest_dir.exists() + po_file = dest_dir / 'temp.po' + + cli.run(['pybabel', 'update', '--init-missing', '-l', 'ja', '-o', po_file, '-i', tmpl_file]) + assert dest_dir.exists() + + with po_file.open() as infp: + assert len(read_po(infp)) == 2