diff options
author | Lars Wirzenius <lars.wirzenius@codethink.co.uk> | 2014-02-04 14:23:26 +0000 |
---|---|---|
committer | Lars Wirzenius <lars.wirzenius@codethink.co.uk> | 2014-02-04 14:23:26 +0000 |
commit | 01f3aaaa6d679535cdd38d9003904dd0080a687c (patch) | |
tree | 2bae2332f50eba94383df542e6199ca3da87468b | |
parent | d8638dd91c6d0a0d5d3934bea8e9df95d02248cf (diff) | |
download | lorry-controller-01f3aaaa6d679535cdd38d9003904dd0080a687c.tar.gz |
Refactor for code clarity; fix race condition
Better method names, clearer APIs, and also avoid a race condition by
not querying STATEDB twice.
-rwxr-xr-x | lorry-controller-webapp | 19 |
1 files changed, 10 insertions, 9 deletions
diff --git a/lorry-controller-webapp b/lorry-controller-webapp index 913275d..0e5fd5c 100755 --- a/lorry-controller-webapp +++ b/lorry-controller-webapp @@ -113,7 +113,7 @@ class StatusRenderer(object): '''Helper class for rendering service status as JSON/HTML''' - def render_as_dict(self, statedb): + def get_status_as_dict(self, statedb): quotes = [ "Never get drunk unless you're willing to pay for it - " "the next day.", @@ -127,12 +127,11 @@ class StatusRenderer(object): } return status - def render_as_html(self, statedb): - status = self.render_as_dict(statedb) + def render_status_as_html(self, status): return STATUS_HTML_TEMPLATE.format(**status) - def write_as_html(self, statedb, filename): - html = self.render_as_html(statedb) + def write_status_as_html(self, status, filename): + html = self.render_status_as_html(status) with open(filename, 'w') as f: f.write(html) @@ -144,8 +143,9 @@ class Status(LorryControllerRoute): def run(self, **kwargs): renderer = StatusRenderer() - renderer.write_as_html(self.statedb, self.app_settings['status-html']) - return renderer.render_as_dict(self.statedb) + status = renderer.get_status_as_dict(self.statedb) + renderer.write_status_as_html(status, self.app_settings['status-html']) + return status class StatusHTML(LorryControllerRoute): @@ -155,8 +155,9 @@ class StatusHTML(LorryControllerRoute): def run(self, **kwargs): renderer = StatusRenderer() - renderer.write_as_html(self.statedb, self.app_settings['status-html']) - return renderer.render_as_html(self.statedb) + status = renderer.get_status_as_dict(self.statedb) + renderer.write_status_as_html(status, self.app_settings['status-html']) + return renderer.render_status_as_html(status) class StartQueue(LorryControllerRoute): |