From 4b5102302f18fe114d236c5cc877a2e17b14b237 Mon Sep 17 00:00:00 2001 From: Joe Portela Date: Tue, 11 Apr 2023 12:34:47 -0400 Subject: [PATCH 1/2] add flag to ignore pot creation date for updates --- babel/messages/catalog.py | 4 +- babel/messages/frontend.py | 8 ++- tests/messages/test_catalog.py | 11 ++++ tests/messages/test_frontend.py | 104 +++++++++++++++++++++++++++++++- 4 files changed, 123 insertions(+), 4 deletions(-) diff --git a/babel/messages/catalog.py b/babel/messages/catalog.py index fe9f45bb3..1e59ed5ae 100644 --- a/babel/messages/catalog.py +++ b/babel/messages/catalog.py @@ -769,6 +769,7 @@ def update( no_fuzzy_matching: bool = False, update_header_comment: bool = False, keep_user_comments: bool = True, + ignore_pot_creation_date: bool = False, ) -> None: """Update the catalog based on the given template catalog. @@ -907,7 +908,8 @@ def _merge(message: Message, oldkey: tuple[str, str] | str, newkey: tuple[str, s # Make updated catalog's POT-Creation-Date equal to the template # used to update the catalog - self.creation_date = template.creation_date + if not ignore_pot_creation_date: + self.creation_date = template.creation_date def _to_fuzzy_match_key(self, key: tuple[str, str] | str) -> str: """Converts a message key to a string suitable for fuzzy matching.""" diff --git a/babel/messages/frontend.py b/babel/messages/frontend.py index af7e1d11d..b8889da10 100644 --- a/babel/messages/frontend.py +++ b/babel/messages/frontend.py @@ -727,11 +727,13 @@ class update_catalog(Command): 'don\'t update the catalog, just return the status. Return code 0 ' 'means nothing would change. Return code 1 means that the catalog ' 'would be updated'), + ('ignore-pot-creation-date=', None, + 'ignore changes to POT-Creation-Date when updating or checking'), ] boolean_options = [ 'omit-header', 'no-wrap', 'ignore-obsolete', 'init-missing', 'no-fuzzy-matching', 'previous', 'update-header-comment', - 'check', + 'check', 'ignore-pot-creation-date', ] def initialize_options(self): @@ -749,6 +751,7 @@ def initialize_options(self): self.update_header_comment = False self.previous = False self.check = False + self.ignore_pot_creation_date = False def finalize_options(self): if not self.input_file: @@ -837,7 +840,8 @@ def run(self): catalog.update( template, self.no_fuzzy_matching, - update_header_comment=self.update_header_comment + update_header_comment=self.update_header_comment, + ignore_pot_creation_date=self.ignore_pot_creation_date, ) tmpname = os.path.join(os.path.dirname(filename), diff --git a/tests/messages/test_catalog.py b/tests/messages/test_catalog.py index da8ad5577..146b2221d 100644 --- a/tests/messages/test_catalog.py +++ b/tests/messages/test_catalog.py @@ -273,6 +273,17 @@ def test_update_po_updates_pot_creation_date(self): localized_catalog.update(template) assert template.creation_date == localized_catalog.creation_date + def test_update_po_ignores_pot_creation_date(self): + template = catalog.Catalog() + localized_catalog = copy.deepcopy(template) + localized_catalog.locale = 'de_DE' + assert template.mime_headers != localized_catalog.mime_headers + assert template.creation_date == localized_catalog.creation_date + template.creation_date = datetime.datetime.now() - \ + datetime.timedelta(minutes=5) + localized_catalog.update(template, ignore_pot_creation_date=True) + assert template.creation_date != localized_catalog.creation_date + def test_update_po_keeps_po_revision_date(self): template = catalog.Catalog() localized_catalog = copy.deepcopy(template) diff --git a/tests/messages/test_frontend.py b/tests/messages/test_frontend.py index 8e1e670ca..821738d23 100644 --- a/tests/messages/test_frontend.py +++ b/tests/messages/test_frontend.py @@ -16,7 +16,7 @@ import sys import time import unittest -from datetime import datetime +from datetime import datetime, timedelta from io import StringIO import pytest @@ -1179,6 +1179,60 @@ def test_update(self): catalog = read_po(infp) assert len(catalog) == 4 # Catalog was updated + def test_update_pot_creation_date(self): + template = Catalog() + template.add("1") + template.add("2") + template.add("3") + tmpl_file = os.path.join(i18n_dir, 'temp-template.pot') + with open(tmpl_file, "wb") as outfp: + write_po(outfp, template) + po_file = os.path.join(i18n_dir, 'temp1.po') + self.cli.run(sys.argv + ['init', + '-l', 'fi', + '-o', po_file, + '-i', tmpl_file + ]) + with open(po_file) as infp: + catalog = read_po(infp) + assert len(catalog) == 3 + original_catalog_creation_date = catalog.creation_date + + # Update the template creation date + template.creation_date -= timedelta(minutes=3) + with open(tmpl_file, "wb") as outfp: + write_po(outfp, template) + + self.cli.run(sys.argv + ['update', + '-l', 'fi_FI', + '-o', po_file, + '-i', tmpl_file]) + + with open(po_file) as infp: + catalog = read_po(infp) + # We didn't ignore the creation date, so expect a diff + assert catalog.creation_date != original_catalog_creation_date + + # Reset the "original" + original_catalog_creation_date = catalog.creation_date + + # Update the template creation date again + # This time, pass the ignore flag and expect the times are different + template.creation_date -= timedelta(minutes=5) + with open(tmpl_file, "wb") as outfp: + write_po(outfp, template) + + self.cli.run(sys.argv + ['update', + '-l', 'fi_FI', + '-o', po_file, + '-i', tmpl_file, + '--ignore-pot-creation-date']) + + with open(po_file) as infp: + catalog = read_po(infp) + # We ignored creation date, so it should not have changed + assert catalog.creation_date == original_catalog_creation_date + def test_check(self): template = Catalog() template.add("1") @@ -1237,6 +1291,54 @@ def test_check(self): '-o', po_file, '-i', tmpl_file]) + def test_check_pot_creation_date(self): + template = Catalog() + template.add("1") + template.add("2") + template.add("3") + tmpl_file = os.path.join(i18n_dir, 'temp-template.pot') + with open(tmpl_file, "wb") as outfp: + write_po(outfp, template) + po_file = os.path.join(i18n_dir, 'temp1.po') + self.cli.run(sys.argv + ['init', + '-l', 'fi_FI', + '-o', po_file, + '-i', tmpl_file + ]) + + # Update the catalog file + self.cli.run(sys.argv + ['update', + '-l', 'fi_FI', + '-o', po_file, + '-i', tmpl_file]) + + # Run a check without introducing any changes to the template + self.cli.run(sys.argv + ['update', + '--check', + '-l', 'fi_FI', + '-o', po_file, + '-i', tmpl_file]) + + # Run a check after changing the template creation date + template.creation_date = datetime.now() - timedelta(minutes=5) + with open(tmpl_file, "wb") as outfp: + write_po(outfp, template) + + # Should fail without --ignore-pot-creation-date flag + with pytest.raises(BaseError): + self.cli.run(sys.argv + ['update', + '--check', + '-l', 'fi_FI', + '-o', po_file, + '-i', tmpl_file]) + # Should pass with --ignore-pot-creation-date flag + self.cli.run(sys.argv + ['update', + '--check', + '-l', 'fi_FI', + '-o', po_file, + '-i', tmpl_file, + '--ignore-pot-creation-date']) + def test_update_init_missing(self): template = Catalog() template.add("1") From 254a81afbc721ff43efa0a26e7a4b39bd3f769d0 Mon Sep 17 00:00:00 2001 From: Joe Portela Date: Wed, 5 Jul 2023 15:15:21 -0400 Subject: [PATCH 2/2] update catalog kwarg to be positive --- babel/messages/catalog.py | 4 ++-- babel/messages/frontend.py | 2 +- tests/messages/test_catalog.py | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/babel/messages/catalog.py b/babel/messages/catalog.py index 1e59ed5ae..8faf046d3 100644 --- a/babel/messages/catalog.py +++ b/babel/messages/catalog.py @@ -769,7 +769,7 @@ def update( no_fuzzy_matching: bool = False, update_header_comment: bool = False, keep_user_comments: bool = True, - ignore_pot_creation_date: bool = False, + update_creation_date: bool = True, ) -> None: """Update the catalog based on the given template catalog. @@ -908,7 +908,7 @@ def _merge(message: Message, oldkey: tuple[str, str] | str, newkey: tuple[str, s # Make updated catalog's POT-Creation-Date equal to the template # used to update the catalog - if not ignore_pot_creation_date: + if update_creation_date: self.creation_date = template.creation_date def _to_fuzzy_match_key(self, key: tuple[str, str] | str) -> str: diff --git a/babel/messages/frontend.py b/babel/messages/frontend.py index b8889da10..6ed1fe5bd 100644 --- a/babel/messages/frontend.py +++ b/babel/messages/frontend.py @@ -841,7 +841,7 @@ def run(self): catalog.update( template, self.no_fuzzy_matching, update_header_comment=self.update_header_comment, - ignore_pot_creation_date=self.ignore_pot_creation_date, + update_creation_date=not self.ignore_pot_creation_date, ) tmpname = os.path.join(os.path.dirname(filename), diff --git a/tests/messages/test_catalog.py b/tests/messages/test_catalog.py index 146b2221d..497707dc9 100644 --- a/tests/messages/test_catalog.py +++ b/tests/messages/test_catalog.py @@ -281,7 +281,7 @@ def test_update_po_ignores_pot_creation_date(self): assert template.creation_date == localized_catalog.creation_date template.creation_date = datetime.datetime.now() - \ datetime.timedelta(minutes=5) - localized_catalog.update(template, ignore_pot_creation_date=True) + localized_catalog.update(template, update_creation_date=False) assert template.creation_date != localized_catalog.creation_date def test_update_po_keeps_po_revision_date(self):