changeset 107:4b42e23644e5

`Message`, `read_po` and `write_po` now all handle user/auto comments correctly. The `Generated-By` header value needed a missing `\n`. The frontends now pass the value of `--copyright-holder` to the Catalog, and removed the `copyright_holder` arg for `write_po` left behind on [105]. Tests changed accordingly.
author palgarvio
date Thu, 14 Jun 2007 06:24:28 +0000
parents 2a00e352c986
children 8ea225f33f28
files babel/messages/catalog.py babel/messages/frontend.py babel/messages/pofile.py babel/messages/tests/catalog.py babel/messages/tests/pofile.py
diffstat 5 files changed, 52 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/babel/messages/catalog.py
+++ b/babel/messages/catalog.py
@@ -35,7 +35,8 @@
 class Message(object):
     """Representation of a single message in a catalog."""
 
-    def __init__(self, id, string='', locations=(), flags=(), comments=()):
+    def __init__(self, id, string='', locations=(), flags=(), auto_comments=(),
+                 user_comments=()):
         """Create the message object.
         
         :param id: the message ID, or a ``(singular, plural)`` tuple for
@@ -56,7 +57,8 @@
             self.flags.add('python-format')
         else:
             self.flags.discard('python-format')
-        self.comments = list(comments)
+        self.auto_comments = list(auto_comments)
+        self.user_comments = list(user_comments)
 
     def __repr__(self):
         return '<%s %r>' % (type(self).__name__, self.id)
@@ -103,6 +105,7 @@
         :type:  `bool`
         """)
 
+
 DEFAULT_HEADER = u"""\
 # Translations template for PROJECT.
 # Copyright (C) YEAR COPYRIGHT HOLDER
@@ -208,7 +211,7 @@
         headers.append(('Content-Type',
                         'text/plain; charset=%s' % self.charset))
         headers.append(('Content-Transfer-Encoding', '8bit'))
-        headers.append(('Generated-By', 'Babel %s' % VERSION))
+        headers.append(('Generated-By', 'Babel %s\n' % VERSION))
         return headers
     mime_headers = property(mime_headers, doc="""\
     The MIME headers of the catalog, used for the special ``msgid ""`` entry.
@@ -373,7 +376,8 @@
                 current.id = message.id
                 current.string = message.string
             current.locations.extend(message.locations)
-            current.comments.extend(message.comments)
+            current.auto_comments.extend(message.auto_comments)
+            current.user_comments.extend(message.user_comments)
             current.flags |= message.flags
             message = current
         else:
@@ -381,7 +385,8 @@
                 assert isinstance(message.string, (list, tuple))
             self._messages[key] = message
 
-    def add(self, id, string=None, locations=(), flags=(), comments=()):
+    def add(self, id, string=None, locations=(), flags=(), auto_comments=(),
+            user_comments=()):
         """Add or update the message with the specified ID.
         
         >>> catalog = Catalog()
@@ -400,7 +405,8 @@
         :param flags: a set or sequence of flags
         :param comments: a list of translator comments
         """
