Skip to content
Myr

[svn] fixes jinja inheritance code

Before adoption: this commit came in with the history the project adopted, so no landing records it.

--HG-- branch : trunk

authored by Armin Ronacher · committed by Armin Ronacher · parent 34a18bf · Browse files · 7 files+65 added−34 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

7 files+65 added−34 removed

  • tests:+12 added−1 removed
  • other:+53 added−33 removed

7 files+65 added−34 removed

modified:jinja/translators/python.py+17 added-15 removed
8 unmodified lines
9 :license: BSD, see LICENSE for more details.10"""11from compiler import ast12from jinja import nodes13from jinja.nodes import get_nodes14from jinja.parser import Parser15from jinja.exceptions import TemplateSyntaxError16from jinja.translators import Translator17
194 unmodified lines
212 ensures that we get the bootstrapping code. It also knows about213 inheritance information. It only occours as outer node, never in214 the tree itself.215 """216 self.indention = 1217218 # if there is a parent template we parse the parent template and219 # update the blocks there. Once this is done we drop the current220 # template in favor of the new one. Do that until we found the221 # root template.222 requirements_todo = []220 blocks = node.blocks.copy()223 parent = None224225 while node.extends is not None:226 # handle all requirements but not those from the
5 unmodified lines
232 # load the template we inherit from and add not known blocks233 # to the block registry, make this template the new root.234 parent = self.environment.loader.parse(node.extends.template,235 node.filename)234 for name, block in parent.blocks.iteritems():235 if name not in blocks:236 blocks[name] = block236237 overwrites = {}238 for n in get_nodes(nodes.Block, node):239 overwrites[n.name] = n240 for n in get_nodes(nodes.Block, parent):241 if n.name in overwrites:242 n.replace(overwrites[n.name])243244 node = parent245240 # if there is a parent template, do the inheritance handling now241 if parent is not None:242 for name, block in blocks.iteritems():243 if name in node.blocks:244 node.blocks[name].replace(block)245246 # look up requirements247 requirements = []248 for req in requirements_todo:249 for n in req:
16 unmodified lines
266 ' finish_var = environment.finish_var\n'267 ' ctx_push = context.push\n'268 ' ctx_pop = context.pop\n'269 ]270 self.indention = 1270271 # we have requirements? add them here.272 if requirements:273 lines.append(self.indent('# REQUIREMENTS'))
214 unmodified lines
488 else.489 """490 rv = self.handle_node(node.body)491 if not rv:493 return self.indent('# EMPTY BLOCK FROM %r, LINE %s' % (492 return self.indent('# EMPTY BLOCK "%s" FROM %r, LINE %s' % (493 node.name,494 node.filename or '?',495 node.lineno496 ))497498 buf = []499 write = lambda x: buf.append(self.indent(x))500501 write('# BLOCK FROM %r, LINE %s' % (501 write('# BLOCK "%s" FROM %r, LINE %s' % (502 node.name,503 node.filename or '?',504 node.lineno505 ))506 write('ctx_push()')
119 unmodified lines
626 return '%s[%s]' % (627 self.handle_node(node.expr),628 self.handle_node(node.subs[0])629 )629 return 'get_attribute(%s, %s)' % (630 return 'get_attribute(%s, (%s,))' % (631 self.handle_node(node.expr),632 self.handle_node(node.subs[0])633 )634
5 unmodified lines
640641 # chain getattrs for speed reasons642 path = [repr(node.attrname)]643 while node.expr.__class__ is ast.Getattr:643 path.append(repr(node.attrname))644 node = node.expr645 path.append(repr(node.attrname))646 path.reverse()647648 return 'get_attribute(%s, %s)' % (649 self.handle_node(node.expr),650 _to_tuple(path)
182 unmodified lines
modified:jinja/environment.py+2 added-2 removed
140 unmodified lines
141 for name in attributes:142 try:143 node = node[name]144 except (TypeError, KeyError, IndexError):145 if not hasattr(obj, name):145 if not hasattr(node, name):146 return Undefined147 r = getattr(obj, 'jinja_allowed_attributes', None)148 if r is not None and name not in r:149 raise SecurityException('unsafe attributed %r accessed' % name)150 node = getattr(obj, name)150 node = getattr(node, name)151 return node152153 def call_function(self, f, args, kwargs, dyn_args, dyn_kwargs):154 """
36 unmodified lines
modified:jinja/nodes.py+23 added-9 removed
24 unmodified lines
25 node.lineno = offset26 todo.extend(node.getChildNodes())272829def get_nodes(nodetype, tree):30 """31 Get all nodes from nodetype in the tree.32 """33 todo = [tree]34 while todo:35 node = todo.pop()36 if node.__class__ is nodetype:37 yield node38 todo.extend(node.getChildNodes())394041class Node(ast.Node):42 """43 jinja node.44 """
35 unmodified lines
8081 getChildren = getChildNodes = lambda s: list(s) + s.get_items()8283 def __repr__(self):72 return '%s(%s)' % (73 self.__class__.__name__,74 list.__repr__(self)75 )84 return 'NodeList(%s)' % list.__repr__(self)858687class Template(NodeList):88 """89 Node that represents a template.90 """9183 def __init__(self, filename, body, blocks, extends):92 def __init__(self, filename, body, extends):93 if body.__class__ is not NodeList:94 body = (body,)95 NodeList.__init__(self, 0, body)87 self.blocks = blocks96 self.extends = extends97 set_filename(filename, self)9899 def get_items(self):92 result = self.blocks.values()100 if self.extends is not None:94 result.append(self.extends)95 return result101 return [self.extends]102 return []103104 def __repr__(self):105 return 'Template(%r, %r, %r)' % (106 self.filename,107 self.extends,108 NodeList.__repr__(self)109 )110111112class ForLoop(Node):113 """114 A node that represents a for loop115 """
230 unmodified lines
modified:jinja/parser.py+11 added-7 removed
12 unmodified lines
13from compiler.misc import set_filename14from jinja import nodes15from jinja.datastructure import TokenStream16from jinja.exceptions import TemplateSyntaxError17try:18 set19except NameError:20 from sets import Set as set212223# callback functions for the subparse method24end_of_block = lambda p, t, d: t == 'block_end'
42 unmodified lines
67 self.filename = filename68 self.tokenstream = environment.lexer.tokenize(source)6970 self.extends = None67 self.blocks = {}71 self.blocks = set()7273 self.directives = {74 'for': self.handle_for_directive,75 'if': self.handle_if_directive,
148 unmodified lines
224 if block_name[1] != 'name':225 raise TemplateSyntaxError('expected \'name\', got %r' %226 block_name[1], lineno)227 if tokens:224 print tokens228 raise TemplateSyntaxError('block got too many arguments, '229 'requires one.', lineno)230231 # check if this block does not exist by now.232 if block_name[2] in self.blocks:233 raise TemplateSyntaxError('block %r defined twice' %230 block_name[2], lineno)234 block_name[2], lineno)235 self.blocks.add(block_name[2])236237 # now parse the body and attach it to the block238 body = self.subparse(end_of_block_tag, True)239 self.close_remaining_block()234 rv = nodes.Block(lineno, block_name[2], body)235 self.blocks[block_name[2]] = rv236 return rv240 return nodes.Block(lineno, block_name[2], body)241242 def handle_extends_directive(self, lineno, gen):243 """244 Handle the extends directive used for inheritance.
153 unmodified lines
398 """399 Parse the template and return a Template node.400 """401 body = self.subparse(None)398 return nodes.Template(self.filename, body, self.blocks, self.extends)402 return nodes.Template(self.filename, body, self.extends)403404 def subparse(self, test, drop_needle=False):405 """406 Helper function used to parse the sourcecode until the test
91 unmodified lines
modified:tests/templates/a.html+3 added
modified:tests/templates/b.html+6 added-1 removed
modified:tests/templates/c.html+3 added