diff options
author | Alex Gr?nholm <alex.gronholm@nextday.fi> | 2011-05-17 03:17:44 +0300 |
---|---|---|
committer | Alex Gr?nholm <alex.gronholm@nextday.fi> | 2011-05-17 03:17:44 +0300 |
commit | 1133cba301b8bc90507b9f98d4d71d45dcaa53d6 (patch) | |
tree | f5e5488d25daaa2f519165c470888ebe3e0c1ace | |
parent | 93822ede9b74c40d170644fb2417b7a7d40dbb3e (diff) | |
download | pastedeploy-1133cba301b8bc90507b9f98d4d71d45dcaa53d6.tar.gz |
PEP 8 cleanups
-rw-r--r-- | paste/deploy/config.py | 34 | ||||
-rw-r--r-- | paste/deploy/converters.py | 6 | ||||
-rw-r--r-- | paste/deploy/epdesc.py | 5 | ||||
-rw-r--r-- | paste/deploy/interfaces.py | 22 | ||||
-rw-r--r-- | paste/deploy/loadwsgi.py | 34 | ||||
-rw-r--r-- | paste/deploy/paster_templates.py | 11 | ||||
-rw-r--r-- | paste/deploy/util.py | 4 |
7 files changed, 89 insertions, 27 deletions
diff --git a/paste/deploy/config.py b/paste/deploy/config.py index 562a61f..1db5680 100644 --- a/paste/deploy/config.py +++ b/paste/deploy/config.py @@ -3,12 +3,14 @@ """Paste Configuration Middleware and Objects""" import threading import re + # Loaded lazily wsgilib = None local = None __all__ = ['DispatchingConfig', 'CONFIG', 'ConfigMiddleware', 'PrefixMiddleware'] + def local_dict(): global config_local, local try: @@ -21,6 +23,7 @@ def local_dict(): config_local.wsgi_dict = result = {} return result + class DispatchingConfig(object): """ @@ -44,7 +47,7 @@ class DispatchingConfig(object): self.dispatching_id = 0 while 1: self._local_key = 'paste.processconfig_%i' % self.dispatching_id - if not local_dict().has_key(self._local_key): + if not self._local_key in local_dict(): break self.dispatching_id += 1 finally: @@ -93,7 +96,7 @@ class DispatchingConfig(object): def pop_process_config(self, conf=None): self._pop_from(self._process_configs, conf) - + def __getattr__(self, attr): conf = self.current_conf() if conf is None: @@ -122,7 +125,7 @@ class DispatchingConfig(object): def __contains__(self, key): # I thought __getattr__ would catch this, but apparently not - return self.has_key(key) + return key in self def __setitem__(self, key, value): # I thought __getattr__ would catch this, but apparently not @@ -131,6 +134,7 @@ class DispatchingConfig(object): CONFIG = DispatchingConfig() + class ConfigMiddleware(object): """ @@ -155,7 +159,7 @@ class ConfigMiddleware(object): from paste import wsgilib popped_config = None if 'paste.config' in environ: - popped_config = environ['paste.config'] + popped_config = environ['paste.config'] conf = environ['paste.config'] = self.config.copy() app_iter = None CONFIG.push_thread_config(conf) @@ -181,6 +185,7 @@ class ConfigMiddleware(object): new_app_iter = wsgilib.add_close(app_iter, close_config) return new_app_iter + def make_config_filter(app, global_conf, **local_conf): conf = global_conf.copy() conf.update(local_conf) @@ -188,16 +193,17 @@ def make_config_filter(app, global_conf, **local_conf): make_config_middleware = ConfigMiddleware.__doc__ + class PrefixMiddleware(object): """Translate a given prefix into a SCRIPT_NAME for the filtered application. - - PrefixMiddleware provides a way to manually override the root prefix + + PrefixMiddleware provides a way to manually override the root prefix (SCRIPT_NAME) of your application for certain, rare situations. - When running an application under a prefix (such as '/james') in + When running an application under a prefix (such as '/james') in FastCGI/apache, the SCRIPT_NAME environment variable is automatically - set to to the appropriate value: '/james'. Pylons' URL generating + set to to the appropriate value: '/james'. Pylons' URL generating functions, such as url_for, always take the SCRIPT_NAME value into account. One situation where PrefixMiddleware is required is when an application @@ -213,9 +219,9 @@ class PrefixMiddleware(object): To filter your application through a PrefixMiddleware instance, add the following to the '[app:main]' section of your .ini file: - + .. code-block:: ini - + filter-with = proxy-prefix [filter:proxy-prefix] @@ -227,7 +233,7 @@ class PrefixMiddleware(object): Also, unless disabled, the ``X-Forwarded-Server`` header will be translated to the ``Host`` header, for cases when that header is - lost in the proxying. Also ``X-Forwarded-Host``, + lost in the proxying. Also ``X-Forwarded-Host``, ``X-Forwarded-Scheme``, and ``X-Forwarded-Proto`` are translated. If ``force_port`` is set, SERVER_PORT and HTTP_HOST will be @@ -249,11 +255,12 @@ class PrefixMiddleware(object): self.regprefix = re.compile("^%s(.*)$" % self.prefix) self.force_port = force_port self.scheme = scheme - + def __call__(self, environ, start_response): url = environ['PATH_INFO'] url = re.sub(self.regprefix, r'\1', url) - if not url: url = '/' + if not url: + url = '/' environ['PATH_INFO'] = url environ['SCRIPT_NAME'] = self.prefix if self.translate_forwarded_server: @@ -283,6 +290,7 @@ class PrefixMiddleware(object): environ['wsgi.url_scheme'] = self.scheme return self.app(environ, start_response) + def make_prefix_middleware( app, global_conf, prefix='/', translate_forwarded_server=True, diff --git a/paste/deploy/converters.py b/paste/deploy/converters.py index 98edd8f..f852e6c 100644 --- a/paste/deploy/converters.py +++ b/paste/deploy/converters.py @@ -1,5 +1,7 @@ # (c) 2005 Ian Bicking and contributors; written for Paste (http://pythonpaste.org) # Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php + + def asbool(obj): if isinstance(obj, (str, unicode)): obj = obj.strip().lower() @@ -12,13 +14,15 @@ def asbool(obj): "String is not true/false: %r" % obj) return bool(obj) + def asint(obj): try: return int(obj) - except (TypeError, ValueError), e: + except (TypeError, ValueError): raise ValueError( "Bad integer value: %r" % obj) + def aslist(obj, sep=None, strip=True): if isinstance(obj, (str, unicode)): lst = obj.split(sep) diff --git a/paste/deploy/epdesc.py b/paste/deploy/epdesc.py index 7c7deae..5f05175 100644 --- a/paste/deploy/epdesc.py +++ b/paste/deploy/epdesc.py @@ -3,6 +3,7 @@ class AppFactoryDescription(object): This gives a factory/function that can create WSGI apps """ + class CompositeFactoryDescription(object): description = """ This gives a factory/function that can create WSGI apps, and has @@ -10,12 +11,14 @@ class CompositeFactoryDescription(object): apps based on name. """ + class FilterAppFactoryDescription(object): description = """ This gives a factory/function that wraps a WSGI application to create another WSGI application (typically applying middleware) """ + class FilterFactoryDescription(object): description = """ This gives a factory/function that return a function that can wrap @@ -23,6 +26,7 @@ class FilterFactoryDescription(object): paste.filter_app_factory is the same thing with less layers. """ + class ServerFactoryDescription(object): description = """ This gives a factory/function that creates a server, that can be @@ -30,6 +34,7 @@ class ServerFactoryDescription(object): paste.server_runner is the same thing with less layers. """ + class ServerRunnerDescription(object): description = """ This gives a factory/function that, given a WSGI application and diff --git a/paste/deploy/interfaces.py b/paste/deploy/interfaces.py index 29163a5..3dbc44e 100644 --- a/paste/deploy/interfaces.py +++ b/paste/deploy/interfaces.py @@ -1,13 +1,15 @@ # (c) 2005 Ian Bicking and contributors; written for Paste (http://pythonpaste.org) # Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php + ############################################################ ## Functions ############################################################ + def loadapp(uri, name=None, relative_to=None, global_conf=None): """ Provided by ``paste.deploy.loadapp``. - + Load the specified URI as a WSGI application (returning IWSGIApp). The ``name`` can be in the URI (typically as ``#name``). If it is and ``name`` is given, the keyword argument overrides the URI. @@ -19,6 +21,7 @@ def loadapp(uri, name=None, relative_to=None, global_conf=None): override the values). ``global_conf`` is copied before modifying. """ + def loadfilter(uri, name=None, relative_to=None, global_conf=None): """ Provided by ``paste.deploy.loadfilter``. @@ -26,6 +29,7 @@ def loadfilter(uri, name=None, relative_to=None, global_conf=None): Like ``loadapp()``, except returns in IFilter object. """ + def loadserver(uri, name=None, relative_to=None, global_conf=None): """ Provided by ``paste.deploy.loadserver``. @@ -33,10 +37,12 @@ def loadserver(uri, name=None, relative_to=None, global_conf=None): Like ``loadapp()``, except returns in IServer object. """ + ############################################################ ## Factories ############################################################ + class IPasteAppFactory(object): """ @@ -55,6 +61,7 @@ class IPasteAppFactory(object): capture these values). """ + class IPasteCompositFactory(object): """ @@ -73,6 +80,7 @@ class IPasteCompositFactory(object): applications. """ + class IPasteFilterFactory(object): """ @@ -85,13 +93,14 @@ class IPasteFilterFactory(object): Returns a IFilter object. """ + class IPasteFilterAppFactory(object): """ This is the spec for the ``paste.filter_app_factory`` protocol/entry_point. """ - + def __call__(wsgi_app, global_conf, **local_conf): """ Returns a WSGI application that wraps ``wsgi_app``. @@ -100,6 +109,7 @@ class IPasteFilterAppFactory(object): objects that implement the IFilter interface. """ + class IPasteServerFactory(object): """ @@ -112,6 +122,7 @@ class IPasteServerFactory(object): Returns a IServer object. """ + class IPasteServerRunner(object): """ @@ -129,6 +140,7 @@ class IPasteServerRunner(object): objects that implement the IServer interface. """ + class ILoader(object): """ @@ -151,16 +163,18 @@ class ILoader(object): """ Return an IFilter object, like ``get_app``. """ - + def get_server(name_or_uri, global_conf=None): """ Return an IServer object, like ``get_app``. """ + ############################################################ ## Objects ############################################################ + class IWSGIApp(object): """ @@ -175,6 +189,7 @@ class IWSGIApp(object): an iterator for the body of the response. """ + class IFilter(object): """ @@ -188,6 +203,7 @@ class IFilter(object): ``wsgi_app`` passed in. """ + class IServer(object): """ diff --git a/paste/deploy/loadwsgi.py b/paste/deploy/loadwsgi.py index 306a8ef..a4a8b4b 100644 --- a/paste/deploy/loadwsgi.py +++ b/paste/deploy/loadwsgi.py @@ -11,12 +11,15 @@ from paste.deploy.util import fix_call __all__ = ['loadapp', 'loadserver', 'loadfilter', 'appconfig'] + ############################################################ ## Utility functions ############################################################ + def import_string(s): - return pkg_resources.EntryPoint.parse("x="+s).load(False) + return pkg_resources.EntryPoint.parse("x=" + s).load(False) + def _aslist(obj): """ @@ -30,6 +33,7 @@ def _aslist(obj): else: return [obj] + def _flatten(lst): """ Flatten a nested list. @@ -41,6 +45,7 @@ def _flatten(lst): result.extend(_flatten(item)) return result + class NicerConfigParser(ConfigParser): def __init__(self, filename, *args, **kw): @@ -69,10 +74,12 @@ class NicerConfigParser(ConfigParser): e.args = tuple(args) raise + ############################################################ ## Object types ############################################################ + class _ObjectType(object): name = None @@ -93,6 +100,7 @@ class _ObjectType(object): return fix_call(context.object, context.global_conf, **context.local_conf) + class _App(_ObjectType): name = 'application' @@ -114,6 +122,7 @@ class _App(_ObjectType): APP = _App() + class _Filter(_ObjectType): name = 'filter' egg_protocols = [['paste.filter_factory', 'paste.filter_app_factory']] @@ -135,6 +144,7 @@ class _Filter(_ObjectType): FILTER = _Filter() + class _Server(_ObjectType): name = 'server' egg_protocols = [['paste.server_factory', 'paste.server_runner']] @@ -156,6 +166,7 @@ class _Server(_ObjectType): SERVER = _Server() + # Virtual type: (@@: There's clearly something crufty here; # this probably could be more elegant) class _PipeLine(_ObjectType): @@ -171,6 +182,7 @@ class _PipeLine(_ObjectType): PIPELINE = _PipeLine() + class _FilterApp(_ObjectType): name = 'filter_app' @@ -181,6 +193,7 @@ class _FilterApp(_ObjectType): FILTER_APP = _FilterApp() + class _FilterWith(_App): name = 'filtered_with' @@ -197,19 +210,24 @@ class _FilterWith(_App): FILTER_WITH = _FilterWith() + ############################################################ ## Loaders ############################################################ + def loadapp(uri, name=None, **kw): return loadobj(APP, uri, name=name, **kw) + def loadfilter(uri, name=None, **kw): return loadobj(FILTER, uri, name=name, **kw) + def loadserver(uri, name=None, **kw): return loadobj(SERVER, uri, name=name, **kw) + def appconfig(uri, name=None, relative_to=None, global_conf=None): context = loadcontext(APP, uri, name=name, relative_to=relative_to, @@ -218,6 +236,7 @@ def appconfig(uri, name=None, relative_to=None, global_conf=None): _loaders = {} + def loadobj(object_type, uri, name=None, relative_to=None, global_conf=None): context = loadcontext( @@ -225,6 +244,7 @@ def loadobj(object_type, uri, name=None, relative_to=None, global_conf=global_conf) return context.create() + def loadcontext(object_type, uri, name=None, relative_to=None, global_conf=None): if '#' in uri: @@ -248,6 +268,7 @@ def loadcontext(object_type, uri, name=None, relative_to=None, uri, path, name=name, relative_to=relative_to, global_conf=global_conf) + def _loadconfig(object_type, uri, path, name, relative_to, global_conf): isabs = os.path.isabs(path) @@ -273,6 +294,7 @@ def _loadconfig(object_type, uri, path, name, relative_to, _loaders['config'] = _loadconfig + def _loadegg(object_type, uri, spec, name, relative_to, global_conf): loader = EggLoader(spec) @@ -280,10 +302,12 @@ def _loadegg(object_type, uri, spec, name, relative_to, _loaders['egg'] = _loadegg + ############################################################ ## Loaders ############################################################ + class _Loader(object): def get_app(self, name=None, global_conf=None): @@ -311,6 +335,7 @@ class _Loader(object): SERVER, name=name, global_conf=global_conf) _absolute_re = re.compile(r'^[a-zA-Z]+:') + def absolute_name(self, name): """ Returns true if the name includes a scheme @@ -319,6 +344,7 @@ class _Loader(object): return False return self._absolute_re.search(name) + class ConfigLoader(_Loader): def __init__(self, filename): @@ -536,8 +562,8 @@ class ConfigLoader(_Loader): found.append(name_prefix) name = 'main' for section in sections: - if section.startswith(name_prefix+':'): - if section[len(name_prefix)+1:].strip() == name: + if section.startswith(name_prefix + ':'): + if section[len(name_prefix) + 1:].strip() == name: found.append(section) return found @@ -598,6 +624,7 @@ class EggLoader(_Loader): % (name, self.spec, ', '.join(_flatten(protocol_options)))) return possible[0] + class LoaderContext(object): def __init__(self, obj, object_type, protocol, @@ -626,6 +653,7 @@ class LoaderContext(object): conf.context = self return conf + class AttrDict(dict): """ A dictionary that can be assigned to. diff --git a/paste/deploy/paster_templates.py b/paste/deploy/paster_templates.py index 4434898..25be21b 100644 --- a/paste/deploy/paster_templates.py +++ b/paste/deploy/paster_templates.py @@ -1,15 +1,17 @@ # (c) 2005 Ian Bicking and contributors; written for Paste (http://pythonpaste.org) # Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php import os + from paste.script.templates import Template + class PasteDeploy(Template): _template_dir = 'paster_templates/paste_deploy' summary = "A web application deployed through paste.deploy" - + egg_plugins = ['PasteDeploy'] - + required_templates = ['PasteScript#basic_package'] def post(self, command, output_dir, vars): @@ -26,8 +28,7 @@ class PasteDeploy(Template): ' main = %(package)s.wsgiapp:make_app\n') % vars, indent=False) if command.verbose: - print '*'*72 + print '*' * 72 print '* Run "paster serve docs/devel_config.ini" to run the sample application' print '* on http://localhost:8080' - print '*'*72 - + print '*' * 72 diff --git a/paste/deploy/util.py b/paste/deploy/util.py index 86c6972..41f8dbd 100644 --- a/paste/deploy/util.py +++ b/paste/deploy/util.py @@ -9,7 +9,7 @@ def fix_type_error(exc_info, callable, varargs, kwargs): Given an exception, this will test if the exception was due to a signature error, and annotate the error with better information if so. - + Usage:: try: @@ -42,7 +42,7 @@ def fix_type_error(exc_info, callable, varargs, kwargs): def _short_repr(v): v = repr(v) if len(v) > 12: - v = v[:8]+'...'+v[-4:] + v = v[:8] + '...' + v[-4:] return v |