Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flag to ignore POT-Creation-Date for updates #999

Merged
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
4 changes: 3 additions & 1 deletion babel/messages/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,7 @@ def update(
no_fuzzy_matching: bool = False,
update_header_comment: bool = False,
keep_user_comments: bool = True,
update_creation_date: bool = True,
) -> None:
"""Update the catalog based on the given template catalog.

Expand Down Expand Up @@ -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 update_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."""
Expand Down
8 changes: 6 additions & 2 deletions babel/messages/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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:
Expand Down Expand Up @@ -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,
update_creation_date=not self.ignore_pot_creation_date,
)

tmpname = os.path.join(os.path.dirname(filename),
Expand Down
11 changes: 11 additions & 0 deletions tests/messages/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, update_creation_date=False)
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)
Expand Down
104 changes: 103 additions & 1 deletion tests/messages/test_frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down