def test_dynamic_msgids(self): sample_source = """ <p i18n:translate=""> Some <span tal:replace="string:strange">dynamic</span> text. </p> <p i18n:translate=""> A <a tal:attributes="href path:dynamic">link</a>. </p> """ p = HTMLTALParser() p.parseString(sample_source) program, macros = p.getCode() engine = POEngine() engine.file = 'sample_source' POTALInterpreter(program, macros, engine, stream=StringIO(), metal=False)() msgids = [] for domain in engine.catalog.values(): msgids += list(domain) msgids.sort() self.assertEqual(msgids, [ 'A <a href="${DYNAMIC_CONTENT}">link</a>.', 'Some ${DYNAMIC_CONTENT} text.' ])
def compilefile(file, mode=None): assert mode in ("html", "xml", None) if mode is None: ext = os.path.splitext(file)[1] if ext.lower() in (".html", ".htm"): mode = "html" else: mode = "xml" # make sure we can find the file prefix = os.path.dirname(os.path.abspath(__file__)) + os.path.sep if (not os.path.exists(file) and os.path.exists(os.path.join(prefix, file))): file = os.path.join(prefix, file) # normalize filenames for test output filename = os.path.abspath(file) if filename.startswith(prefix): filename = filename[len(prefix):] filename = filename.replace(os.sep, '/') # test files expect slashes # parse from zope.tal.talgenerator import TALGenerator if mode == "html": from zope.tal.htmltalparser import HTMLTALParser p = HTMLTALParser(gen=TALGenerator(source_file=filename, xml=0)) else: from zope.tal.talparser import TALParser p = TALParser(gen=TALGenerator(source_file=filename)) p.parseFile(file) return p.getCode()
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 __call__(self, value, *args, **kw): gen = TALGenerator(getEngine(), xml=1, source_file=None) parser = HTMLTALParser(gen) try: parser.parseString(value) except Exception, err: return ("Validation Failed(%s): \n %s" % (self.name, err))
def insertHTMLStructure(self, text, repldict): from zope.tal.htmltalparser import HTMLTALParser gen = AltTALGenerator(repldict, self.engine, 0) p = HTMLTALParser(gen) # Raises an exception if text is invalid p.parseString(text) program, macros = p.getCode() self.interpret(program)
def insertHTMLStructure(self, text, repldict): from zope.tal.htmltalparser import HTMLTALParser gen = AltTALGenerator(repldict, self.engine._engine, 0) p = HTMLTALParser(gen) # Raises an exception if text is invalid p.parseString(text) program, macros = p.getCode() self.interpret(program)
def test_dynamic_msgids(self): sample_source = """ <p i18n:translate=""> Some <span tal:replace="string:strange">dynamic</span> text. </p> <p i18n:translate=""> A <a tal:attributes="href path:dynamic">link</a>. </p> """ p = HTMLTALParser() p.parseString(sample_source) program, macros = p.getCode() engine = POEngine() engine.file = 'sample_source' POTALInterpreter(program, macros, engine, stream=StringIO(), metal=False)() msgids = [] for domain in engine.catalog.values(): msgids += list(domain) msgids.sort() self.assertEqual(msgids, ['A <a href="${DYNAMIC_CONTENT}">link</a>.', 'Some ${DYNAMIC_CONTENT} text.'])
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 cook(cls, source_file, text, engine, content_type): if content_type == 'text/html': gen = TALGenerator(engine, xml=0, source_file=source_file) parser = HTMLTALParser(gen) else: gen = TALGenerator(engine, source_file=source_file) parser = TALParser(gen) parser.parseString(text) program, macros = parser.getCode() return cls(program), macros
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 _cook(self): self._v_program = None if not self._source: return parser = HTMLTALParser(TALGenerator(TrustedEngine)) try: parser.parseString(self.source) self._v_program, macros = parser.getCode() self._v_errors = () except Exception, e: self._v_program = None self._v_errors = [unicode(err) for err in sys.exc_info()[:2]]
def tal_strings(dir, domain="zope", include_default_domain=False, exclude=()): """Retrieve all TAL messages from `dir` that are in the `domain`. """ # We import zope.tal.talgettext here because we can't rely on the # right sys path until app_dir has run from zope.tal.talgettext import POEngine, POTALInterpreter from zope.tal.htmltalparser import HTMLTALParser from zope.tal.talparser import TALParser import sys # i18n patch if templates using special characters reload(sys) sys.setdefaultencoding('UTF8') engine = POEngine() class Devnull(object): def write(self, s): pass for filename in (find_files(dir, '*.*pt', exclude=tuple(exclude)) + find_files(dir, '*.html', exclude=tuple(exclude)) + find_files(dir, '*.kupu', exclude=tuple(exclude)) + find_files(dir, '*.pox', exclude=tuple(exclude)) + find_files(dir, '*.xsl', exclude=tuple(exclude))): try: engine.file = filename name, ext = os.path.splitext(filename) if ext == '.html' or ext.endswith('pt'): p = HTMLTALParser() else: p = TALParser() p.parseFile(filename) program, macros = p.getCode() POTALInterpreter(program, macros, engine, stream=Devnull(), metal=False)() except: # Hee hee, I love bare excepts! print 'There was an error processing', filename traceback.print_exc() # See whether anything in the domain was found if not domain in engine.catalog: return {} # We do not want column numbers. catalog = engine.catalog[domain].copy() # When the Domain is 'default', then this means that none was found; # Include these strings; yes or no? if include_default_domain: catalog.update(engine.catalog['default']) for msgid, locations in catalog.items(): catalog[msgid] = [(l[0], l[1][0]) for l in locations] return catalog
def talToHtml(tal): """ Expects TAL-string, returns interpreted HTML-string. Works only with string-(and python?)-expressions, not with path-expressions. """ generator = TALGenerator(xml=0, source_file=None) parser = HTMLTALParser(generator) parser.parseString(tal) program, macros = parser.getCode() engine = DummyEngine(macros) result = StringIO() interpreter = TALInterpreter(program, {}, engine, stream=result) interpreter() tal = result.getvalue().strip() return tal
def test_potalinterpreter_translate_default(self): sample_source = '<p i18n:translate="">text</p>' p = HTMLTALParser() p.parseString(sample_source) program, macros = p.getCode() engine = POEngine() engine.file = 'sample_source' interpreter = POTALInterpreter( program, macros, engine, stream=StringIO(), metal=False) # We simply call this, to make sure we don't get a NameError # for 'unicode' in python 3. # The return value (strangely: 'x') is not interesting here. interpreter.translate('text') msgids = [] for domain in engine.catalog.values(): msgids += list(domain) self.assertIn('text', msgids)
def test_potalinterpreter_translate_default(self): sample_source = '<p i18n:translate="">text</p>' p = HTMLTALParser() p.parseString(sample_source) program, macros = p.getCode() engine = POEngine() engine.file = 'sample_source' interpreter = POTALInterpreter(program, macros, engine, stream=StringIO(), metal=False) # We simply call this, to make sure we don't get a NameError # for 'unicode' in python 3. # The return value (strangely: 'x') is not interesting here. interpreter.translate('text') msgids = [] for domain in engine.catalog.values(): msgids += list(domain) self.assertIn('text', msgids)
def _cook(self): """Compile the TAL and METAL statments. Cooking must not fail due to compilation errors in templates. """ engine = self.pt_getEngine() source_file = self.pt_source_file() if self.content_type == 'text/html': gen = TALGenerator(engine, xml=0, source_file=source_file) parser = HTMLTALParser(gen) else: gen = TALGenerator(engine, source_file=source_file) parser = TALParser(gen) self._v_errors = () try: parser.parseString(self._text) self._v_program, self._v_macros = parser.getCode() except: etype, e = sys.exc_info()[:2] self._v_errors = [ "Compilation failed", "%s.%s: %s" % (etype.__module__, etype.__name__, e) ] self._v_cooked = 1
def cook(self): """Compile the TAL and METAL statments. Cooking must not fail due to compilation errors in templates. """ engine = self.pt_getEngine() source_file = self.pt_source_file() if self.content_type == 'text/html': gen = TALGenerator(engine, xml=0, source_file=source_file) parser = HTMLTALParser(gen) else: gen = TALGenerator(engine, source_file=source_file) parser = TALParser(gen) self._v_errors = () try: #### the patch text = self._text text = expression.sub('', text) text = expression2.sub('', text) parser.parseString(text) #parser.parseString(self._text) self._v_program, self._v_macros = parser.getCode() except: self._v_errors = ["Compilation failed", "%s: %s" % sys.exc_info()[:2]] self._v_warnings = parser.getWarnings() self._v_cooked = 1
def compilefile(file, mode=None): assert mode in ("html", "xml", None) if mode is None: ext = os.path.splitext(file)[1] if ext.lower() in (".html", ".htm"): mode = "html" else: mode = "xml" from zope.tal.talgenerator import TALGenerator filename = os.path.abspath(file) prefix = os.path.dirname(os.path.abspath(__file__)) + os.path.sep if filename.startswith(prefix): filename = filename[len(prefix):] filename = filename.replace(os.sep, '/') # test files expect slashes if mode == "html": from zope.tal.htmltalparser import HTMLTALParser p = HTMLTALParser(gen=TALGenerator(source_file=filename, xml=0)) else: from zope.tal.talparser import TALParser p = TALParser(gen=TALGenerator(source_file=filename)) p.parseFile(file) return p.getCode()
def _cook(self): """Compile the TAL and METAL statments. Cooking must not fail due to compilation errors in templates. """ engine = self.pt_getEngine() source_file = self.pt_source_file() if self.content_type == 'text/html': gen = TALGenerator(engine, xml=0, source_file=source_file) parser = HTMLTALParser(gen) else: gen = TALGenerator(engine, source_file=source_file) parser = TALParser(gen) self._v_errors = () try: parser.parseString(self._text) self._v_program, self._v_macros = parser.getCode() except: etype, e = sys.exc_info()[:2] self._v_errors = ["Compilation failed", "%s.%s: %s" % (etype.__module__, etype.__name__, e)] self._v_cooked = 1
def tal_strings(dir, domain="zope", include_default_domain=False): """Retrieve all TAL messages from `dir` that are in the `domain`. """ # We import zope.tal.talgettext here because we can't rely on the # right sys path until app_dir has run from zope.tal.talgettext import POEngine, POTALInterpreter from zope.tal.htmltalparser import HTMLTALParser engine = POEngine() class Devnull(object): def write(self, s): pass for filename in find_files(dir, '*.pt'): try: engine.file = filename p = HTMLTALParser() p.parseFile(filename) program, macros = p.getCode() POTALInterpreter(program, macros, engine, stream=Devnull(), metal=False)() except: # Hee hee, I love bare excepts! print 'There was an error processing', filename traceback.print_exc() # See whether anything in the domain was found if not engine.catalog.has_key(domain): return {} # We do not want column numbers. catalog = engine.catalog[domain].copy() # When the Domain is 'default', then this means that none was found; # Include these strings; yes or no? if include_default_domain: catalog.update(engine.catalog['zope']) for msgid, locations in catalog.items(): catalog[msgid] = map(lambda l: (l[0], l[1][0]), locations) return catalog
def tal_strings(dir, domain="zope", include_default_domain=False, exclude=()): """Retrieve all TAL messages from `dir` that are in the `domain`. """ # We import zope.tal.talgettext here because we can't rely on the # right sys path until app_dir has run from zope.tal.talgettext import POEngine, POTALInterpreter from zope.tal.htmltalparser import HTMLTALParser from zope.tal.talparser import TALParser engine = POEngine() class Devnull(object): def write(self, s): pass for filename in (find_files(dir, '*.*pt', exclude=tuple(exclude)) + find_files(dir, '*.html', exclude=tuple(exclude)) + find_files(dir, '*.kupu', exclude=tuple(exclude)) + find_files(dir, '*.pox', exclude=tuple(exclude)) + find_files(dir, '*.xsl', exclude=tuple(exclude))): try: engine.file = filename name, ext = os.path.splitext(filename) if ext == '.html' or ext.endswith('pt'): p = HTMLTALParser() else: p = TALParser() p.parseFile(filename) program, macros = p.getCode() POTALInterpreter(program, macros, engine, stream=Devnull(), metal=False)() except: # Hee hee, I love bare excepts! print 'There was an error processing', filename traceback.print_exc() # See whether anything in the domain was found if not domain in engine.catalog: return {} # We do not want column numbers. catalog = engine.catalog[domain].copy() # When the Domain is 'default', then this means that none was found; # Include these strings; yes or no? if include_default_domain: catalog.update(engine.catalog['default']) for msgid, locations in catalog.items(): catalog[msgid] = [(l[0], l[1][0]) for l in locations] return catalog
def findStaticTranslationText(page_template, func_name_list): def iterate(node, target_name, function): if isinstance(node, list): for i in node: iterate(i, target_name, function) elif isinstance(node, tuple) and node: if node[0]==target_name: function(node) else: for i in node[1:]: iterate(i, target_name, function) text_dict = {} def addText(node): if len(node)!=2: node = (node[0], node[1:]) program = [node] macros = {} engine = MyDummyEngine(macros) output = StringIO() interpreter = MyDummyTALInterpreter(program, macros, engine, output) interpreter() if interpreter._i18n_message_id_dict is not None: text_dict.update(interpreter._i18n_message_id_dict) def addTextFromPythonExpression(node): if node[0]=='insertText': tal_expression = node[1] if isinstance(tal_expression, (tuple, list)): tal_expression = tal_expression[0] elif node[0] in ('setLocal', 'setGlobal'): if len(node)==2: tal_expression = node[1][1] elif len(node)==3: tal_expression = node[2] else: return else: return tal_expression = tal_expression[1:-1] match = name_match(tal_expression) if match: type_, expression = match.group(1, 2) if type_=='python': # clean up expression expression = expression.strip() expression = expression.replace('\n', ' ') Base_getFunctionFirstArgumentValue = page_template.Base_getFunctionFirstArgumentValue for func_name in func_name_list: for message in Base_getFunctionFirstArgumentValue(func_name, expression): text_dict[message] = None if page_template.html(): generator = TALGenerator(xml=0) parser = HTMLTALParser(generator) else: generator = TALGenerator(xml=1) parser = TALParser(generator) parser.parseString(page_template._text) iterate(parser.gen.program, 'insertTranslation', addText) iterate(parser.gen.program, 'insertText', addTextFromPythonExpression) iterate(parser.gen.program, 'setLocal', addTextFromPythonExpression) iterate(parser.gen.program, 'setGlobal', addTextFromPythonExpression) return text_dict.keys()
def main(): try: opts, args = getopt.getopt(sys.argv[1:], 'ho:u:', ['help', 'output=', 'update=']) except getopt.error as msg: usage(1, msg) outfile = None engine = None update_mode = False for opt, arg in opts: if opt in ('-h', '--help'): usage(0) elif opt in ('-o', '--output'): outfile = arg elif opt in ('-u', '--update'): update_mode = True if outfile is None: outfile = arg engine = UpdatePOEngine(filename=arg) if not args: print('nothing to do') return # We don't care about the rendered output of the .pt file class Devnull(object): def write(self, s): pass # check if we've already instantiated an engine; # if not, use the stupidest one available if not engine: engine = POEngine() # process each file specified for filename in args: try: engine.file = filename p = HTMLTALParser() p.parseFile(filename) program, macros = p.getCode() POTALInterpreter(program, macros, engine, stream=Devnull(), metal=False)() except: # Hee hee, I love bare excepts! print('There was an error processing', filename) traceback.print_exc() # Now output the keys in the engine. Write them to a file if --output or # --update was specified; otherwise use standard out. if (outfile is None): outfile = sys.stdout else: outfile = file(outfile, update_mode and "a" or "w") catalog = {} for domain in engine.catalog: catalog.update(engine.catalog[domain]) messages = catalog.copy() try: messages.update(engine.base) except AttributeError: pass if '' not in messages: print(pot_header % { 'time': time.ctime(), 'version': __version__ }, file=outfile) # TODO: You should not sort by msgid, but by filename and position. (SR) msgids = sorted(catalog) for msgid in msgids: positions = engine.catalog[msgid] for filename, position in positions: outfile.write('#: %s:%s\n' % (filename, position[0])) outfile.write('msgid "%s"\n' % msgid) outfile.write('msgstr ""\n') outfile.write('\n')
def tal_strings(dir, domain="zope", include_default_domain=False, exclude=()): """Retrieve all TAL messages from `dir` that are in the `domain`. """ # We import zope.tal.talgettext here because we can't rely on the # right sys path until app_dir has run from zope.tal.talgettext import POEngine, POTALInterpreter from zope.tal.htmltalparser import HTMLTALParser from zope.tal.talparser import TALParser engine = POEngine() class Devnull(object): def write(self, s): pass for filename in (find_files(dir, '*.*pt', exclude=tuple(exclude)) + find_files(dir, '*.html', exclude=tuple(exclude)) + find_files(dir, '*.kupu', exclude=tuple(exclude)) + find_files(dir, '*.pox', exclude=tuple(exclude)) + find_files(dir, '*.xsl', exclude=tuple(exclude))): engine.file = filename name, ext = os.path.splitext(filename) # First try with standard zope.tal parsers. if ext == '.html' or ext.endswith('pt'): parser = HTMLTALParser() else: parser = TALParser() try: parser.parseFile(filename) program, macros = parser.getCode() POTALInterpreter(program, macros, engine, stream=Devnull(), metal=False)() except KeyboardInterrupt: raise except: # Hee hee, I love bare excepts! if ext == '.html' or ext.endswith('pt'): # We can have one retry with our own generator. gen = DudeGenerator(xml=0) parser = HTMLTALParser(gen=gen) try: parser.parseFile(filename) program, macros = parser.getCode() POTALInterpreter(program, macros, engine, stream=Devnull(), metal=False)() except: # Hee hee, I love bare excepts! print 'There was an error processing', filename traceback.print_exc() else: print 'There was an error processing', filename traceback.print_exc() # See whether anything in the domain was found if domain not in engine.catalog: return {} # We do not want column numbers. catalog = engine.catalog[domain].copy() # When the Domain is 'default', then this means that none was found; # Include these strings; yes or no? if include_default_domain: catalog.update(engine.catalog['default']) for msgid, locations in catalog.items(): catalog[msgid] = [(l[0], l[1][0]) for l in locations] return catalog
# We don't care about the rendered output of the .pt file class Devnull(object): def write(self, s): pass # check if we've already instantiated an engine; # if not, use the stupidest one available if not engine: engine = POEngine() # process each file specified for filename in args: try: engine.file = filename p = HTMLTALParser() p.parseFile(filename) program, macros = p.getCode() POTALInterpreter(program, macros, engine, stream=Devnull(), metal=False)() except: # Hee hee, I love bare excepts! print 'There was an error processing', filename traceback.print_exc() # Now output the keys in the engine. Write them to a file if --output or # --update was specified; otherwise use standard out. if (outfile is None): outfile = sys.stdout else: outfile = file(outfile, update_mode and "a" or "w")
def tal_strings(dir, domain="zope", include_default_domain=False, exclude=()): """Retrieve all TAL messages from `dir` that are in the `domain`. """ # We import zope.tal.talgettext here because we can't rely on the # right sys path until app_dir has run from zope.pagetemplate.pagetemplatefile import sniff_type from zope.pagetemplate.pagetemplatefile import XML_PREFIX_MAX_LENGTH from zope.tal.talgettext import POEngine, POTALInterpreter from zope.tal.htmltalparser import HTMLTALParser from zope.tal.talparser import TALParser engine = POEngine() class Devnull(object): def write(self, s): pass filenames = find_files(dir, '*.pt', exclude=tuple(exclude)) \ + find_files(dir, '*.html', exclude=tuple(exclude)) \ + find_files(dir, '*.xml', exclude=tuple(exclude)) for filename in sorted(filenames): # This is taken from zope/pagetemplate/pagetemplatefile.py (r40504) f = open(filename, "rb") try: text = f.read(XML_PREFIX_MAX_LENGTH) except: f.close() raise type_ = sniff_type(text) if type_ == "text/xml": text += f.read() else: # For HTML, we really want the file read in text mode: f.close() f = open(filename) text = f.read() f.close() try: engine.file = filename if type_ != "text/xml": p = HTMLTALParser() else: p = TALParser() p.parseString(text) program, macros = p.getCode() POTALInterpreter(program, macros, engine, stream=Devnull(), metal=False)() except: # Hee hee, I love bare excepts! print 'There was an error processing', filename traceback.print_exc() # See whether anything in the domain was found if not engine.catalog.has_key(domain): return {} # We do not want column numbers. catalog = engine.catalog[domain].copy() # When the Domain is 'default', then this means that none was found; # Include these strings; yes or no? if include_default_domain: catalog.update(engine.catalog['zope']) for msgid, locations in catalog.items(): catalog[msgid] = map(lambda l: (l[0], l[1][0]), locations) return catalog
def findStaticTranslationText(page_template, func_name_list): def iterate(node, target_name, function): if type(node) is list: for i in node: iterate(i, target_name, function) elif type(node) is tuple and node: if node[0]==target_name: function(node) else: for i in node[1:]: iterate(i, target_name, function) text_dict = {} def addText(node): if len(node)!=2: node = (node[0], node[1:]) program = [node] macros = {} engine = MyDummyEngine(macros) output = StringIO() interpreter = MyDummyTALInterpreter(program, macros, engine, output) interpreter() if interpreter._i18n_message_id_dict is not None: text_dict.update(interpreter._i18n_message_id_dict) def addTextFromPythonExpression(node): if node[0]=='insertText': tal_expression = node[1] if isinstance(tal_expression, (tuple, list)): tal_expression = tal_expression[0] elif node[0] in ('setLocal', 'setGlobal'): if len(node)==2: tal_expression = node[1][1] elif len(node)==3: tal_expression = node[2] else: return else: return tal_expression = tal_expression[1:-1] match = name_match(tal_expression) if match: type, expression = match.group(1, 2) if type=='python': # clean up expression expression = expression.strip() expression = expression.replace('\n', ' ') Base_getFunctionFirstArgumentValue = page_template.Base_getFunctionFirstArgumentValue for func_name in func_name_list: for message in Base_getFunctionFirstArgumentValue(func_name, expression): text_dict[message] = None if page_template.html(): generator = TALGenerator(xml=0) parser = HTMLTALParser(generator) else: generator = TALGenerator(xml=1) parser = TALParser(generator) parser.parseString(page_template._text) iterate(parser.gen.program, 'insertTranslation', addText) iterate(parser.gen.program, 'insertText', addTextFromPythonExpression) iterate(parser.gen.program, 'setLocal', addTextFromPythonExpression) iterate(parser.gen.program, 'setGlobal', addTextFromPythonExpression) return text_dict.keys()
def main(): try: opts, args = getopt.getopt( sys.argv[1:], 'ho:u:', ['help', 'output=', 'update=']) except getopt.error as msg: usage(1, msg) outfile = None engine = None update_mode = False for opt, arg in opts: if opt in ('-h', '--help'): usage(0) elif opt in ('-o', '--output'): outfile = arg elif opt in ('-u', '--update'): update_mode = True if outfile is None: outfile = arg engine = UpdatePOEngine(filename=arg) if not args: print('nothing to do') return # We don't care about the rendered output of the .pt file class Devnull(object): def write(self, s): pass # check if we've already instantiated an engine; # if not, use the stupidest one available if not engine: engine = POEngine() # process each file specified for filename in args: try: engine.file = filename p = HTMLTALParser() p.parseFile(filename) program, macros = p.getCode() POTALInterpreter(program, macros, engine, stream=Devnull(), metal=False)() except: # Hee hee, I love bare excepts! print('There was an error processing', filename) traceback.print_exc() # Now output the keys in the engine. Write them to a file if --output or # --update was specified; otherwise use standard out. if (outfile is None): outfile = sys.stdout else: outfile = file(outfile, update_mode and "a" or "w") catalog = {} for domain in engine.catalog: catalog.update(engine.catalog[domain]) messages = catalog.copy() try: messages.update(engine.base) except AttributeError: pass if '' not in messages: print(pot_header % {'time': time.ctime(), 'version': __version__}, file=outfile) # TODO: You should not sort by msgid, but by filename and position. (SR) msgids = sorted(catalog) for msgid in msgids: positions = engine.catalog[msgid] for filename, position in positions: outfile.write('#: %s:%s\n' % (filename, position[0])) outfile.write('msgid "%s"\n' % msgid) outfile.write('msgstr ""\n') outfile.write('\n')
def _compile(self, source, source_file=None): generator = TALGenerator(xml=0, source_file=source_file) parser = HTMLTALParser(generator) parser.parseString(source) program, macros = parser.getCode() return program, macros
def parse(self, eng, s, fn): gen = TALGenerator(expressionCompiler=eng, xml=0, source_file=fn) parser = HTMLTALParser(gen) parser.parseString(s) program, macros = parser.getCode() return program, macros
# We don't care about the rendered output of the .pt file class Devnull(object): def write(self, s): pass # check if we've already instantiated an engine; # if not, use the stupidest one available if not engine: engine = POEngine() # process each file specified for filename in args: try: engine.file = filename p = HTMLTALParser() p.parseFile(filename) program, macros = p.getCode() POTALInterpreter(program, macros, engine, stream=Devnull(), metal=False)() except: # Hee hee, I love bare excepts! print 'There was an error processing', filename traceback.print_exc() # Now output the keys in the engine. Write them to a file if --output or # --update was specified; otherwise use standard out. if (outfile is None): outfile = sys.stdout
def _compile(self, source): parser = HTMLTALParser() parser.parseString(source) program, macros = parser.getCode() return program, macros
def tal_strings(dir, domain="zope", include_default_domain=False, exclude=()): """Retrieve all TAL messages from `dir` that are in the `domain`. """ # We import zope.tal.talgettext here because we can't rely on the # right sys path until app_dir has run from zope.pagetemplate.pagetemplatefile import sniff_type from zope.pagetemplate.pagetemplatefile import XML_PREFIX_MAX_LENGTH from zope.tal.talgettext import POEngine, POTALInterpreter from zope.tal.htmltalparser import HTMLTALParser from zope.tal.talparser import TALParser engine = POEngine() class Devnull(object): def write(self, s): pass filenames = find_files(dir, '*.pt', exclude=tuple(exclude)) \ + find_files(dir, '*.html', exclude=tuple(exclude)) \ + find_files(dir, '*.xml', exclude=tuple(exclude)) for filename in sorted(filenames): # This is taken from zope/pagetemplate/pagetemplatefile.py (r40504) f = open(filename, "rb") try: text = f.read(XML_PREFIX_MAX_LENGTH) except: f.close() raise type_ = sniff_type(text) if type_ == "text/xml": text += f.read() else: # For HTML, we really want the file read in text mode: f.close() f = open(filename) text = f.read() f.close() try: engine.file = filename if type_ != "text/xml": p = HTMLTALParser() else: p = TALParser() p.parseString(text) program, macros = p.getCode() POTALInterpreter(program, macros, engine, stream=Devnull(), metal=False)() except: # Hee hee, I love bare excepts! print 'There was an error processing', filename traceback.print_exc() # See whether anything in the domain was found if not engine.catalog.has_key(domain): return {} # We do not want column numbers. catalog = engine.catalog[domain].copy() # When the Domain is 'default', then this means that none was found; # Include these strings; yes or no? if include_default_domain: defaultCatalog = engine.catalog.get('default') if defaultCatalog == None: engine.catalog['default'] = {} catalog.update(engine.catalog['default']) for msgid, locations in catalog.items(): catalog[msgid] = map(lambda l: (l[0], l[1][0]), locations) return catalog