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