Anton Shestakov <av6@dwimlabs.net>, Wed, 15 Jun 2016 21:35:37 +0800
viewer: use absolute imports and integer division
viewer.py
Permissions: -rwxr-xr-x
from __future__ import absolute_import, division from tornado.escape import json_decode from tornado.ioloop import IOLoop from tornado.options import define, options from tornado.web import Application, RequestHandler, URLSpec from candolint import uimodules from candolint.models import database, Project, Change, Check rel = lambda *x: os.path.abspath(os.path.join(os.path.dirname(__file__), *x)) define('listen', metavar='IP', default='127.0.0.1') define('port', metavar='PORT', default=8033, type=int) define('xheaders', metavar='True|False', default=False, type=bool) define('debug', metavar='True|False', default=False, type=bool) class BaseHandler(RequestHandler): super(BaseHandler, self).prepare() if not database.is_closed(): class IndexHandler(BaseHandler): .select(Check, Project, Change) .group_by(Check.project)) self.render('index.html', checks=checks) class ProjectHandler(BaseHandler): def get(self, domain, user, name): project = Project.select().where( Project.domain == domain, Project.user == (user if user != '-' else None), Project.name == name).get() .where(Check.project == project) self.render('project.html', project=project, checks=checks) class CheckHandler(BaseHandler): def get(self, domain, user, name, check_num): project = Project.select().where( Project.domain == domain, Project.user == (user if user != '-' else None), Project.name == name).get() if check_num == 'latest': check = Check.get(project=project) check = Check.get(project=project, ordinal=check_num) lines = json_decode(check.lines) self.render('check.html', project=project, check=check, lines=lines) class StatusHandler(BaseHandler): def get(self, domain, user, name): project = Project.select().where( Project.domain == domain, Project.user == (user if user != '-' else None), Project.name == name).get() check = Check.get(project=project) parts = [('#555', 30, 14.5, 'lint')] parts.append(('#777', 62, 30.5, 'unknown')) elif check.errors or check.warnings: w = 7 + 6 * len(str(check.errors)) + 7 parts.append(('#da314b', w, w / 2 - 0.5, check.errors)) w = 7 + 6 * len(str(check.warnings)) + 7 parts.append(('#faa732', w, w / 2 - 0.5, check.warnings)) parts.append(('#8cc14c', 40, 19.5, 'none')) width = sum(p[1] for p in parts) self.set_header('Content-Type', 'image/svg+xml; charset=utf-8') self.render('status.svg', width=width, parts=parts, height=20) class CandolintViewer(Application): URLSpec(r'/', IndexHandler), URLSpec(r'/([.a-z0-9_-]+)/([^/]+)/([^/]+)', ProjectHandler), URLSpec(r'/([.a-z0-9_-]+)/([^/]+)/([^/]+)/([\d]+|latest)', CheckHandler), URLSpec(r'/([.a-z0-9_-]+)/([^/]+)/([^/]+)/status\.svg', StatusHandler), static_path=rel('static'), template_path=rel('templates'), super(CandolintViewer, self).__init__(handlers, **settings) logging.getLogger('peewee').setLevel(logging.DEBUG) database.init(rel('database.sqlite')) def listen(self, port, address='', **kwargs): name = self.__class__.__name__ logging.info('%s is serving on %s:%d', name, address, port) super(CandolintViewer, self).listen(port, address, **kwargs) options.parse_command_line() application = CandolintViewer() application.listen(options.port, address=options.listen, xheaders=options.xheaders) IOLoop.instance().start() if __name__ == '__main__':