Skip to content

Commit

Permalink
Merge branch 'tmpl_function_eval'
Browse files Browse the repository at this point in the history
Conflicts:
	doc/templates.md
  • Loading branch information
tsufeki committed Jul 18, 2013
2 parents 3d446f7 + 5caf4fc commit c9acf49
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
7 changes: 7 additions & 0 deletions doc/templates.md
Expand Up @@ -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).

8 changes: 8 additions & 0 deletions qygmy/templates/functions.py
Expand Up @@ -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.