summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <lars.wirzenius@codethink.co.uk>2014-03-25 14:57:21 +0000
committerLars Wirzenius <lars.wirzenius@codethink.co.uk>2014-03-25 14:57:21 +0000
commitbc518bff343c5df24707b9e13c8e711605a9e28c (patch)
tree6162e86d0e28daba878561f3a4c34db5c19d97f2
parent217813639d8152ee5f8b76c9fb2bcca664dff0a2 (diff)
downloadlorry-controller-bc518bff343c5df24707b9e13c8e711605a9e28c.tar.gz
Get rid of global templates variable
-rwxr-xr-xlorry-controller-webapp39
-rw-r--r--lorrycontroller/route.py3
2 files changed, 23 insertions, 19 deletions
diff --git a/lorry-controller-webapp b/lorry-controller-webapp
index cb2b75a..64b53a1 100755
--- a/lorry-controller-webapp
+++ b/lorry-controller-webapp
@@ -35,12 +35,6 @@ from flup.server.fcgi import WSGIServer
import lorrycontroller
-# This is a global variable to hold the source of every template we
-# use for HTML generation. It will be filled in by the main
-# application class at startup.
-templates = collections.defaultdict(str)
-
-
class StatusRenderer(object):
'''Helper class for rendering service status as JSON/HTML'''
@@ -63,11 +57,11 @@ class StatusRenderer(object):
status.update(self.get_free_disk_space(work_directory))
return status
- def render_status_as_html(self, status):
- return bottle.template(templates['status'], **status)
+ def render_status_as_html(self, template, status):
+ return bottle.template(template, **status)
- def write_status_as_html(self, status, filename):
- html = self.render_status_as_html(status)
+ def write_status_as_html(self, template, status, filename):
+ html = self.render_status_as_html(template, status)
try:
with open(filename, 'w') as f:
f.write(html)
@@ -152,7 +146,10 @@ class Status(lorrycontroller.LorryControllerRoute):
statedb = self.open_statedb()
status = renderer.get_status_as_dict(
statedb, self.app_settings['statedb'])
- renderer.write_status_as_html(status, self.app_settings['status-html'])
+ renderer.write_status_as_html(
+ self._templates['status'],
+ status,
+ self.app_settings['status-html'])
return status
@@ -167,8 +164,12 @@ class StatusHTML(lorrycontroller.LorryControllerRoute):
statedb = self.open_statedb()
status = renderer.get_status_as_dict(
statedb, self.app_settings['statedb'])
- renderer.write_status_as_html(status, self.app_settings['status-html'])
- return renderer.render_status_as_html(status)
+ renderer.write_status_as_html(
+ self._templates['status'],
+ status,
+ self.app_settings['status-html'])
+ return renderer.render_status_as_html(
+ self._templates['status'], status)
class ReadConfiguration(lorrycontroller.LorryControllerRoute):
@@ -412,7 +413,7 @@ class ShowLorryHTML(lorrycontroller.LorryControllerRoute):
(parts.scheme, host, '', '', '', ''))
return bottle.template(
- templates['lorry'],
+ self._templates['lorry'],
http_server_root=http_server_root,
lorry=lorry_info,
timestamp=timestamp)
@@ -635,7 +636,7 @@ class ListAllJobsHTML(lorrycontroller.LorryControllerRoute):
'job_infos': self.get_jobs(statedb),
'timestamp': time.strftime('%Y-%m-%d %H:%M:%S UTC', time.gmtime()),
}
- return bottle.template(templates['list-jobs'], **values)
+ return bottle.template(self._templates['list-jobs'], **values)
def get_jobs(self, statedb):
jobs = []
@@ -676,7 +677,7 @@ class ShowJob(lorrycontroller.LorryControllerRoute):
time.strftime('%Y-%m-%d %H:%M:%S UTC', time.gmtime()),
}
- return bottle.template(templates['job'], **variables)
+ return bottle.template(self._templates['job'], **variables)
class RemoveJob(lorrycontroller.LorryControllerRoute):
@@ -1004,12 +1005,12 @@ class WEBAPP(cliapp.Application):
def process_args(self, args):
self.settings.require('statedb')
- self.load_templates()
+ templates = self.load_templates()
webapp = bottle.Bottle()
for route_class in self.find_routes():
- route = route_class(self.settings)
+ route = route_class(self.settings, templates)
webapp.route(
path=route.path,
method=route.http_method,
@@ -1022,12 +1023,14 @@ class WEBAPP(cliapp.Application):
self.run_debug_server(webapp)
def load_templates(self):
+ templates = {}
for basename in os.listdir(self.settings['templates']):
if basename.endswith('.tpl'):
name = basename[:-len('.tpl')]
pathname = os.path.join(self.settings['templates'], basename)
with open(pathname) as f:
templates[name] = f.read()
+ return templates
def run_wsgi_server(self, webapp):
WSGIServer(webapp).run()
diff --git a/lorrycontroller/route.py b/lorrycontroller/route.py
index cce66ca..91a406e 100644
--- a/lorrycontroller/route.py
+++ b/lorrycontroller/route.py
@@ -33,8 +33,9 @@ class LorryControllerRoute(object):
'''
- def __init__(self, app_settings):
+ def __init__(self, app_settings, templates):
self.app_settings = app_settings
+ self._templates = templates
self._statedb = None
def open_statedb(self):