Debian Codegraph @

pecan/__init__.py

raw
1 from .core import (
2 abort, override_template, Pecan, Request, Response, load_app,
3 redirect, render, request, response
4 )
5 from .decorators import expose
6 from .hooks import RequestViewerHook
7
8 from .middleware.debug import DebugMiddleware
9 from .middleware.errordocument import ErrorDocumentMiddleware
10 from .middleware.recursive import RecursiveMiddleware
11 from .middleware.static import StaticFileMiddleware
12 from .routing import route
13
14 from .configuration import set_config, Config
15 from .configuration import _runtime_conf as conf
16 from . import middleware
17
18 from logging import captureWarnings
19 from logging.config import dictConfig
20
21 import warnings
22
23
24 __all__ = [
25 'make_app', 'load_app', 'Pecan', 'Request', 'Response', 'request',
26 'response', 'override_template', 'expose', 'conf', 'set_config', 'render',
27 'abort', 'redirect', 'route'
28 ]
29
30
31 def make_app(root, **kw):
32 '''
33 Utility for creating the Pecan application object. This function should
34 generally be called from the ``setup_app`` function in your project's
35 ``app.py`` file.
36
37 :param root: A string representing a root controller object (e.g.,
38 "myapp.controller.root.RootController")
39 :param static_root: The relative path to a directory containing static
40 files. Serving static files is only enabled when
41 debug mode is set.
42 :param debug: A flag to enable debug mode. This enables the debug
43 middleware and serving static files.
44 :param wrap_app: A function or middleware class to wrap the Pecan app.
45 This must either be a wsgi middleware class or a
46 function that returns a wsgi application. This wrapper
47 is applied first before wrapping the application in
48 other middlewares such as Pecan's debug middleware.
49 This should be used if you want to use middleware to
50 perform authentication or intercept all requests before
51 they are routed to the root controller.
52 :param logging: A dictionary used to configure logging. This uses
53 ``logging.config.dictConfig``.
54
55 All other keyword arguments are passed in to the Pecan app constructor.
56
57 :returns: a ``Pecan`` object.
58 '''
59 # Pass logging configuration (if it exists) on to the Python logging module
60 logging = kw.get('logging', {})
61 debug = kw.get('debug', False)
62 if logging:
63 if debug:
64 captureWarnings(True)
65 warnings.simplefilter("default", DeprecationWarning)
66
67 if isinstance(logging, Config):
68 logging = logging.to_dict()
69 if 'version' not in logging:
70 logging['version'] = 1
71 dictConfig(logging)
72
73 # Instantiate the WSGI app by passing **kw onward
74 app = Pecan(root, **kw)
75
76 # Optionally wrap the app in another WSGI app
77 wrap_app = kw.get('wrap_app', None)
78 if wrap_app:
79 app = wrap_app(app)
80
81 # Configuration for serving custom error messages
82 errors = kw.get('errors', getattr(conf.app, 'errors', {}))
83 if errors:
84 app = middleware.errordocument.ErrorDocumentMiddleware(app, errors)
85
86 # Included for internal redirect support
87 app = middleware.recursive.RecursiveMiddleware(app)
88
89 # When in debug mode, load exception debugging middleware
90 static_root = kw.get('static_root', None)
91 if debug:
92 debug_kwargs = getattr(conf, 'debug', {})
93 debug_kwargs.setdefault('context_injectors', []).append(
94 lambda environ: {
95 'request': environ.get('pecan.locals', {}).get('request')
96 }
97 )
98 app = DebugMiddleware(
99 app,
100 **debug_kwargs
101 )
102
103 # Support for serving static files (for development convenience)
104 if static_root:
105 app = middleware.static.StaticFileMiddleware(app, static_root)
106
107 elif static_root:
108 warnings.warn(
109 "`static_root` is only used when `debug` is True, ignoring",
110 RuntimeWarning
111 )
112
113 if hasattr(conf, 'requestviewer'):
114 warnings.warn(''.join([
115 "`pecan.conf.requestviewer` is deprecated. To apply the ",
116 "`RequestViewerHook` to your application, add it to ",
117 "`pecan.conf.app.hooks` or manually in your project's `app.py` ",
118 "file."]),
119 DeprecationWarning
120 )
121
122 return app