changeset 191:c171a0041ad2 trunk

Add an option to the frontend commands for catalog updating that removes completely any obsolete messages, instead of putting them comments.
author cmlenz
date Sun, 01 Jul 2007 17:59:44 +0000
parents 5041d90edf0c
children d6d9ac8470ec
files babel/messages/frontend.py babel/messages/pofile.py babel/messages/tests/pofile.py doc/cmdline.txt doc/setup.txt
diffstat 5 files changed, 40 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/babel/messages/frontend.py
+++ b/babel/messages/frontend.py
@@ -452,7 +452,10 @@
          "'<output_dir>/<locale>/LC_MESSAGES/<domain>.po')"),
         ('locale=', 'l',
          'locale of the catalog to compile'),
+        ('ignore-obsolete=', None,
+         'whether to omit obsolete messages from the output')
     ]
+    boolean_options = ['ignore_obsolete']
 
     def initialize_options(self):
         self.domain = 'messages'
@@ -460,6 +463,7 @@
         self.output_dir = None
         self.output_file = None
         self.locale = None
+        self.ignore_obsolete = False
 
     def finalize_options(self):
         if not self.input_file:
@@ -504,7 +508,7 @@
 
             outfile = open(po_file, 'w')
             try:
-                write_po(outfile, catalog)
+                write_po(outfile, catalog, ignore_obsolete=self.ignore_obsolete)
             finally:
                 outfile.close()
 
@@ -853,8 +857,12 @@
                                "<domain>.po')")
         parser.add_option('--locale', '-l', dest='locale', metavar='LOCALE',
                           help='locale of the translations catalog')
+        parser.add_option('--ignore-obsolete', dest='ignore_obsolete',
+                          action='store_true',
+                          help='do not include obsolete messages in the output '
+                               '(default %default)'),
 
-        parser.set_defaults(domain='messages')
+        parser.set_defaults(domain='messages', ignore_obsolete=False)
         options, args = parser.parse_args(argv)
 
         if not options.input_file:
@@ -898,7 +906,8 @@
 
             outfile = open(po_file, 'w')
             try:
-                write_po(outfile, catalog)
+                write_po(outfile, catalog,
+                         ignore_obsolete=options.ignore_obsolete)
             finally:
                 outfile.close()
 
--- a/babel/messages/pofile.py
+++ b/babel/messages/pofile.py
@@ -298,7 +298,7 @@
     return u'""\n' + u'\n'.join([(prefix + escape(l)) for l in lines])
 
 def write_po(fileobj, catalog, width=76, no_location=False, omit_header=False,
-             sort_output=False, sort_by_file=False):
+             sort_output=False, sort_by_file=False, ignore_obsolete=False):
     r"""Write a ``gettext`` PO (portable object) template file for a given
     message catalog to the provided file-like object.
     
@@ -330,6 +330,10 @@
     :param no_location: do not emit a location comment for every message
     :param omit_header: do not include the ``msgid ""`` entry at the top of the
                         output
+    :sort_output: whether to sort the messages in the output by msgid
+    :sort_by_file: whether to sort the messages in the output by their locations
+    :ignore_obsolete: whether to ignore obsolete messages and not include them
+                      in the output; by default they are included as comments
     """
     def _normalize(key, prefix=''):
         return normalize(key, prefix=prefix, width=width) \
@@ -397,8 +401,9 @@
         _write_message(message)
         _write('\n')
 
-    for message in catalog.obsolete.values():
-        for comment in message.user_comments:
-            _write_comment(comment)
-        _write_message(message, prefix='#~ ')
-        _write('\n')
+    if not ignore_obsolete:
+        for message in catalog.obsolete.values():
+            for comment in message.user_comments:
+                _write_comment(comment)
+            _write_message(message, prefix='#~ ')
+            _write('\n')
--- a/babel/messages/tests/pofile.py
+++ b/babel/messages/tests/pofile.py
@@ -194,6 +194,18 @@
 #~ "multiple lines, and should still be handled\n"
 #~ "correctly.\n"''', buf.getvalue().strip())
 
+    def test_po_with_obsolete_message_ignored(self):
+        catalog = Catalog()
+        catalog.add(u'foo', u'Voh', locations=[('main.py', 1)])
+        catalog.obsolete['bar'] = Message(u'bar', u'Bahr',
+                                          locations=[('utils.py', 3)],
+                                          user_comments=['User comment'])
+        buf = StringIO()
+        pofile.write_po(buf, catalog, omit_header=True, ignore_obsolete=True)
+        self.assertEqual('''#: main.py:1
+msgid "foo"
+msgstr "Voh"''', buf.getvalue().strip())
+
 
 def suite():
     suite = unittest.TestSuite()
--- a/doc/cmdline.txt
+++ b/doc/cmdline.txt
@@ -170,6 +170,8 @@
                             '<output_dir>/<locale>/LC_MESSAGES/<domain>.po')
       -l LOCALE, --locale=LOCALE
                             locale of the translations catalog
+      --ignore-obsolete     do not include obsolete messages in the output
+                            (default False)
 
 If ``output_dir`` is specified, but ``output-file`` is not, the default
 filename of the output file will be::
--- a/doc/setup.txt
+++ b/doc/setup.txt
@@ -323,6 +323,9 @@
   +-----------------------------+----------------------------------------------+
   | ``--locale``                | locale for the new localized string          |
   +-----------------------------+----------------------------------------------+
+  | ``--ignore-obsolete``       | do not include obsolete messages in the      |
+  |                             | output                                       |
+  +-----------------------------+----------------------------------------------+
 
 If ``output-dir`` is specified, but ``output-file`` is not, the default filename
 of the output file will be::
Copyright (C) 2012-2017 Edgewall Software