[gobject-introspection/wip/doctool-improvements: 10/14] mallardwriter: Use a TemplateLookup for grabbing templates



commit fb69b6c36e96868f051974f3262f26670fb21857
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Wed Jan 9 03:04:28 2013 -0500

    mallardwriter: Use a TemplateLookup for grabbing templates
    
    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 |   25 +++++++++++++++----------
 1 files changed, 15 insertions(+), 10 deletions(-)
---
diff --git a/giscanner/mallardwriter.py b/giscanner/mallardwriter.py
index 0970181..c8ab67d 100644
--- a/giscanner/mallardwriter.py
+++ b/giscanner/mallardwriter.py
@@ -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,



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]