Пример #1
0
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()
Пример #2
0
    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 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())
Пример #4
0
    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>\n',
            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>\n',
            result.getvalue())
Пример #5
0
 def insertXMLStructure(self, text, repldict):
     from zope.tal.talparser import TALParser
     gen = AltTALGenerator(repldict, self.engine, 0)
     p = TALParser(gen)
     gen.enable(0)
     p.parseFragment('<!DOCTYPE foo PUBLIC "foo" "bar"><foo>')
     gen.enable(1)
     p.parseFragment(text)  # Raises an exception if text is invalid
     gen.enable(0)
     p.parseFragment('</foo>', 1)
     program, macros = gen.getCode()
     self.interpret(program)
Пример #6
0
    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
Пример #7
0
 def insertXMLStructure(self, text, repldict):
     from zope.tal.talparser import TALParser
     gen = AltTALGenerator(repldict, self.engine._engine, 0)
     p = TALParser(gen)
     gen.enable(0)
     p.parseFragment('<!DOCTYPE foo PUBLIC "foo" "bar"><foo>')
     gen.enable(1)
     p.parseFragment(text) # Raises an exception if text is invalid
     gen.enable(0)
     p.parseFragment('</foo>', 1)
     program, macros = gen.getCode()
     self.interpret(program)
Пример #8
0
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
Пример #9
0
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()
Пример #10
0
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()
Пример #11
0
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