Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 29 additions & 25 deletions babel/messages/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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:
Expand All @@ -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):
Expand Down Expand Up @@ -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:
Expand Down
19 changes: 19 additions & 0 deletions tests/messages/frontend/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading