changeset 546:10de195cfb04

catalog.add() now returns the message instance (closes #245)
author fschwarz
date Sat, 19 Mar 2011 19:28:59 +0000
parents 1d386483ccb6
children 274f9a6485d4
files babel/messages/catalog.py babel/messages/mofile.py babel/messages/pofile.py babel/messages/tests/catalog.py babel/messages/tests/mofile.py babel/messages/tests/pofile.py
diffstat 6 files changed, 27 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/babel/messages/catalog.py
+++ b/babel/messages/catalog.py
@@ -607,6 +607,7 @@
 
         >>> catalog = Catalog()
         >>> catalog.add(u'foo')
+        <Message ...>
         >>> catalog[u'foo']
         <Message u'foo' (flags: [])>
 
@@ -626,10 +627,14 @@
         :param lineno: the line number on which the msgid line was found in the
                        PO file, if any
         :param context: the message context
+        :return: the newly added message
+        :rtype: `Message`
         """
-        self[id] = Message(id, string, list(locations), flags, auto_comments,
-                           user_comments, previous_id, lineno=lineno,
-                           context=context)
+        message = Message(id, string, list(locations), flags, auto_comments,
+                          user_comments, previous_id, lineno=lineno,
+                          context=context)
+        self[id] = message
+        return message
 
     def check(self):
         """Run various validation checks on the translations in the catalog.
@@ -672,13 +677,19 @@
         >>> from babel.messages import Catalog
         >>> template = Catalog()
         >>> template.add('green', locations=[('main.py', 99)])
+        <Message ...>
         >>> template.add('blue', locations=[('main.py', 100)])
+        <Message ...>
         >>> template.add(('salad', 'salads'), locations=[('util.py', 42)])
+        <Message ...>
         >>> catalog = Catalog(locale='de_DE')
         >>> catalog.add('blue', u'blau', locations=[('main.py', 98)])
+        <Message ...>
         >>> catalog.add('head', u'Kopf', locations=[('util.py', 33)])
+        <Message ...>
         >>> catalog.add(('salad', 'salads'), (u'Salat', u'Salate'),
         ...             locations=[('util.py', 38)])
+        <Message ...>
 
         >>> catalog.update(template)
         >>> len(catalog)
--- a/babel/messages/mofile.py
+++ b/babel/messages/mofile.py
@@ -125,10 +125,15 @@
     
     >>> catalog = Catalog(locale='en_US')
     >>> catalog.add('foo', 'Voh')
+    <Message ...>
     >>> catalog.add((u'bar', u'baz'), (u'Bahr', u'Batz'))
+    <Message ...>
     >>> catalog.add('fuz', 'Futz', flags=['fuzzy'])
+    <Message ...>
     >>> catalog.add('Fizz', '')
+    <Message ...>
     >>> catalog.add(('Fuzz', 'Fuzzes'), ('', ''))
+    <Message ...>
     >>> buf = StringIO()
     
     >>> write_mo(buf, catalog)
--- a/babel/messages/pofile.py
+++ b/babel/messages/pofile.py
@@ -345,7 +345,9 @@
     >>> catalog = Catalog()
     >>> catalog.add(u'foo %(name)s', locations=[('main.py', 1)],
     ...             flags=('fuzzy',))
+    <Message...>
     >>> catalog.add((u'bar', u'baz'), locations=[('main.py', 3)])
+    <Message...>
     >>> from StringIO import StringIO
     >>> buf = StringIO()
     >>> write_po(buf, catalog, omit_header=True)
--- a/babel/messages/tests/catalog.py
+++ b/babel/messages/tests/catalog.py
@@ -57,6 +57,10 @@
 
 
 class CatalogTestCase(unittest.TestCase):
+    def test_add_returns_message_instance(self):
+        cat = catalog.Catalog()
+        message = cat.add('foo')
+        self.assertEquals('foo', message.id)
 
     def test_two_messages_with_same_singular(self):
         cat = catalog.Catalog()
--- a/babel/messages/tests/mofile.py
+++ b/babel/messages/tests/mofile.py
@@ -78,7 +78,7 @@
 
 def suite():
     suite = unittest.TestSuite()
-    suite.addTest(doctest.DocTestSuite(mofile))
+    suite.addTest(doctest.DocTestSuite(mofile, optionflags=doctest.ELLIPSIS))
     suite.addTest(unittest.makeSuite(ReadMoTestCase))
     suite.addTest(unittest.makeSuite(WriteMoTestCase))
     return suite
--- a/babel/messages/tests/pofile.py
+++ b/babel/messages/tests/pofile.py
@@ -498,7 +498,7 @@
 
 def suite():
     suite = unittest.TestSuite()
-    suite.addTest(doctest.DocTestSuite(pofile))
+    suite.addTest(doctest.DocTestSuite(pofile, optionflags=doctest.ELLIPSIS))
     suite.addTest(unittest.makeSuite(ReadPoTestCase))
     suite.addTest(unittest.makeSuite(WritePoTestCase))
     return suite
Copyright (C) 2012-2017 Edgewall Software