Commit 2feef0d3 authored by Fred Drake's avatar Fred Drake

parent 38793800
...@@ -88,12 +88,16 @@ Dummy TALES engine so that I can test out the TAL implementation. ...@@ -88,12 +88,16 @@ Dummy TALES engine so that I can test out the TAL implementation.
import re import re
import sys import sys
import string from string import rfind, strip
import driver
from TALDefs import NAME_RE, TALError, TALESError from TALDefs import NAME_RE, TALError, TALESError
Default = [] Default = []
name_match = re.compile(r"(?s)(%s):(.*)\Z" % NAME_RE).match
class DummyEngine: class DummyEngine:
def __init__(self, macros=None): def __init__(self, macros=None):
...@@ -128,8 +132,9 @@ class DummyEngine: ...@@ -128,8 +132,9 @@ class DummyEngine:
self.globals[name] = value self.globals[name] = value
def evaluate(self, expression): def evaluate(self, expression):
expression = self.uncompile(expression) assert expression[:1] == "$" == expression[-1:], expression
m = re.match(r"(?s)(%s):(.*)\Z" % NAME_RE, expression) expression = expression[1:-1]
m = name_match(expression)
if m: if m:
type, expr = m.group(1, 2) type, expr = m.group(1, 2)
else: else:
...@@ -138,7 +143,7 @@ class DummyEngine: ...@@ -138,7 +143,7 @@ class DummyEngine:
if type in ("string", "str"): if type in ("string", "str"):
return expr return expr
if type in ("path", "var", "global", "local"): if type in ("path", "var", "global", "local"):
expr = string.strip(expr) expr = strip(expr)
if self.locals.has_key(expr): if self.locals.has_key(expr):
return self.locals[expr] return self.locals[expr]
elif self.globals.has_key(expr): elif self.globals.has_key(expr):
...@@ -146,8 +151,7 @@ class DummyEngine: ...@@ -146,8 +151,7 @@ class DummyEngine:
else: else:
raise TALESError("unknown variable: %s" % `expr`) raise TALESError("unknown variable: %s" % `expr`)
if type == "not": if type == "not":
v = self.evaluate(expr) return not self.evaluate(expr)
return not v
if type == "exists": if type == "exists":
return self.locals.has_key(expr) or self.globals.has_key(expr) return self.locals.has_key(expr) or self.globals.has_key(expr)
if type == "python": if type == "python":
...@@ -180,15 +184,15 @@ class DummyEngine: ...@@ -180,15 +184,15 @@ class DummyEngine:
return self.evaluate(expr) return self.evaluate(expr)
def evaluateMacro(self, macroName): def evaluateMacro(self, macroName):
macroName = self.uncompile(macroName) assert macroName[:1] == "$" == macroName[-1:], macroName
macroName = macroName[1:-1]
file, localName = self.findMacroFile(macroName) file, localName = self.findMacroFile(macroName)
if not file: if not file:
# Local macro # Local macro
macro = self.macros[localName] macro = self.macros[localName]
else: else:
# External macro # External macro
from driver import compilefile program, macros = driver.compilefile(file)
program, macros = compilefile(file)
macro = macros.get(localName) macro = macros.get(localName)
if not macro: if not macro:
raise TALESError("macro %s not found in file %s" % raise TALESError("macro %s not found in file %s" %
...@@ -199,14 +203,13 @@ class DummyEngine: ...@@ -199,14 +203,13 @@ class DummyEngine:
file, localName = self.findMacroFile(macroName) file, localName = self.findMacroFile(macroName)
if not file: if not file:
return file, localName return file, localName
from driver import parsefile doc = driver.parsefile(file)
doc = parsefile(file)
return doc, localName return doc, localName
def findMacroFile(self, macroName): def findMacroFile(self, macroName):
if not macroName: if not macroName:
raise TALESError("empty macro name") raise TALESError("empty macro name")
i = string.rfind(macroName, '/') i = rfind(macroName, '/')
if i < 0: if i < 0:
# No slash -- must be a locally defined macro # No slash -- must be a locally defined macro
return None, macroName return None, macroName
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment