Edgewall Software

ApiDocs/0.5.x/genshi.template.eval

genshi.template.eval

Support for "safe" evaluation of Python expressions.

Code

Abstract base class for the Expression and Suite classes.

Expression

Evaluates Python expressions used in templates.

>>> data = dict(test='Foo', items=[1, 2, 3], dict={'some': 'thing'})
>>> Expression('test').evaluate(data)
'Foo'
>>> Expression('items[0]').evaluate(data)
1
>>> Expression('items[-1]').evaluate(data)
3
>>> Expression('dict["some"]').evaluate(data)
'thing'

Similar to e.g. Javascript, expressions in templates can use the dot notation for attribute access to access items in mappings:

>>> Expression('dict.some').evaluate(data)
'thing'

This also works the other way around: item access can be used to access any object attribute:

>>> class MyClass(object):
...     myattr = 'Bar'
>>> data = dict(mine=MyClass(), key='myattr')
>>> Expression('mine.myattr').evaluate(data)
'Bar'
>>> Expression('mine["myattr"]').evaluate(data)
'Bar'
>>> Expression('mine[key]').evaluate(data)
'Bar'

All of the standard Python operators are available to template expressions. Built-in functions such as len() are also available in template expressions:

>>> data = dict(items=[1, 2, 3])
>>> Expression('len(items)').evaluate(data)
3

evaluate(self, data)

Evaluate the expression against the given data dictionary.

param data:a mapping containing the data to evaluate against
return:the result of the evaluation

Suite

Executes Python statements used in templates.

>>> data = dict(test='Foo', items=[1, 2, 3], dict={'some': 'thing'})
>>> Suite("foo = dict['some']").execute(data)
>>> data['foo']
'thing'

execute(self, data)

Execute the suite in the given data dictionary.

param data:a mapping containing the data to execute in

UndefinedError

Exception thrown when a template expression attempts to access a variable not defined in the context.

see:LenientLookup, StrictLookup

Undefined

Represents a reference to an undefined variable.

Unlike the Python runtime, template expressions can refer to an undefined variable without causing a NameError to be raised. The result will be an instance of the Undefined class, which is treated the same as False in conditions, but raise an exception on any other operation:

>>> foo = Undefined('foo')
>>> bool(foo)
False
>>> list(foo)
[]
>>> print foo
undefined

However, calling an undefined variable, or trying to access an attribute of that variable, will raise an exception that includes the name used to reference that undefined variable.

>>> foo('bar')
Traceback (most recent call last):
    ...
UndefinedError: "foo" not defined
>>> foo.bar
Traceback (most recent call last):
    ...
UndefinedError: "foo" not defined
see:LenientLookup

LookupBase

Abstract base class for variable lookup implementations.

globals(cls, data)

Construct the globals dictionary to use as the execution context for the expression or suite.

lookup_name(cls, data, name)

(Not documented)

lookup_attr(cls, obj, key)

(Not documented)

lookup_item(cls, obj, key)

(Not documented)

undefined(cls, key, owner=UNDEFINED)

Can be overridden by subclasses to specify behavior when undefined variables are accessed.

param key:the name of the variable
param owner:the owning object, if the variable is accessed as a member

LenientLookup

Default variable lookup mechanism for expressions.

When an undefined variable is referenced using this lookup style, the reference evaluates to an instance of the Undefined class:

>>> expr = Expression('nothing', lookup='lenient')
>>> undef = expr.evaluate({})
>>> undef
<Undefined 'nothing'>

The same will happen when a non-existing attribute or item is accessed on an existing object:

>>> expr = Expression('something.nil', lookup='lenient')
>>> expr.evaluate({'something': dict()})
<Undefined 'nil'>

See the documentation of the Undefined class for details on the behavior of such objects.

see:StrictLookup

undefined(cls, key, owner=UNDEFINED)

Return an Undefined object.

StrictLookup

Strict variable lookup mechanism for expressions.

Referencing an undefined variable using this lookup style will immediately raise an UndefinedError:

>>> expr = Expression('nothing', lookup='strict')
>>> expr.evaluate({})
Traceback (most recent call last):
    ...
UndefinedError: "nothing" not defined

The same happens when a non-existing attribute or item is accessed on an existing object:

>>> expr = Expression('something.nil', lookup='strict')
>>> expr.evaluate({'something': dict()})
Traceback (most recent call last):
    ...
UndefinedError: {} has no member named "nil"

undefined(cls, key, owner=UNDEFINED)

Raise an UndefinedError immediately.

ASTTransformer

General purpose base class for AST transformations.

Every visitor method can be overridden to return an AST node that has been altered or replaced in some way.

visit(self, node)

(Not documented)

visitExpression(self, node)

(Not documented)

visitModule(self, node)

(Not documented)

visitStmt(self, node)

(Not documented)

visitCallFunc(self, node)

(Not documented)

visitClass(self, node)

(Not documented)

visitFrom(self, node)

(Not documented)

visitFunction(self, node)

(Not documented)

visitGetattr(self, node)

(Not documented)

visitLambda(self, node)

(Not documented)

visitSubscript(self, node)

(Not documented)

visitAssert(self, node)

(Not documented)

visitAssign(self, node)

(Not documented)

visitAssAttr(self, node)

(Not documented)

visitAugAssign(self, node)

(Not documented)

visitDecorators(self, node)

(Not documented)

visitExec(self, node)

(Not documented)

visitFor(self, node)

(Not documented)

visitIf(self, node)

(Not documented)

visitImport(self, node)

(Not documented)

visitRaise(self, node)

(Not documented)

visitReturn(self, node)

(Not documented)

visitTryExcept(self, node)

(Not documented)

visitTryFinally(self, node)

(Not documented)

visitWhile(self, node)

(Not documented)

visitWith(self, node)

(Not documented)

visitYield(self, node)

(Not documented)

visitCompare(self, node)

(Not documented)

visitIfExp(self, node)

(Not documented)

visitDict(self, node)

(Not documented)

visitGenExpr(self, node)

(Not documented)

visitGenExprFor(self, node)

(Not documented)

visitGenExprIf(self, node)

(Not documented)

visitGenExprInner(self, node)

(Not documented)

visitKeyword(self, node)

(Not documented)

visitList(self, node)

(Not documented)

visitListComp(self, node)

(Not documented)

visitListCompFor(self, node)

(Not documented)

visitListCompIf(self, node)

(Not documented)

visitSlice(self, node)

(Not documented)

visitSliceobj(self, node)

(Not documented)

visitTuple(self, node)

(Not documented)

TemplateASTTransformer

Concrete AST transformer that implements the AST transformations needed for code embedded in templates.

visitConst(self, node)

(Not documented)

visitAssName(self, node)

(Not documented)

visitAugAssign(self, node)

(Not documented)

visitClass(self, node)

(Not documented)

visitFrom(self, node)

(Not documented)

visitFunction(self, node)

(Not documented)

visitGenExpr(self, node)

(Not documented)

visitImport(self, node)

(Not documented)

visitLambda(self, node)

(Not documented)

visitListComp(self, node)

(Not documented)

visitName(self, node)

(Not documented)

ExpressionASTTransformer

Concrete AST transformer that implements the AST transformations needed for code embedded in templates.

visitGetattr(self, node)

(Not documented)

visitSubscript(self, node)

(Not documented)


See ApiDocs/0.5.x, Documentation