Download:
child 101:bb31b74cc331
parent 99:b0bcc3b9dc46
100:403f302df870
Anton Shestakov <av6@dwimlabs.net>, Mon, 04 Jul 2016 17:37:55 +0800
viewer: handlers deserve a separate module

3 файлов изменено, 126 вставок(+), 117 удалений(-) [+]
candolint/handlers.py file | annotate | diff | comparison | revisions
tests/test_viewer.py file | annotate | diff | comparison | revisions
viewer.py file | annotate | diff | comparison | revisions
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/candolint/handlers.py Mon Jul 04 17:37:55 2016 +0800
@@ -0,0 +1,120 @@
+#!/usr/bin/env python
+from __future__ import absolute_import, division
+
+import traceback
+
+from peewee import DoesNotExist
+from tornado.escape import json_decode
+from tornado.web import HTTPError, RequestHandler
+from tornado.web import ErrorHandler as BaseErrorHandler
+
+from candolint.models import database, Project, Change, Check
+
+
+def get_or_404(query, *args, **kwargs):
+ try:
+ return query.get(*args, **kwargs)
+ except DoesNotExist:
+ raise HTTPError(404)
+
+
+def get_project_or_404(domain, user, name):
+ pq = Project.select().where(
+ Project.domain == domain,
+ Project.user == (user if user != '-' else None),
+ Project.name == name)
+ return get_or_404(pq)
+
+
+class BaseHandler(RequestHandler):
+ def prepare(self):
+ database.connect()
+ super(BaseHandler, self).prepare()
+
+ def on_finish(self):
+ if not database.is_closed():
+ database.close()
+
+ def write_error(self, status_code, **kwargs):
+ data = {
+ 'code': status_code,
+ 'message': self._reason,
+ 'debug_message': ''
+ }
+ if 'exc_info' in kwargs:
+ if self.settings.get('serve_traceback'):
+ fexc = traceback.format_exception(*kwargs['exc_info'])
+ data['debug_message'] = '\n'.join(fexc)
+ if status_code == 404:
+ self.render('404.html', **data)
+ else:
+ self.render('500.html', **data)
+
+
+class IndexHandler(BaseHandler):
+ def get(self):
+ checks = (Check
+ .select(Check, Project, Change)
+ .join(Project)
+ .switch(Check)
+ .join(Change)
+ .group_by(Check.project))
+ self.render('index.html', checks=checks)
+
+
+class ProjectHandler(BaseHandler):
+ def get(self, domain, user, name):
+ project = get_project_or_404(domain, user, name)
+ checks = (Check
+ .select(Check, Change)
+ .join(Change)
+ .where(Check.project == project)
+ .limit(10))
+ self.render('project.html', project=project, checks=checks)
+
+
+class CheckHandler(BaseHandler):
+ def get(self, domain, user, name, check_num, format_='html'):
+ project = get_project_or_404(domain, user, name)
+ if check_num == 'latest':
+ check = get_or_404(Check, project=project)
+ else:
+ check = get_or_404(Check, project=project, ordinal=check_num)
+ lines = json_decode(check.lines)
+ if format_ == 'raw':
+ self.set_header('Content-Type', 'text/plain; charset=utf-8')
+ for line in lines:
+ self.write(line['text'])
+ self.write('\n')
+ self.finish()
+ else:
+ self.render('check.html', project=project, check=check, lines=lines)
+
+
+class StatusHandler(BaseHandler):
+ def get(self, domain, user, name):
+ project = get_project_or_404(domain, user, name)
+ check = get_or_404(Check, project=project)
+ parts = [('#555', 30, 14.5, 'lint')]
+ if not check.success:
+ parts.append(('#777', 62, 30.5, 'unknown'))
+ elif check.errors or check.warnings:
+ if check.errors:
+ msg = self.locale.translate('{} error', '{} errors', check.errors)
+ text = msg.format(check.errors)
+ width = 7 + 6 * len(text) + 7
+ parts.append(('#da314b', width, width // 2 - 0.5, text))
+ if check.warnings:
+ msg = self.locale.translate('{} warning', '{} warnings', check.warnings)
+ text = msg.format(check.warnings)
+ width = 7 + 6 * len(text) + 9
+ parts.append(('#faa732', width, width // 2 + 0.5, text))
+ else:
+ 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 ErrorHandler(BaseHandler, BaseErrorHandler):
+ pass
--- a/tests/test_viewer.py Mon Jul 04 17:12:56 2016 +0800
+++ b/tests/test_viewer.py Mon Jul 04 17:37:55 2016 +0800
@@ -3,8 +3,9 @@
from tornado.web import HTTPError
from tornado.testing import AsyncHTTPTestCase
+from candolint.handlers import get_project_or_404
from candolint.models import database, Project, Change, Check
-from viewer import get_project_or_404, CandolintViewer
+from viewer import CandolintViewer
def setup_module():
--- a/viewer.py Mon Jul 04 17:12:56 2016 +0800
+++ b/viewer.py Mon Jul 04 17:37:55 2016 +0800
@@ -1,19 +1,16 @@
#!/usr/bin/env python
-from __future__ import absolute_import, division
+from __future__ import absolute_import
import logging
import os
-import traceback
-from peewee import DoesNotExist
-from tornado.escape import json_decode
from tornado.ioloop import IOLoop
from tornado.options import define, options
-from tornado.web import Application, HTTPError, RequestHandler, URLSpec
-from tornado.web import ErrorHandler as BaseErrorHandler
+from tornado.web import Application, URLSpec
from candolint import uimodules
-from candolint.models import database, Project, Change, Check
+from candolint.handlers import IndexHandler, ProjectHandler, CheckHandler, StatusHandler, ErrorHandler
+from candolint.models import database
rel = lambda *x: os.path.abspath(os.path.join(os.path.dirname(__file__), *x))
@@ -25,115 +22,6 @@
define('debug', metavar='True|False', default=False, type=bool)
-def get_or_404(query, *args, **kwargs):
- try:
- return query.get(*args, **kwargs)
- except DoesNotExist:
- raise HTTPError(404)
-
-
-def get_project_or_404(domain, user, name):
- pq = Project.select().where(
- Project.domain == domain,
- Project.user == (user if user != '-' else None),
- Project.name == name)
- return get_or_404(pq)
-
-
-class BaseHandler(RequestHandler):
- def prepare(self):
- database.connect()
- super(BaseHandler, self).prepare()
-
- def on_finish(self):
- if not database.is_closed():
- database.close()
-
- def write_error(self, status_code, **kwargs):
- data = {
- 'code': status_code,
- 'message': self._reason,
- 'debug_message': ''
- }
- if 'exc_info' in kwargs:
- if self.settings.get('serve_traceback'):
- fexc = traceback.format_exception(*kwargs['exc_info'])
- data['debug_message'] = '\n'.join(fexc)
- if status_code == 404:
- self.render('404.html', **data)
- else:
- self.render('500.html', **data)
-
-
-class IndexHandler(BaseHandler):
- def get(self):
- checks = (Check
- .select(Check, Project, Change)
- .join(Project)
- .switch(Check)
- .join(Change)
- .group_by(Check.project))
- self.render('index.html', checks=checks)
-
-
-class ProjectHandler(BaseHandler):
- def get(self, domain, user, name):
- project = get_project_or_404(domain, user, name)
- checks = (Check
- .select(Check, Change)
- .join(Change)
- .where(Check.project == project)
- .limit(10))
- self.render('project.html', project=project, checks=checks)
-
-
-class CheckHandler(BaseHandler):
- def get(self, domain, user, name, check_num, format_='html'):
- project = get_project_or_404(domain, user, name)
- if check_num == 'latest':
- check = get_or_404(Check, project=project)
- else:
- check = get_or_404(Check, project=project, ordinal=check_num)
- lines = json_decode(check.lines)
- if format_ == 'raw':
- self.set_header('Content-Type', 'text/plain; charset=utf-8')
- for line in lines:
- self.write(line['text'])
- self.write('\n')
- self.finish()
- else:
- self.render('check.html', project=project, check=check, lines=lines)
-
-
-class StatusHandler(BaseHandler):
- def get(self, domain, user, name):
- project = get_project_or_404(domain, user, name)
- check = get_or_404(Check, project=project)
- parts = [('#555', 30, 14.5, 'lint')]
- if not check.success:
- parts.append(('#777', 62, 30.5, 'unknown'))
- elif check.errors or check.warnings:
- if check.errors:
- msg = self.locale.translate('{} error', '{} errors', check.errors)
- text = msg.format(check.errors)
- width = 7 + 6 * len(text) + 7
- parts.append(('#da314b', width, width // 2 - 0.5, text))
- if check.warnings:
- msg = self.locale.translate('{} warning', '{} warnings', check.warnings)
- text = msg.format(check.warnings)
- width = 7 + 6 * len(text) + 9
- parts.append(('#faa732', width, width // 2 + 0.5, text))
- else:
- 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 ErrorHandler(BaseHandler, BaseErrorHandler):
- pass
-
-
class CandolintViewer(Application):
def __init__(self):
handlers = [