Mercurial > p > roundup > code
annotate roundup/cgi/PageTemplates/PythonExpr.py @ 1767:fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Thu, 28 Aug 2003 04:46:54 +0000 |
| parents | 93b80ad11ca8 |
| children | fc52d57c6c3e |
| rev | line source |
|---|---|
| 1049 | 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 ############################################################################## | |
| 13 | |
| 14 """Generic Python Expression Handler | |
|
1071
c08b3820edd1
Adhering to ZPL
Richard Jones <richard@users.sourceforge.net>
parents:
1065
diff
changeset
|
15 |
|
c08b3820edd1
Adhering to ZPL
Richard Jones <richard@users.sourceforge.net>
parents:
1065
diff
changeset
|
16 Modified for Roundup 0.5 release: |
|
c08b3820edd1
Adhering to ZPL
Richard Jones <richard@users.sourceforge.net>
parents:
1065
diff
changeset
|
17 |
|
c08b3820edd1
Adhering to ZPL
Richard Jones <richard@users.sourceforge.net>
parents:
1065
diff
changeset
|
18 - more informative traceback info |
|
c08b3820edd1
Adhering to ZPL
Richard Jones <richard@users.sourceforge.net>
parents:
1065
diff
changeset
|
19 |
| 1049 | 20 """ |
| 21 | |
|
1257
93b80ad11ca8
merged Zope Collector #372 fix from ZPT CVS trunk
Richard Jones <richard@users.sourceforge.net>
parents:
1071
diff
changeset
|
22 __version__='$Revision: 1.4 $'[11:-2] |
| 1049 | 23 |
| 24 from TALES import CompilerError | |
| 25 from string import strip, split, join, replace, lstrip | |
| 26 from sys import exc_info | |
| 27 | |
| 28 class getSecurityManager: | |
| 29 '''Null security manager''' | |
| 30 def validate(self, *args, **kwargs): | |
| 31 return 1 | |
| 32 addContext = removeContext = validateValue = validate | |
| 33 | |
| 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() | |
| 45 | |
| 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) | |
| 51 | |
| 52 def _bind_used_names(self, econtext): | |
| 53 # Bind template variables | |
| 54 names = {} | |
| 55 vars = econtext.vars | |
|
1257
93b80ad11ca8
merged Zope Collector #372 fix from ZPT CVS trunk
Richard Jones <richard@users.sourceforge.net>
parents:
1071
diff
changeset
|
56 getType = econtext.getCompiler().getTypes().get |
| 1049 | 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 | |
| 66 | |
| 67 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
|
68 __traceback_info__ = 'python expression "%s"'%self.expr |
| 1049 | 69 f = self._f |
| 70 f.func_globals.update(self._bind_used_names(econtext)) | |
| 71 return f() | |
| 72 | |
| 73 def __str__(self): | |
| 74 return 'Python expression "%s"' % self.expr | |
| 75 def __repr__(self): | |
| 76 return '<PythonExpr %s>' % self.expr | |
| 77 | |
| 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, | |
|
1257
93b80ad11ca8
merged Zope Collector #372 fix from ZPT CVS trunk
Richard Jones <richard@users.sourceforge.net>
parents:
1071
diff
changeset
|
86 self._econtext.getCompiler())(self._econtext) |
| 1049 | 87 |
