Commits

Andriy Kornatskyy committed a0706c1

Added hoocks for template import as module.

Comments (0)

Files changed (1)

src/wheezy/template/engine.py

 from wheezy.template.builder import SourceBuilder
 from wheezy.template.builder import builder_scan
 from wheezy.template.comp import allocate_lock
-from wheezy.template.comp import compile_source
 from wheezy.template.lexer import Lexer
 from wheezy.template.lexer import lexer_scan
 from wheezy.template.parser import Parser
 
 class Engine(object):
 
-    def __init__(self, loader, extensions, template_class=None):
+    def __init__(self, loader, extensions, template_class=None,
+            compiler_class=None):
         self.templates = {}
         self.renders = {}
+        self.modules = {}
+        self.global_vars = {'_r': self.render, '_i': self.import_name}
         self.loader = loader
         self.template_class = template_class or Template
+        if not compiler_class:
+            from wheezy.template.compiler import Compiler as compiler_class
+        self.compiler = compiler_class(
+                global_vars=self.global_vars,
+                source_lineno=-2)
         self.lock = allocate_lock()
-        lexer_rules, preprocessors = lexer_scan(extensions)
-        self.lexer = Lexer(lexer_rules, preprocessors)
-        parser_rules, parser_configs = parser_scan(extensions)
-        self.parser = Parser(parser_rules, parser_configs)
-        builder_rules = builder_scan(extensions)
-        self.builder = SourceBuilder(builder_rules)
-        self.global_vars = {'_r': self.render}
+        self.lexer = Lexer(*lexer_scan(extensions))
+        self.parser = Parser(*parser_scan(extensions))
+        self.builder = SourceBuilder(builder_scan(extensions))
 
     def get_template(self, name):
         try:
 
     # region: internal details
 
+    def import_name(self, name):
+        try:
+            return self.modules[name]
+        except KeyError:
+            self.compile_import(name)
+            return self.modules[name]
+
     def compile_template(self, name):
         self.lock.acquire(1)
         try:
                     raise IOError('Template "%s" not found.' % name)
                 tokens = self.lexer.tokenize(template_source)
                 nodes = list(self.parser.parse(tokens))
-                #from pprint import pprint
-                #pprint(nodes)
-                module_source = self.builder.build_render(nodes)
-                #print(name.center(80, '-'))
-                #from wheezy.template.utils import print_source
-                #print_source(module_source, -1)
-                render_template = compile_source(
-                    module_source, name, self.global_vars, -2)
+                source = self.builder.build_render(nodes)
+
+                #self.print_debug(name, tokens, nodes, source)
+
+                render_template = self.compiler.compile_source(
+                        source, name)['render']
                 self.renders[name] = render_template
                 self.templates[name] = self.template_class(
                         name, render_template)
         finally:
             self.lock.release()
 
+    def compile_import(self, name):
+        self.lock.acquire(1)
+        try:
+            if name not in self.modules:
+                template_source = self.loader.load(name)
+                if template_source == None:
+                    raise IOError('Import "%s" not found.' % name)
+                tokens = self.lexer.tokenize(template_source)
+                nodes = list(self.parser.parse(tokens))
+                source = self.builder.build_source(nodes)
+
+                #self.print_debug(name, tokens, nodes, module_source)
+
+                self.modules[name] = self.compiler.compile_module(
+                        source, name)
+        finally:
+            self.lock.release()
+
+    def print_debug(self, name, tokens, nodes, module_source):
+        print(name.center(80, '-'))
+        from pprint import pprint
+        pprint(tokens)
+        pprint(nodes)
+        from wheezy.template.utils import print_source
+        print_source(module_source, -1)
+
 
 class Template(object):