Ejemplo n.º 1
0
def time_tal(fn, count):
    p = HTMLTALParser()
    p.parseFile(fn)
    program, macros = p.getCode()
    engine = DummyEngine(macros)
    engine.globals = data
    tal = TALInterpreter(program,
                         macros,
                         engine,
                         StringIO(),
                         wrap=0,
                         tal=1,
                         strictinsert=0)
    return time_apply(tal, (), {}, count)
Ejemplo n.º 2
0
 def setUp(self):
     program = []
     macros = {}
     engine = DummyEngine()
     self.interpreter = TALInterpreter(program, macros, engine)
     self.sio = self.interpreter.stream = StringIO()
     self.interpreter._pending_source_annotation = True
Ejemplo n.º 3
0
    def test_div_in_p_using_macro(self):
        dummy, macros = self._compile('<p metal:define-macro="M">Booh</p>')
        engine = DummyEngine(macros)
        program, dummy = self._compile(
            '<p metal:use-macro="M"><div>foo</div></p>')
        interpreter = TALInterpreter(program, {}, engine)

        output = interpreter()
        self.assertEqual(output, '<p><div>foo</div></p>')
Ejemplo n.º 4
0
def profile_tal(fn, count, profiler):
    p = HTMLTALParser()
    p.parseFile(fn)
    program, macros = p.getCode()
    engine = DummyEngine(macros)
    engine.globals = data
    tal = TALInterpreter(program,
                         macros,
                         engine,
                         StringIO(),
                         wrap=0,
                         tal=1,
                         strictinsert=0)
    for i in range(4):
        tal()
    r = [None] * count
    for i in r:
        profiler.runcall(tal)
Ejemplo n.º 5
0
 def evaluatePathOrVar(self, expr):
     if expr == 'here/currentTime':
         return {
             'hours': 6,
             'minutes': 59,
             'ampm': 'PM',
         }
     elif expr == 'context/@@object_name':
         return '7'
     elif expr == 'request/submitter':
         return '*****@*****.**'
     return DummyEngine.evaluatePathOrVar(self, expr)
Ejemplo n.º 6
0
 def setUp(self):
     s = self._read(('input', 'pnome_template.pt'))
     self.pnome_program, pnome_macros = self._compile(s)
     s = self._read(('input', 'acme_template.pt'))
     self.acme_program, acme_macros = self._compile(s)
     s = self._read(('input', 'document_list.pt'))
     self.doclist_program, doclist_macros = self._compile(s)
     macros = {
         'pnome_macros_page': pnome_macros['page'],
         'acme_macros_page': acme_macros['page'],
         }
     self.engine = DummyEngine(macros)
Ejemplo n.º 7
0
def test():
    import sys
    p = TALParser()
    file = "tests/input/test01.xml"
    if sys.argv[1:]:
        file = sys.argv[1]
    p.parseFile(file)
    program, macros = p.getCode()
    from zpt._zope.tal.talinterpreter import TALInterpreter
    from zpt._zope.tal.dummyengine import DummyEngine
    engine = DummyEngine(macros)
    TALInterpreter(program, macros, engine, sys.stdout, wrap=0)()
Ejemplo n.º 8
0
    def test_source_positions(self):
        # Ensure source file and position are set correctly by TAL
        macros = {}
        eng = DummyEngine(macros)
        page1_program, page1_macros = self.parse(eng, page1, 'page1')
        main_template_program, main_template_macros = self.parse(
            eng, main_template, 'main_template')
        footer_program, footer_macros = self.parse(eng, footer, 'footer')

        macros['main'] = main_template_macros['main']
        macros['foot'] = footer_macros['foot']

        stream = StringIO()
        interp = TALInterpreter(page1_program, macros, eng, stream)
        interp()
        self.assertEqual(
            stream.getvalue().strip(), expected.strip(),
            "Got result:\n%s\nExpected:\n%s" % (stream.getvalue(), expected))
