diff options
Diffstat (limited to 'examples/coolmagic')
| -rw-r--r-- | examples/coolmagic/__init__.py | 2 | ||||
| -rw-r--r-- | examples/coolmagic/application.py | 33 | ||||
| -rw-r--r-- | examples/coolmagic/helpers.py | 2 | ||||
| -rw-r--r-- | examples/coolmagic/utils.py | 41 | ||||
| -rw-r--r-- | examples/coolmagic/views/static.py | 10 |
5 files changed, 51 insertions, 37 deletions
diff --git a/examples/coolmagic/__init__.py b/examples/coolmagic/__init__.py index 88d5db80..0526f1d6 100644 --- a/examples/coolmagic/__init__.py +++ b/examples/coolmagic/__init__.py @@ -8,4 +8,4 @@ :copyright: 2007 Pallets :license: BSD-3-Clause """ -from coolmagic.application import make_app +from .application import make_app diff --git a/examples/coolmagic/application.py b/examples/coolmagic/application.py index 10ffd2a2..730f4d83 100644 --- a/examples/coolmagic/application.py +++ b/examples/coolmagic/application.py @@ -12,11 +12,18 @@ :copyright: 2007 Pallets :license: BSD-3-Clause """ -from os import path, listdir -from coolmagic.utils import Request, local_manager +from os import listdir +from os import path + +from werkzeug.exceptions import HTTPException +from werkzeug.exceptions import NotFound from werkzeug.middleware.shared_data import SharedDataMiddleware -from werkzeug.routing import Map, Rule, RequestRedirect -from werkzeug.exceptions import HTTPException, NotFound +from werkzeug.routing import Map +from werkzeug.routing import RequestRedirect +from werkzeug.routing import Rule + +from .utils import local_manager +from .utils import Request class CoolMagicApplication(object): @@ -27,17 +34,17 @@ class CoolMagicApplication(object): def __init__(self, config): self.config = config - for fn in listdir(path.join(path.dirname(__file__), 'views')): - if fn.endswith('.py') and fn != '__init__.py': - __import__('coolmagic.views.' + fn[:-3]) + for fn in listdir(path.join(path.dirname(__file__), "views")): + if fn.endswith(".py") and fn != "__init__.py": + __import__("coolmagic.views." + fn[:-3]) from coolmagic.utils import exported_views + rules = [ # url for shared data. this will always be unmatched # because either the middleware or the webserver # handles that request first. - Rule('/public/<path:file>', - endpoint='shared_data') + Rule("/public/<path:file>", endpoint="shared_data") ] self.views = {} for endpoint, (func, rule, extra) in exported_views.items(): @@ -53,7 +60,7 @@ class CoolMagicApplication(object): endpoint, args = urls.match(req.path) resp = self.views[endpoint](**args) except NotFound: - resp = self.views['static.not_found']() + resp = self.views["static.not_found"]() except (HTTPException, RequestRedirect) as e: resp = e return resp(environ, start_response) @@ -68,9 +75,9 @@ def make_app(config=None): app = CoolMagicApplication(config) # static stuff - app = SharedDataMiddleware(app, { - '/public': path.join(path.dirname(__file__), 'public') - }) + app = SharedDataMiddleware( + app, {"/public": path.join(path.dirname(__file__), "public")} + ) # clean up locals app = local_manager.make_middleware(app) diff --git a/examples/coolmagic/helpers.py b/examples/coolmagic/helpers.py index 54638335..4cd4ac45 100644 --- a/examples/coolmagic/helpers.py +++ b/examples/coolmagic/helpers.py @@ -8,7 +8,7 @@ :copyright: 2007 Pallets :license: BSD-3-Clause """ -from coolmagic.utils import ThreadedRequest +from .utils import ThreadedRequest #: a thread local proxy request object diff --git a/examples/coolmagic/utils.py b/examples/coolmagic/utils.py index b69f95a4..f4cf20d5 100644 --- a/examples/coolmagic/utils.py +++ b/examples/coolmagic/utils.py @@ -11,17 +11,21 @@ :copyright: 2007 Pallets :license: BSD-3-Clause """ -from os.path import dirname, join -from jinja2 import Environment, FileSystemLoader -from werkzeug.local import Local, LocalManager -from werkzeug.wrappers import BaseRequest, BaseResponse +from os.path import dirname +from os.path import join + +from jinja2 import Environment +from jinja2 import FileSystemLoader +from werkzeug.local import Local +from werkzeug.local import LocalManager +from werkzeug.wrappers import BaseRequest +from werkzeug.wrappers import BaseResponse local = Local() local_manager = LocalManager([local]) template_env = Environment( - loader=FileSystemLoader(join(dirname(__file__), 'templates'), - use_memcache=False) + loader=FileSystemLoader(join(dirname(__file__), "templates"), use_memcache=False) ) exported_views = {} @@ -31,19 +35,23 @@ def export(string, template=None, **extra): Decorator for registering view functions and adding templates to it. """ + def wrapped(f): - endpoint = (f.__module__ + '.' + f.__name__)[16:] + endpoint = (f.__module__ + "." + f.__name__)[16:] if template is not None: old_f = f + def f(**kwargs): rv = old_f(**kwargs) if not isinstance(rv, Response): rv = TemplateResponse(template, **(rv or {})) return rv + f.__name__ = old_f.__name__ f.__doc__ = old_f.__doc__ exported_views[endpoint] = (f, string, extra) return f + return wrapped @@ -59,7 +67,8 @@ class Request(BaseRequest): The concrete request object used in the WSGI application. It has some helper functions that can be used to build URLs. """ - charset = 'utf-8' + + charset = "utf-8" def __init__(self, environ, url_adapter): BaseRequest.__init__(self, environ) @@ -74,9 +83,8 @@ class ThreadedRequest(object): """ def __getattr__(self, name): - if name == '__members__': - return [x for x in dir(local.request) if not - x.startswith('_')] + if name == "__members__": + return [x for x in dir(local.request) if not x.startswith("_")] return getattr(local.request, name) def __setattr__(self, name, value): @@ -87,8 +95,9 @@ class Response(BaseResponse): """ The concrete response object for the WSGI application. """ - charset = 'utf-8' - default_mimetype = 'text/html' + + charset = "utf-8" + default_mimetype = "text/html" class TemplateResponse(Response): @@ -98,9 +107,7 @@ class TemplateResponse(Response): def __init__(self, template_name, **values): from coolmagic import helpers - values.update( - request=local.request, - h=helpers - ) + + values.update(request=local.request, h=helpers) template = template_env.get_template(template_name) Response.__init__(self, template.render(values)) diff --git a/examples/coolmagic/views/static.py b/examples/coolmagic/views/static.py index edb09372..f4f95409 100644 --- a/examples/coolmagic/views/static.py +++ b/examples/coolmagic/views/static.py @@ -11,22 +11,22 @@ from coolmagic.utils import export -@export('/', template='static/index.html') +@export("/", template="static/index.html") def index(): pass -@export('/about', template='static/about.html') +@export("/about", template="static/about.html") def about(): pass -@export('/broken') +@export("/broken") def broken(): - raise RuntimeError('that\'s really broken') + raise RuntimeError("that's really broken") -@export(None, template='static/not_found.html') +@export(None, template="static/not_found.html") def not_found(): """ This function is always executed if an url does not |
