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
def translate(self, msgid, default=None, i18ndict=None, obj=None): # pylint:disable=arguments-differ try: self._i18n_message_id_dict[msgid] = None except TypeError: self._i18n_message_id_dict = {msgid:None} return TALInterpreter.translate(self, msgid, default, i18ndict, obj)
def translate(self, msgid, default=None, i18ndict=None, obj=None): try: self._i18n_message_id_dict[msgid] = None except TypeError: self._i18n_message_id_dict = {msgid:None} return TALInterpreter.translate(self, msgid, default, i18ndict, obj)
def _check(self, program, expected): result = StringIO() self.interpreter = TALInterpreter(program, {}, self.engine, stream=result) self.interpreter() self.assertEqual(expected, result.getvalue())
def translate(self, msgid, default=None, i18ndict=None, obj=None): try: self._i18n_message_id_dict[msgid] = None except TypeError: self._i18n_message_id_dict = {msgid: None} return TALInterpreter.translate(self, msgid, default, i18ndict, obj)
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) # 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(context) TALInterpreter(program, macros, context, output, tal=True)() return output.getvalue()
def test_for_correct_msgids(self): self.engine.translationDomain.clearMsgids() result = StringIO() #GChapelle: #I have the feeling the i18n:translate with the i18n:name is wrong # #program, macros = self._compile( # '<div i18n:translate="">This is text for ' # '<span i18n:translate="" tal:content="bar" ' # 'i18n:name="bar_name"/>.</div>') program, _macros = self._compile( '<div i18n:translate="">This is text for ' '<span tal:content="bar" ' 'i18n:name="bar_name"/>.</div>') self.interpreter = TALInterpreter(program, {}, self.engine, stream=result) self.interpreter() msgids = self.engine.translationDomain.getMsgids('default') 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>', result.getvalue())
def test_for_raw_msgids(self): # Test for Issue 314: i18n:translate removes line breaks from # <pre>...</pre> contents # HTML mode self.engine.translationDomain.clearMsgids() 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 = self.engine.translationDomain.getMsgids('default') 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>', result.getvalue()) # XML mode self.engine.translationDomain.clearMsgids() result = StringIO() parser = TALParser() parser.parseString( '<?xml version="1.0"?>\n' '<pre xmlns:i18n="http://xml.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 = self.engine.translationDomain.getMsgids('default') 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>', result.getvalue())
def __call__(self, context, macros, **options): output = StringIO(u'') interpreter = TALInterpreter( self.program, macros, context, stream=output, **options ) interpreter() return output.getvalue()
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>')
def renderMacro(self, name, **kwargs): """Render the given macro in Python code. """ self._cook_check() if self._v_errors: e = str(self._v_errors) return "Page Template %s has errors: %s" % (self.id, e) output = StringIO() context = self.pt_getContext() context.update(kwargs) engine = TALInterpreter( self._v_program, self._v_macros, getEngine().getContext(context), output, strictinsert=0 ) engine.interpret(self._v_macros[name]) return output.getvalue()
def test_preview_acme_template_source(self): # Render METAL attributes in acme_template result = StringIO() interpreter = TALInterpreter(self.acme_program, {}, self.engine, stream=result, tal=False) interpreter() actual = result.getvalue().strip() expected = self._read(('output', 'acme_template_source.html')).strip() self.assertEqual(actual, expected)
def test_div_in_p_using_macro(self): # We have not found a solution for this # and it is a deep and undocumented HTML parser issue. # Fred is looking into this. 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>')
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 zope.tal.talinterpreter import TALInterpreter from zope.tal.dummyengine import DummyEngine engine = DummyEngine(macros) TALInterpreter(program, macros, engine, sys.stdout, wrap=0)()
def test_preview_acme_template(self): # An ACME designer is previewing the ACME design. For the # purposes of this use case, extending a macro should act the # same as using a macro. result = StringIO() interpreter = TALInterpreter(self.acme_program, {}, self.engine, stream=result) interpreter() actual = result.getvalue().strip() expected = self._read(('output', 'acme_template.html')).strip() self.assertEqual(actual, expected)
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)
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())
def test_for_correct_msgids_translate_name(self): self.engine.translationDomain.clearMsgids() 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 = self.engine.translationDomain.getMsgids('default') msgids.sort() self.assertEqual(2, len(msgids)) self.assertEqual('This is text for ${bar_name}.', msgids[1][0]) self.assertEqual({'bar_name': '<span>BARVALUE</span>'}, msgids[1][1]) self.assertEqual('<div>THIS IS TEXT FOR <span>BARVALUE</span>.</div>', result.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 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 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 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))
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)
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)