From: Michael Birtwell Date: Fri, 8 Jul 2016 13:33:12 +0000 (+0100) Subject: read_po: Support multiline obsolete units X-Git-Tag: v2.4.0~20^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=832e58ab0c564aa50ff93b7680c12f524c5948bc;p=thirdparty%2Fbabel.git read_po: Support multiline obsolete units --- diff --git a/babel/messages/pofile.py b/babel/messages/pofile.py index 194e9619..bb07b4bb 100644 --- a/babel/messages/pofile.py +++ b/babel/messages/pofile.py @@ -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) diff --git a/tests/messages/test_pofile.py b/tests/messages/test_pofile.py index 1bac3603..a271a846 100644 --- a/tests/messages/test_pofile.py +++ b/tests/messages/test_pofile.py @@ -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