diff options
author | Ryan Petrello <lists@ryanpetrello.com> | 2013-08-08 14:16:36 -0400 |
---|---|---|
committer | Ryan Petrello <lists@ryanpetrello.com> | 2013-08-08 14:40:57 -0400 |
commit | fb2cf166aaf5b5af461879ec5b7a46806e8aee45 (patch) | |
tree | 55e258c72fc496b79d77c6d43e0750c752b89cec /pecan | |
parent | 38c64ff3518470ff668a06725abb4d2f2d5ab7ba (diff) | |
download | pecan-fb2cf166aaf5b5af461879ec5b7a46806e8aee45.tar.gz |
Make some notable changes to how ``pecan.conf.app`` is passed to a new app.
* Now pecan.conf.app items are treated as keywords passed directly to Pecan
apps. This means no more tinkering with *both* your configuration file *and*
your app.py file.
* Removed support for the RequestViewerHook config file shortcut (because now
you can easily specify hooks via configuration).
Diffstat (limited to 'pecan')
-rw-r--r-- | pecan/__init__.py | 22 | ||||
-rw-r--r-- | pecan/core.py | 21 | ||||
-rw-r--r-- | pecan/scaffolds/base/+package+/app.py_tmpl | 14 | ||||
-rw-r--r-- | pecan/tests/test_hooks.py | 29 |
4 files changed, 34 insertions, 52 deletions
diff --git a/pecan/__init__.py b/pecan/__init__.py index cf175b8..cf93e05 100644 --- a/pecan/__init__.py +++ b/pecan/__init__.py @@ -25,8 +25,7 @@ __all__ = [ ] -def make_app(root, static_root=None, logging={}, debug=False, - wrap_app=None, **kw): +def make_app(root, **kw): ''' Utility for creating the Pecan application object. This function should generally be called from the ``setup_app`` function in your project's @@ -37,8 +36,6 @@ def make_app(root, static_root=None, logging={}, debug=False, :param static_root: The relative path to a directory containing static files. Serving static files is only enabled when debug mode is set. - :param logging: A dictionary used to configure logging. This uses - ``logging.config.dictConfig``. :param debug: A flag to enable debug mode. This enables the debug middleware and serving static files. :param wrap_app: A function or middleware class to wrap the Pecan app. @@ -49,18 +46,15 @@ def make_app(root, static_root=None, logging={}, debug=False, This should be used if you want to use middleware to perform authentication or intercept all requests before they are routed to the root controller. + :param logging: A dictionary used to configure logging. This uses + ``logging.config.dictConfig``. All other keyword arguments are passed in to the Pecan app constructor. :returns: a ``Pecan`` object. ''' - # A shortcut for the RequestViewerHook middleware. - if hasattr(conf, 'requestviewer'): - existing_hooks = kw.get('hooks', []) - existing_hooks.append(RequestViewerHook(conf.requestviewer)) - kw['hooks'] = existing_hooks - # Pass logging configuration (if it exists) on to the Python logging module + logging = kw.get('logging', {}) if logging: if isinstance(logging, Config): logging = logging.to_dict() @@ -72,17 +66,21 @@ def make_app(root, static_root=None, logging={}, debug=False, app = Pecan(root, **kw) # Optionally wrap the app in another WSGI app + wrap_app = kw.get('wrap_app', None) if wrap_app: app = wrap_app(app) # Configuration for serving custom error messages - if hasattr(conf.app, 'errors'): - app = ErrorDocumentMiddleware(app, conf.app.errors) + errors = kw.get('errors', getattr(conf.app, 'errors', {})) + if errors: + app = ErrorDocumentMiddleware(app, errors) # Included for internal redirect support app = RecursiveMiddleware(app) # When in debug mode, load our exception dumping middleware + static_root = kw.get('static_root', None) + debug = kw.get('debug', False) if debug: app = DebugMiddleware(app) diff --git a/pecan/core.py b/pecan/core.py index c09763d..8f6e32f 100644 --- a/pecan/core.py +++ b/pecan/core.py @@ -173,11 +173,12 @@ class Pecan(object): :param root: A string representing a root controller object (e.g., "myapp.controller.root.RootController") - :param default_renderer: The default rendering engine to use. Defaults - to mako. - :param template_path: The default relative path to use for templates. - Defaults to 'templates'. - :param hooks: A list of Pecan hook objects to use for this application. + :param default_renderer: The default template rendering engine to use. + Defaults to mako. + :param template_path: A relative file system path (from the project root) + where template files live. Defaults to 'templates'. + :param hooks: A callable which returns a list of + :class:`pecan.hooks.PecanHook`s :param custom_renderers: Custom renderer objects, as a dictionary keyed by engine name. :param extra_template_vars: Any variables to inject into the template @@ -195,9 +196,9 @@ class Pecan(object): ) def __init__(self, root, default_renderer='mako', - template_path='templates', hooks=[], custom_renderers={}, - extra_template_vars={}, force_canonical=True, - guess_content_type_from_ext=True): + template_path='templates', hooks=lambda: [], + custom_renderers={}, extra_template_vars={}, + force_canonical=True, guess_content_type_from_ext=True, **kw): if isinstance(root, six.string_types): root = self.__translate_root__(root) @@ -205,7 +206,11 @@ class Pecan(object): self.root = root self.renderers = RendererFactory(custom_renderers, extra_template_vars) self.default_renderer = default_renderer + # pre-sort these so we don't have to do it per-request + if six.callable(hooks): + hooks = hooks() + self.hooks = list(sorted( hooks, key=operator.attrgetter('priority') diff --git a/pecan/scaffolds/base/+package+/app.py_tmpl b/pecan/scaffolds/base/+package+/app.py_tmpl index 191fc11..098deb4 100644 --- a/pecan/scaffolds/base/+package+/app.py_tmpl +++ b/pecan/scaffolds/base/+package+/app.py_tmpl @@ -5,16 +5,6 @@ from ${package} import model def setup_app(config): model.init_model() + app_conf = dict(config.app) - return make_app( - config.app.root, - static_root=config.app.static_root, - template_path=config.app.template_path, - logging=getattr(config, 'logging', {}), - debug=getattr(config.app, 'debug', False), - force_canonical=getattr(config.app, 'force_canonical', True), - guess_content_type_from_ext=getattr( - config.app, - 'guess_content_type_from_ext', - True), - ) + return make_app(app_conf.pop('root'), **app_conf) diff --git a/pecan/tests/test_hooks.py b/pecan/tests/test_hooks.py index 66947ca..9c3192e 100644 --- a/pecan/tests/test_hooks.py +++ b/pecan/tests/test_hooks.py @@ -1017,21 +1017,6 @@ class TestTransactionHook(PecanTestCase): class TestRequestViewerHook(PecanTestCase): - def test_hook_from_config(self): - from pecan.configuration import _runtime_conf as conf - conf['requestviewer'] = { - 'blacklist': ['/favicon.ico'] - } - - class RootController(object): - pass - - app = make_app(RootController()) - while hasattr(app, 'application'): - app = app.application - del conf.__values__['requestviewer'] - assert app.hooks - def test_basic_single_default_hook(self): _stdout = StringIO() @@ -1043,7 +1028,9 @@ class TestRequestViewerHook(PecanTestCase): app = TestApp( make_app( - RootController(), hooks=[RequestViewerHook(writer=_stdout)] + RootController(), hooks=lambda: [ + RequestViewerHook(writer=_stdout) + ] ) ) response = app.get('/') @@ -1074,7 +1061,9 @@ class TestRequestViewerHook(PecanTestCase): app = TestApp( make_app( - RootController(), hooks=[RequestViewerHook(writer=_stdout)] + RootController(), hooks=lambda: [ + RequestViewerHook(writer=_stdout) + ] ) ) response = app.get('/404', expect_errors=True) @@ -1104,7 +1093,7 @@ class TestRequestViewerHook(PecanTestCase): app = TestApp( make_app( RootController(), - hooks=[ + hooks=lambda: [ RequestViewerHook( config={'items': ['path']}, writer=_stdout ) @@ -1139,7 +1128,7 @@ class TestRequestViewerHook(PecanTestCase): app = TestApp( make_app( RootController(), - hooks=[ + hooks=lambda: [ RequestViewerHook( config={'blacklist': ['/']}, writer=_stdout ) @@ -1166,7 +1155,7 @@ class TestRequestViewerHook(PecanTestCase): app = TestApp( make_app( RootController(), - hooks=[ + hooks=lambda: [ RequestViewerHook( config={'items': ['date']}, writer=_stdout ) |