]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
read_po: Support multiline obsolete units
authorMichael Birtwell <michael.birtwell@starleaf.com>
Fri, 8 Jul 2016 13:33:12 +0000 (14:33 +0100)
committerMichael Birtwell <michael.birtwell@starleaf.com>
Fri, 8 Jul 2016 13:33:12 +0000 (14:33 +0100)
babel/messages/pofile.py
tests/messages/test_pofile.py

index 194e961972a0bebd6135cab55c909eeef4c8f960..bb07b4bbd76e8e86eadbc53cc80a53e8a3ec7cd0 100644 (file)
@@ -164,6 +164,34 @@ class _PoFileParser(object):
             elif self.in_msgctxt:
                 self.context.append(line.rstrip())
 
+    def _process_comment(self, line):
+
+        self.in_msgid = self.in_msgstr = False
+        if self.messages and self.translations:
+            self._add_message()
+        if line[1:].startswith(':'):
+            for location in line[2:].lstrip().split():
+                pos = location.rfind(':')
+                if pos >= 0:
+                    try:
+                        lineno = int(location[pos + 1:])
+                    except ValueError:
+                        continue
+                    self.locations.append((location[:pos], lineno))
+                else:
+                    self.locations.append((location, None))
+        elif line[1:].startswith(','):
+            for flag in line[2:].lstrip().split(','):
+                self.flags.append(flag.strip())
+        elif line[1:].startswith('.'):
+            # These are called auto-comments
+            comment = line[2:].strip()
+            if comment:  # Just check that we're not adding empty comments
+                self.auto_comments.append(comment)
+        else:
+            # These are called user comments
+            self.user_comments.append(line[1:].strip())
+
     def parse(self, fileobj):
 
         for lineno, line in enumerate(fileobj.readlines()):
@@ -171,34 +199,11 @@ class _PoFileParser(object):
             if not isinstance(line, text_type):
                 line = line.decode(self.catalog.charset)
             if line.startswith('#'):
-                self.in_msgid = self.in_msgstr = False
-                if self.messages and self.translations:
-                    self._add_message()
-                if line[1:].startswith(':'):
-                    for location in line[2:].lstrip().split():
-                        pos = location.rfind(':')
-                        if pos >= 0:
-                            try:
-                                lineno = int(location[pos + 1:])
-                            except ValueError:
-                                continue
-                            self.locations.append((location[:pos], lineno))
-                        else:
-                            self.locations.append((location, None))
-                elif line[1:].startswith(','):
-                    for flag in line[2:].lstrip().split(','):
-                        self.flags.append(flag.strip())
-                elif line[1:].startswith('~'):
+                if line[1:].startswith('~'):
                     self.obsolete = True
                     self._process_message_line(lineno, line[2:].lstrip())
-                elif line[1:].startswith('.'):
-                    # These are called auto-comments
-                    comment = line[2:].strip()
-                    if comment:  # Just check that we're not adding empty comments
-                        self.auto_comments.append(comment)
                 else:
-                    # These are called user comments
-                    self.user_comments.append(line[1:].strip())
+                    self._process_comment(line)
             else:
                 self._process_message_line(lineno, line)
 
index 1bac3603c2759c6218b87087e14fe5d5e55877eb..a271a846917b70399a633eee2fba3eed71b203eb 100644 (file)
@@ -164,6 +164,48 @@ msgstr "Bahr"
         self.assertEqual(1, len(catalog))
         self.assertEqual(0, len(catalog.obsolete))
 
+    def test_multi_line_obsolete_message(self):
+        buf = StringIO(r'''# This is an obsolete message
+#~ msgid ""
+#~ "foo"
+#~ "foo"
+#~ msgstr ""
+#~ "Voh"
+#~ "Vooooh"
+
+# This message is not obsolete
+#: main.py:1
+msgid "bar"
+msgstr "Bahr"
+''')
+        catalog = pofile.read_po(buf)
+        self.assertEqual(1, len(catalog.obsolete))
+        message = catalog.obsolete[u'foofoo']
+        self.assertEqual(u'foofoo', message.id)
+        self.assertEqual(u'VohVooooh', message.string)
+        self.assertEqual(['This is an obsolete message'], message.user_comments)
+
+    def test_unit_following_multi_line_obsolete_message(self):
+        buf = StringIO(r'''# This is an obsolete message
+#~ msgid ""
+#~ "foo"
+#~ "fooooooo"
+#~ msgstr ""
+#~ "Voh"
+#~ "Vooooh"
+
+# This message is not obsolete
+#: main.py:1
+msgid "bar"
+msgstr "Bahr"
+''')
+        catalog = pofile.read_po(buf)
+        self.assertEqual(1, len(catalog))
+        message = catalog[u'bar']
+        self.assertEqual(u'bar', message.id)
+        self.assertEqual(u'Bahr', message.string)
+        self.assertEqual(['This message is not obsolete'], message.user_comments)
+
     def test_with_context(self):
         buf = BytesIO(b'''# Some string in the menu
 #: main.py:1