-        self[id] = Message(id, string, list(locations), flags, comments)
+        self[id] = Message(id, string, list(locations), flags, auto_comments,
+                           user_comments)
 
     def _key_for(self, id):
         """The key for a message is just the singular ID even for pluralizable
--- a/babel/messages/frontend.py
+++ b/babel/messages/frontend.py
@@ -154,6 +154,7 @@
             catalog = Catalog(project=self.distribution.get_name(),
                               version=self.distribution.get_version(),
                               msgid_bugs_address=self.msgid_bugs_address,
+                              copyright_holder=self.copyright_holder,
                               charset=self.charset)
 
             for dirname, (method_map, options_map) in mappings.items():
@@ -175,15 +176,14 @@
                 for filename, lineno, message, comments in extracted:
                     filepath = os.path.normpath(os.path.join(dirname, filename))
                     catalog.add(message, None, [(filepath, lineno)],
-                                comments=comments)
+                                auto_comments=comments)
 
             log.info('writing PO template file to %s' % self.output_file)
             write_po(outfile, catalog, width=self.width,
                      no_location=self.no_location,
                      omit_header=self.omit_header,
                      sort_output=self.sort_output,
-                     sort_by_file=self.sort_by_file,
-                     copyright_holder=self.copyright_holder)
+                     sort_by_file=self.sort_by_file)
         finally:
             outfile.close()
 
@@ -455,6 +455,7 @@
 
         try:
             catalog = Catalog(msgid_bugs_address=options.msgid_bugs_address,
+                              copyright_holder=options.copyright_holder,
                               charset=options.charset)
 
             for dirname in args:
--- a/babel/messages/pofile.py
+++ b/babel/messages/pofile.py
@@ -68,11 +68,14 @@
     >>> for message in catalog:
     ...     if message.id:
     ...         print (message.id, message.string)
-    ...         print ' ', (message.locations, message.flags, message.comments)
+    ...         print ' ', (message.locations, message.flags)
+    ...         print ' ', (message.user_comments, message.auto_comments)
     ('foo %(name)s', '')
-      ([('main.py', 1)], set(['fuzzy', 'python-format']), [])
+      ([('main.py', 1)], set(['fuzzy', 'python-format']))
+      ([], [])
     (('bar', 'baz'), ('', ''))
-      ([('main.py', 3)], set([]), ['A user comment', 'An auto comment'])
+      ([('main.py', 3)], set([]))
+      (['An auto comment'], ['A user comment'])
     
     :param fileobj: the file-like object to read the PO file from
     :return: an iterator over ``(message, translation, location)`` tuples
@@ -84,7 +87,8 @@
     translations = []
     locations = []
     flags = []
-    comments = []
+    user_comments = []
+    auto_comments = []
     in_msgid = in_msgstr = False
     in_header = True
     header_lines = []
@@ -99,9 +103,10 @@
             string = tuple([t[1] for t in translations])
         else:
             string = translations[0][1]
-        catalog.add(msgid, string, list(locations), set(flags), list(comments))
+        catalog.add(msgid, string, list(locations), set(flags),
+                    list(user_comments), list(auto_comments))
         del messages[:]; del translations[:]; del locations[:];
-        del flags[:]; del comments[:]
+        del flags[:]; del auto_comments[:]; del user_comments[:]
 
     for line in fileobj.readlines():
         line = line.strip()
@@ -124,13 +129,13 @@
                     comment = line[2:].strip()
                     if comment:
                         # Just check that we're not adding empty comments
-                        comments.append(comment)
+                        auto_comments.append(comment)
                 elif line[1:].startswith(' '):
                     # These are called user comments
                     comment = line[1:].strip()
                     if comment:
                         # Just check that we're not adding empty comments
-                        comments.append(comment)
+                        user_comments.append(comment)
         else:
             in_header = False
             if line.startswith('msgid_plural'):
@@ -310,8 +315,13 @@
                 comment_header = u'\n'.join(lines) + u'\n'
             _write(comment_header)
 
-        if message.comments:
-            for comment in message.comments:
+        if message.user_comments:
+            for comment in message.user_comments:
+                for line in wrap(comment, width, break_long_words=False):
+                    _write('# %s\n' % line.strip())
+
+        if message.auto_comments:
+            for comment in message.auto_comments:
                 for line in wrap(comment, width, break_long_words=False):
                     _write('#. %s\n' % line.strip())
 
--- a/babel/messages/tests/catalog.py
+++ b/babel/messages/tests/catalog.py
@@ -24,14 +24,14 @@
         assert catalog.PYTHON_FORMAT('foo %d bar')
         assert catalog.PYTHON_FORMAT('foo %s bar')
         assert catalog.PYTHON_FORMAT('foo %r bar')        
-    
+
     def test_translator_comments(self):
-        mess = catalog.Message('foo', comments=['Comment About `foo`'])
-        self.assertEqual(mess.comments, ['Comment About `foo`'])        
+        mess = catalog.Message('foo', user_comments=['Comment About `foo`'])
+        self.assertEqual(mess.user_comments, ['Comment About `foo`'])        
         mess = catalog.Message('foo',
-                               comments=['Comment 1 About `foo`',
+                               auto_comments=['Comment 1 About `foo`',
                                          'Comment 2 About `foo`'])
-        self.assertEqual(mess.comments, ['Comment 1 About `foo`',
+        self.assertEqual(mess.auto_comments, ['Comment 1 About `foo`',
                                          'Comment 2 About `foo`'])
 
 
@@ -46,16 +46,16 @@
     def test_update_message_updates_comments(self):
         cat = catalog.Catalog()
         cat[u'foo'] = catalog.Message('foo', locations=[('main.py', 5)])
-        self.assertEqual(cat[u'foo'].comments, [])
+        self.assertEqual(cat[u'foo'].auto_comments, [])
+        self.assertEqual(cat[u'foo'].user_comments, [])
         # Update cat[u'foo'] with a new location and a comment
         cat[u'foo'] = catalog.Message('foo', locations=[('main.py', 7)],
-                                      comments=['Foo Bar comment 1'])
-        self.assertEqual(cat[u'foo'].comments, ['Foo Bar comment 1'])
+                                      user_comments=['Foo Bar comment 1'])
+        self.assertEqual(cat[u'foo'].user_comments, ['Foo Bar comment 1'])
         # now add yet another location with another comment
         cat[u'foo'] = catalog.Message('foo', locations=[('main.py', 9)],
-                                      comments=['Foo Bar comment 2'])        
-        self.assertEqual(cat[u'foo'].comments,
-                         ['Foo Bar comment 1', 'Foo Bar comment 2'])
+                                      auto_comments=['Foo Bar comment 2'])        
+        self.assertEqual(cat[u'foo'].auto_comments, ['Foo Bar comment 2'])
 
 
 def suite():
--- a/babel/messages/tests/pofile.py
+++ b/babel/messages/tests/pofile.py
@@ -89,10 +89,10 @@
     def test_pot_with_translator_comments(self):
         catalog = Catalog()
         catalog.add(u'foo', locations=[('main.py', 1)],
-                    comments=['Comment About `foo`'])
+                    auto_comments=['Comment About `foo`'])
         catalog.add(u'bar', locations=[('utils.py', 3)],
-                    comments=['Comment About `bar` with',
-                              'multiple lines.'])
+                    user_comments=['Comment About `bar` with',
+                                   'multiple lines.'])
         buf = StringIO()
         pofile.write_po(buf, catalog, omit_header=True)
         self.assertEqual('''#. Comment About `foo`
@@ -100,8 +100,8 @@
 msgid "foo"
 msgstr ""
 
-#. Comment About `bar` with
-#. multiple lines.
+# Comment About `bar` with
+# multiple lines.
 #: utils.py:3
 msgid "bar"
 msgstr ""''', buf.getvalue().strip())
Copyright (C) 2012-2017 Edgewall Software