|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +from __future__ import unicode_literals, absolute_import |
| 3 | + |
| 4 | +import six |
| 5 | +import importlib |
| 6 | +import tornado.web |
| 7 | + |
| 8 | +settings = {} |
| 9 | + |
| 10 | +def load_tornado_settings(*modules): |
| 11 | + settings.update({'MODULES': modules}) |
| 12 | + kwargs = {} |
| 13 | + mods = [] |
| 14 | + config = Config() |
| 15 | + config.update(**settings) |
| 16 | + try: |
| 17 | + setting_mod = importlib.import_module('my_settings') |
| 18 | + if hasattr(setting_mod, 'load_settings'): |
| 19 | + getattr(setting_mod, 'load_uris')(config, **kwargs) |
| 20 | + except ImportError: |
| 21 | + pass |
| 22 | + |
| 23 | + for module in modules: |
| 24 | + try: |
| 25 | + mods.append(importlib.import_module('%s.routes' % module)) |
| 26 | + except ImportError: |
| 27 | + raise ImportError( |
| 28 | + "Could not import routers '%s' (Is it on sys.path?)" % ( |
| 29 | + module)) |
| 30 | + |
| 31 | + for mod in mods: |
| 32 | + if hasattr(mod, 'load_uris'): |
| 33 | + getattr(mod, 'load_uris')(config, **kwargs) |
| 34 | + |
| 35 | + return config |
| 36 | + |
| 37 | +class RequestHandler(tornado.web.RequestHandler): |
| 38 | + on_initialize_decorators = [] |
| 39 | + |
| 40 | + def initialize(self): |
| 41 | + request = self.request |
| 42 | + meth = getattr(self, self.request.method.lower(), None) |
| 43 | + if meth is None and self.request.method == 'HEAD': |
| 44 | + meth = getattr(self, 'get', None) |
| 45 | + assert meth is not None, 'Unimplemented method %r' % request.method |
| 46 | + |
| 47 | + for decorator in self.on_initialize_decorators: |
| 48 | + meth = decorator(meth) |
| 49 | + |
| 50 | + setattr(self, self.request.method.lower(), meth) |
| 51 | + |
| 52 | + def set_headers(self, items): |
| 53 | + if items is None: |
| 54 | + return |
| 55 | + for k, v in items: |
| 56 | + self.set_header(k, v) |
| 57 | + |
| 58 | + def write_error(self, status_code, **kwargs): |
| 59 | + """Override to implement custom error pages. |
| 60 | + |
| 61 | + ``write_error`` may call `write`, `render`, `set_header`, etc |
| 62 | + to produce output as usual. |
| 63 | + |
| 64 | + If this error was caused by an uncaught exception (including |
| 65 | + HTTPError), an ``exc_info`` triple will be available as |
| 66 | + ``kwargs["exc_info"]``. Note that this exception may not be |
| 67 | + the "current" exception for purposes of methods like |
| 68 | + ``sys.exc_info()`` or ``traceback.format_exc``. |
| 69 | + """ |
| 70 | + self.finish({ |
| 71 | + "code": status_code, |
| 72 | + "message": self._reason, |
| 73 | + }) |
| 74 | + |
| 75 | +class Config(object): |
| 76 | + def __getitem__(self, item): |
| 77 | + return getattr(self, item) |
| 78 | + |
| 79 | + def update(self, **kw): |
| 80 | + for name, value in kw.items(): |
| 81 | + self.__setattr__(name, value) |
| 82 | + |
| 83 | + def setdefault(self, key, default=None): |
| 84 | + try: |
| 85 | + return getattr(self, key) |
| 86 | + except AttributeError: |
| 87 | + setattr(self, key, default) |
| 88 | + return default |
| 89 | + |
| 90 | + def uri_tuple(self, route, url_prefix): |
| 91 | + route['resource'].endpoint = route['endpoint'] |
| 92 | + route['resource'].blueprint = url_prefix.replace('/', '_') |
| 93 | + if url_prefix: |
| 94 | + return r'/' + url_prefix + route['urls'][0], route['resource'] |
| 95 | + return route['urls'][0], route['resource'] |
| 96 | + |
| 97 | + def update_uri(self, routes, url_prefix=r''): |
| 98 | + self.ROUTES.extend(routes) |
| 99 | + self.URIS.extend([self.uri_tuple(r, url_prefix) for r in routes]) |
| 100 | + |
| 101 | + URIS = [] |
| 102 | + ROUTES = [] |
| 103 | + DEBUG = True |
| 104 | + PORT = 5000 |
| 105 | + WORKER = 1 |
0 commit comments