Example #1
0
 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))
Example #2
0
 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.'])
Example #3
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)

    # 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 _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
Example #5
0
 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 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()
Example #7
0
 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))
Example #8
0
 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)
Example #9
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:
        #### 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
Example #10
0
 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 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
Example #12
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
Example #13
0
    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]]
Example #14
0
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
Example #18
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()
Example #19
0
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()
Example #20
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:
        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 _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
Example #22
0
 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
Example #23
0
 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
Example #24
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
Example #25
0
 def _compile(self, source):
     parser = HTMLTALParser()
     parser.parseString(source)
     program, macros = parser.getCode()
     return program, macros