Пример #1
0
    def _cook(self):
        """Compile the TAL and METAL statments.

        Cooking must not fail due to compilation errors in templates.
        """
        source_file = self.pt_source_file()
        if self.html():
            gen = TALGenerator(getEngine(), xml=0, source_file=source_file)
            parser = HTMLTALParser(gen)
        else:
            gen = TALGenerator(getEngine(), source_file=source_file)
            parser = TALParser(gen)

        self._v_errors = ()
        try:
            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
Пример #2
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()
 def _compile(self, source):
     parser = HTMLTALParser()
     parser.parseString(source)
     program, macros = parser.getCode()
     return program, macros
Пример #4
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
Пример #5
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
Пример #6
0
 def _compile(self, source):
     parser = HTMLTALParser()
     parser.parseString(source)
     program, macros = parser.getCode()
     return program, macros