[Piglit] [PATCH 2/7] summary.py: use mako's TemplateLookup

Dylan Baker baker.dylan.c at gmail.com
Thu Oct 17 15:09:38 CEST 2013


This buys two things, a little less code duplication, and allows us to
load one mako template into another one.

Signed-off-by: Dylan Baker <baker.dylan.c at gmail.com>
---
 framework/summary.py | 54 +++++++++++++++++++++++-----------------------------
 1 file changed, 24 insertions(+), 30 deletions(-)

diff --git a/framework/summary.py b/framework/summary.py
index 31c1c26..8494d31 100644
--- a/framework/summary.py
+++ b/framework/summary.py
@@ -25,7 +25,7 @@ import string
 from itertools import izip_longest
 from shutil import copy
 from json import loads
-from mako.template import Template
+from mako.lookup import TemplateLookup
 
 import core
 # a local variable status exists, prevent accidental overloading by renaming
@@ -431,20 +431,22 @@ class Summary:
         of dicts, which mako turns into pretty HTML.
         """
 
+        # Lookup all templates in the template dir
+        templates = TemplateLookup(directories=["templates"],
+                                   output_encoding="utf-8",
+                                   module_directory=".makotmp")
+
+        # Load the necissary templates
+        testindex = templates.get_template("testrun_info.mako")
+        testfile = templates.get_template("test_result.mako")
+        makoindex = templates.get_template("index.mako")
+        empty_status = templates.get_template("empty_status.mako")
+
         # Copy static files
         copy("templates/index.css", path.join(destination, "index.css"))
         copy("templates/result.css", path.join(destination, "result.css"))
 
-        # Create the mako object for creating the test/index.html file
-        testindex = Template(filename="templates/testrun_info.mako",
-                             output_encoding="utf-8",
-                             module_directory=".makotmp")
-
-        # Create the mako object for the individual result files
-        testfile = Template(filename="templates/test_result.mako",
-                            output_encoding="utf-8",
-                            module_directory=".makotmp")
-
+        # Some sort definitions
         resultCss = path.join(destination, "result.css")
         index = path.join(destination, "index.html")
 
@@ -488,25 +490,17 @@ class Summary:
                     file.close()
 
         # Finally build the root html files: index, regressions, etc
-        index = Template(filename="templates/index.mako",
-                         output_encoding="utf-8",
-                         module_directory=".makotmp")
-
-        empty_status = Template(filename="templates/empty_status.mako",
-                                output_encoding="utf-8",
-                                module_directory=".makotmp")
-
         pages = ('changes', 'problems', 'skipped', 'fixes', 'regressions')
 
         # Index.html is a bit of a special case since there is index, all, and
         # alltests, where the other pages all use the same name. ie,
         # changes.html, self.changes, and page=changes.
         file = open(path.join(destination, "index.html"), 'w')
-        file.write(index.render(results=HTMLIndex(self, self.tests['all']),
-                                page='all',
-                                pages=pages,
-                                colnum=len(self.results),
-                                exclude=exclude))
+        file.write(makoindex.render(results=HTMLIndex(self, self.tests['all']),
+                                    page='all',
+                                    pages=pages,
+                                    colnum=len(self.results),
+                                    exclude=exclude))
         file.close()
 
         # Generate the rest of the pages
@@ -514,12 +508,12 @@ class Summary:
             file = open(path.join(destination, page + '.html'), 'w')
             # If there is information to display display it
             if self.tests[page]:
-                file.write(index.render(results=HTMLIndex(self,
-                                                          self.tests[page]),
-                                        pages=pages,
-                                        page=page,
-                                        colnum=len(self.results),
-                                        exclude=exclude))
+                file.write(makoindex.render(
+                    results=HTMLIndex(self, self.tests[page]),
+                    pages=pages,
+                    page=page,
+                    colnum=len(self.results),
+                    exclude=exclude))
             # otherwise provide an empty page
             else:
                 file.write(empty_status.render(page=page, pages=pages))
-- 
1.8.1.5



More information about the Piglit mailing list