QuitableFlask   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 61
rs 10
c 0
b 0
f 0
wmc 13

3 Methods

Rating   Name   Duplication   Size   Complexity  
A quit() 0 5 2
A logger() 0 3 1
F run() 0 48 10
1
#!/usr/bin/env python
2
# -*- encoding: utf-8 -*-
3
# vim: set et sw=4 ts=4 sts=4 ff=unix fenc=utf8:
4
# Author: Binux<[email protected]>
5
#         http://binux.me
6
# Created on 2014-02-22 23:17:13
7
8
import os
9
import sys
10
import logging
11
logger = logging.getLogger("webui")
12
13
from six import reraise
14
from six.moves import builtins
15
from six.moves.urllib.parse import urljoin
16
from flask import Flask
17
from pyspider.fetcher import tornado_fetcher
18
19
if os.name == 'nt':
20
    import mimetypes
21
    mimetypes.add_type("text/css", ".css", True)
22
23
24
class QuitableFlask(Flask):
25
    """Add quit() method to Flask object"""
26
27
    @property
28
    def logger(self):
29
        return logger
30
31
    def run(self, host=None, port=None, debug=None, **options):
32
        import tornado.wsgi
33
        import tornado.ioloop
34
        import tornado.httpserver
35
        import tornado.web
36
37
        if host is None:
38
            host = '127.0.0.1'
39
        if port is None:
40
            server_name = self.config['SERVER_NAME']
41
            if server_name and ':' in server_name:
42
                port = int(server_name.rsplit(':', 1)[1])
43
            else:
44
                port = 5000
45
        if debug is not None:
46
            self.debug = bool(debug)
47
48
        hostname = host
49
        port = port
50
        application = self
51
        use_reloader = self.debug
52
        use_debugger = self.debug
53
54
        if use_debugger:
55
            from werkzeug.debug import DebuggedApplication
56
            application = DebuggedApplication(application, True)
57
58
        try:
59
            from .webdav import dav_app
60
        except ImportError as e:
61
            logger.warning('WebDav interface not enabled: %r', e)
62
            dav_app = None
63
        if dav_app:
64
            from werkzeug.wsgi import DispatcherMiddleware
65
            application = DispatcherMiddleware(application, {
66
                '/dav': dav_app
67
            })
68
69
        container = tornado.wsgi.WSGIContainer(application)
70
        self.http_server = tornado.httpserver.HTTPServer(container)
71
        self.http_server.listen(port, hostname)
72
        if use_reloader:
73
            from tornado import autoreload
74
            autoreload.start()
75
76
        self.logger.info('webui running on %s:%s', hostname, port)
77
        self.ioloop = tornado.ioloop.IOLoop.current()
78
        self.ioloop.start()
79
80
    def quit(self):
81
        if hasattr(self, 'ioloop'):
82
            self.ioloop.add_callback(self.http_server.stop)
83
            self.ioloop.add_callback(self.ioloop.stop)
84
        self.logger.info('webui exiting...')
85
86
87
app = QuitableFlask('webui',
88
                    static_folder=os.path.join(os.path.dirname(__file__), 'static'),
89
                    template_folder=os.path.join(os.path.dirname(__file__), 'templates'))
90
app.secret_key = os.urandom(24)
91
app.jinja_env.line_statement_prefix = '#'
92
app.jinja_env.globals.update(builtins.__dict__)
93
94
app.config.update({
95
    'fetch': lambda x: tornado_fetcher.Fetcher(None, None, async=False).fetch(x),
96
    'taskdb': None,
97
    'projectdb': None,
98
    'scheduler_rpc': None,
99
    'queues': dict(),
100
    'process_time_limit': 30,
101
})
102
103
104
def cdn_url_handler(error, endpoint, kwargs):
105
    if endpoint == 'cdn':
106
        path = kwargs.pop('path')
107
        # cdn = app.config.get('cdn', 'http://cdn.staticfile.org/')
108
        # cdn = app.config.get('cdn', '//cdnjs.cloudflare.com/ajax/libs/')
109
        cdn = app.config.get('cdn', '//cdnjscn.b0.upaiyun.com/libs/')
110
        return urljoin(cdn, path)
111
    else:
112
        exc_type, exc_value, tb = sys.exc_info()
113
        if exc_value is error:
114
            reraise(exc_type, exc_value, tb)
115
        else:
116
            raise error
117
app.handle_url_build_error = cdn_url_handler
118