Code

more doc, bugfix in Batch
[roundup.git] / roundup / cgi / PageTemplates / PythonExpr.py
1 ##############################################################################
2 #
3 # Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
4
5 # This software is subject to the provisions of the Zope Public License,
6 # Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
10 # FOR A PARTICULAR PURPOSE
11
12 ##############################################################################
14 """Generic Python Expression Handler
16 Modified for Roundup 0.5 release:
18 - more informative traceback info
20 """
22 __version__='$Revision: 1.3 $'[11:-2]
24 from TALES import CompilerError
25 from string import strip, split, join, replace, lstrip
26 from sys import exc_info
28 class getSecurityManager:
29     '''Null security manager'''
30     def validate(self, *args, **kwargs):
31         return 1
32     addContext = removeContext = validateValue = validate
34 class PythonExpr:
35     def __init__(self, name, expr, engine):
36         self.expr = expr = replace(strip(expr), '\n', ' ')
37         try:
38             d = {}
39             exec 'def f():\n return %s\n' % strip(expr) in d
40             self._f = d['f']
41         except:
42             raise CompilerError, ('Python expression error:\n'
43                                   '%s: %s') % exc_info()[:2]
44         self._get_used_names()
46     def _get_used_names(self):
47         self._f_varnames = vnames = []
48         for vname in self._f.func_code.co_names:
49             if vname[0] not in '$_':
50                 vnames.append(vname)
52     def _bind_used_names(self, econtext):
53         # Bind template variables
54         names = {}
55         vars = econtext.vars
56         getType = econtext._engine.getTypes().get
57         for vname in self._f_varnames:
58             has, val = vars.has_get(vname)
59             if not has:
60                 has = val = getType(vname)
61                 if has:
62                     val = ExprTypeProxy(vname, val, econtext)
63             if has:
64                 names[vname] = val
65         return names
67     def __call__(self, econtext):
68         __traceback_info__ = 'python expression "%s"'%self.expr
69         f = self._f
70         f.func_globals.update(self._bind_used_names(econtext))        
71         return f()
73     def __str__(self):
74         return 'Python expression "%s"' % self.expr
75     def __repr__(self):
76         return '<PythonExpr %s>' % self.expr
78 class ExprTypeProxy:
79     '''Class that proxies access to an expression type handler'''
80     def __init__(self, name, handler, econtext):
81         self._name = name
82         self._handler = handler
83         self._econtext = econtext
84     def __call__(self, text):
85         return self._handler(self._name, text,
86                              self._econtext._engine)(self._econtext)