diff options
Diffstat (limited to 'lorrycontroller/showlorry.py')
-rw-r--r-- | lorrycontroller/showlorry.py | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/lorrycontroller/showlorry.py b/lorrycontroller/showlorry.py new file mode 100644 index 0000000..fc336a5 --- /dev/null +++ b/lorrycontroller/showlorry.py @@ -0,0 +1,86 @@ +# Copyright (C) 2014 Codethink Limited +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; version 2 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + +import json +import logging +import time +import urlparse + +import bottle + +import lorrycontroller + + +class ShowLorry(lorrycontroller.LorryControllerRoute): + + http_method = 'GET' + path = '/1.0/lorry/<path:path>' + + def run(self, **kwargs): + logging.info('%s %s called', self.http_method, self.path) + statedb = self.open_statedb() + try: + return statedb.get_lorry_info(kwargs['path']) + except lorrycontroller.LorryNotFoundError as e: + bottle.abort(404, str(e)) + + +class ShowLorryHTML(lorrycontroller.LorryControllerRoute): + + http_method = 'GET' + path = '/1.0/lorry-html/<path:path>' + + def run(self, **kwargs): + logging.info('%s %s called', self.http_method, self.path) + statedb = self.open_statedb() + try: + lorry_info = statedb.get_lorry_info(kwargs['path']) + except lorrycontroller.LorryNotFoundError as e: + bottle.abort(404, str(e)) + + renderer = lorrycontroller.StatusRenderer() + shower = lorrycontroller.JobShower() + + lorry_obj = json.loads(lorry_info['text']).values()[0] + lorry_info['url'] = lorry_obj['url'] + + lorry_info['interval_nice'] = renderer.format_secs_nicely( + lorry_info['interval']) + + lorry_info['last_run_nice'] = time.strftime( + '%Y-%m-%d %H:%M:%S UTC', + time.gmtime(lorry_info['last_run'])) + + lorry_info['disk_usage_nice'] = shower.format_bytesize( + lorry_info['disk_usage']) + + now = statedb.get_current_time() + + due = lorry_info['last_run'] + lorry_info['interval'] + lorry_info['due_nice'] = renderer.format_due_nicely(due, now) + + timestamp = time.strftime('%Y-%m-%d %H:%M:%S UTC', time.gmtime(now)) + + parts = urlparse.urlparse(bottle.request.url) + host, port = parts.netloc.split(':', 1) + http_server_root = urlparse.urlunparse( + (parts.scheme, host, '', '', '', '')) + + return bottle.template( + self._templates['lorry'], + http_server_root=http_server_root, + lorry=lorry_info, + timestamp=timestamp) |