diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2015-03-25 16:04:16 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2015-03-25 16:04:16 -0400 |
commit | 22fb044c3cb177aa197af1877c1c67e3f70066b7 (patch) | |
tree | 11cfb88f09a0ef722dac4e60feb36dedb358cccd /mako/runtime.py | |
parent | 6e78429b202362a9ed99decdf79bde978ce354de (diff) | |
download | external_python_mako-22fb044c3cb177aa197af1877c1c67e3f70066b7.tar.gz external_python_mako-22fb044c3cb177aa197af1877c1c67e3f70066b7.tar.bz2 external_python_mako-22fb044c3cb177aa197af1877c1c67e3f70066b7.zip |
- get flake8 to pass fully for mako/ (tests are a much bigger deal)
Diffstat (limited to 'mako/runtime.py')
-rw-r--r-- | mako/runtime.py | 106 |
1 files changed, 68 insertions, 38 deletions
diff --git a/mako/runtime.py b/mako/runtime.py index 870efcc..8d2f4a9 100644 --- a/mako/runtime.py +++ b/mako/runtime.py @@ -13,6 +13,7 @@ import sys class Context(object): + """Provides runtime namespace, output buffer, and various callstacks for templates. @@ -80,7 +81,6 @@ class Context(object): """Push a ``caller`` callable onto the callstack for this :class:`.Context`.""" - self.caller_stack.append(caller) def pop_caller(self): @@ -182,7 +182,9 @@ class Context(object): x.pop('next', None) return c + class CallerStack(list): + def __init__(self): self.nextcaller = None @@ -211,6 +213,7 @@ class CallerStack(list): class Undefined(object): + """Represents an undefined value in a template. All template modules have a constant value @@ -218,6 +221,7 @@ class Undefined(object): object. """ + def __str__(self): raise NameError("Undefined") @@ -230,7 +234,9 @@ class Undefined(object): UNDEFINED = Undefined() STOP_RENDERING = "" + class LoopStack(object): + """a stack for LoopContexts that implements the context manager protocol to automatically pop off the top of the stack on context exit """ @@ -270,6 +276,7 @@ class LoopStack(object): class LoopContext(object): + """A magic loop variable. Automatically accessible in any ``% for`` block. @@ -335,8 +342,10 @@ class LoopContext(object): class _NSAttr(object): + def __init__(self, parent): self.__parent = parent + def __getattr__(self, key): ns = self.__parent while ns: @@ -346,7 +355,9 @@ class _NSAttr(object): ns = ns.inherits raise AttributeError(key) + class Namespace(object): + """Provides access to collections of rendering methods, which can be local, from other templates, or from imported modules. @@ -363,8 +374,8 @@ class Namespace(object): """ def __init__(self, name, context, - callables=None, inherits=None, - populate_self=True, calling_uri=None): + callables=None, inherits=None, + populate_self=True, calling_uri=None): self.name = name self.context = context self.inherits = inherits @@ -463,8 +474,8 @@ class Namespace(object): return self.context.namespaces[key] else: ns = TemplateNamespace(uri, self.context._copy(), - templateuri=uri, - calling_uri=self._templateuri) + templateuri=uri, + calling_uri=self._templateuri) self.context.namespaces[key] = ns return ns @@ -525,17 +536,19 @@ class Namespace(object): val = getattr(self.inherits, key) else: raise AttributeError( - "Namespace '%s' has no member '%s'" % - (self.name, key)) + "Namespace '%s' has no member '%s'" % + (self.name, key)) setattr(self, key, val) return val + class TemplateNamespace(Namespace): + """A :class:`.Namespace` specific to a :class:`.Template` instance.""" def __init__(self, name, context, template=None, templateuri=None, - callables=None, inherits=None, - populate_self=True, calling_uri=None): + callables=None, inherits=None, + populate_self=True, calling_uri=None): self.name = name self.context = context self.inherits = inherits @@ -544,7 +557,7 @@ class TemplateNamespace(Namespace): if templateuri is not None: self.template = _lookup_template(context, templateuri, - calling_uri) + calling_uri) self._templateuri = self.template.module._template_uri elif template is not None: self.template = template @@ -554,8 +567,8 @@ class TemplateNamespace(Namespace): if populate_self: lclcallable, lclcontext = \ - _populate_self_namespace(context, self.template, - self_ns=self) + _populate_self_namespace(context, self.template, + self_ns=self) @property def module(self): @@ -590,6 +603,7 @@ class TemplateNamespace(Namespace): if self.callables: for key in self.callables: yield (key, self.callables[key]) + def get(key): callable_ = self.template._get_def_callable(key) return compat.partial(callable_, self.context) @@ -607,17 +621,19 @@ class TemplateNamespace(Namespace): else: raise AttributeError( - "Namespace '%s' has no member '%s'" % - (self.name, key)) + "Namespace '%s' has no member '%s'" % + (self.name, key)) setattr(self, key, val) return val + class ModuleNamespace(Namespace): + """A :class:`.Namespace` specific to a Python module instance.""" def __init__(self, name, context, module, - callables=None, inherits=None, - populate_self=True, calling_uri=None): + callables=None, inherits=None, + populate_self=True, calling_uri=None): self.name = name self.context = context self.inherits = inherits @@ -646,7 +662,6 @@ class ModuleNamespace(Namespace): if compat.callable(callable_): yield key, compat.partial(callable_, self.context) - def __getattr__(self, key): if key in self.callables: val = self.callables[key] @@ -657,11 +672,12 @@ class ModuleNamespace(Namespace): val = getattr(self.inherits, key) else: raise AttributeError( - "Namespace '%s' has no member '%s'" % - (self.name, key)) + "Namespace '%s' has no member '%s'" % + (self.name, key)) setattr(self, key, val) return val + def supports_caller(func): """Apply a caller_stack compatibility decorator to a plain Python function. @@ -678,6 +694,7 @@ def supports_caller(func): context.caller_stack._pop_frame() return wrap_stackframe + def capture(context, callable_, *args, **kwargs): """Execute the given template def, capturing the output into a buffer. @@ -688,9 +705,9 @@ def capture(context, callable_, *args, **kwargs): if not compat.callable(callable_): raise exceptions.RuntimeException( - "capture() function expects a callable as " - "its argument (i.e. capture(func, *args, **kwargs))" - ) + "capture() function expects a callable as " + "its argument (i.e. capture(func, *args, **kwargs))" + ) context._push_buffer() try: callable_(*args, **kwargs) @@ -698,6 +715,7 @@ def capture(context, callable_, *args, **kwargs): buf = context._pop_buffer() return buf.getvalue() + def _decorate_toplevel(fn): def decorate_render(render_fn): def go(context, *args, **kw): @@ -712,24 +730,28 @@ def _decorate_toplevel(fn): return go return decorate_render + def _decorate_inline(context, fn): def decorate_render(render_fn): dec = fn(render_fn) + def go(*args, **kw): return dec(context, *args, **kw) return go return decorate_render + def _include_file(context, uri, calling_uri, **kwargs): """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) + context._clean_inheritance_tokens(), + template) callable_(ctx, **_kwargs_for_include(callable_, context._data, **kwargs)) + 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 @@ -744,9 +766,9 @@ def _inherit_from(context, uri, calling_uri): ih = ih.inherits lclcontext = context._locals({'next': ih}) ih.inherits = TemplateNamespace("self:%s" % template.uri, - lclcontext, - template=template, - populate_self=False) + lclcontext, + template=template, + populate_self=False) context._data['parent'] = lclcontext._data['local'] = ih.inherits callable_ = getattr(template.module, '_mako_inherit', None) if callable_ is not None: @@ -759,23 +781,25 @@ def _inherit_from(context, uri, calling_uri): gen_ns(context) return (template.callable_, lclcontext) + def _lookup_template(context, uri, relativeto): lookup = context._with_template.lookup if lookup is None: raise exceptions.TemplateLookupException( - "Template '%s' has no TemplateLookup associated" % - context._with_template.uri) + "Template '%s' has no TemplateLookup associated" % + context._with_template.uri) uri = lookup.adjust_uri(uri, relativeto) try: return lookup.get_template(uri) except exceptions.TopLevelLookupException: raise exceptions.TemplateLookupException(str(compat.exception_as())) + def _populate_self_namespace(context, template, self_ns=None): if self_ns is None: self_ns = TemplateNamespace('self:%s' % template.uri, - context, template=template, - populate_self=False) + context, template=template, + populate_self=False) context._data['self'] = context._data['local'] = self_ns if hasattr(template.module, '_mako_inherit'): ret = template.module._mako_inherit(template, context) @@ -783,6 +807,7 @@ def _populate_self_namespace(context, template, self_ns=None): return ret return (template.callable_, context) + def _render(template, callable_, args, data, as_unicode=False): """create a Context and return the string output of the given template and template callable.""" @@ -793,17 +818,18 @@ def _render(template, callable_, args, data, as_unicode=False): buf = compat.StringIO() else: buf = util.FastEncodingBuffer( - as_unicode=as_unicode, - encoding=template.output_encoding, - errors=template.encoding_errors) + as_unicode=as_unicode, + encoding=template.output_encoding, + errors=template.encoding_errors) context = Context(buf, **data) context._outputting_as_unicode = as_unicode context._set_with_template(template) _render_context(template, callable_, context, *args, - **_kwargs_for_callable(callable_, data)) + **_kwargs_for_callable(callable_, data)) return context._pop_buffer().getvalue() + def _kwargs_for_callable(callable_, data): argspec = compat.inspect_func_args(callable_) # for normal pages, **pageargs is usually present @@ -818,6 +844,7 @@ def _kwargs_for_callable(callable_, data): kwargs[arg] = data[arg] return kwargs + def _kwargs_for_include(callable_, data, **kwargs): argspec = compat.inspect_func_args(callable_) namedargs = argspec[0] + [v for v in argspec[1:3] if v is not None] @@ -826,6 +853,7 @@ def _kwargs_for_include(callable_, data, **kwargs): kwargs[arg] = data[arg] return kwargs + def _render_context(tmpl, callable_, context, *args, **kwargs): import mako.template as template # create polymorphic 'self' namespace for this @@ -839,6 +867,7 @@ def _render_context(tmpl, callable_, context, *args, **kwargs): (inherit, lclcontext) = _populate_self_namespace(context, tmpl.parent) _exec_template(callable_, context, args=args, kwargs=kwargs) + def _exec_template(callable_, context, args=None, kwargs=None): """execute a rendering callable given the callable, a Context, and optional explicit arguments @@ -860,6 +889,7 @@ def _exec_template(callable_, context, args=None, kwargs=None): else: callable_(context, *args, **kwargs) + def _render_error(template, context, error): if template.error_handler: result = template.error_handler(context, error) @@ -869,11 +899,11 @@ def _render_error(template, context, error): error_template = exceptions.html_error_template() if context._outputting_as_unicode: context._buffer_stack[:] = [ - util.FastEncodingBuffer(as_unicode=True)] + util.FastEncodingBuffer(as_unicode=True)] else: context._buffer_stack[:] = [util.FastEncodingBuffer( - error_template.output_encoding, - error_template.encoding_errors)] + error_template.output_encoding, + error_template.encoding_errors)] context._set_with_template(error_template) error_template.render_context(context, error=error) |