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 #2
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()
Example #3
0
    def build_headers(self):
        self.compiled_headers = {}
        tales_context = TALESEngine.getContext(self.tales_namespace())

        for name, expression in self.response_headers.iteritems():
            __traceback_info__ = "Header %s: %s" % (name, expression)
            self.compiled_headers[name] = tales_context.evaluate(expression)
Example #4
0
 def _getExprContext(self):
     """A plone specific expression context"""
     context = self.context
     portal = getToolByName(context, 'portal_url').getPortalObject()
     pm = getToolByName(context, 'portal_membership')
     return Engine.getContext({'context': context,
                               'user': self._get_user(),
                               'portal': portal,
                               'checkPermission': pm.checkPermission})
Example #5
0
 def tales_namespace(self, file_context={}):
     tales_ns = Namespace(
         innerslot=self.innerslot,
         macros=self.macros,
         )
     tales_ns.update(TALESEngine.getBaseNames())
     tales_ns.update(self.context)
     tales_ns.update(file_context)
     tales_ns.pop("__builtins__", None)
     return tales_ns
Example #6
0
 def _getExprContext(self):
     """A plone specific expression context"""
     context = self.context
     portal = getToolByName(context, 'portal_url').getPortalObject()
     pm = getToolByName(context, 'portal_membership')
     return Engine.getContext({
         'context': context,
         'user': self._get_user(),
         'portal': portal,
         'checkPermission': pm.checkPermission
     })
Example #7
0
def registerSlotExprType():
    # Register the 'slot:' expression type.

    # Register with Products.PageTemplates.
    try:
        from Products.PageTemplates.Expressions import getEngine
    except ImportError:
        log.exception("Unable to register the slot expression type")
    else:
        engine = getEngine()
        if not engine.getTypes().has_key('slot'):
            engine.registerType('slot', SlotExpr)

    # Register with zope.tales.
    try:
        from zope.tales.engine import Engine
    except ImportError:
        log.exception("Unable to register the slot expression type")
    else:
        if not Engine.getTypes().has_key('slot'):
            Engine.registerType('slot', SlotExpr)
Example #8
0
def registerSlotExprType():
    # Register the 'slot:' expression type.

    # Register with Products.PageTemplates.
    try:
        from Products.PageTemplates.Expressions import getEngine
    except ImportError:
        log.exception("Unable to register the slot expression type")
    else:
        engine = getEngine()
        if not engine.getTypes().has_key('slot'):
            engine.registerType('slot', SlotExpr)

    # Register with zope.tales.
    try:
        from zope.tales.engine import Engine
    except ImportError:
        log.exception("Unable to register the slot expression type")
    else:
        if not Engine.getTypes().has_key('slot'):
            Engine.registerType('slot', SlotExpr)
Example #9
0
 def _getExprContext(self):
     """Dumb tal expression context, please override"""
     return Engine.getContext({
         'context': self.context,
         'user': self._get_user()
     })
Example #10
0
 def write(self):
     if not self.write_expr:
         return None
     return Engine.compile(self.write_expr)
Example #11
0
 def read(self):
     if not self.read_expr:
         return None
     return Engine.compile(self.read_expr)
Example #12
0
 def _getExprContext(self):
     """Dumb tal expression context, please override"""
     return Engine.getContext({'context': self.context,
                               'user': self._get_user()})
Example #13
0
 def write(self):
     if not self.write_expr:
         return None
     return Engine.compile(self.write_expr)
Example #14
0
 def read(self):
     if not self.read_expr:
         return None
     return Engine.compile(self.read_expr)
Example #15
0
def registerTALESExpressionType(name, handler):
    Engine.registerType(name, handler)
Example #16
0
def registerTALESExpressionType(name, handler):
    Engine.registerType(name, handler)
Example #17
0
def register(name, handler):
    """Register tales expression type."""
    Engine.registerType(name, handler)