Skip to content
Snippets Groups Projects
Commit 395e690c authored by Mike Bayer's avatar Mike Bayer
Browse files

cstringio, template lookup

parent 9cf1c1b3
No related branches found
No related tags found
No related merge requests found
"""provides the Compiler object for generating module source code.""" """provides the Compiler object for generating module source code."""
from StringIO import StringIO
import time import time
from mako.pygen import PythonPrinter from mako.pygen import PythonPrinter
from mako import util, ast, parsetree from mako import util, ast, parsetree
...@@ -12,7 +11,7 @@ class Compiler(object): ...@@ -12,7 +11,7 @@ class Compiler(object):
self.node = node self.node = node
self.filename = filename self.filename = filename
def render(self): def render(self):
buf = StringIO() buf = util.StringIO()
printer = PythonPrinter(buf) printer = PythonPrinter(buf)
# module-level names, python code # module-level names, python code
...@@ -174,7 +173,7 @@ class _GenerateRenderMethod(object): ...@@ -174,7 +173,7 @@ class _GenerateRenderMethod(object):
def visitIncludeTag(self, node): def visitIncludeTag(self, node):
self.write_source_comment(node) self.write_source_comment(node)
self.printer.writeline("context.include_file(%s, import=%s)" % (repr(node.attributes['file']), repr(node.attributes.get('import', False)))) self.printer.writeline("runtime.include_file(context, %s, import_symbols=%s)" % (repr(node.attributes['file']), repr(node.attributes.get('import', False))))
def visitNamespaceTag(self, node): def visitNamespaceTag(self, node):
self.write_source_comment(node) self.write_source_comment(node)
self.printer.writeline("def make_namespace():") self.printer.writeline("def make_namespace():")
......
...@@ -16,4 +16,7 @@ class SyntaxException(MakoException): ...@@ -16,4 +16,7 @@ class SyntaxException(MakoException):
def __init__(self, message, lineno, pos): def __init__(self, message, lineno, pos):
MakoException.__init__(self, message + " at line: %d char: %d" % (lineno, pos)) MakoException.__init__(self, message + " at line: %d char: %d" % (lineno, pos))
self.lineno =lineno self.lineno =lineno
self.pos = pos self.pos = pos
\ No newline at end of file
class TemplateLookupException(MakoException):
pass
\ No newline at end of file
...@@ -3,11 +3,11 @@ from mako import exceptions ...@@ -3,11 +3,11 @@ from mako import exceptions
class Context(object): class Context(object):
"""provides runtime namespace and output buffer for templates.""" """provides runtime namespace and output buffer for templates."""
def __init__(self, buffer, **data): def __init__(self, template, buffer, **data):
self.buffer = buffer self.buffer = buffer
self.data = data self.data = data
# the Template instance currently rendering with this context. # the Template instance currently rendering with this context.
self.with_template = None self.with_template = template
def __getitem__(self, key): def __getitem__(self, key):
return self.data[key] return self.data[key]
def get(self, key, default=None): def get(self, key, default=None):
...@@ -19,8 +19,7 @@ class Context(object): ...@@ -19,8 +19,7 @@ class Context(object):
with the given keyword arguments.""" with the given keyword arguments."""
x = self.data.copy() x = self.data.copy()
x.update(args) x.update(args)
c = Context(self.buffer, **x) c = Context(self.with_template, self.buffer, **x)
c.with_template = self.with_template
return c return c
class Namespace(object): class Namespace(object):
...@@ -53,4 +52,8 @@ class Namespace(object): ...@@ -53,4 +52,8 @@ class Namespace(object):
pass pass
raise exceptions.RuntimeException("Namespace '%s' has no member '%s'" % (self.name, key)) raise exceptions.RuntimeException("Namespace '%s' has no member '%s'" % (self.name, key))
def include_file(context, uri, import_symbols):
lookup = context.with_template.lookup
template = lookup.get_template(uri)
template.render_context(context)
\ No newline at end of file
...@@ -4,8 +4,8 @@ as well as template runtime operations.""" ...@@ -4,8 +4,8 @@ as well as template runtime operations."""
from mako.lexer import Lexer from mako.lexer import Lexer
from mako.codegen import Compiler from mako.codegen import Compiler
from mako.runtime import Context from mako.runtime import Context
from mako import util
import imp, time, inspect, weakref, sys import imp, time, inspect, weakref, sys
from StringIO import StringIO
_modules = weakref.WeakValueDictionary() _modules = weakref.WeakValueDictionary()
_inmemory_templates = weakref.WeakValueDictionary() _inmemory_templates = weakref.WeakValueDictionary()
...@@ -17,7 +17,7 @@ class _ModuleMarker(object): ...@@ -17,7 +17,7 @@ class _ModuleMarker(object):
class Template(object): class Template(object):
"""a compiled template""" """a compiled template"""
def __init__(self, text=None, module=None, identifier=None, filename=None, format_exceptions=True, error_handler=None): def __init__(self, text=None, module=None, identifier=None, filename=None, format_exceptions=True, error_handler=None, lookup=None):
"""construct a new Template instance using either literal template text, or a previously loaded template module """construct a new Template instance using either literal template text, or a previously loaded template module
text - textual template source, or None if a module is to be provided text - textual template source, or None if a module is to be provided
...@@ -46,6 +46,7 @@ class Template(object): ...@@ -46,6 +46,7 @@ class Template(object):
self.callable_ = self.module.render self.callable_ = self.module.render
self.format_exceptions = format_exceptions self.format_exceptions = format_exceptions
self.error_handler = error_handler self.error_handler = error_handler
self.lookup = lookup
_modules[module.__name__] = _ModuleMarker(module) _modules[module.__name__] = _ModuleMarker(module)
source = property(lambda self:_get_template_source(self.callable_), doc="""return the template source code for this Template.""") source = property(lambda self:_get_template_source(self.callable_), doc="""return the template source code for this Template.""")
...@@ -89,8 +90,8 @@ def _compile_text(text, identifier, filename): ...@@ -89,8 +90,8 @@ def _compile_text(text, identifier, filename):
def _render(template, callable_, *args, **data): def _render(template, callable_, *args, **data):
"""given a Template and a callable_ from that template, create a Context and return the string output.""" """given a Template and a callable_ from that template, create a Context and return the string output."""
buf = StringIO() buf = util.StringIO()
context = Context(buf, **data) context = Context(template, buf, **data)
kwargs = {} kwargs = {}
argspec = inspect.getargspec(callable_) argspec = inspect.getargspec(callable_)
namedargs = argspec[0] + [v for v in argspec[1:3] if v is not None] namedargs = argspec[0] + [v for v in argspec[1:3] if v is not None]
...@@ -101,7 +102,6 @@ def _render(template, callable_, *args, **data): ...@@ -101,7 +102,6 @@ def _render(template, callable_, *args, **data):
return buf.getvalue() return buf.getvalue()
def _render_context(template, callable_, context, *args, **kwargs): def _render_context(template, callable_, context, *args, **kwargs):
context.with_template = template
_exec_template(callable_, context, args=args, kwargs=kwargs) _exec_template(callable_, context, args=args, kwargs=kwargs)
def _exec_template(callable_, context, args=None, kwargs=None): def _exec_template(callable_, context, args=None, kwargs=None):
......
...@@ -5,4 +5,9 @@ except: ...@@ -5,4 +5,9 @@ except:
import sets import sets
Set = sets.Set Set = sets.Set
try:
from cStringIO import StringIO
except:
from StringIO import StringIO
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment