From 53a7f8bb422161a742e43526d96309606f64b5f6 Mon Sep 17 00:00:00 2001 From: Mike Bayer <mike_mp@zzzcomputing.com> Date: Fri, 24 Nov 2006 18:40:13 +0000 Subject: [PATCH] more inheritance tests and turning on attribute-embedded expressions --- lib/mako/codegen.py | 7 +++---- lib/mako/parsetree.py | 13 ++++++++----- test/inheritance.py | 10 +++++++--- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/lib/mako/codegen.py b/lib/mako/codegen.py index ff801a5..fb54df1 100644 --- a/lib/mako/codegen.py +++ b/lib/mako/codegen.py @@ -206,7 +206,7 @@ class _GenerateRenderMethod(object): def visitIncludeTag(self, node): self.write_source_comment(node) - self.printer.writeline("runtime.include_file(context, %s, import_symbols=%s)" % (repr(node.attributes['file']), repr(node.attributes.get('import', False)))) + self.printer.writeline("runtime.include_file(context, %s, import_symbols=%s)" % (node.parsed_attributes['file'], repr(node.attributes.get('import', False)))) def visitNamespaceTag(self, node): self.write_source_comment(node) @@ -222,7 +222,7 @@ class _GenerateRenderMethod(object): n.accept_visitor(vis) self.printer.writeline("return [%s]" % (','.join(export))) self.printer.writeline(None) - self.printer.writeline("%s = runtime.Namespace(%s, context.clean_inheritance_tokens(), templateuri=%s, callables=make_namespace())" % (node.name, repr(node.name), repr(node.attributes['file']))) + self.printer.writeline("%s = runtime.Namespace(%s, context.clean_inheritance_tokens(), templateuri=%s, callables=make_namespace())" % (node.name, repr(node.name), node.parsed_attributes['file'])) def visitComponentTag(self, node): pass @@ -334,8 +334,7 @@ class _Identifiers(object): for n in node.nodes: n.accept_visitor(self) def visitIncludeTag(self, node): - # TODO: expressions for attributes - pass + self.check_declared(node) def visitNamespaceTag(self, node): self.check_declared(node) if node is self.node: diff --git a/lib/mako/parsetree.py b/lib/mako/parsetree.py index 5f49ae7..fece1af 100644 --- a/lib/mako/parsetree.py +++ b/lib/mako/parsetree.py @@ -192,8 +192,8 @@ class Tag(Node): for x in re.split(r'(\${.+?})', self.attributes[key]): m = re.match(r'^\${(.+?)}$', x) if m: - #code = ast.PythonCode(m.group(1), self.lineno, self.pos) - #undeclared_identifiers = undeclared_identifiers.union(code.undeclared_identifiers) + code = ast.PythonCode(m.group(1), self.lineno, self.pos) + undeclared_identifiers = undeclared_identifiers.union(code.undeclared_identifiers) expr.append(m.group(1)) else: expr.append(repr(x)) @@ -203,7 +203,12 @@ class Tag(Node): raise exceptions.CompileException("Attibute '%s' in tag '%s' does not allow embedded expressions" %(key, self.keyword), self.lineno, self.pos) self.parsed_attributes[key] = repr(self.attributes[key]) else: - raise exceptions.CompileException("Invalid attribute for tag '%s': '%s'" %(self.keyword, key), self.lineno, self.pos) + raise exceptions.CompileException("Invalid attribute for tag '%s': '%s'" %(self.keyword, key), self.lineno, self.pos) + self.expression_undeclared_identifiers = undeclared_identifiers + def declared_identifiers(self): + return [] + def undeclared_identifiers(self): + return self.expression_undeclared_identifiers def __repr__(self): return "%s(%s, %s, %s, %s)" % (self.__class__.__name__, repr(self.keyword), repr(self.attributes), repr((self.lineno, self.pos)), repr([repr(x) for x in self.nodes])) @@ -219,8 +224,6 @@ class NamespaceTag(Tag): self.name = attributes['name'] def declared_identifiers(self): return [self.name] - def undeclared_identifiers(self): - return [] class ComponentTag(Tag): __keyword__ = 'component' diff --git a/test/inheritance.py b/test/inheritance.py index ccd9b15..0df7ef0 100644 --- a/test/inheritance.py +++ b/test/inheritance.py @@ -95,7 +95,6 @@ ${next.body()} <%inherit file="base"/> this is index. a is: ${self.a()} - <%include file="secondary"/> """) @@ -105,6 +104,7 @@ ${next.body()} a is: ${self.a()} """) + print collection.get_template("index").code print collection.get_template("index").render() def test_namespaces(self): @@ -124,7 +124,7 @@ ${next.body()} collection.put_string("layout", """ <html> <%inherit file="base"/> - <%component name="a">layout_a</%component> + <%component name="a()">layout_a</%component> This is the layout.. ${next.body()} </html> @@ -137,13 +137,17 @@ ${next.body()} a is: ${self.a()} sc.a is: ${sc.a()} sc.b is: ${sc.b()} + sc.c is: ${sc.c()} + sc.body is: ${sc.body()} """) collection.put_string("secondary",""" <%inherit file="layout"/> - <%component name="c">secondary_c. a is ${self.a()} b is ${self.b()}</%component> + <%component name="c">secondary_c. a is ${self.a()} b is ${self.b()} d is ${self.d()}</%component> + <%component name="d">secondary_d.</%component> this is secondary. a is: ${self.a()} + c is: ${self.c()} """) print collection.get_template('index').render() -- GitLab