"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "roundup/cgi/PageTemplates/PythonExpr.py" between
roundup-1.6.1.tar.gz and roundup-2.0.0.tar.gz

About: Roundup is an highly customisable issue-tracking system with command-line, web and e-mail interfaces (written in Python).

PythonExpr.py  (roundup-1.6.1):PythonExpr.py  (roundup-2.0.0)
skipping to change at line 20 skipping to change at line 20
# FOR A PARTICULAR PURPOSE # FOR A PARTICULAR PURPOSE
# #
############################################################################## ##############################################################################
# Modified for Roundup: # Modified for Roundup:
# #
# 1. more informative traceback info # 1. more informative traceback info
"""Generic Python Expression Handler """Generic Python Expression Handler
""" """
from TALES import CompilerError import symtable
from .TALES import CompilerError
from sys import exc_info from sys import exc_info
class getSecurityManager: class getSecurityManager:
'''Null security manager''' '''Null security manager'''
def validate(self, *args, **kwargs): def validate(self, *args, **kwargs):
return 1 return 1
addContext = removeContext = validateValue = validate addContext = removeContext = validateValue = validate
class PythonExpr: class PythonExpr:
def __init__(self, name, expr, engine): def __init__(self, name, expr, engine):
self.expr = expr = expr.strip().replace('\n', ' ') self.expr = expr = expr.strip().replace('\n', ' ')
try: try:
d = {} d = {}
exec 'def f():\n return %s\n' % expr.strip() in d self.f_code = 'def f():\n return %s\n' % expr.strip()
exec(self.f_code, d)
self._f = d['f'] self._f = d['f']
except: except:
raise CompilerError, ('Python expression error:\n' raise CompilerError(('Python expression error:\n'
'%s: %s') % exc_info()[:2] '%s: %s') % exc_info()[:2])
self._get_used_names() self._get_used_names()
def _get_used_names(self): def _get_used_names(self):
self._f_varnames = vnames = [] self._f_varnames = vnames = []
for vname in self._f.func_code.co_names: for vname in self._get_from_symtab():
if vname[0] not in '$_': if vname[0] not in '$_.':
vnames.append(vname) vnames.append(vname)
def _get_from_symtab(self):
"""
Get the variables used in the 'f' function.
"""
variables = set()
table = symtable.symtable(self.f_code, "<string>", "exec")
if table.has_children():
variables.update(self._walk_children(table))
return variables
def _walk_children(self, sym):
"""
Get the variables at this level. Recurse to get them all.
"""
variables = set()
for child in sym.get_children():
variables.update(set(child.get_identifiers()))
if child.has_children():
variables.update(self._walk_children(child))
return variables
def _bind_used_names(self, econtext, _marker=[]): def _bind_used_names(self, econtext, _marker=[]):
# Bind template variables # Bind template variables
names = {'CONTEXTS': econtext.contexts} names = {'CONTEXTS': econtext.contexts}
vars = econtext.vars variables = econtext.vars
getType = econtext.getCompiler().getTypes().get getType = econtext.getCompiler().getTypes().get
for vname in self._f_varnames: for vname in self._f_varnames:
val = vars.get(vname, _marker) val = variables.get(vname, _marker)
if val is _marker: if val is _marker:
has = val = getType(vname) has = val = getType(vname)
if has: if has:
val = ExprTypeProxy(vname, val, econtext) val = ExprTypeProxy(vname, val, econtext)
names[vname] = val names[vname] = val
else: else:
names[vname] = val names[vname] = val
return names return names
def __call__(self, econtext): def __call__(self, econtext):
__traceback_info__ = 'python expression "%s"'%self.expr __traceback_info__ = 'python expression "%s"'%self.expr
f = self._f f = self._f
f.func_globals.update(self._bind_used_names(econtext)) f.__globals__.update(self._bind_used_names(econtext))
return f() return f()
def __str__(self): def __str__(self):
return 'Python expression "%s"' % self.expr return 'Python expression "%s"' % self.expr
def __repr__(self): def __repr__(self):
return '<PythonExpr %s>' % self.expr return '<PythonExpr %s>' % self.expr
class ExprTypeProxy: class ExprTypeProxy:
'''Class that proxies access to an expression type handler''' '''Class that proxies access to an expression type handler'''
def __init__(self, name, handler, econtext): def __init__(self, name, handler, econtext):
 End of changes. 8 change blocks. 
9 lines changed or deleted 33 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)