Mercurial > p > roundup > code
annotate roundup/cgi/PageTemplates/PythonExpr.py @ 5132:0142b4fb5a2d
issue2550648 - partial fix for problem in this issue. Ezio Melotti
reported that the expression editor allowed the user to generate an
expression using retired values. To align the expression editor with
the simple dropdown search item, retired values are now removed from
the expression editor.
Do we really want this though? Supposed a keyword is retired and I
want to search for an issue with that retired keyword? Do we have a
best policy document that says to remove retired keywords from all
places it could possibly be used? It could be argued that the simple
search dropdown is wrong and should allow selecting retired values.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Fri, 08 Jul 2016 19:31:02 -0400 |
| parents | 6e3e4f24c753 |
| children | 35ea9b1efc14 |
| rev | line source |
|---|---|
| 1049 | 1 ############################################################################## |
| 2 # | |
| 3 # Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved. | |
|
2349
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
4 # |
| 1049 | 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 | |
|
2349
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
11 # |
|
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
12 ############################################################################## |
|
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
13 # Modified for Roundup: |
| 1049 | 14 # |
|
2349
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
15 # 1. more informative traceback info |
| 1049 | 16 |
| 17 """Generic Python Expression Handler | |
|
2349
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
18 """ |
|
1071
c08b3820edd1
Adhering to ZPL
Richard Jones <richard@users.sourceforge.net>
parents:
1065
diff
changeset
|
19 |
| 1049 | 20 from TALES import CompilerError |
| 21 from sys import exc_info | |
| 22 | |
| 23 class getSecurityManager: | |
| 24 '''Null security manager''' | |
| 25 def validate(self, *args, **kwargs): | |
| 26 return 1 | |
| 27 addContext = removeContext = validateValue = validate | |
| 28 | |
| 29 class PythonExpr: | |
| 30 def __init__(self, name, expr, engine): | |
|
2349
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
31 self.expr = expr = expr.strip().replace('\n', ' ') |
| 1049 | 32 try: |
| 33 d = {} | |
|
2349
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
34 exec 'def f():\n return %s\n' % expr.strip() in d |
| 1049 | 35 self._f = d['f'] |
| 36 except: | |
| 37 raise CompilerError, ('Python expression error:\n' | |
| 38 '%s: %s') % exc_info()[:2] | |
| 39 self._get_used_names() | |
| 40 | |
| 41 def _get_used_names(self): | |
| 42 self._f_varnames = vnames = [] | |
| 43 for vname in self._f.func_code.co_names: | |
| 44 if vname[0] not in '$_': | |
| 45 vnames.append(vname) | |
| 46 | |
|
2349
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
47 def _bind_used_names(self, econtext, _marker=[]): |
| 1049 | 48 # Bind template variables |
|
2349
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
49 names = {'CONTEXTS': econtext.contexts} |
| 1049 | 50 vars = econtext.vars |
|
1257
93b80ad11ca8
merged Zope Collector #372 fix from ZPT CVS trunk
Richard Jones <richard@users.sourceforge.net>
parents:
1071
diff
changeset
|
51 getType = econtext.getCompiler().getTypes().get |
| 1049 | 52 for vname in self._f_varnames: |
|
2349
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
53 val = vars.get(vname, _marker) |
|
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
54 if val is _marker: |
| 1049 | 55 has = val = getType(vname) |
| 56 if has: | |
| 57 val = ExprTypeProxy(vname, val, econtext) | |
|
2349
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
58 names[vname] = val |
|
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
59 else: |
| 1049 | 60 names[vname] = val |
| 61 return names | |
| 62 | |
| 63 def __call__(self, econtext): | |
|
1065
0f9aa62917bd
much nicer error messages when there's a templating error
Richard Jones <richard@users.sourceforge.net>
parents:
1049
diff
changeset
|
64 __traceback_info__ = 'python expression "%s"'%self.expr |
| 1049 | 65 f = self._f |
|
2349
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
66 f.func_globals.update(self._bind_used_names(econtext)) |
| 1049 | 67 return f() |
| 68 | |
| 69 def __str__(self): | |
| 70 return 'Python expression "%s"' % self.expr | |
| 71 def __repr__(self): | |
| 72 return '<PythonExpr %s>' % self.expr | |
| 73 | |
| 74 class ExprTypeProxy: | |
| 75 '''Class that proxies access to an expression type handler''' | |
| 76 def __init__(self, name, handler, econtext): | |
| 77 self._name = name | |
| 78 self._handler = handler | |
| 79 self._econtext = econtext | |
| 80 def __call__(self, text): | |
| 81 return self._handler(self._name, text, | |
|
1257
93b80ad11ca8
merged Zope Collector #372 fix from ZPT CVS trunk
Richard Jones <richard@users.sourceforge.net>
parents:
1071
diff
changeset
|
82 self._econtext.getCompiler())(self._econtext) |
| 1049 | 83 |
