Code

- issue2550728: remove buggy parentheses in TAL/DummyEngine.py.
[roundup.git] / roundup / cgi / TAL / DummyEngine.py
1 ##############################################################################
2 #
3 # Copyright (c) 2001, 2002 Zope Corporation and Contributors.
4 # All Rights Reserved.
5 #
6 # This software is subject to the provisions of the Zope Public License,
7 # Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
8 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
9 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
11 # FOR A PARTICULAR PURPOSE.
12 #
13 ##############################################################################
14 # Modifications for Roundup:
15 # 1. commented out ITALES references
16 # 2. implemented ustr as str
17 """
18 Dummy TALES engine so that I can test out the TAL implementation.
19 """
21 import re
22 import sys
24 from TALDefs import NAME_RE, TALESError, ErrorInfo
25 #from ITALES import ITALESCompiler, ITALESEngine
26 #from DocumentTemplate.DT_Util import ustr
27 ustr = str
29 IDomain = None
30 if sys.modules.has_key('Zope'):
31     try:
32         from Zope.I18n.ITranslationService import ITranslationService
33         from Zope.I18n.IDomain import IDomain
34     except ImportError:
35         pass
36 if IDomain is None:
37     # Before 2.7, or not in Zope
38     class ITranslationService: pass
39     class IDomain: pass
41 class _Default:
42     pass
43 Default = _Default()
45 name_match = re.compile(r"(?s)(%s):(.*)\Z" % NAME_RE).match
47 class CompilerError(Exception):
48     pass
50 class DummyEngine:
52     position = None
53     source_file = None
55     #__implements__ = ITALESCompiler, ITALESEngine
57     def __init__(self, macros=None):
58         if macros is None:
59             macros = {}
60         self.macros = macros
61         dict = {'nothing': None, 'default': Default}
62         self.locals = self.globals = dict
63         self.stack = [dict]
64         self.translationService = DummyTranslationService()
66     def getCompilerError(self):
67         return CompilerError
69     def getCompiler(self):
70         return self
72     def setSourceFile(self, source_file):
73         self.source_file = source_file
75     def setPosition(self, position):
76         self.position = position
78     def compile(self, expr):
79         return "$%s$" % expr
81     def uncompile(self, expression):
82         assert expression.startswith("$") and expression.endswith("$"),expression
83         return expression[1:-1]
85     def beginScope(self):
86         self.stack.append(self.locals)
88     def endScope(self):
89         assert len(self.stack) > 1, "more endScope() than beginScope() calls"
90         self.locals = self.stack.pop()
92     def setLocal(self, name, value):
93         if self.locals is self.stack[-1]:
94             # Unmerge this scope's locals from previous scope of first set
95             self.locals = self.locals.copy()
96         self.locals[name] = value
98     def setGlobal(self, name, value):
99         self.globals[name] = value
101     def evaluate(self, expression):
102         assert expression.startswith("$") and expression.endswith("$"), expression
103         expression = expression[1:-1]
104         m = name_match(expression)
105         if m:
106             type, expr = m.group(1, 2)
107         else:
108             type = "path"
109             expr = expression
110         if type in ("string", "str"):
111             return expr
112         if type in ("path", "var", "global", "local"):
113             return self.evaluatePathOrVar(expr)
114         if type == "not":
115             return not self.evaluate(expr)
116         if type == "exists":
117             return self.locals.has_key(expr) or self.globals.has_key(expr)
118         if type == "python":
119             try:
120                 return eval(expr, self.globals, self.locals)
121             except:
122                 raise TALESError("evaluation error in %s" % `expr`)
123         if type == "position":
124             # Insert the current source file name, line number,
125             # and column offset.
126             if self.position:
127                 lineno, offset = self.position
128             else:
129                 lineno, offset = None, None
130             return '%s (%s,%s)' % (self.source_file, lineno, offset)
131         raise TALESError("unrecognized expression: " + `expression`)
133     def evaluatePathOrVar(self, expr):
134         expr = expr.strip()
135         if self.locals.has_key(expr):
136             return self.locals[expr]
137         elif self.globals.has_key(expr):
138             return self.globals[expr]
139         else:
140             raise TALESError("unknown variable: %s" % `expr`)
142     def evaluateValue(self, expr):
143         return self.evaluate(expr)
145     def evaluateBoolean(self, expr):
146         return self.evaluate(expr)
148     def evaluateText(self, expr):
149         text = self.evaluate(expr)
150         if text is not None and text is not Default:
151             text = ustr(text)
152         return text
154     def evaluateStructure(self, expr):
155         # XXX Should return None or a DOM tree
156         return self.evaluate(expr)
158     def evaluateSequence(self, expr):
159         # XXX Should return a sequence
160         return self.evaluate(expr)
162     def evaluateMacro(self, macroName):
163         assert macroName.startswith("$") and macroName.endswith("$"), macroName
164         macroName = macroName[1:-1]
165         file, localName = self.findMacroFile(macroName)
166         if not file:
167             # Local macro
168             macro = self.macros[localName]
169         else:
170             # External macro
171             import driver
172             program, macros = driver.compilefile(file)
173             macro = macros.get(localName)
174             if not macro:
175                 raise TALESError("macro %s not found in file %s" %
176                                  (localName, file))
177         return macro
179     def findMacroDocument(self, macroName):
180         file, localName = self.findMacroFile(macroName)
181         if not file:
182             return file, localName
183         import driver
184         doc = driver.parsefile(file)
185         return doc, localName
187     def findMacroFile(self, macroName):
188         if not macroName:
189             raise TALESError("empty macro name")
190         i = macroName.rfind('/')
191         if i < 0:
192             # No slash -- must be a locally defined macro
193             return None, macroName
194         else:
195             # Up to last slash is the filename
196             fileName = macroName[:i]
197             localName = macroName[i+1:]
198             return fileName, localName
200     def setRepeat(self, name, expr):
201         seq = self.evaluateSequence(expr)
202         return Iterator(name, seq, self)
204     def createErrorInfo(self, err, position):
205         return ErrorInfo(err, position)
207     def getDefault(self):
208         return Default
210     def translate(self, domain, msgid, mapping, default=None):
211         return self.translationService.translate(domain, msgid, mapping,
212                                                  default=default)
215 class Iterator:
217     # This is not an implementation of a Python iterator.  The next()
218     # method returns true or false to indicate whether another item is
219     # available; if there is another item, the iterator instance calls
220     # setLocal() on the evaluation engine passed to the constructor.
222     def __init__(self, name, seq, engine):
223         self.name = name
224         self.seq = seq
225         self.engine = engine
226         self.nextIndex = 0
228     def next(self):
229         i = self.nextIndex
230         try:
231             item = self.seq[i]
232         except IndexError:
233             return 0
234         self.nextIndex = i+1
235         self.engine.setLocal(self.name, item)
236         return 1
238 class DummyDomain:
239     __implements__ = IDomain
241     def translate(self, msgid, mapping=None, context=None,
242                   target_language=None, default=None):
243         # This is a fake translation service which simply uppercases non
244         # ${name} placeholder text in the message id.
245         #
246         # First, transform a string with ${name} placeholders into a list of
247         # substrings.  Then upcase everything but the placeholders, then glue
248         # things back together.
250         # simulate an unknown msgid by returning None
251         if msgid == "don't translate me":
252             text = default
253         else:
254             text = msgid.upper()
256         def repl(m, mapping=mapping):
257             return ustr(mapping[m.group(m.lastindex).lower()])
258         cre = re.compile(r'\$(?:(%s)|\{(%s)\})' % (NAME_RE, NAME_RE))
259         return cre.sub(repl, text)
261 class DummyTranslationService:
262     __implements__ = ITranslationService
264     def translate(self, domain, msgid, mapping=None, context=None,
265                   target_language=None, default=None):
266         return self.getDomain(domain).translate(msgid, mapping, context,
267                                                 target_language,
268                                                 default=default)
270     def getDomain(self, domain):
271         return DummyDomain()