diff --git a/doc/templates.md b/doc/templates.md index 1eaf6e0..c52a7b7 100644 --- a/doc/templates.md +++ b/doc/templates.md @@ -131,3 +131,10 @@ Operations on variables if it's empty or non-existing. - `$set(name,value)`: set `%name%` to `value`. - `$unset(name)`: unset `%name%`. + +`$python()` +----------- + +- `$python(expr,imports)`: evaluate an arbitrary python expression `expr`. + `imports` is a space-separated list of modules to import (can be omitted). + diff --git a/qygmy/templates/functions.py b/qygmy/templates/functions.py index f6143f1..f0597cb 100644 --- a/qygmy/templates/functions.py +++ b/qygmy/templates/functions.py @@ -127,3 +127,11 @@ def context_time(ctx, seconds): else: return ctx['__timefmt__'][2].format(sign, m, s) +def context_python(ctx, expression, imports=''): + modules = {'__builtins__': __builtins__} + if imports: + for m in imports.split(): + topname = m.split('.', 1)[0] + modules[topname] = __import__(m) + return str(eval(expression, modules, ctx)) +