comparison roundup/cgi/PageTemplates/TALES.py @ 2349:b43efe461b3e

update PageTemplates to latest Zope codebase
author Richard Jones <richard@users.sourceforge.net>
date Fri, 21 May 2004 05:56:46 +0000
parents fc52d57c6c3e
children 29779f041ee7
comparison
equal deleted inserted replaced
2348:8c2402a78bb0 2349:b43efe461b3e
1 ############################################################################## 1 ##############################################################################
2 # 2 #
3 # Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved. 3 # Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
4 # 4 #
5 # This software is subject to the provisions of the Zope Public License, 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. 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 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 8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS 9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
10 # FOR A PARTICULAR PURPOSE 10 # FOR A PARTICULAR PURPOSE
11 #
12 ##############################################################################
13 # Modified for Roundup:
11 # 14 #
12 ############################################################################## 15 # 1. changed imports to import from roundup.cgi
16 # 2. implemented ustr as atr
13 """TALES 17 """TALES
14 18
15 An implementation of a generic TALES engine 19 An implementation of a generic TALES engine
16
17 Modified for Roundup 0.5 release:
18
19 - changed imports to import from roundup.cgi
20 """ 20 """
21 __docformat__ = 'restructuredtext' 21
22 22 __version__='$Revision: 1.7 $'[11:-2]
23 __version__='$Revision: 1.6 $'[11:-2]
24 23
25 import re, sys 24 import re, sys
26 from roundup.cgi import ZTUtils 25 from roundup.cgi import ZTUtils
26 from weakref import ref
27 from MultiMapping import MultiMapping 27 from MultiMapping import MultiMapping
28 from GlobalTranslationService import getGlobalTranslationService
29
30 ustr = str
28 31
29 StringType = type('') 32 StringType = type('')
30 33
31 NAME_RE = r"[a-zA-Z][a-zA-Z0-9_]*" 34 NAME_RE = r"[a-zA-Z][a-zA-Z0-9_]*"
32 _parse_expr = re.compile(r"(%s):" % NAME_RE).match 35 _parse_expr = re.compile(r"(%s):" % NAME_RE).match
45 '''TALES Compiler Error''' 48 '''TALES Compiler Error'''
46 49
47 class Default: 50 class Default:
48 '''Retain Default''' 51 '''Retain Default'''
49 Default = Default() 52 Default = Default()
50
51 _marker = []
52 53
53 class SafeMapping(MultiMapping): 54 class SafeMapping(MultiMapping):
54 '''Mapping with security declarations and limited method exposure. 55 '''Mapping with security declarations and limited method exposure.
55 56
56 Since it subclasses MultiMapping, this class can be used to wrap 57 Since it subclasses MultiMapping, this class can be used to wrap
62 push = pop = None 63 push = pop = None
63 64
64 _push = MultiMapping.push 65 _push = MultiMapping.push
65 _pop = MultiMapping.pop 66 _pop = MultiMapping.pop
66 67
67 def has_get(self, key, _marker=[]):
68 v = self.get(key, _marker)
69 return v is not _marker, v
70 68
71 class Iterator(ZTUtils.Iterator): 69 class Iterator(ZTUtils.Iterator):
72 def __init__(self, name, seq, context): 70 def __init__(self, name, seq, context):
73 ZTUtils.Iterator.__init__(self, seq) 71 ZTUtils.Iterator.__init__(self, seq)
74 self.name = name 72 self.name = name
75 self._context = context 73 self._context_ref = ref(context)
76 74
77 def next(self): 75 def next(self):
78 if ZTUtils.Iterator.next(self): 76 if ZTUtils.Iterator.next(self):
79 self._context.setLocal(self.name, self.item) 77 context = self._context_ref()
78 if context is not None:
79 context.setLocal(self.name, self.item)
80 return 1 80 return 1
81 return 0 81 return 0
82 82
83 83
84 class ErrorInfo: 84 class ErrorInfo:
136 handler = self.types[type] 136 handler = self.types[type]
137 except KeyError: 137 except KeyError:
138 raise CompilerError, ( 138 raise CompilerError, (
139 'Unrecognized expression type "%s".' % type) 139 'Unrecognized expression type "%s".' % type)
140 return handler(type, expr, self) 140 return handler(type, expr, self)
141 141
142 def getContext(self, contexts=None, **kwcontexts): 142 def getContext(self, contexts=None, **kwcontexts):
143 if contexts is not None: 143 if contexts is not None:
144 if kwcontexts: 144 if kwcontexts:
145 kwcontexts.update(contexts) 145 kwcontexts.update(contexts)
146 else: 146 else:
221 isinstance=isinstance, StringType=StringType): 221 isinstance=isinstance, StringType=StringType):
222 if isinstance(expression, StringType): 222 if isinstance(expression, StringType):
223 expression = self._compiler.compile(expression) 223 expression = self._compiler.compile(expression)
224 __traceback_supplement__ = ( 224 __traceback_supplement__ = (
225 TALESTracebackSupplement, self, expression) 225 TALESTracebackSupplement, self, expression)
226 v = expression(self) 226 return expression(self)
227 return v
228 227
229 evaluateValue = evaluate 228 evaluateValue = evaluate
230 evaluateBoolean = evaluate 229 evaluateBoolean = evaluate
231 230
232 def evaluateText(self, expr): 231 def evaluateText(self, expr):
233 text = self.evaluate(expr) 232 text = self.evaluate(expr)
234 if text is Default or text is None: 233 if text is Default or text is None:
235 return text 234 return text
236 return str(text) 235 if isinstance(text, unicode):
236 return text
237 else:
238 return ustr(text)
237 239
238 def evaluateStructure(self, expr): 240 def evaluateStructure(self, expr):
239 return self.evaluate(expr) 241 return self.evaluate(expr)
240 evaluateStructure = evaluate 242 evaluateStructure = evaluate
241 243
254 self.source_file = source_file 256 self.source_file = source_file
255 257
256 def setPosition(self, position): 258 def setPosition(self, position):
257 self.position = position 259 self.position = position
258 260
259 261 def translate(self, domain, msgid, mapping=None,
262 context=None, target_language=None, default=None):
263 if context is None:
264 context = self.contexts.get('here')
265 return getGlobalTranslationService().translate(
266 domain, msgid, mapping=mapping,
267 context=context,
268 default=default,
269 target_language=target_language)
260 270
261 class TALESTracebackSupplement: 271 class TALESTracebackSupplement:
262 """Implementation of ITracebackSupplement""" 272 """Implementation of ITracebackSupplement"""
263 def __init__(self, context, expression): 273 def __init__(self, context, expression):
264 self.context = context 274 self.context = context
270 def getInfo(self, as_html=0): 280 def getInfo(self, as_html=0):
271 import pprint 281 import pprint
272 data = self.context.contexts.copy() 282 data = self.context.contexts.copy()
273 s = pprint.pformat(data) 283 s = pprint.pformat(data)
274 if not as_html: 284 if not as_html:
275 return ' - Names:\n %s' % string.replace(s, '\n', '\n ') 285 return ' - Names:\n %s' % s.replace('\n', '\n ')
276 else: 286 else:
277 from cgi import escape 287 from cgi import escape
278 return '<b>Names:</b><pre>%s</pre>' % (escape(s)) 288 return '<b>Names:</b><pre>%s</pre>' % (escape(s))
279 return None
280
281 289
282 290
283 class SimpleExpr: 291 class SimpleExpr:
284 '''Simple example of an expression type handler''' 292 '''Simple example of an expression type handler'''
285 def __init__(self, name, expr, engine): 293 def __init__(self, name, expr, engine):
287 self._expr = expr 295 self._expr = expr
288 def __call__(self, econtext): 296 def __call__(self, econtext):
289 return self._name, self._expr 297 return self._name, self._expr
290 def __repr__(self): 298 def __repr__(self):
291 return '<SimpleExpr %s %s>' % (self._name, `self._expr`) 299 return '<SimpleExpr %s %s>' % (self._name, `self._expr`)
292

Roundup Issue Tracker: http://roundup-tracker.org/