Skip to content

Commit 9ef53c6

Browse files
authored
Add flag to ignore POT-Creation-Date for updates (#999)
1 parent 1747d22 commit 9ef53c6

File tree

4 files changed

+123
-4
lines changed

4 files changed

+123
-4
lines changed

babel/messages/catalog.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,7 @@ def update(
769769
no_fuzzy_matching: bool = False,
770770
update_header_comment: bool = False,
771771
keep_user_comments: bool = True,
772+
update_creation_date: bool = True,
772773
) -> None:
773774
"""Update the catalog based on the given template catalog.
774775
@@ -907,7 +908,8 @@ def _merge(message: Message, oldkey: tuple[str, str] | str, newkey: tuple[str, s
907908

908909
# Make updated catalog's POT-Creation-Date equal to the template
909910
# used to update the catalog
910-
self.creation_date = template.creation_date
911+
if update_creation_date:
912+
self.creation_date = template.creation_date
911913

912914
def _to_fuzzy_match_key(self, key: tuple[str, str] | str) -> str:
913915
"""Converts a message key to a string suitable for fuzzy matching."""

babel/messages/frontend.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -727,11 +727,13 @@ class update_catalog(Command):
727727
'don\'t update the catalog, just return the status. Return code 0 '
728728
'means nothing would change. Return code 1 means that the catalog '
729729
'would be updated'),
730+
('ignore-pot-creation-date=', None,
731+
'ignore changes to POT-Creation-Date when updating or checking'),
730732
]
731733
boolean_options = [
732734
'omit-header', 'no-wrap', 'ignore-obsolete', 'init-missing',
733735
'no-fuzzy-matching', 'previous', 'update-header-comment',
734-
'check',
736+
'check', 'ignore-pot-creation-date',
735737
]
736738

737739
def initialize_options(self):
@@ -749,6 +751,7 @@ def initialize_options(self):
749751
self.update_header_comment = False
750752
self.previous = False
751753
self.check = False
754+
self.ignore_pot_creation_date = False
752755

753756
def finalize_options(self):
754757
if not self.input_file:
@@ -837,7 +840,8 @@ def run(self):
837840

838841
catalog.update(
839842
template, self.no_fuzzy_matching,
840-
update_header_comment=self.update_header_comment
843+
update_header_comment=self.update_header_comment,
844+
update_creation_date=not self.ignore_pot_creation_date,
841845
)
842846

843847
tmpname = os.path.join(os.path.dirname(filename),

tests/messages/test_catalog.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,17 @@ def test_update_po_updates_pot_creation_date(self):
273273
localized_catalog.update(template)
274274
assert template.creation_date == localized_catalog.creation_date
275275

276+
def test_update_po_ignores_pot_creation_date(self):
277+
template = catalog.Catalog()
278+
localized_catalog = copy.deepcopy(template)
279+
localized_catalog.locale = 'de_DE'
280+
assert template.mime_headers != localized_catalog.mime_headers
281+
assert template.creation_date == localized_catalog.creation_date
282+
template.creation_date = datetime.datetime.now() - \
283+
datetime.timedelta(minutes=5)
284+
localized_catalog.update(template, update_creation_date=False)
285+
assert template.creation_date != localized_catalog.creation_date
286+
276287
def test_update_po_keeps_po_revision_date(self):
277288
template = catalog.Catalog()
278289
localized_catalog = copy.deepcopy(template)

tests/messages/test_frontend.py

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import sys
1717
import time
1818
import unittest
19-
from datetime import datetime
19+
from datetime import datetime, timedelta
2020
from io import StringIO
2121

2222
import pytest
@@ -1179,6 +1179,60 @@ def test_update(self):
11791179
catalog = read_po(infp)
11801180
assert len(catalog) == 4 # Catalog was updated
11811181

1182+
def test_update_pot_creation_date(self):
1183+
template = Catalog()
1184+
template.add("1")
1185+
template.add("2")
1186+
template.add("3")
1187+
tmpl_file = os.path.join(i18n_dir, 'temp-template.pot')
1188+
with open(tmpl_file, "wb") as outfp:
1189+
write_po(outfp, template)
1190+
po_file = os.path.join(i18n_dir, 'temp1.po')
1191+
self.cli.run(sys.argv + ['init',
1192+
'-l', 'fi',
1193+
'-o', po_file,
1194+
'-i', tmpl_file
1195+
])
1196+
with open(po_file) as infp:
1197+
catalog = read_po(infp)
1198+
assert len(catalog) == 3
1199+
original_catalog_creation_date = catalog.creation_date
1200+
1201+
# Update the template creation date
1202+
template.creation_date -= timedelta(minutes=3)
1203+
with open(tmpl_file, "wb") as outfp:
1204+
write_po(outfp, template)
1205+
1206+
self.cli.run(sys.argv + ['update',
1207+
'-l', 'fi_FI',
1208+
'-o', po_file,
1209+
'-i', tmpl_file])
1210+
1211+
with open(po_file) as infp:
1212+
catalog = read_po(infp)
1213+
# We didn't ignore the creation date, so expect a diff
1214+
assert catalog.creation_date != original_catalog_creation_date
1215+
1216+
# Reset the "original"
1217+
original_catalog_creation_date = catalog.creation_date
1218+
1219+
# Update the template creation date again
1220+
# This time, pass the ignore flag and expect the times are different
1221+
template.creation_date -= timedelta(minutes=5)
1222+
with open(tmpl_file, "wb") as outfp:
1223+
write_po(outfp, template)
1224+
1225+
self.cli.run(sys.argv + ['update',
1226+
'-l', 'fi_FI',
1227+
'-o', po_file,
1228+
'-i', tmpl_file,
1229+
'--ignore-pot-creation-date'])
1230+
1231+
with open(po_file) as infp:
1232+
catalog = read_po(infp)
1233+
# We ignored creation date, so it should not have changed
1234+
assert catalog.creation_date == original_catalog_creation_date
1235+
11821236
def test_check(self):
11831237
template = Catalog()
11841238
template.add("1")
@@ -1237,6 +1291,54 @@ def test_check(self):
12371291
'-o', po_file,
12381292
'-i', tmpl_file])
12391293

1294+
def test_check_pot_creation_date(self):
1295+
template = Catalog()
1296+
template.add("1")
1297+
template.add("2")
1298+
template.add("3")
1299+
tmpl_file = os.path.join(i18n_dir, 'temp-template.pot')
1300+
with open(tmpl_file, "wb") as outfp:
1301+
write_po(outfp, template)
1302+
po_file = os.path.join(i18n_dir, 'temp1.po')
1303+
self.cli.run(sys.argv + ['init',
1304+
'-l', 'fi_FI',
1305+
'-o', po_file,
1306+
'-i', tmpl_file
1307+
])
1308+
1309+
# Update the catalog file
1310+
self.cli.run(sys.argv + ['update',
1311+
'-l', 'fi_FI',
1312+
'-o', po_file,
1313+
'-i', tmpl_file])
1314+
1315+
# Run a check without introducing any changes to the template
1316+
self.cli.run(sys.argv + ['update',
1317+
'--check',
1318+
'-l', 'fi_FI',
1319+
'-o', po_file,
1320+
'-i', tmpl_file])
1321+
1322+
# Run a check after changing the template creation date
1323+
template.creation_date = datetime.now() - timedelta(minutes=5)
1324+
with open(tmpl_file, "wb") as outfp:
1325+
write_po(outfp, template)
1326+
1327+
# Should fail without --ignore-pot-creation-date flag
1328+
with pytest.raises(BaseError):
1329+
self.cli.run(sys.argv + ['update',
1330+
'--check',
1331+
'-l', 'fi_FI',
1332+
'-o', po_file,
1333+
'-i', tmpl_file])
1334+
# Should pass with --ignore-pot-creation-date flag
1335+
self.cli.run(sys.argv + ['update',
1336+
'--check',
1337+
'-l', 'fi_FI',
1338+
'-o', po_file,
1339+
'-i', tmpl_file,
1340+
'--ignore-pot-creation-date'])
1341+
12401342
def test_update_init_missing(self):
12411343
template = Catalog()
12421344
template.add("1")

0 commit comments

Comments
 (0)