]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Reuse InitCatalog's guts in UpdateCatalog (#1244) master
authorAarni Koskela <akx@iki.fi>
Sat, 10 Jan 2026 12:53:16 +0000 (14:53 +0200)
committerGitHub <noreply@github.com>
Sat, 10 Jan 2026 12:53:16 +0000 (14:53 +0200)
Fixes #1139 (since `_init_catalog` creates directories on the way)

Co-authored-by: lando <du33169@qq.com>
babel/messages/frontend.py
tests/messages/frontend/test_cli.py

index 1e13b6cc6b24a28ed33f9b03118e910b890ac09b..884a7000681ff7d0011ae15d1529cda6268214af 100644 (file)
@@ -594,6 +594,23 @@ class ExtractMessages(CommandMixin):
         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 @@ class InitCatalog(CommandMixin):
             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 @@ class InitCatalog(CommandMixin):
             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 @@ 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:
index 9eb2c4cd0af3f6fc032ed61b33f8508284e705ce..200632ec3505d2123c315fe446a7d55564ac9c01 100644 (file)
@@ -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