diff options
author | Ryan Petrello <lists@ryanpetrello.com> | 2012-03-13 14:16:08 -0700 |
---|---|---|
committer | Ryan Petrello <lists@ryanpetrello.com> | 2012-03-13 16:21:44 -0700 |
commit | bded674abd5afbf2bb10b30fa820d7002a0b42f4 (patch) | |
tree | 47c912f7b1b1bec1f1696f00b833195481c81743 /pecan | |
parent | 02a80731c48feb814dce727dd95db1510a7eb08e (diff) | |
download | pecan-bded674abd5afbf2bb10b30fa820d7002a0b42f4.tar.gz |
Cleaning up cruft, simplifying default config, and improving docs.
Diffstat (limited to 'pecan')
-rw-r--r-- | pecan/__init__.py | 18 | ||||
-rw-r--r-- | pecan/configuration.py | 10 | ||||
-rw-r--r-- | pecan/core.py | 41 | ||||
-rw-r--r-- | pecan/deploy.py | 4 | ||||
-rw-r--r-- | pecan/templates/project/+package+/app.py_tmpl | 6 | ||||
-rw-r--r-- | pecan/templates/project/config.py_tmpl | 2 | ||||
-rw-r--r-- | pecan/tests/templates/error_for.html | 1 | ||||
-rw-r--r-- | pecan/tests/test_conf.py | 13 | ||||
-rw-r--r-- | pecan/tests/test_util.py | 12 | ||||
-rw-r--r-- | pecan/util.py | 25 |
10 files changed, 22 insertions, 110 deletions
diff --git a/pecan/__init__.py b/pecan/__init__.py index 8f95f28..2828e1b 100644 --- a/pecan/__init__.py +++ b/pecan/__init__.py @@ -1,6 +1,3 @@ -''' -''' - from paste.cascade import Cascade from paste.errordocument import make_errordocument from paste.recursive import RecursiveMiddleware @@ -11,7 +8,7 @@ from weberror.evalexception import EvalException from core import ( abort, override_template, Pecan, load_app, redirect, render, - request, response, ValidationException + request, response ) from decorators import expose from hooks import RequestViewerHook @@ -29,18 +26,21 @@ __all__ = [ def make_app(root, static_root=None, debug=False, errorcfg={}, wrap_app=None, logging=False, **kw): - ''' - ''' + # 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 + # Instantiate the WSGI app by passing **kw onward app = Pecan(root, **kw) + # Optionally wrap the app in another WSGI app if wrap_app: app = wrap_app(app) + # Included for internal redirect support app = RecursiveMiddleware(app) + # Support for interactive debugging (and error reporting) if debug: app = EvalException( app, @@ -49,9 +49,13 @@ def make_app(root, static_root=None, debug=False, errorcfg={}, ) else: app = ErrorMiddleware(app, **errorcfg) - app = make_errordocument(app, conf, **dict(conf.app.errors)) + # Configuration for serving custom error messages + if hasattr(conf.app, 'errors'): + app = make_errordocument(app, conf, **dict(conf.app.errors)) + # Support for serving static files (for development convenience) if static_root: app = Cascade([StaticURLParser(static_root), app]) + # Support for simple Apache-style logs if isinstance(logging, dict) or logging == True: app = TransLogger(app, **(isinstance(logging, dict) and logging or {})) return app diff --git a/pecan/configuration.py b/pecan/configuration.py index bc4b757..a903ac8 100644 --- a/pecan/configuration.py +++ b/pecan/configuration.py @@ -17,13 +17,7 @@ DEFAULT = { 'root': None, 'modules': [], 'static_root': 'public', - 'template_path': '', - 'debug': False, - 'logging': False, - 'force_canonical': True, - 'errors': { - '__force_dict__': True - } + 'template_path': '' } } @@ -190,7 +184,7 @@ def initconf(): def set_config(config, overwrite=False): ''' - Updates the global configuration a filename. + Updates the global configuration. :param config: Can be a dictionary containing configuration, or a string which diff --git a/pecan/core.py b/pecan/core.py index 261c83d..ce71260 100644 --- a/pecan/core.py +++ b/pecan/core.py @@ -1,7 +1,7 @@ from configuration import _runtime_conf, set_config from templating import RendererFactory from routing import lookup_controller, NonCanonicalPath -from util import _cfg, splitext, encode_if_needed +from util import _cfg, encode_if_needed from webob import Request, Response, exc from threading import local @@ -9,6 +9,7 @@ from itertools import chain from mimetypes import guess_type, add_type from paste.recursive import ForwardRequestException from urlparse import urlsplit, urlunsplit +from os.path import splitext try: from simplejson import loads @@ -116,17 +117,6 @@ def redirect(location=None, internal=False, code=None, headers={}, raise exc.status_map[code](location=location, headers=headers) -def error_for(field): - ''' - A convenience function for fetching the validation error for a - particular field in a form. - - :param field: The name of the field to get the error for. - ''' - - return request.pecan['validation_errors'].get(field, '') - - def render(template, namespace): ''' Render the specified template using the Pecan rendering framework @@ -142,31 +132,6 @@ def render(template, namespace): return state.app.render(template, namespace) -class ValidationException(ForwardRequestException): - ''' - This exception is raised when a validation error occurs using Pecan's - built-in validation framework. - ''' - - def __init__(self, location=None, errors={}): - if state.controller is not None: - cfg = _cfg(state.controller) - else: - cfg = {} - if location is None and 'error_handler' in cfg: - location = cfg['error_handler'] - if callable(location): - location = location() - if 'pecan.params' not in request.environ: - request.environ['pecan.params'] = dict(request.params) - request.environ[ - 'pecan.validation_errors' - ] = request.pecan['validation_errors'] - request.environ['REQUEST_METHOD'] = 'GET' - request.environ['pecan.validation_redirected'] = True - ForwardRequestException.__init__(self, location) - - def load_app(config): ''' Used to load a ``Pecan`` application and its environment based on passed @@ -383,8 +348,6 @@ class Pecan(object): ) if template == 'json': renderer = self.renderers.get('json', self.template_path) - else: - namespace['error_for'] = error_for if ':' in template: renderer = self.renderers.get( template.split(':')[0], diff --git a/pecan/deploy.py b/pecan/deploy.py index 3559e99..08019f9 100644 --- a/pecan/deploy.py +++ b/pecan/deploy.py @@ -2,4 +2,8 @@ from core import load_app def deploy(config): + """ + Given a config (dictionary of relative filename), returns a configured + WSGI app. + """ return load_app(config) diff --git a/pecan/templates/project/+package+/app.py_tmpl b/pecan/templates/project/+package+/app.py_tmpl index 02b554f..2b2ec7e 100644 --- a/pecan/templates/project/+package+/app.py_tmpl +++ b/pecan/templates/project/+package+/app.py_tmpl @@ -8,8 +8,8 @@ def setup_app(config): return make_app( config.app.root, static_root = config.app.static_root, - debug = config.app.debug, - logging = config.app.logging, template_path = config.app.template_path, - force_canonical = config.app.force_canonical + debug = getattr(config.app, 'debug', None), + logging = getattr(config.app, 'logging', None), + force_canonical = getattr(config.app, 'force_canonical', None) ) diff --git a/pecan/templates/project/config.py_tmpl b/pecan/templates/project/config.py_tmpl index 2aa3a65..081060b 100644 --- a/pecan/templates/project/config.py_tmpl +++ b/pecan/templates/project/config.py_tmpl @@ -12,8 +12,6 @@ app = { 'template_path' : '%(confdir)s/${package}/templates', 'reload' : True, 'debug' : True, - 'logging' : False, - 'force_canonical' : True, 'errors' : { '404' : '/error/404', '__force_dict__' : True diff --git a/pecan/tests/templates/error_for.html b/pecan/tests/templates/error_for.html deleted file mode 100644 index fcd47b6..0000000 --- a/pecan/tests/templates/error_for.html +++ /dev/null @@ -1 +0,0 @@ -${error_for('colors-1')}
\ No newline at end of file diff --git a/pecan/tests/test_conf.py b/pecan/tests/test_conf.py index 3df8c8f..459bd02 100644 --- a/pecan/tests/test_conf.py +++ b/pecan/tests/test_conf.py @@ -23,7 +23,6 @@ class TestConf(TestCase): 'test_config/config.py' ))) - self.assertTrue(conf.app.debug) self.assertEqual(conf.app.root, None) self.assertEqual(conf.app.template_path, 'myproject/templates') self.assertEqual(conf.app.static_root, 'public') @@ -41,7 +40,6 @@ class TestConf(TestCase): 'test_config/empty.py' ))) - self.assertFalse(conf.app.debug) self.assertEqual(conf.app.root, None) self.assertEqual(conf.app.template_path, '') self.assertEqual(conf.app.static_root, 'public') @@ -58,7 +56,6 @@ class TestConf(TestCase): 'test_config/forcedict.py' ))) - self.assertFalse(conf.app.debug) self.assertEqual(conf.app.root, None) self.assertEqual(conf.app.template_path, '') self.assertEqual(conf.app.static_root, 'public') @@ -99,7 +96,6 @@ class TestConf(TestCase): os.path.dirname(__file__), 'test_config', 'config.py' ) conf = configuration.conf_from_file(path) - self.assertTrue(conf.app.debug) def test_config_illegal_ids(self): from pecan import configuration @@ -197,9 +193,6 @@ class TestConf(TestCase): assert isinstance(as_dict, dict) assert as_dict['server']['host'] == '0.0.0.0' assert as_dict['server']['port'] == '8080' - assert as_dict['app']['debug'] == False - assert as_dict['app']['errors'] == {} - assert as_dict['app']['force_canonical'] == True assert as_dict['app']['modules'] == [] assert as_dict['app']['root'] == None assert as_dict['app']['static_root'] == 'public' @@ -217,9 +210,6 @@ class TestConf(TestCase): assert isinstance(as_dict, dict) assert as_dict['server']['host'] == '0.0.0.0' assert as_dict['server']['port'] == '8080' - assert as_dict['app']['debug'] == False - assert as_dict['app']['errors'] == {} - assert as_dict['app']['force_canonical'] == True assert as_dict['app']['modules'] == [] assert as_dict['app']['root'] == None assert as_dict['app']['static_root'] == 'public' @@ -238,9 +228,6 @@ class TestConf(TestCase): assert isinstance(as_dict, dict) assert as_dict['prefix_server']['prefix_host'] == '0.0.0.0' assert as_dict['prefix_server']['prefix_port'] == '8080' - assert as_dict['prefix_app']['prefix_debug'] == False - assert as_dict['prefix_app']['prefix_errors'] == {} - assert as_dict['prefix_app']['prefix_force_canonical'] == True assert as_dict['prefix_app']['prefix_modules'] == [] assert as_dict['prefix_app']['prefix_root'] == None assert as_dict['prefix_app']['prefix_static_root'] == 'public' diff --git a/pecan/tests/test_util.py b/pecan/tests/test_util.py deleted file mode 100644 index 3294eec..0000000 --- a/pecan/tests/test_util.py +++ /dev/null @@ -1,12 +0,0 @@ -from pecan.util import compat_splitext - - -def test_compat_splitext(): - assert ('foo', '.bar') == compat_splitext('foo.bar') - assert ('/foo/bar', '.txt') == compat_splitext('/foo/bar.txt') - assert ('/foo/bar', '') == compat_splitext('/foo/bar') - assert ('.bashrc', '') == compat_splitext('.bashrc') - assert ('..bashrc', '') == compat_splitext('..bashrc') - assert ('/.bashrc', '') == compat_splitext('/.bashrc') - assert ('/foo.bar/.bashrc', '') == compat_splitext('/foo.bar/.bashrc') - assert ('/foo.js', '.js') == compat_splitext('/foo.js.js') diff --git a/pecan/util.py b/pecan/util.py index 7f29a91..697dec8 100644 --- a/pecan/util.py +++ b/pecan/util.py @@ -12,31 +12,6 @@ def _cfg(f): return f._pecan -def compat_splitext(path): - """ - This method emulates the behavior os.path.splitext introduced in python 2.6 - """ - basename = os.path.basename(path) - - index = basename.rfind('.') - if index > 0: - root = basename[:index] - if root.count('.') != index: - return ( - os.path.join(os.path.dirname(path), root), - basename[index:] - ) - - return (path, '') - - -# use the builtin splitext unless we're python 2.5 -if sys.version_info >= (2, 6): - from os.path import splitext -else: # pragma no cover - splitext = compat_splitext # noqa - - if sys.version_info >= (2, 6, 5): def encode_if_needed(s): return s |