Mercurial > p > roundup > code
annotate roundup/cgi/TAL/TALInterpreter.py @ 8528:fed0f839c260
fix: replace except: with except Exception: (by haosenwang1018@github)
Remove bare 'except:' statements replace with 'except Exception'.
In roundup_xmlrpc_server.py I changed Exception to BaseException so
the database gets closed on signals as well. The rest of the changes
do not look like they affect data integrity and are commited as
supplied.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Thu, 26 Feb 2026 09:46:02 -0500 |
| parents | 883c9e90b403 |
| children |
| rev | line source |
|---|---|
| 1049 | 1 ############################################################################## |
| 2 # | |
| 3 # Copyright (c) 2001, 2002 Zope Corporation and Contributors. | |
| 4 # All Rights Reserved. | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
5 # |
| 1049 | 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 | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
11 # FOR A PARTICULAR PURPOSE. |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
12 # |
| 1049 | 13 ############################################################################## |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
14 # Modifications for Roundup: |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
15 # 1. implemented ustr as str |
| 1049 | 16 """ |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
17 Interpreter for a pre-compiled TAL program. |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
18 """ |
| 1049 | 19 |
| 20 import sys | |
| 21 import getopt | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
22 import re |
|
5803
d79ba7365abb
Yet another use of cgi.escape. Fixed.
John Rouillard <rouilj@ieee.org>
parents:
5478
diff
changeset
|
23 |
|
d79ba7365abb
Yet another use of cgi.escape. Fixed.
John Rouillard <rouilj@ieee.org>
parents:
5478
diff
changeset
|
24 |
|
5837
883c9e90b403
Fix problem with cgi.escape being depricated a different way. This way
John Rouillard <rouilj@ieee.org>
parents:
5803
diff
changeset
|
25 from roundup.anypy.html import html_escape as escape |
|
5418
55f09ca366c4
Python 3 preparation: StringIO.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
26 from roundup.anypy.strings import StringIO |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
27 #from DocumentTemplate.DT_Util import ustr |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
28 ustr = str |
| 1049 | 29 |
|
5388
d26921b851c3
Python 3 preparation: make relative imports explicit.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5381
diff
changeset
|
30 from .TALDefs import TAL_VERSION, TALError, METALError, attrEscape |
|
d26921b851c3
Python 3 preparation: make relative imports explicit.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5381
diff
changeset
|
31 from .TALDefs import isCurrentVersion, getProgramVersion, getProgramMode |
|
d26921b851c3
Python 3 preparation: make relative imports explicit.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5381
diff
changeset
|
32 from .TALGenerator import TALGenerator |
|
d26921b851c3
Python 3 preparation: make relative imports explicit.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5381
diff
changeset
|
33 from .TranslationContext import TranslationContext |
| 1049 | 34 |
| 35 BOOLEAN_HTML_ATTRS = [ | |
| 36 # List of Boolean attributes in HTML that should be rendered in | |
| 37 # minimized form (e.g. <img ismap> rather than <img ismap="">) | |
| 38 # From http://www.w3.org/TR/xhtml1/#guidelines (C.10) | |
| 39 # XXX The problem with this is that this is not valid XML and | |
| 40 # can't be parsed back! | |
| 41 "compact", "nowrap", "ismap", "declare", "noshade", "checked", | |
| 42 "disabled", "readonly", "multiple", "selected", "noresize", | |
| 43 "defer" | |
| 44 ] | |
| 45 | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
46 def normalize(text): |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
47 # Now we need to normalize the whitespace in implicit message ids and |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
48 # implicit $name substitution values by stripping leading and trailing |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
49 # whitespace, and folding all internal whitespace to a single space. |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
50 return ' '.join(text.split()) |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
51 |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
52 |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
53 NAME_RE = r"[a-zA-Z][a-zA-Z0-9_]*" |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
54 _interp_regex = re.compile(r'(?<!\$)(\$(?:%(n)s|{%(n)s}))' %({'n': NAME_RE})) |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
55 _get_var_regex = re.compile(r'%(n)s' %({'n': NAME_RE})) |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
56 |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
57 def interpolate(text, mapping): |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
58 """Interpolate ${keyword} substitutions. |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
59 |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
60 This is called when no translation is provided by the translation |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
61 service. |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
62 """ |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
63 if not mapping: |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
64 return text |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
65 # Find all the spots we want to substitute. |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
66 to_replace = _interp_regex.findall(text) |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
67 # Now substitute with the variables in mapping. |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
68 for string in to_replace: |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
69 var = _get_var_regex.findall(string)[0] |
|
5381
0942fe89e82e
Python 3 preparation: change "x.has_key(y)" to "y in x".
Joseph Myers <jsm@polyomino.org.uk>
parents:
5378
diff
changeset
|
70 if var in mapping: |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
71 # Call ustr because we may have an integer for instance. |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
72 subst = ustr(mapping[var]) |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
73 try: |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
74 text = text.replace(string, subst) |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
75 except UnicodeError: |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
76 # subst contains high-bit chars... |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
77 # As we have no way of knowing the correct encoding, |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
78 # substitue something instead of raising an exception. |
|
5377
12fe83f90f0d
Python 3 preparation: use repr() instead of ``.
Joseph Myers <jsm@polyomino.org.uk>
parents:
2348
diff
changeset
|
79 subst = repr(subst)[1:-1] |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
80 text = text.replace(string, subst) |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
81 return text |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
82 |
| 1049 | 83 |
| 84 class AltTALGenerator(TALGenerator): | |
| 85 | |
| 86 def __init__(self, repldict, expressionCompiler=None, xml=0): | |
| 87 self.repldict = repldict | |
| 88 self.enabled = 1 | |
| 89 TALGenerator.__init__(self, expressionCompiler, xml) | |
| 90 | |
| 91 def enable(self, enabled): | |
| 92 self.enabled = enabled | |
| 93 | |
| 94 def emit(self, *args): | |
| 95 if self.enabled: | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
96 TALGenerator.emit(self, *args) |
| 1049 | 97 |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
98 def emitStartElement(self, name, attrlist, taldict, metaldict, i18ndict, |
| 1049 | 99 position=(None, None), isend=0): |
| 100 metaldict = {} | |
| 101 taldict = {} | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
102 i18ndict = {} |
| 1049 | 103 if self.enabled and self.repldict: |
| 104 taldict["attributes"] = "x x" | |
| 105 TALGenerator.emitStartElement(self, name, attrlist, | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
106 taldict, metaldict, i18ndict, |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
107 position, isend) |
| 1049 | 108 |
| 109 def replaceAttrs(self, attrlist, repldict): | |
| 110 if self.enabled and self.repldict: | |
| 111 repldict = self.repldict | |
| 112 self.repldict = None | |
| 113 return TALGenerator.replaceAttrs(self, attrlist, repldict) | |
| 114 | |
| 115 | |
| 116 class TALInterpreter: | |
| 117 | |
| 118 def __init__(self, program, macros, engine, stream=None, | |
| 119 debug=0, wrap=60, metal=1, tal=1, showtal=-1, | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
120 strictinsert=1, stackLimit=100, i18nInterpolate=1): |
| 1049 | 121 self.program = program |
| 122 self.macros = macros | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
123 self.engine = engine # Execution engine (aka context) |
| 1049 | 124 self.Default = engine.getDefault() |
| 125 self.stream = stream or sys.stdout | |
| 126 self._stream_write = self.stream.write | |
| 127 self.debug = debug | |
| 128 self.wrap = wrap | |
| 129 self.metal = metal | |
| 130 self.tal = tal | |
| 131 if tal: | |
| 132 self.dispatch = self.bytecode_handlers_tal | |
| 133 else: | |
| 134 self.dispatch = self.bytecode_handlers | |
| 135 assert showtal in (-1, 0, 1) | |
| 136 if showtal == -1: | |
| 137 showtal = (not tal) | |
| 138 self.showtal = showtal | |
| 139 self.strictinsert = strictinsert | |
| 140 self.stackLimit = stackLimit | |
| 141 self.html = 0 | |
| 142 self.endsep = "/>" | |
| 143 self.endlen = len(self.endsep) | |
| 144 self.macroStack = [] | |
| 145 self.position = None, None # (lineno, offset) | |
| 146 self.col = 0 | |
| 147 self.level = 0 | |
| 148 self.scopeLevel = 0 | |
| 149 self.sourceFile = None | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
150 self.i18nStack = [] |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
151 self.i18nInterpolate = i18nInterpolate |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
152 self.i18nContext = TranslationContext() |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
153 |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
154 def StringIO(self): |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
155 # Third-party products wishing to provide a full Unicode-aware |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
156 # StringIO can do so by monkey-patching this method. |
|
5478
c8902d398434
removed FasterStringIO
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5420
diff
changeset
|
157 return StringIO() |
| 1049 | 158 |
| 159 def saveState(self): | |
| 160 return (self.position, self.col, self.stream, | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
161 self.scopeLevel, self.level, self.i18nContext) |
| 1049 | 162 |
| 163 def restoreState(self, state): | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
164 (self.position, self.col, self.stream, scopeLevel, level, i18n) = state |
| 1049 | 165 self._stream_write = self.stream.write |
| 166 assert self.level == level | |
| 167 while self.scopeLevel > scopeLevel: | |
| 168 self.engine.endScope() | |
| 169 self.scopeLevel = self.scopeLevel - 1 | |
| 170 self.engine.setPosition(self.position) | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
171 self.i18nContext = i18n |
| 1049 | 172 |
| 173 def restoreOutputState(self, state): | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
174 (dummy, self.col, self.stream, scopeLevel, level, i18n) = state |
| 1049 | 175 self._stream_write = self.stream.write |
| 176 assert self.level == level | |
| 177 assert self.scopeLevel == scopeLevel | |
| 178 | |
| 179 def pushMacro(self, macroName, slots, entering=1): | |
| 180 if len(self.macroStack) >= self.stackLimit: | |
| 181 raise METALError("macro nesting limit (%d) exceeded " | |
|
5377
12fe83f90f0d
Python 3 preparation: use repr() instead of ``.
Joseph Myers <jsm@polyomino.org.uk>
parents:
2348
diff
changeset
|
182 "by %s" % (self.stackLimit, repr(macroName))) |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
183 self.macroStack.append([macroName, slots, entering, self.i18nContext]) |
| 1049 | 184 |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
185 def popMacro(self): |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
186 return self.macroStack.pop() |
| 1049 | 187 |
| 188 def __call__(self): | |
| 189 assert self.level == 0 | |
| 190 assert self.scopeLevel == 0 | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
191 assert self.i18nContext.parent is None |
| 1049 | 192 self.interpret(self.program) |
| 193 assert self.level == 0 | |
| 194 assert self.scopeLevel == 0 | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
195 assert self.i18nContext.parent is None |
| 1049 | 196 if self.col > 0: |
| 197 self._stream_write("\n") | |
| 198 self.col = 0 | |
| 199 | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
200 def stream_write(self, s, |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
201 len=len): |
| 1049 | 202 self._stream_write(s) |
|
1309
309f125f86cc
removed use of string/strop from TAL/TALInterpreter
Richard Jones <richard@users.sourceforge.net>
parents:
1049
diff
changeset
|
203 i = s.rfind('\n') |
| 1049 | 204 if i < 0: |
| 205 self.col = self.col + len(s) | |
| 206 else: | |
| 207 self.col = len(s) - (i + 1) | |
| 208 | |
| 209 bytecode_handlers = {} | |
| 210 | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
211 def interpretWithStream(self, program, stream): |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
212 oldstream = self.stream |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
213 self.stream = stream |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
214 self._stream_write = stream.write |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
215 try: |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
216 self.interpret(program) |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
217 finally: |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
218 self.stream = oldstream |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
219 self._stream_write = oldstream.write |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
220 |
|
1409
8dc60d87ab42
Fixed a backlog of bug reports, and worked on python 2.3 compatibility:
Richard Jones <richard@users.sourceforge.net>
parents:
1309
diff
changeset
|
221 def interpret(self, program): |
| 1049 | 222 oldlevel = self.level |
| 223 self.level = oldlevel + 1 | |
| 224 handlers = self.dispatch | |
| 225 try: | |
| 226 if self.debug: | |
| 227 for (opcode, args) in program: | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
228 s = "%sdo_%s(%s)\n" % (" "*self.level, opcode, |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
229 repr(args)) |
| 1049 | 230 if len(s) > 80: |
| 231 s = s[:76] + "...\n" | |
| 232 sys.stderr.write(s) | |
| 233 handlers[opcode](self, args) | |
| 234 else: | |
| 235 for (opcode, args) in program: | |
| 236 handlers[opcode](self, args) | |
| 237 finally: | |
| 238 self.level = oldlevel | |
| 239 | |
| 240 def do_version(self, version): | |
| 241 assert version == TAL_VERSION | |
| 242 bytecode_handlers["version"] = do_version | |
| 243 | |
| 244 def do_mode(self, mode): | |
| 245 assert mode in ("html", "xml") | |
| 246 self.html = (mode == "html") | |
| 247 if self.html: | |
| 248 self.endsep = " />" | |
| 249 else: | |
| 250 self.endsep = "/>" | |
| 251 self.endlen = len(self.endsep) | |
| 252 bytecode_handlers["mode"] = do_mode | |
| 253 | |
| 254 def do_setSourceFile(self, source_file): | |
| 255 self.sourceFile = source_file | |
| 256 self.engine.setSourceFile(source_file) | |
| 257 bytecode_handlers["setSourceFile"] = do_setSourceFile | |
| 258 | |
| 259 def do_setPosition(self, position): | |
| 260 self.position = position | |
| 261 self.engine.setPosition(position) | |
| 262 bytecode_handlers["setPosition"] = do_setPosition | |
| 263 | |
| 264 def do_startEndTag(self, stuff): | |
| 265 self.do_startTag(stuff, self.endsep, self.endlen) | |
| 266 bytecode_handlers["startEndTag"] = do_startEndTag | |
| 267 | |
|
5393
46b2fd6d342e
Python 3 preparation: avoid implicit tuple parameter unpacking.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5388
diff
changeset
|
268 def do_startTag(self, name_attrList, |
| 1049 | 269 end=">", endlen=1, _len=len): |
| 270 # The bytecode generator does not cause calls to this method | |
| 271 # for start tags with no attributes; those are optimized down | |
| 272 # to rawtext events. Hence, there is no special "fast path" | |
| 273 # for that case. | |
|
5393
46b2fd6d342e
Python 3 preparation: avoid implicit tuple parameter unpacking.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5388
diff
changeset
|
274 (name, attrList) = name_attrList |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
275 L = ["<", name] |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
276 append = L.append |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
277 col = self.col + _len(name) + 1 |
| 1049 | 278 wrap = self.wrap |
| 279 align = col + 1 | |
|
5400
2120f77554d5
Python 3 preparation: use // and __truediv__ as needed.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5393
diff
changeset
|
280 if align >= wrap//2: |
| 1049 | 281 align = 4 # Avoid a narrow column far to the right |
| 282 attrAction = self.dispatch["<attrAction>"] | |
| 283 try: | |
| 284 for item in attrList: | |
| 285 if _len(item) == 2: | |
| 286 name, s = item | |
| 287 else: | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
288 # item[2] is the 'action' field: |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
289 if item[2] in ('metal', 'tal', 'xmlns', 'i18n'): |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
290 if not self.showtal: |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
291 continue |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
292 ok, name, s = self.attrAction(item) |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
293 else: |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
294 ok, name, s = attrAction(self, item) |
| 1049 | 295 if not ok: |
| 296 continue | |
| 297 slen = _len(s) | |
| 298 if (wrap and | |
| 299 col >= align and | |
| 300 col + 1 + slen > wrap): | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
301 append("\n") |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
302 append(" "*align) |
| 1049 | 303 col = align + slen |
| 304 else: | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
305 append(" ") |
| 1049 | 306 col = col + 1 + slen |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
307 append(s) |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
308 append(end) |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
309 self._stream_write("".join(L)) |
| 1049 | 310 col = col + endlen |
| 311 finally: | |
| 312 self.col = col | |
| 313 bytecode_handlers["startTag"] = do_startTag | |
| 314 | |
| 315 def attrAction(self, item): | |
| 316 name, value, action = item[:3] | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
317 if action == 'insert': |
| 1049 | 318 return 0, name, value |
| 319 macs = self.macroStack | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
320 if action == 'metal' and self.metal and macs: |
| 1049 | 321 if len(macs) > 1 or not macs[-1][2]: |
| 322 # Drop all METAL attributes at a use-depth above one. | |
| 323 return 0, name, value | |
| 324 # Clear 'entering' flag | |
| 325 macs[-1][2] = 0 | |
| 326 # Convert or drop depth-one METAL attributes. | |
|
1309
309f125f86cc
removed use of string/strop from TAL/TALInterpreter
Richard Jones <richard@users.sourceforge.net>
parents:
1049
diff
changeset
|
327 i = name.rfind(":") + 1 |
| 1049 | 328 prefix, suffix = name[:i], name[i:] |
| 329 if suffix == "define-macro": | |
| 330 # Convert define-macro as we enter depth one. | |
| 331 name = prefix + "use-macro" | |
| 332 value = macs[-1][0] # Macro name | |
| 333 elif suffix == "define-slot": | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
334 name = prefix + "fill-slot" |
| 1049 | 335 elif suffix == "fill-slot": |
| 336 pass | |
| 337 else: | |
| 338 return 0, name, value | |
| 339 | |
| 340 if value is None: | |
| 341 value = name | |
| 342 else: | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
343 value = '%s="%s"' % (name, attrEscape(value)) |
| 1049 | 344 return 1, name, value |
| 345 | |
| 346 def attrAction_tal(self, item): | |
| 347 name, value, action = item[:3] | |
| 348 ok = 1 | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
349 expr, xlat, msgid = item[3:] |
|
1309
309f125f86cc
removed use of string/strop from TAL/TALInterpreter
Richard Jones <richard@users.sourceforge.net>
parents:
1049
diff
changeset
|
350 if self.html and name.lower() in BOOLEAN_HTML_ATTRS: |
| 1049 | 351 evalue = self.engine.evaluateBoolean(item[3]) |
| 352 if evalue is self.Default: | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
353 if action == 'insert': # Cancelled insert |
| 1049 | 354 ok = 0 |
| 355 elif evalue: | |
| 356 value = None | |
| 357 else: | |
| 358 ok = 0 | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
359 elif expr is not None: |
| 1049 | 360 evalue = self.engine.evaluateText(item[3]) |
| 361 if evalue is self.Default: | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
362 if action == 'insert': # Cancelled insert |
| 1049 | 363 ok = 0 |
| 364 else: | |
| 365 if evalue is None: | |
| 366 ok = 0 | |
| 367 value = evalue | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
368 else: |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
369 evalue = None |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
370 |
| 1049 | 371 if ok: |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
372 if xlat: |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
373 translated = self.translate(msgid or value, value, {}) |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
374 if translated is not None: |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
375 value = translated |
| 1049 | 376 if value is None: |
| 377 value = name | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
378 elif evalue is self.Default: |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
379 value = attrEscape(value) |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
380 else: |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
381 value = escape(value, quote=1) |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
382 value = '%s="%s"' % (name, value) |
| 1049 | 383 return ok, name, value |
| 384 bytecode_handlers["<attrAction>"] = attrAction | |
| 385 | |
| 386 def no_tag(self, start, program): | |
| 387 state = self.saveState() | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
388 self.stream = stream = self.StringIO() |
| 1049 | 389 self._stream_write = stream.write |
| 390 self.interpret(start) | |
| 391 self.restoreOutputState(state) | |
| 392 self.interpret(program) | |
| 393 | |
|
5393
46b2fd6d342e
Python 3 preparation: avoid implicit tuple parameter unpacking.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5388
diff
changeset
|
394 def do_optTag(self, args_tuple, |
| 1049 | 395 omit=0): |
|
5393
46b2fd6d342e
Python 3 preparation: avoid implicit tuple parameter unpacking.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5388
diff
changeset
|
396 (name, cexpr, tag_ns, isend, start, program) = args_tuple |
| 1049 | 397 if tag_ns and not self.showtal: |
| 398 return self.no_tag(start, program) | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
399 |
| 1049 | 400 self.interpret(start) |
| 401 if not isend: | |
| 402 self.interpret(program) | |
| 403 s = '</%s>' % name | |
| 404 self._stream_write(s) | |
| 405 self.col = self.col + len(s) | |
| 406 | |
| 407 def do_optTag_tal(self, stuff): | |
| 408 cexpr = stuff[1] | |
| 409 if cexpr is not None and (cexpr == '' or | |
| 410 self.engine.evaluateBoolean(cexpr)): | |
| 411 self.no_tag(stuff[-2], stuff[-1]) | |
| 412 else: | |
| 413 self.do_optTag(stuff) | |
| 414 bytecode_handlers["optTag"] = do_optTag | |
| 415 | |
|
5393
46b2fd6d342e
Python 3 preparation: avoid implicit tuple parameter unpacking.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5388
diff
changeset
|
416 def do_rawtextBeginScope(self, args_tuple): |
|
46b2fd6d342e
Python 3 preparation: avoid implicit tuple parameter unpacking.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5388
diff
changeset
|
417 (s, col, position, closeprev, dict) = args_tuple |
| 1049 | 418 self._stream_write(s) |
| 419 self.col = col | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
420 self.position = position |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
421 self.engine.setPosition(position) |
| 1049 | 422 if closeprev: |
| 423 engine = self.engine | |
| 424 engine.endScope() | |
| 425 engine.beginScope() | |
| 426 else: | |
| 427 self.engine.beginScope() | |
| 428 self.scopeLevel = self.scopeLevel + 1 | |
| 429 | |
|
5393
46b2fd6d342e
Python 3 preparation: avoid implicit tuple parameter unpacking.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5388
diff
changeset
|
430 def do_rawtextBeginScope_tal(self, args_tuple): |
|
46b2fd6d342e
Python 3 preparation: avoid implicit tuple parameter unpacking.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5388
diff
changeset
|
431 (s, col, position, closeprev, dict) = args_tuple |
| 1049 | 432 self._stream_write(s) |
| 433 self.col = col | |
| 434 engine = self.engine | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
435 self.position = position |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
436 engine.setPosition(position) |
| 1049 | 437 if closeprev: |
| 438 engine.endScope() | |
| 439 engine.beginScope() | |
| 440 else: | |
| 441 engine.beginScope() | |
| 442 self.scopeLevel = self.scopeLevel + 1 | |
| 443 engine.setLocal("attrs", dict) | |
| 444 bytecode_handlers["rawtextBeginScope"] = do_rawtextBeginScope | |
| 445 | |
| 446 def do_beginScope(self, dict): | |
| 447 self.engine.beginScope() | |
| 448 self.scopeLevel = self.scopeLevel + 1 | |
| 449 | |
| 450 def do_beginScope_tal(self, dict): | |
| 451 engine = self.engine | |
| 452 engine.beginScope() | |
| 453 engine.setLocal("attrs", dict) | |
| 454 self.scopeLevel = self.scopeLevel + 1 | |
| 455 bytecode_handlers["beginScope"] = do_beginScope | |
| 456 | |
| 457 def do_endScope(self, notused=None): | |
| 458 self.engine.endScope() | |
| 459 self.scopeLevel = self.scopeLevel - 1 | |
| 460 bytecode_handlers["endScope"] = do_endScope | |
| 461 | |
| 462 def do_setLocal(self, notused): | |
| 463 pass | |
| 464 | |
|
5393
46b2fd6d342e
Python 3 preparation: avoid implicit tuple parameter unpacking.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5388
diff
changeset
|
465 def do_setLocal_tal(self, name_expr): |
|
46b2fd6d342e
Python 3 preparation: avoid implicit tuple parameter unpacking.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5388
diff
changeset
|
466 (name, expr) = name_expr |
| 1049 | 467 self.engine.setLocal(name, self.engine.evaluateValue(expr)) |
| 468 bytecode_handlers["setLocal"] = do_setLocal | |
| 469 | |
|
5393
46b2fd6d342e
Python 3 preparation: avoid implicit tuple parameter unpacking.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5388
diff
changeset
|
470 def do_setGlobal_tal(self, name_expr): |
|
46b2fd6d342e
Python 3 preparation: avoid implicit tuple parameter unpacking.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5388
diff
changeset
|
471 (name, expr) = name_expr |
| 1049 | 472 self.engine.setGlobal(name, self.engine.evaluateValue(expr)) |
| 473 bytecode_handlers["setGlobal"] = do_setLocal | |
| 474 | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
475 def do_beginI18nContext(self, settings): |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
476 get = settings.get |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
477 self.i18nContext = TranslationContext(self.i18nContext, |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
478 domain=get("domain"), |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
479 source=get("source"), |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
480 target=get("target")) |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
481 bytecode_handlers["beginI18nContext"] = do_beginI18nContext |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
482 |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
483 def do_endI18nContext(self, notused=None): |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
484 self.i18nContext = self.i18nContext.parent |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
485 assert self.i18nContext is not None |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
486 bytecode_handlers["endI18nContext"] = do_endI18nContext |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
487 |
| 1049 | 488 def do_insertText(self, stuff): |
| 489 self.interpret(stuff[1]) | |
| 490 | |
| 491 def do_insertText_tal(self, stuff): | |
| 492 text = self.engine.evaluateText(stuff[0]) | |
| 493 if text is None: | |
| 494 return | |
| 495 if text is self.Default: | |
| 496 self.interpret(stuff[1]) | |
| 497 return | |
| 498 s = escape(text) | |
| 499 self._stream_write(s) | |
|
1309
309f125f86cc
removed use of string/strop from TAL/TALInterpreter
Richard Jones <richard@users.sourceforge.net>
parents:
1049
diff
changeset
|
500 i = s.rfind('\n') |
| 1049 | 501 if i < 0: |
| 502 self.col = self.col + len(s) | |
| 503 else: | |
| 504 self.col = len(s) - (i + 1) | |
| 505 bytecode_handlers["insertText"] = do_insertText | |
| 506 | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
507 def do_i18nVariable(self, stuff): |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
508 varname, program, expression = stuff |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
509 if expression is None: |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
510 # The value is implicitly the contents of this tag, so we have to |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
511 # evaluate the mini-program to get the value of the variable. |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
512 state = self.saveState() |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
513 try: |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
514 tmpstream = self.StringIO() |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
515 self.interpretWithStream(program, tmpstream) |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
516 value = normalize(tmpstream.getvalue()) |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
517 finally: |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
518 self.restoreState(state) |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
519 else: |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
520 # Evaluate the value to be associated with the variable in the |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
521 # i18n interpolation dictionary. |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
522 value = self.engine.evaluate(expression) |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
523 # Either the i18n:name tag is nested inside an i18n:translate in which |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
524 # case the last item on the stack has the i18n dictionary and string |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
525 # representation, or the i18n:name and i18n:translate attributes are |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
526 # in the same tag, in which case the i18nStack will be empty. In that |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
527 # case we can just output the ${name} to the stream |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
528 i18ndict, srepr = self.i18nStack[-1] |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
529 i18ndict[varname] = value |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
530 placeholder = '${%s}' % varname |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
531 srepr.append(placeholder) |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
532 self._stream_write(placeholder) |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
533 bytecode_handlers['i18nVariable'] = do_i18nVariable |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
534 |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
535 def do_insertTranslation(self, stuff): |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
536 i18ndict = {} |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
537 srepr = [] |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
538 obj = None |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
539 self.i18nStack.append((i18ndict, srepr)) |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
540 msgid = stuff[0] |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
541 # We need to evaluate the content of the tag because that will give us |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
542 # several useful pieces of information. First, the contents will |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
543 # include an implicit message id, if no explicit one was given. |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
544 # Second, it will evaluate any i18nVariable definitions in the body of |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
545 # the translation (necessary for $varname substitutions). |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
546 # |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
547 # Use a temporary stream to capture the interpretation of the |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
548 # subnodes, which should /not/ go to the output stream. |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
549 tmpstream = self.StringIO() |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
550 self.interpretWithStream(stuff[1], tmpstream) |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
551 default = tmpstream.getvalue() |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
552 # We only care about the evaluated contents if we need an implicit |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
553 # message id. All other useful information will be in the i18ndict on |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
554 # the top of the i18nStack. |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
555 if msgid == '': |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
556 msgid = normalize(default) |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
557 self.i18nStack.pop() |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
558 # See if there is was an i18n:data for msgid |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
559 if len(stuff) > 2: |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
560 obj = self.engine.evaluate(stuff[2]) |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
561 xlated_msgid = self.translate(msgid, default, i18ndict, obj) |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
562 assert xlated_msgid is not None, self.position |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
563 self._stream_write(xlated_msgid) |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
564 bytecode_handlers['insertTranslation'] = do_insertTranslation |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
565 |
| 1049 | 566 def do_insertStructure(self, stuff): |
| 567 self.interpret(stuff[2]) | |
| 568 | |
|
5393
46b2fd6d342e
Python 3 preparation: avoid implicit tuple parameter unpacking.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5388
diff
changeset
|
569 def do_insertStructure_tal(self, expr_repldict_block): |
|
46b2fd6d342e
Python 3 preparation: avoid implicit tuple parameter unpacking.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5388
diff
changeset
|
570 (expr, repldict, block) = expr_repldict_block |
| 1049 | 571 structure = self.engine.evaluateStructure(expr) |
| 572 if structure is None: | |
| 573 return | |
| 574 if structure is self.Default: | |
| 575 self.interpret(block) | |
| 576 return | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
577 text = ustr(structure) |
| 1049 | 578 if not (repldict or self.strictinsert): |
| 579 # Take a shortcut, no error checking | |
| 580 self.stream_write(text) | |
| 581 return | |
| 582 if self.html: | |
| 583 self.insertHTMLStructure(text, repldict) | |
| 584 else: | |
| 585 self.insertXMLStructure(text, repldict) | |
| 586 bytecode_handlers["insertStructure"] = do_insertStructure | |
| 587 | |
| 588 def insertHTMLStructure(self, text, repldict): | |
|
5388
d26921b851c3
Python 3 preparation: make relative imports explicit.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5381
diff
changeset
|
589 from .HTMLTALParser import HTMLTALParser |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
590 gen = AltTALGenerator(repldict, self.engine.getCompiler(), 0) |
| 1049 | 591 p = HTMLTALParser(gen) # Raises an exception if text is invalid |
| 592 p.parseString(text) | |
| 593 program, macros = p.getCode() | |
| 594 self.interpret(program) | |
| 595 | |
| 596 def insertXMLStructure(self, text, repldict): | |
|
5388
d26921b851c3
Python 3 preparation: make relative imports explicit.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5381
diff
changeset
|
597 from .TALParser import TALParser |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
598 gen = AltTALGenerator(repldict, self.engine.getCompiler(), 0) |
| 1049 | 599 p = TALParser(gen) |
| 600 gen.enable(0) | |
| 601 p.parseFragment('<!DOCTYPE foo PUBLIC "foo" "bar"><foo>') | |
| 602 gen.enable(1) | |
| 603 p.parseFragment(text) # Raises an exception if text is invalid | |
| 604 gen.enable(0) | |
| 605 p.parseFragment('</foo>', 1) | |
| 606 program, macros = gen.getCode() | |
| 607 self.interpret(program) | |
| 608 | |
|
5393
46b2fd6d342e
Python 3 preparation: avoid implicit tuple parameter unpacking.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5388
diff
changeset
|
609 def do_loop(self, name_expr_block): |
|
46b2fd6d342e
Python 3 preparation: avoid implicit tuple parameter unpacking.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5388
diff
changeset
|
610 (name, expr, block) = name_expr_block |
| 1049 | 611 self.interpret(block) |
| 612 | |
|
5393
46b2fd6d342e
Python 3 preparation: avoid implicit tuple parameter unpacking.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5388
diff
changeset
|
613 def do_loop_tal(self, name_expr_block): |
|
46b2fd6d342e
Python 3 preparation: avoid implicit tuple parameter unpacking.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5388
diff
changeset
|
614 (name, expr, block) = name_expr_block |
| 1049 | 615 iterator = self.engine.setRepeat(name, expr) |
| 616 while iterator.next(): | |
| 617 self.interpret(block) | |
| 618 bytecode_handlers["loop"] = do_loop | |
| 619 | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
620 def translate(self, msgid, default, i18ndict, obj=None): |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
621 if obj: |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
622 i18ndict.update(obj) |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
623 if not self.i18nInterpolate: |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
624 return msgid |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
625 # XXX We need to pass in one of context or target_language |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
626 return self.engine.translate(self.i18nContext.domain, |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
627 msgid, i18ndict, default=default) |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
628 |
|
5393
46b2fd6d342e
Python 3 preparation: avoid implicit tuple parameter unpacking.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5388
diff
changeset
|
629 def do_rawtextColumn(self, s_col): |
|
46b2fd6d342e
Python 3 preparation: avoid implicit tuple parameter unpacking.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5388
diff
changeset
|
630 (s, col) = s_col |
| 1049 | 631 self._stream_write(s) |
| 632 self.col = col | |
| 633 bytecode_handlers["rawtextColumn"] = do_rawtextColumn | |
| 634 | |
|
5393
46b2fd6d342e
Python 3 preparation: avoid implicit tuple parameter unpacking.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5388
diff
changeset
|
635 def do_rawtextOffset(self, s_offset): |
|
46b2fd6d342e
Python 3 preparation: avoid implicit tuple parameter unpacking.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5388
diff
changeset
|
636 (s, offset) = s_offset |
| 1049 | 637 self._stream_write(s) |
| 638 self.col = self.col + offset | |
| 639 bytecode_handlers["rawtextOffset"] = do_rawtextOffset | |
| 640 | |
|
5393
46b2fd6d342e
Python 3 preparation: avoid implicit tuple parameter unpacking.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5388
diff
changeset
|
641 def do_condition(self, condition_block): |
|
46b2fd6d342e
Python 3 preparation: avoid implicit tuple parameter unpacking.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5388
diff
changeset
|
642 (condition, block) = condition_block |
| 1049 | 643 if not self.tal or self.engine.evaluateBoolean(condition): |
| 644 self.interpret(block) | |
| 645 bytecode_handlers["condition"] = do_condition | |
| 646 | |
|
5393
46b2fd6d342e
Python 3 preparation: avoid implicit tuple parameter unpacking.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5388
diff
changeset
|
647 def do_defineMacro(self, macroName_macro): |
|
46b2fd6d342e
Python 3 preparation: avoid implicit tuple parameter unpacking.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5388
diff
changeset
|
648 (macroName, macro) = macroName_macro |
| 1049 | 649 macs = self.macroStack |
| 650 if len(macs) == 1: | |
| 651 entering = macs[-1][2] | |
| 652 if not entering: | |
| 653 macs.append(None) | |
| 654 self.interpret(macro) | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
655 assert macs[-1] is None |
| 1049 | 656 macs.pop() |
| 657 return | |
| 658 self.interpret(macro) | |
| 659 bytecode_handlers["defineMacro"] = do_defineMacro | |
| 660 | |
|
5393
46b2fd6d342e
Python 3 preparation: avoid implicit tuple parameter unpacking.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5388
diff
changeset
|
661 def do_useMacro(self, args_tuple): |
|
46b2fd6d342e
Python 3 preparation: avoid implicit tuple parameter unpacking.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5388
diff
changeset
|
662 (macroName, macroExpr, compiledSlots, block) = args_tuple |
| 1049 | 663 if not self.metal: |
| 664 self.interpret(block) | |
| 665 return | |
| 666 macro = self.engine.evaluateMacro(macroExpr) | |
| 667 if macro is self.Default: | |
| 668 macro = block | |
| 669 else: | |
| 670 if not isCurrentVersion(macro): | |
| 671 raise METALError("macro %s has incompatible version %s" % | |
|
5377
12fe83f90f0d
Python 3 preparation: use repr() instead of ``.
Joseph Myers <jsm@polyomino.org.uk>
parents:
2348
diff
changeset
|
672 (repr(macroName), repr(getProgramVersion(macro))), |
| 1049 | 673 self.position) |
| 674 mode = getProgramMode(macro) | |
| 675 if mode != (self.html and "html" or "xml"): | |
| 676 raise METALError("macro %s has incompatible mode %s" % | |
|
5377
12fe83f90f0d
Python 3 preparation: use repr() instead of ``.
Joseph Myers <jsm@polyomino.org.uk>
parents:
2348
diff
changeset
|
677 (repr(macroName), repr(mode)), self.position) |
| 1049 | 678 self.pushMacro(macroName, compiledSlots) |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
679 prev_source = self.sourceFile |
| 1049 | 680 self.interpret(macro) |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
681 if self.sourceFile != prev_source: |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
682 self.engine.setSourceFile(prev_source) |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
683 self.sourceFile = prev_source |
| 1049 | 684 self.popMacro() |
| 685 bytecode_handlers["useMacro"] = do_useMacro | |
| 686 | |
|
5393
46b2fd6d342e
Python 3 preparation: avoid implicit tuple parameter unpacking.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5388
diff
changeset
|
687 def do_fillSlot(self, slotName_block): |
| 1049 | 688 # This is only executed if the enclosing 'use-macro' evaluates |
| 689 # to 'default'. | |
|
5393
46b2fd6d342e
Python 3 preparation: avoid implicit tuple parameter unpacking.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5388
diff
changeset
|
690 (slotName, block) = slotName_block |
| 1049 | 691 self.interpret(block) |
| 692 bytecode_handlers["fillSlot"] = do_fillSlot | |
| 693 | |
|
5393
46b2fd6d342e
Python 3 preparation: avoid implicit tuple parameter unpacking.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5388
diff
changeset
|
694 def do_defineSlot(self, slotName_block): |
|
46b2fd6d342e
Python 3 preparation: avoid implicit tuple parameter unpacking.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5388
diff
changeset
|
695 (slotName, block) = slotName_block |
| 1049 | 696 if not self.metal: |
| 697 self.interpret(block) | |
| 698 return | |
| 699 macs = self.macroStack | |
| 700 if macs and macs[-1] is not None: | |
| 701 macroName, slots = self.popMacro()[:2] | |
| 702 slot = slots.get(slotName) | |
| 703 if slot is not None: | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
704 prev_source = self.sourceFile |
| 1049 | 705 self.interpret(slot) |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
706 if self.sourceFile != prev_source: |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
707 self.engine.setSourceFile(prev_source) |
|
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
708 self.sourceFile = prev_source |
| 1049 | 709 self.pushMacro(macroName, slots, entering=0) |
| 710 return | |
| 711 self.pushMacro(macroName, slots) | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
712 # Falling out of the 'if' allows the macro to be interpreted. |
| 1049 | 713 self.interpret(block) |
| 714 bytecode_handlers["defineSlot"] = do_defineSlot | |
| 715 | |
|
5393
46b2fd6d342e
Python 3 preparation: avoid implicit tuple parameter unpacking.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5388
diff
changeset
|
716 def do_onError(self, block_handler): |
|
46b2fd6d342e
Python 3 preparation: avoid implicit tuple parameter unpacking.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5388
diff
changeset
|
717 (block, handler) = block_handler |
| 1049 | 718 self.interpret(block) |
| 719 | |
|
5393
46b2fd6d342e
Python 3 preparation: avoid implicit tuple parameter unpacking.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5388
diff
changeset
|
720 def do_onError_tal(self, block_handler): |
|
46b2fd6d342e
Python 3 preparation: avoid implicit tuple parameter unpacking.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5388
diff
changeset
|
721 (block, handler) = block_handler |
| 1049 | 722 state = self.saveState() |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
723 self.stream = stream = self.StringIO() |
| 1049 | 724 self._stream_write = stream.write |
| 725 try: | |
| 726 self.interpret(block) | |
|
8528
fed0f839c260
fix: replace except: with except Exception: (by haosenwang1018@github)
John Rouillard <rouilj@ieee.org>
parents:
5837
diff
changeset
|
727 except Exception: |
| 1049 | 728 exc = sys.exc_info()[1] |
| 729 self.restoreState(state) | |
| 730 engine = self.engine | |
| 731 engine.beginScope() | |
| 732 error = engine.createErrorInfo(exc, self.position) | |
| 733 engine.setLocal('error', error) | |
| 734 try: | |
| 735 self.interpret(handler) | |
| 736 finally: | |
| 737 engine.endScope() | |
| 738 else: | |
| 739 self.restoreOutputState(state) | |
| 740 self.stream_write(stream.getvalue()) | |
| 741 bytecode_handlers["onError"] = do_onError | |
| 742 | |
| 743 bytecode_handlers_tal = bytecode_handlers.copy() | |
| 744 bytecode_handlers_tal["rawtextBeginScope"] = do_rawtextBeginScope_tal | |
| 745 bytecode_handlers_tal["beginScope"] = do_beginScope_tal | |
| 746 bytecode_handlers_tal["setLocal"] = do_setLocal_tal | |
| 747 bytecode_handlers_tal["setGlobal"] = do_setGlobal_tal | |
| 748 bytecode_handlers_tal["insertStructure"] = do_insertStructure_tal | |
| 749 bytecode_handlers_tal["insertText"] = do_insertText_tal | |
| 750 bytecode_handlers_tal["loop"] = do_loop_tal | |
| 751 bytecode_handlers_tal["onError"] = do_onError_tal | |
| 752 bytecode_handlers_tal["<attrAction>"] = attrAction_tal | |
| 753 bytecode_handlers_tal["optTag"] = do_optTag_tal | |
| 754 | |
| 755 | |
|
2348
8c2402a78bb0
beginning getting ZPT up to date: TAL first
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
756 def _write_ValueError(s): |
|
5378
35ea9b1efc14
Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5377
diff
changeset
|
757 raise ValueError("I/O operation on closed file") |
