mallardwriter: Use a TemplateLookup for grabbing templates
authorJasper St. Pierre <jstpierre@mecheye.net>
Wed, 9 Jan 2013 08:04:28 +0000 (03:04 -0500)
committerJasper St. Pierre <jstpierre@mecheye.net>
Wed, 9 Jan 2013 08:16:13 +0000 (03:16 -0500)
This removes us from the need to do path manipulation, and also
allows templates to include or import helpers from another with
the <%include/> or <%namespace/> helper control tags.

giscanner/mallardwriter.py

index 09701817c65df8850392f95357d0647e8584c66c..c8ab67dd7ad234041126bc841b8eb47e6e40ebc1 100644 (file)
@@ -27,6 +27,7 @@ import tempfile
 
 from xml.sax import saxutils
 from mako.template import Template
+from mako.lookup import TemplateLookup
 
 from . import ast
 from .utils import to_underscores
@@ -407,6 +408,19 @@ class MallardWriter(object):
         self._formatter = formatter_class(self._transformer)
         self._language = self._formatter.language
 
+        self._lookup = self._get_template_lookup()
+
+    def _get_template_lookup(self):
+        if 'UNINSTALLED_INTROSPECTION_SRCDIR' in os.environ:
+            top_srcdir = os.environ['UNINSTALLED_INTROSPECTION_SRCDIR']
+            template_dir = os.path.join(top_srcdir, 'giscanner')
+        else:
+            template_dir = os.path.dirname(__file__)
+
+        return TemplateLookup(directories=[template_dir],
+                              module_directory=tempfile.gettempdir(),
+                              output_encoding='utf-8')
+
     def write(self, output):
         nodes = [self._transformer.namespace]
         for node in self._transformer.namespace.itervalues():
@@ -433,19 +447,10 @@ class MallardWriter(object):
     def _render_node(self, node, output):
         namespace = self._transformer.namespace
 
-        if 'UNINSTALLED_INTROSPECTION_SRCDIR' in os.environ:
-            top_srcdir = os.environ['UNINSTALLED_INTROSPECTION_SRCDIR']
-            template_dir = os.path.join(top_srcdir, 'giscanner')
-        else:
-            template_dir = os.path.dirname(__file__)
-
         template_name = make_template_name(node, self._language)
         page_id = make_page_id(node)
 
-        file_name = os.path.join(template_dir, template_name)
-        file_name = os.path.abspath(file_name)
-        template = Template(filename=file_name, output_encoding='utf-8',
-                            module_directory=tempfile.gettempdir())
+        template = self._lookup.get_template(template_name)
         result = template.render(namespace=namespace,
                                  node=node,
                                  page_id=page_id,