Ejemplo n.º 9
0
def interpretit(it,
                engine=None,
                stream=None,
                tal=1,
                showtal=-1,
                strictinsert=1,
                i18nInterpolate=1,
                sourceAnnotations=0):
    from zpt._zope.tal.talinterpreter import TALInterpreter
    program, macros = it
    assert zpt._zope.tal.taldefs.isCurrentVersion(program)
    if engine is None:
        engine = DummyEngine(macros)
    TALInterpreter(program,
                   macros,
                   engine,
                   stream,
                   wrap=0,
                   tal=tal,
                   showtal=showtal,
                   strictinsert=strictinsert,
                   i18nInterpolate=i18nInterpolate,
                   sourceAnnotations=sourceAnnotations)()
Ejemplo n.º 10
0
 def __init__(self, macros=None):
     DummyEngine.__init__(self, macros)
     self.translationDomain = TestTranslations()
Ejemplo n.º 11
0
 def compare(self, INPUT, EXPECTED):
     program, macros = self._compile(INPUT)
     sio = StringIO()
     interp = TALInterpreter(program, {}, DummyEngine(), sio, wrap=60)
     interp()
     self.assertEqual(sio.getvalue(), EXPECTED)
Ejemplo n.º 12
0
 def setUp(self):
     dummy, macros = self._compile('<p metal:define-macro="M">Booh</p>')
     self.macro = macros['M']
     self.engine = DummyEngine(macros)
     program, dummy = self._compile('<p metal:use-macro="M">Bah</p>')
     self.interpreter = TALInterpreter(program, {}, self.engine)
Ejemplo n.º 13
0
 def setUp(self):
     self.engine = DummyEngine()
Ejemplo n.º 14
0
 def setUp(self):
     self.engine = DummyEngine()
     # Make sure we'll translate the msgid not its unicode representation
     self.engine.setLocal('foo',
         self.factory('FoOvAlUe${empty}', 'default', {'empty': ''}))
     self.engine.setLocal('bar', 'BaRvAlUe')
