diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2006-12-11 06:54:15 +0000 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2006-12-11 06:54:15 +0000 |
commit | 001f173c95950acb76d763966a1477aa4fe27345 (patch) | |
tree | 64762d5b29c0ef4df8fe8176dc169cf835a3c44f /lib/mako | |
parent | 5ec5518d19c0c8b97c21f5e7bfb2ada9fcfd6754 (diff) | |
download | external_python_mako-001f173c95950acb76d763966a1477aa4fe27345.tar.gz external_python_mako-001f173c95950acb76d763966a1477aa4fe27345.tar.bz2 external_python_mako-001f173c95950acb76d763966a1477aa4fe27345.zip |
some refinements to the **kwargs sent to the main render() method
Diffstat (limited to 'lib/mako')
-rw-r--r-- | lib/mako/codegen.py | 26 | ||||
-rw-r--r-- | lib/mako/runtime.py | 28 |
2 files changed, 33 insertions, 21 deletions
diff --git a/lib/mako/codegen.py b/lib/mako/codegen.py index 6bb505c..4de9f9b 100644 --- a/lib/mako/codegen.py +++ b/lib/mako/codegen.py @@ -4,7 +4,7 @@ # This module is part of Mako and is released under # the MIT License: http://www.opensource.org/licenses/mit-license.php -"""provides the Compiler object for generating module source code.""" +"""provides functionality for rendering a parsetree constructing into module source code.""" import time import re @@ -14,6 +14,7 @@ from mako import util, ast, parsetree, filters MAGIC_NUMBER = 1 def compile(node, uri, filename=None): + """generate module source code given a parsetree node, uri, and optional source filename""" buf = util.FastEncodingBuffer() printer = PythonPrinter(buf) _GenerateRenderMethod(printer, _CompileContext(uri, filename), node) @@ -50,10 +51,11 @@ class _GenerateRenderMethod(object): buffered = filtered = False cached = pagetag is not None and eval(pagetag.attributes.get('cached', 'False')) if args is None: - args = ['context', '**kwargs'] + args = ['context'] else: - args = [a for a in ['context'] + args + ['**kwargs']] - + args = [a for a in ['context'] + args] + if not self.in_def: + args.append('**kwargs') self.write_render_callable(pagetag or node, name, args, buffered, filtered, cached) @@ -134,8 +136,10 @@ class _GenerateRenderMethod(object): self.identifier_stack.append(self.compiler.identifiers.branch(self.node)) + if not self.in_def: + self.printer.writeline("context = context.locals_(kwargs)") if not self.in_def and len(self.identifiers.locally_assigned) > 0: - self.printer.writeline("__locals = kwargs") + self.printer.writeline("__locals = {}") self.write_variable_declares(self.identifiers, toplevel=True) @@ -159,7 +163,7 @@ class _GenerateRenderMethod(object): """write the module-level inheritance-determination callable.""" self.printer.writeline("def _mako_inherit(template, context):") self.printer.writeline("_mako_generate_namespaces(context)") - self.printer.writeline("return runtime.inherit_from(context, %s, _template_uri)" % (node.parsed_attributes['file'])) + self.printer.writeline("return runtime._inherit_from(context, %s, _template_uri)" % (node.parsed_attributes['file'])) self.printer.writeline(None) def write_namespaces(self, namespaces): @@ -194,7 +198,7 @@ class _GenerateRenderMethod(object): callable_name = "make_namespace()" else: callable_name = "None" - self.printer.writeline("ns = runtime.Namespace(%s, context.clean_inheritance_tokens(), templateuri=%s, callables=%s, calling_uri=_template_uri)" % (repr(node.name), node.parsed_attributes.get('file', 'None'), callable_name)) + self.printer.writeline("ns = runtime.Namespace(%s, context._clean_inheritance_tokens(), templateuri=%s, callables=%s, calling_uri=_template_uri)" % (repr(node.name), node.parsed_attributes.get('file', 'None'), callable_name)) if eval(node.attributes.get('inheritable', "False")): self.printer.writeline("context['self'].%s = ns" % (node.name)) self.printer.writeline("context.namespaces[(render, %s)] = ns" % repr(node.name)) @@ -245,7 +249,7 @@ class _GenerateRenderMethod(object): self.compiler.has_imports = True for ident, ns in self.compiler.namespaces.iteritems(): if ns.attributes.has_key('import'): - self.printer.writeline("_mako_get_namespace(context, %s).populate(_import_ns, %s)" % (repr(ident), repr(re.split(r'\s*,\s*', ns.attributes['import'])))) + self.printer.writeline("_mako_get_namespace(context, %s)._populate(_import_ns, %s)" % (repr(ident), repr(re.split(r'\s*,\s*', ns.attributes['import'])))) for ident in to_write: if ident in comp_idents: @@ -258,9 +262,9 @@ class _GenerateRenderMethod(object): self.printer.writeline("%s = _mako_get_namespace(context, %s)" % (ident, repr(ident))) else: if getattr(self.compiler, 'has_ns_imports', False): - self.printer.writeline("%s = _import_ns.get(%s, kwargs.get(%s, context.get(%s, UNDEFINED)))" % (ident, repr(ident), repr(ident), repr(ident))) + self.printer.writeline("%s = _import_ns.get(%s, context.get(%s, UNDEFINED))" % (ident, repr(ident), repr(ident))) else: - self.printer.writeline("%s = kwargs.get(%s, context.get(%s, UNDEFINED))" % (ident, repr(ident), repr(ident))) + self.printer.writeline("%s = context.get(%s, UNDEFINED)" % (ident, repr(ident))) def write_source_comment(self, node): """write a source comment containing the line number of the corresponding template line.""" @@ -403,7 +407,7 @@ class _GenerateRenderMethod(object): def visitIncludeTag(self, node): self.write_source_comment(node) - self.printer.writeline("runtime.include_file(context, %s, _template_uri)" % (node.parsed_attributes['file'])) + self.printer.writeline("runtime._include_file(context, %s, _template_uri)" % (node.parsed_attributes['file'])) def visitNamespaceTag(self, node): pass diff --git a/lib/mako/runtime.py b/lib/mako/runtime.py index bc4f274..0b163fb 100644 --- a/lib/mako/runtime.py +++ b/lib/mako/runtime.py @@ -4,7 +4,8 @@ # This module is part of Mako and is released under # the MIT License: http://www.opensource.org/licenses/mit-license.php -"""provides the Context class, the runtime namespace for templates.""" +"""provides runtime services for templates, including Context, Namespace, and various helper functions.""" + from mako import exceptions, util import inspect, sys @@ -50,10 +51,12 @@ class Context(object): return c def locals_(self, d): """create a new Context with a copy of this Context's current state, updated with the given dictionary.""" + if len(d) == 0: + return self c = self._copy() c._data.update(d) return c - def clean_inheritance_tokens(self): + def _clean_inheritance_tokens(self): """create a new copy of this Context with tokens related to inheritance state removed.""" c = self._copy() x = c._data @@ -96,18 +99,23 @@ class Namespace(object): module = property(lambda s:s._module or s.template.module) filename = property(lambda s:s._module and s._module.__file__ or s.template.filename) - - def get_namespace(self, filename): + uri = property(lambda s:s.template.uri) + + def get_namespace(self, uri): """return a namespace corresponding to the given template filename.""" - key = (self, filename) + key = (self, uri) if self.context.namespaces.has_key(key): return context.namespaces[key] else: - ns = Namespace(filename, self.context, templateuri=filename, calling_uri=self._templateuri) + ns = Namespace(uri, self.context, templateuri=uri, calling_uri=self._templateuri) self.context.namespaces[key] = ns return ns - def populate(self, d, l): + def include_file(self, uri): + """include a file at the given uri""" + _include_file(self.context, uri, self._templateuri) + + def _populate(self, d, l): for ident in l: if ident == '*': for (k, v) in self._get_star(): @@ -167,13 +175,13 @@ def capture(context, callable_, *args, **kwargs): buf = context.pop_buffer() return buf.getvalue() -def include_file(context, uri, calling_uri): +def _include_file(context, uri, calling_uri): """locate the template from the given uri and include it in the current output.""" template = _lookup_template(context, uri, calling_uri) - (callable_, ctx) = _populate_self_namespace(context.clean_inheritance_tokens(), template) + (callable_, ctx) = _populate_self_namespace(context._clean_inheritance_tokens(), template) callable_(ctx) -def inherit_from(context, uri, calling_uri): +def _inherit_from(context, uri, calling_uri): """called by the _inherit method in template modules to set up the inheritance chain at the start of a template's execution.""" if uri is None: |