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 = [
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:
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):
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:
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