Ejemplo n.º 15
0
class I18NCornerTestCaseBase(TestCaseBase):

    def factory(self, msgid, default, mapping={}):
        raise NotImplementedError("abstract method")

    def setUp(self):
        self.engine = DummyEngine()
        # Make sure we'll translate the msgid not its unicode representation
        self.engine.setLocal('foo',
            self.factory('FoOvAlUe${empty}', 'default', {'empty': ''}))
        self.engine.setLocal('bar', 'BaRvAlUe')

    def _check(self, program, expected):
        result = StringIO()
        self.interpreter = TALInterpreter(program, {}, self.engine,
                                          stream=result)
        self.interpreter()
        self.assertEqual(expected, result.getvalue())

    def test_simple_messageid_translate(self):
        # This test is mainly here to make sure our DummyEngine works
        # correctly.
        program, macros = self._compile(
            '<span i18n:translate="" tal:content="foo"/>')
        self._check(program, '<span>FOOVALUE</span>\n')

        program, macros = self._compile(
            '<span i18n:translate="" tal:replace="foo"/>')
        self._check(program, 'FOOVALUE\n')

    def test_replace_with_messageid_and_i18nname(self):
        program, macros = self._compile(
            '<div i18n:translate="" >'
            '<span i18n:translate="" tal:replace="foo" i18n:name="foo_name"/>'
            '</div>')
        self._check(program, '<div>FOOVALUE</div>\n')

    def test_pythonexpr_replace_with_messageid_and_i18nname(self):
        program, macros = self._compile(
            '<div i18n:translate="" >'
            '<span i18n:translate="" tal:replace="python: foo"'
            '    i18n:name="foo_name"/>'
            '</div>')
        self._check(program, '<div>FOOVALUE</div>\n')

    def test_structure_replace_with_messageid_and_i18nname(self):
        program, macros = self._compile(
            '<div i18n:translate="" >'
            '<span i18n:translate="" tal:replace="structure foo"'
            '    i18n:name="foo_name"/>'
            '</div>')
        self._check(program, '<div>FOOVALUE</div>\n')

    def test_complex_replace_with_messageid_and_i18nname(self):
        program, macros = self._compile(
            '<div i18n:translate="" >'
            '<em tal:omit-tag="" i18n:name="foo_name">'
            '<span i18n:translate="" tal:replace="foo"/>'
            '</em>'
            '</div>')
        self._check(program, '<div>FOOVALUE</div>\n')

    def test_content_with_messageid_and_i18nname(self):
        program, macros = self._compile(
            '<div i18n:translate="" >'
            '<span i18n:translate="" tal:content="foo" i18n:name="foo_name"/>'
            '</div>')
        self._check(program, '<div><span>FOOVALUE</span></div>\n')

    def test_content_with_messageid_and_i18nname_and_i18ntranslate(self):
        # Let's tell the user this is incredibly silly!
        self.assertRaises(
            I18NError, self._compile,
            '<span i18n:translate="" tal:content="foo" i18n:name="foo_name"/>')

    def test_content_with_explicit_messageid(self):
        # Let's tell the user this is incredibly silly!
        self.assertRaises(
            I18NError, self._compile,
            '<span i18n:translate="ID" tal:content="foo" />')

    def test_content_with_plaintext_and_i18nname_and_i18ntranslate(self):
        # Let's tell the user this is incredibly silly!
        self.assertRaises(
            I18NError, self._compile,
            '<span i18n:translate="" i18n:name="color_name">green</span>')

    def test_translate_static_text_as_dynamic(self):
        program, macros = self._compile(
            '<div i18n:translate="">This is text for '
            '<span i18n:translate="" tal:content="bar" i18n:name="bar_name"/>.'
            '</div>')
        self._check(program,
                    '<div>THIS IS TEXT FOR <span>BaRvAlUe</span>.</div>\n')

    def test_translate_static_text_as_dynamic_from_bytecode(self):
        program =  [('version', TAL_VERSION),
 ('mode', 'html'),
('setPosition', (1, 0)),
('beginScope', {'i18n:translate': ''}),
('startTag', ('div', [('i18n:translate', '', 'i18n')])),
('insertTranslation',
 ('',
  [('rawtextOffset', ('This is text for ', 17)),
   ('setPosition', (1, 40)),
   ('beginScope',
    {'tal:content': 'bar', 'i18n:name': 'bar_name', 'i18n:translate': ''}),
   ('i18nVariable',
       ('bar_name',
        [('startTag',
           ('span',
            [('i18n:translate', '', 'i18n'),
             ('tal:content', 'bar', 'tal'),
             ('i18n:name', 'bar_name', 'i18n')])),
         ('insertTranslation',
           ('',
             [('insertText', ('$bar$', []))])),
         ('rawtextOffset', ('</span>', 7))],
        None,
        0)),
   ('endScope', ()),
   ('rawtextOffset', ('.', 1))])),
('endScope', ()),
('rawtextOffset', ('</div>', 6)) 
]
        self._check(program,
                    '<div>THIS IS TEXT FOR <span>BARVALUE</span>.</div>\n')

    def _getCollectingTranslationDomain(self):
        class CollectingTranslationDomain(DummyTranslationDomain):
            data = []

            def translate(self, msgid, mapping=None,
                          context=None, target_language=None, default=None):
                self.data.append((msgid, mapping))
                return DummyTranslationDomain.translate(
                    self,
                    msgid, mapping, context, target_language, default)

        xlatdmn = CollectingTranslationDomain()
        self.engine.translationDomain = xlatdmn
        return xlatdmn

    def test_for_correct_msgids(self):
        xlatdmn = self._getCollectingTranslationDomain()
        result = StringIO()
        program, macros = self._compile(
            '<div i18n:translate="">This is text for '
            '<span i18n:translate="" tal:content="bar" '
            'i18n:name="bar_name"/>.</div>')
        self.interpreter = TALInterpreter(program, {}, self.engine,
                                          stream=result)
        self.interpreter()
        msgids = list(xlatdmn.data)
        msgids.sort()
        self.assertEqual(1, len(msgids))
        self.assertEqual('This is text for ${bar_name}.', msgids[0][0])
        self.assertEqual({'bar_name': '<span>BaRvAlUe</span>'}, msgids[0][1])
        self.assertEqual(
            '<div>THIS IS TEXT FOR <span>BaRvAlUe</span>.</div>\n',
            result.getvalue())

    def test_i18ntranslate_i18nname_and_attributes(self):
        # Test for Issue 301: Bug with i18n:name and i18n:translate
        # on the same element
        xlatdmn = self._getCollectingTranslationDomain()
        result = StringIO()
        program, macros = self._compile(
            '<p i18n:translate="">'
            'Some static text and a <a tal:attributes="href string:url"'
            ' i18n:name="link" i18n:translate="">link text</a>.</p>')
        self.interpreter = TALInterpreter(program, {}, self.engine,
                                          stream=result)
        self.interpreter()
        msgids = list(xlatdmn.data)
        msgids.sort()
        self.assertEqual(2, len(msgids))
        self.assertEqual('Some static text and a ${link}.', msgids[0][0])
        self.assertEqual({'link': '<a href="url">LINK TEXT</a>'}, msgids[0][1])
        self.assertEqual('link text', msgids[1][0])
        self.assertEqual(
            '<p>SOME STATIC TEXT AND A <a href="url">LINK TEXT</a>.</p>\n',
            result.getvalue())

    def test_for_raw_msgids(self):
        # Test for Issue 314: i18n:translate removes line breaks from
        # <pre>...</pre> contents
        # HTML mode
        xlatdmn = self._getCollectingTranslationDomain()
        result = StringIO()
        program, macros = self._compile(
            '<div i18n:translate=""> This is text\n'
            ' \tfor\n div. </div>'
            '<pre i18n:translate=""> This is text\n'
            ' <b>\tfor</b>\n pre. </pre>')
        self.interpreter = TALInterpreter(program, {}, self.engine,
                                          stream=result)
        self.interpreter()
        msgids = list(xlatdmn.data)
        msgids.sort()
        self.assertEqual(2, len(msgids))
        self.assertEqual(' This is text\n <b>\tfor</b>\n pre. ', msgids[0][0])
        self.assertEqual('This is text for div.', msgids[1][0])
        self.assertEqual(
            '<div>THIS IS TEXT FOR DIV.</div>'
            '<pre> THIS IS TEXT\n <B>\tFOR</B>\n PRE. </pre>\n',
            result.getvalue())

        # XML mode
        xlatdmn = self._getCollectingTranslationDomain()
        result = StringIO()
        parser = TALParser()
        parser.parseString(
            '<?xml version="1.0"?>\n'
            '<pre xmlns:i18n="http://xml.zpt._zope.org/namespaces/i18n"'
            ' i18n:translate=""> This is text\n'
            ' <b>\tfor</b>\n barvalue. </pre>')
        program, macros = parser.getCode()
        self.interpreter = TALInterpreter(program, {}, self.engine,
                                          stream=result)
        self.interpreter()
        msgids = list(xlatdmn.data)
        msgids.sort()
        self.assertEqual(1, len(msgids))
        self.assertEqual('This is text <b> for</b> barvalue.', msgids[0][0])
        self.assertEqual(
            '<?xml version="1.0"?>\n'
            '<pre>THIS IS TEXT <B> FOR</B> BARVALUE.</pre>\n',
            result.getvalue())

    def test_raw_msgids_and_i18ntranslate_i18nname(self):
        xlatdmn = self._getCollectingTranslationDomain()
        result = StringIO()
        program, macros = self._compile(
            '<div i18n:translate=""> This is text\n \tfor\n'
            '<pre i18n:name="bar" i18n:translate=""> \tbar\n </pre>.</div>')
        self.interpreter = TALInterpreter(program, {}, self.engine,
                                          stream=result)
        self.interpreter()
        msgids = list(xlatdmn.data)
        msgids.sort()
        self.assertEqual(2, len(msgids))
        self.assertEqual(' \tbar\n ', msgids[0][0])
        self.assertEqual('This is text for ${bar}.', msgids[1][0])
        self.assertEqual({'bar': '<pre> \tBAR\n </pre>'}, msgids[1][1])
        self.assertEqual(
            u'<div>THIS IS TEXT FOR <pre> \tBAR\n </pre>.</div>\n',
            result.getvalue())

    def test_for_handling_unicode_vars(self):
        # Make sure that non-ASCII Unicode is substituted correctly.
        # http://collector.zpt._zope.org/Zope3-dev/264
        program, macros = self._compile(
            "<div i18n:translate='' tal:define='bar python:unichr(0xC0)'>"
            "Foo <span tal:replace='bar' i18n:name='bar' /></div>")
        self._check(program, u"<div>FOO \u00C0</div>\n")
Ejemplo n.º 16
0
 def __init__(self, macros=None):
     self.catalog = {}
     DummyEngine.__init__(self, macros)