changeset 47:11249e75ced6

allow more than one template path
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 13 Aug 2012 15:41:32 -0700
parents fcd98303a90d
children bfea9feb5179
files setup.py talosnames/web.py
diffstat 2 files changed, 28 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/setup.py	Mon Aug 13 15:14:53 2012 -0700
+++ b/setup.py	Mon Aug 13 15:41:32 2012 -0700
@@ -7,6 +7,11 @@
 version = "0.0"
 dependencies = ['webob', 'CommandParser', 'talos', 'buildbot', 'PyYAML']
 
+try:
+    import json
+except ImportError:
+    dependencies.append('simplejson')
+
 # allow use of setuptools/distribute or distutils
 kw = {}
 try:
--- a/talosnames/web.py	Mon Aug 13 15:14:53 2012 -0700
+++ b/talosnames/web.py	Mon Aug 13 15:41:32 2012 -0700
@@ -14,12 +14,19 @@
 from subprocess import list2cmdline
 from webob import Request, Response, exc
 
+try:
+    import json
+except ImportError:
+    import simplejson as json
+
 here = os.path.dirname(os.path.abspath(__file__))
-template = os.path.join(here, 'templates', 'index.html')
 
 class Handler(object):
 
+    paths = {'tbpl': 'tbpl.html'}
+
     def __init__(self, **kw):
+
         self.api = TalosNames()
 
         # get data
@@ -39,7 +46,6 @@
                      'tbpl': dict([(suite, self.api.tbpl_name(suite))
                                    for suite in suites]),
                      'tests': tests,
-                     'list2cmdline': list2cmdline
                      }
 
         paint = {}
@@ -85,13 +91,17 @@
         self.data['chrome'] = chrome
         self.data['test_type'] = test_type
 
+        self.data['json'] = json.dumps(self.data)
+
+        self.data['list2cmdline'] = list2cmdline
+
     def __call__(self, environ, start_response):
         request = Request(environ)
         response = Response(content_type='text/html',
-                            body=self.render(request))
+                            body=self.render(request.path_info, request))
         return response(environ, start_response)
 
-    def render(self, request=None):
+    def render(self, template='index.html', request=None):
 
         # make a local copy of the data
         data = self.data.copy()
@@ -120,10 +130,16 @@
                     data['suites'] = [i for i in data['suites']
                                       if not data['enabled'][i]]
 
+        path = self.template_path(template)
+        contents = file(path).read()
+        template = tempita.HTMLTemplate(contents)
+        return template.substitute(data)
 
-        contents = file(template).read()
-        _template = tempita.HTMLTemplate(contents)
-        return _template.substitute(data)
+    def template_path(self, path):
+        """returns template path"""
+        path = path.strip('/')
+        template = self.paths.get(path, 'index.html')
+        return os.path.join(here, 'templates', template)
 
 def main(args=sys.argv[1:]):
     parser = optparse.OptionParser()