Skip to content
Myr

updated macro stuff

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 4f62a9f · Browse files · 2 files+51 added−14 removed
This commit

State

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

Diff stats

2 files+51 added−14 removed

  • other:+51 added−14 removed

2 files+51 added−14 removed

modified:jinja2/compiler.py+3 added-1 removed
379 unmodified lines
380 arg_tuple))381 for arg in node.defaults:382 self.visit(arg)383 self.write(', ')384 self.write('), %r)' % accesses_arguments)384 self.write('), %r, make_undefined)' % accesses_arguments)385386 def visit_ExprStmt(self, node, frame):387 self.newline(node)388 self.visit(node, frame)
57 unmodified lines
446 assignment_frame = frame447 self.visit(node.target, assignment_frame)448 self.write(' = ')449 self.visit(node.node, frame)450451 # make sure toplevel assignments are added to the context.452 if frame.toplevel:453 for name in assignment_frame.assigned_names:454 self.writeline('context[%r] = l_%s' % (name, name))455
121 unmodified lines
modified:jinja2/runtime.py+48 added-13 removed
12 unmodified lines
13except ImportError:14 defaultdict = None151617__all__ = ['extends', 'subscribe', 'TemplateContext']17__all__ = ['extends', 'subscribe', 'TemplateContext', 'Macro']181920def extends(template, namespace):21 """This loads a template (and evaluates it) and replaces the blocks."""
10 unmodified lines
32 return undefined_factory(attr=argument)333435class TemplateContext(dict):36 """37 Holds the variables of the local template or of the global one. It's38 not save to use this class outside of the compiled code. For example39 update and other methods will not work as they seem (they don't update40 the exported variables for example).41 """4243 def __init__(self, globals, undefined_factory, filename):38 dict.__init__(self)39 self.globals = globals44 dict.__init__(self, globals)45 self.exported = set()46 self.undefined_factory = undefined_factory47 self.filename = filename48 self.filters = {}49 self.tests = {}5051 def __setitem__(self, key, value):52 """If we set items to the dict we track the variables set so53 that includes can access the exported variables."""54 dict.__setitem__(self, key, value)55 self.exported.add(key)5657 def __delitem__(self, key):58 """On delete we no longer export it."""59 dict.__delitem__(self, key)60 self.exported.dicard(key)6162 def get_exported(self):63 """Get a dict of all exported variables."""64 return dict((k, self[k]) for k in self.exported)6566 # if there is a default dict, dict has a __missing__ method we can use.67 if defaultdict is None:68 def __getitem__(self, name):69 if name in self:70 return self[name]50 elif name in self.globals:51 return self.globals[name]71 return self.undefined_factory(name)72 else:73 def __missing__(self, key):55 try:56 return self.globals[key]57 except:58 return self.undefined_factory(key)74 return self.undefined_factory(key)757677class Macro(object):78 """79 Wraps a macor80 """8163 def __init__(self, func, name, arguments, defaults, catch_all):82 def __init__(self, func, name, arguments, defaults, catch_all, \83 undefined_factory):84 self.func = func85 self.name = name86 self.arguments = arguments87 self.defaults = defaults88 self.catch_all = catch_all89 self.undefined_factory = undefined_factory9091 def __call__(self, *args, **kwargs):71 if len(args) > len(self.arguments):92 arg_count = len(self.arguments)93 if len(args) > arg_count:94 raise TypeError('macro %r takes not more than %d argument(s).' %95 (self.name, len(self.arguments)))96 arguments = {}75 # XXX: assemble arguments76 return u''.join(self.func(*args, **kwargs))97 for idx, name in enumerate(self.arguments):98 try:99 value = args[idx]100 except IndexError:101 try:102 value = kwargs.pop(name)103 except KeyError:104 try:105 value = self.defaults[idx - arg_count]106 except IndexError:107 value = self.undefined_factory(name)108 arguments['l_' + name] = arg109 if self.catch_all:110 arguments['l_arguments'] = kwargs111 return u''.join(self.func(**arguments))