Repository: python-babel/babel
Pull request #320 (2016-01-08 08:19:31)

Make catalog header updating an option
The change in e0e7ef168856bb had an unexpected and likely undesired effect
when updating catalogs with, e.g. translator names in the header comment.

It's better to make the updating an option and revert back to the pre-2.2
behavior by default.

Fixes https://github.com/python-babel/babel/issues/318

codecov-io: ## [Current coverage][1] is `84.25%`
> Merging **#320** into **master** will decrease coverage by **-0.01%** as of [`40e18c6`][3]


```diff
@@            master    #320   diff @@
======================================
  Files           22      22       
  Stmts         3776    3778     +2
  Branches         0       0       
  Methods          0       0       
======================================
+ Hit           3182    3183     +1
  Partial          0       0       
- Missed         594     595     +1
```

> Review entire [Coverage Diff][4] as of [`40e18c6`][3]


[1]: https://codecov.io/github/python-babel/babel?ref=40e18c6ea63994a2bc0f8b69f0d4bd32f7d81e16
[2]: https://codecov.io/github/python-babel/babel/features/suggestions?ref=40e18c6ea63994a2bc0f8b69f0d4bd32f7d81e16
[3]: https://codecov.io/github/python-babel/babel/commit/40e18c6ea63994a2bc0f8b69f0d4bd32f7d81e16
[4]: https://codecov.io/github/python-babel/babel/compare/3aa3f2902af8d95c2ecb378db818cf9c70375e5e...40e18c6ea63994a2bc0f8b69f0d4bd32f7d81e16

> Powered by [Codecov](https://codecov.io). Updated on successful CI builds.
sils1297: @akx needs more test coverage.
akx: @sils1297 The lack of test coverage would be fixed by merging #311. Right now the distutils versions of command code are not tested at all, but the other copies are.

I published the option only in the distutils code anticipating that merge.
ajaeger: Tested this locally and it works as expected - fixes issue #318 in my setup. Thanks!
akx: I can also just _remove_ the new option in the distutils code, but that seems like doing it just to make Codecov happy, and we're not writing code for CI robots. :)
sils1297: @akx then lets merge #311 first?
akx: Hey @ajaeger, if you have the time, could you check that this branch still works for your UC (after it was rebased)? Thank you!

Patch:
diff --git a/babel/messages/catalog.py b/babel/messages/catalog.py
index f72a34f..ca4a568 100644
--- a/babel/messages/catalog.py
+++ b/babel/messages/catalog.py
@@ -673,7 +673,7 @@ class Catalog(object):
         if key in self._messages:
             del self._messages[key]
 
-    def update(self, template, no_fuzzy_matching=False):
+    def update(self, template, no_fuzzy_matching=False, update_header_comment=False):
         """Update the catalog based on the given template catalog.
 
         >>> from babel.messages import Catalog
@@ -798,9 +798,10 @@ class Catalog(object):
             if no_fuzzy_matching or msgid not in fuzzy_matches:
                 self.obsolete[msgid] = remaining[msgid]
 
-        # Allow the updated catalog's header to be rewritten based on the
-        # template's header
-        self.header_comment = template.header_comment
+        if update_header_comment:
+            # Allow the updated catalog's header to be rewritten based on the
+            # template's header
+            self.header_comment = template.header_comment
 
         # Make updated catalog's POT-Creation-Date equal to the template
         # used to update the catalog
diff --git a/babel/messages/frontend.py b/babel/messages/frontend.py
index 5f6b141..d9919f6 100755
--- a/babel/messages/frontend.py
+++ b/babel/messages/frontend.py
@@ -545,10 +545,12 @@ class update_catalog(Command):
          'whether to omit obsolete messages from the output'),
         ('no-fuzzy-matching', 'N',
          'do not use fuzzy matching'),
+        ('update-header-comment', None,
+         'update target header comment'),
         ('previous', None,
          'keep previous msgids of translated messages')
     ]
-    boolean_options = ['ignore_obsolete', 'no_fuzzy_matching', 'previous']
+    boolean_options = ['ignore_obsolete', 'no_fuzzy_matching', 'previous', 'update_header_comment']
 
     def initialize_options(self):
         self.domain = 'messages'
@@ -560,6 +562,7 @@ class update_catalog(Command):
         self.no_wrap = False
         self.ignore_obsolete = False
         self.no_fuzzy_matching = False
+        self.update_header_comment = False
         self.previous = False
 
     def finalize_options(self):
@@ -619,7 +622,10 @@ class update_catalog(Command):
             finally:
                 infile.close()
 
-            catalog.update(template, self.no_fuzzy_matching)
+            catalog.update(
+                template, self.no_fuzzy_matching,
+                update_header_comment=self.update_header_comment
+            )
 
             tmpname = os.path.join(os.path.dirname(filename),
                                    tempfile.gettempprefix() +


Test patch:
diff --git a/tests/messages/test_catalog.py b/tests/messages/test_catalog.py
index 7d18d80..3eeaf64 100644
--- a/tests/messages/test_catalog.py
+++ b/tests/messages/test_catalog.py
@@ -440,7 +440,6 @@ def test_catalog_update():
 
     cat.update(template)
     assert len(cat) == 3
-    assert cat.header_comment == template.header_comment  # Header comment also gets updated
 
     msg1 = cat['green']
     msg1.string
@@ -457,6 +456,9 @@ def test_catalog_update():
     assert not 'head' in cat
     assert list(cat.obsolete.values())[0].id == 'head'
 
+    cat.update(template, update_header_comment=True)
+    assert cat.header_comment == template.header_comment  # Header comment also gets updated
+
 
 def test_datetime_parsing():
     val1 = catalog._parse_datetime_header('2006-06-28 23:24+0200')