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)
    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))
 def test_raw_msgids_and_i18ntranslate_i18nname(self):
     self.engine.translationDomain.clearMsgids()
     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 = self.engine.translationDomain.getMsgids('default')
     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>'),
         result.getvalue())
Beispiel #4
0
def talEval(expression, context, extra=None):
    """
    Perform a TAL eval on the expression.
    """
    # First, account for the possibility that it is merely TALES; if there are
    # no <tal> in it at all (nor the ${python:} you can do with this function),
    # just send it to talesEval
    isTales = '<tal' not in expression and '${python:' not in expression
    if isTales:
        return talesEvalStr(expression, context, extra)

    contextDict = {
        'context': context,
        'here': context,
        'nothing': None,
        'now': DateTime(),
    }
    if isinstance(extra, dict):
        contextDict.update(extra)

    # Next, as a convenience, replace all ${} blocks that aren't inside a <tal>
    # with <tal:block content="..."/> equivalent
    chunks = TAG.split(expression)
    modified = []
    for chunk in chunks:
        if chunk.startswith('<tal'):
            modified.append(chunk)
        else:
            modified.append(TPLBLOCK.sub(_chunk_repl, chunk))
    expression = ''.join(modified)

    # Finally, compile the expression and apply context
    gen = TALGenerator(Engine, xml=0)
    parser = HTMLTALParser(gen)
    parser.parseString(expression)
    program, macros = parser.getCode()
    output = cStringIO.StringIO()
    context = Engine.getContext(contextDict)
    TALInterpreter(program, macros, context, output, tal=True)()
    return output.getvalue()
 def test_i18ntranslate_i18nname_and_attributes(self):
     # Test for Issue 301: Bug with i18n:name and i18n:translate
     # on the same element
     self.engine.translationDomain.clearMsgids()
     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 = self.engine.translationDomain.getMsgids('default')
     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>',
         result.getvalue())
 def test_define_slot_restores_source_file_if_no_exception(self):
     _m_program, m_macros = self._compile("""
         <div metal:define-macro="amacro">
           <div metal:define-slot="aslot">
           </div>
           <tal:x replace="no_such_thing" />
         </div>
         """,
                                          source_file='macros.pt')
     p_program, _p_macros = self._compile("""
         <div metal:use-macro="amacro">
           <div metal:fill-slot="aslot">
           </div>
         </div>
         """,
                                          source_file='page.pt')
     engine = DummyEngine(macros=m_macros)
     interp = TALInterpreter(p_program, {}, engine, StringIO())
     # Expect TALExpressionError: unknown variable: 'no_such_thing'
     self.assertRaises(TALExpressionError, interp)
     # Now the engine should know where the error occurred
     self.assertEqual(engine.source_file, 'macros.pt')
     self.assertEqual(engine.position, (5, 14))
Beispiel #7
0
def interpretit(it,
                engine=None,
                stream=None,
                tal=1,
                showtal=-1,
                strictinsert=1,
                i18nInterpolate=1,
                sourceAnnotations=0):
    from zope.tal.talinterpreter import TALInterpreter
    program, macros = it
    assert 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)()
    def pt_render(self,
                  namespace,
                  source=False,
                  sourceAnnotations=False,
                  showtal=False):
        """Render this Page Template"""
        self._cook_check()
        __traceback_supplement__ = (PageTemplateTracebackSupplement, self,
                                    namespace)
        if self._v_errors:
            raise PTRuntimeError(str(self._v_errors))

        output = StringIO(u'')
        context = self.pt_getEngineContext(namespace)
        TALInterpreter(self._v_program,
                       self._v_macros,
                       context,
                       output,
                       tal=not source,
                       showtal=showtal,
                       strictinsert=0,
                       sourceAnnotations=sourceAnnotations)()
        return output.getvalue()
 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)
 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)