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