Commits

Mike Bayer  committed 717d8a8

step 1, three separate classes, new magic number

  • Participants
  • Parent commits a3b2aba

Comments (0)

Files changed (2)

File mako/codegen.py

 from mako.pygen import PythonPrinter
 from mako import util, ast, parsetree, filters
 
-MAGIC_NUMBER = 5
+MAGIC_NUMBER = 6
 
 def compile(node, 
                 uri, 
                 callable_name = "make_namespace()"
             else:
                 callable_name = "None"
-            self.printer.writeline(
-                            "ns = runtime.Namespace(%r, context._clean_inheritance_tokens(),"
-                            " templateuri=%s, callables=%s, calling_uri=_template_uri, module=%s)" %
-                            (
-                                node.name,
-                                node.parsed_attributes.get('file', 'None'), 
-                                callable_name, 
-                                node.parsed_attributes.get('module', 'None'))
+
+            if 'file' in node.parsed_attributes:
+                self.printer.writeline(
+                                "ns = runtime.TemplateNamespace(%r, context._clean_inheritance_tokens(),"
+                                " templateuri=%s, callables=%s, calling_uri=_template_uri)" %
+                                (
+                                    node.name,
+                                    node.parsed_attributes.get('file', 'None'), 
+                                    callable_name, 
+                                )
+                            )
+            elif 'module' in node.parsed_attributes:
+                self.printer.writeline(
+                                "ns = runtime.ModuleNamespace(%r, context._clean_inheritance_tokens(),"
+                                " callables=%s, calling_uri=_template_uri, module=%s)" %
+                                (
+                                    node.name,
+                                    callable_name, 
+                                    node.parsed_attributes.get('module', 'None')
+                                )
+                            )
+            else:
+                self.printer.writeline(
+                                "ns = runtime.Namespace(%r, context._clean_inheritance_tokens(),"
+                                " callables=%s, calling_uri=_template_uri)" %
+                                (
+                                    node.name,
+                                    callable_name, 
+                                )
                             )
             if eval(node.attributes.get('inheritable', "False")):
                 self.printer.writeline("context['self'].%s = ns" % (node.name))

File mako/runtime.py

  
       """
  
-    def __init__(self, name, context, module=None, 
-                            template=None, templateuri=None, 
+    def __init__(self, name, context, 
                             callables=None, inherits=None, 
                             populate_self=True, calling_uri=None):
         self.name = name
-        if module is not None:
-            mod = __import__(module)
-            for token in module.split('.')[1:]:
-                mod = getattr(mod, token)
-            self._module = mod
-        else:
-            self._module = None
-        if templateuri is not None:
-            self.template = _lookup_template(context, templateuri, calling_uri)
-            self._templateuri = self.template.module._template_uri
-        else:
-            self.template = template
-            if self.template is not None:
-                self._templateuri = self.template.module._template_uri
         self.context = context
         self.inherits = inherits
         if callables is not None:
             self.callables = dict([(c.func_name, c) for c in callables])
-        else:
-            self.callables = None
-        if populate_self and self.template is not None:
-            lclcallable, lclcontext = \
-                        _populate_self_namespace(context, self.template, self_ns=self)
- 
+
+    callables = None
+
+    _module = None
+
     template = None
     """The :class:`.Template` object referenced by this
         :class:`.Namespace`, if any.
         if self.context.namespaces.has_key(key):
             return self.context.namespaces[key]
         else:
-            ns = Namespace(uri, self.context._copy(), 
+            ns = TemplateNamespace(uri, self.context._copy(), 
                                 templateuri=uri, 
                                 calling_uri=self._templateuri) 
             self.context.namespaces[key] = ns
                     "Namespace '%s' has no member '%s'" % 
                     (self.name, key))
 
+class TemplateNamespace(Namespace):
+    def __init__(self, name, context, template=None, templateuri=None, 
+                            callables=None, inherits=None, 
+                            populate_self=True, calling_uri=None):
+        self.name = name
+        self.context = context
+        self.inherits = inherits
+        if callables is not None:
+            self.callables = dict([(c.func_name, c) for c in callables])
+
+        if templateuri is not None:
+            self.template = _lookup_template(context, templateuri, 
+                                                calling_uri)
+            self._templateuri = self.template.module._template_uri
+        elif template is not None:
+            self.template = template
+            self._templateuri = template.module._template_uri
+        else:
+            raise TypeError("'template' argument is required.")
+
+        if populate_self:
+            lclcallable, lclcontext = \
+                        _populate_self_namespace(context, self.template, 
+                                                    self_ns=self)
+
+class ModuleNamespace(Namespace):
+    def __init__(self, name, context, module, 
+                            callables=None, inherits=None, 
+                            populate_self=True, calling_uri=None):
+        self.name = name
+        self.context = context
+        self.inherits = inherits
+        if callables is not None:
+            self.callables = dict([(c.func_name, c) for c in callables])
+
+        mod = __import__(module)
+        for token in module.split('.')[1:]:
+            mod = getattr(mod, token)
+        self._module = mod
+
+
 def supports_caller(func):
     """Apply a caller_stack compatibility decorator to a plain
     Python function.
     while ih.inherits is not None:
         ih = ih.inherits
     lclcontext = context.locals_({'next':ih})
-    ih.inherits = Namespace("self:%s" % template.uri, 
+    ih.inherits = TemplateNamespace("self:%s" % template.uri, 
                                 lclcontext, 
                                 template = template, 
                                 populate_self=False)
 
 def _populate_self_namespace(context, template, self_ns=None):
     if self_ns is None:
-        self_ns = Namespace('self:%s' % template.uri, 
+        self_ns = TemplateNamespace('self:%s' % template.uri, 
                                 context, template=template, 
                                 populate_self=False)
     context._data['self'] = context._data['local'] = self_ns