Permalink
Browse files

Merge branch 'tmpl_function_eval'

Conflicts:
	doc/templates.md
  • Loading branch information...
2 parents 3d446f7 + 5caf4fc commit c9acf49df23f2c9ab2d464f52bc20afecfe6ea0b @tsufeki committed Jul 18, 2013
Showing with 15 additions and 0 deletions.
  1. +7 −0 doc/templates.md
  2. +8 −0 qygmy/templates/functions.py
View
@@ -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).
+
@@ -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))
+

0 comments on commit c9acf49

Please sign in to comment.