- py3k adjustments for babel/lingua
diff --git a/mako/ext/extract.py b/mako/ext/extract.py index 0d9c60c..5ce5175 100644 --- a/mako/ext/extract.py +++ b/mako/ext/extract.py
@@ -6,19 +6,22 @@ class MessageExtractor(object): def process_file(self, fileobj): - template_node = lexer.Lexer(fileobj.read(), - input_encoding=self.config['encoding']).parse() + template_node = lexer.Lexer( + fileobj.read(), + input_encoding=self.config['encoding']).parse() for extracted in self.extract_nodes(template_node.get_children()): yield extracted def extract_nodes(self, nodes): translator_comments = [] in_translator_comments = False - comment_tags = filter(None, re.split(r'\s+', self.config['comment-tags'])) + comment_tags = list( + filter(None, re.split(r'\s+', self.config['comment-tags']))) for node in nodes: child_nodes = None - if in_translator_comments and isinstance(node, parsetree.Text) and \ + if in_translator_comments and \ + isinstance(node, parsetree.Text) and \ not node.content.strip(): # Ignore whitespace within translator comments continue @@ -26,13 +29,14 @@ if isinstance(node, parsetree.Comment): value = node.text.strip() if in_translator_comments: - translator_comments.extend(self._split_comment(node.lineno, value)) + translator_comments.extend( + self._split_comment(node.lineno, value)) continue for comment_tag in comment_tags: if value.startswith(comment_tag): in_translator_comments = True - translator_comments.extend(self._split_comment(node.lineno, - value)) + translator_comments.extend( + self._split_comment(node.lineno, value)) continue if isinstance(node, parsetree.DefTag): @@ -67,14 +71,17 @@ translator_comments[-1][0] < node.lineno - 1: translator_comments = [] - translator_strings = [comment[1] for comment in translator_comments] + translator_strings = [ + comment[1] for comment in translator_comments] if isinstance(code, compat.text_type): code = code.encode('ascii', 'backslashreplace') used_translator_comments = False code = compat.byte_buffer(code) - for message in self.process_python(code, node.lineno, translator_strings): + + for message in self.process_python( + code, node.lineno, translator_strings): yield message used_translator_comments = True @@ -88,7 +95,7 @@ @staticmethod def _split_comment(lineno, comment): - """Return the multiline comment at lineno split into a list of comment line - numbers and the accompanying comment line""" + """Return the multiline comment at lineno split into a list of + comment line numbers and the accompanying comment line""" return [(lineno + index, line) for index, line in enumerate(comment.splitlines())]
diff --git a/mako/ext/linguaplugin.py b/mako/ext/linguaplugin.py index 2f970d9..a809072 100644 --- a/mako/ext/linguaplugin.py +++ b/mako/ext/linguaplugin.py
@@ -3,15 +3,16 @@ from lingua.extractors import Message from lingua.extractors import get_extractor from mako.ext.extract import MessageExtractor +from mako import compat class LinguaMakoExtractor(Extractor, MessageExtractor): '''Mako templates''' extensions = ['.mako'] default_config = { - 'encoding': 'utf-8', - 'comment-tags': '', - } + 'encoding': 'utf-8', + 'comment-tags': '', + } def __call__(self, filename, options, fileobj=None): self.options = options @@ -23,13 +24,15 @@ def process_python(self, code, code_lineno, translator_strings): source = code.getvalue().strip() - if source.endswith(':'): - source += ' pass' + if source.endswith(compat.b(':')): + source += compat.b(' pass') code = io.BytesIO(source) - for msg in self.python_extractor(self.filename, self.options, code, code_lineno): + for msg in self.python_extractor( + self.filename, self.options, code, code_lineno): if translator_strings: msg = Message(msg.msgctxt, msg.msgid, msg.msgid_plural, msg.flags, - u' '.join(translator_strings + [msg.comment]), + compat.u(' ').join( + translator_strings + [msg.comment]), msg.tcomment, msg.location) yield msg
diff --git a/test/ext/test_babelplugin.py b/test/ext/test_babelplugin.py index e1cc409..c66260e 100644 --- a/test/ext/test_babelplugin.py +++ b/test/ext/test_babelplugin.py
@@ -2,6 +2,7 @@ import os import unittest from .. import TemplateTest, template_base, skip_if +from mako import compat try: import babel.messages.extract as babel @@ -21,27 +22,28 @@ class Test_extract(unittest.TestCase): @skip() def test_parse_python_expression(self): - input = io.BytesIO(b'<p>${_("Message")}</p>') + input = io.BytesIO(compat.b('<p>${_("Message")}</p>')) messages = list(extract(input, ['_'], [], {})) - self.assertEqual(messages, [(1, '_', u'Message', [])]) + self.assertEqual(messages, [(1, '_', compat.u('Message'), [])]) @skip() def test_python_gettext_call(self): - input = io.BytesIO(b'<p>${_("Message")}</p>') + input = io.BytesIO(compat.b('<p>${_("Message")}</p>')) messages = list(extract(input, ['_'], [], {})) - self.assertEqual(messages, [(1, '_', u'Message', [])]) + self.assertEqual(messages, [(1, '_', compat.u('Message'), [])]) @skip() def test_translator_comment(self): - input = io.BytesIO(b''' + input = io.BytesIO(compat.b(''' <p> ## TRANSLATORS: This is a comment. ${_("Message")} - </p>''') + </p>''')) messages = list(extract(input, ['_'], ['TRANSLATORS:'], {})) self.assertEqual( messages, - [(4, '_', u'Message', [u'TRANSLATORS: This is a comment.'])]) + [(4, '_', compat.u('Message'), + [compat.u('TRANSLATORS: This is a comment.')])]) class ExtractMakoTestCase(TemplateTest): @@ -64,13 +66,13 @@ (41, '_', 'Goodbye', ['TRANSLATOR: Good bye']), (44, '_', 'Babel', []), (45, 'ungettext', ('hella', 'hellas', None), []), - (62, '_', 'The', ['TRANSLATOR: Ensure so and', 'so, thanks']), - (62, 'ungettext', ('bunny', 'bunnies', None), []), - (68, '_', 'Goodbye, really!', ['TRANSLATOR: HTML comment']), - (71, '_', 'P.S. byebye', []), - (77, '_', 'Top', []), - (83, '_', 'foo', []), - (83, '_', 'hoho', []), + (62, '_', 'The', ['TRANSLATOR: Ensure so and', 'so, thanks']), + (62, 'ungettext', ('bunny', 'bunnies', None), []), + (68, '_', 'Goodbye, really!', ['TRANSLATOR: HTML comment']), + (71, '_', 'P.S. byebye', []), + (77, '_', 'Top', []), + (83, '_', 'foo', []), + (83, '_', 'hoho', []), (85, '_', 'bar', []), (92, '_', 'Inside a p tag', ['TRANSLATOR: <p> tag is ok?']), (95, '_', 'Later in a p tag', ['TRANSLATOR: also this']),