Remove module-level factory methods in favor of having a factory class-method
on wsgi components themselves. Local options from config are passed to the __init__ method of the component as kwargs.
This commit is contained in:
62
nova/wsgi.py
62
nova/wsgi.py
@@ -67,15 +67,28 @@ class Application(object):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def factory(cls, global_config, **local_config):
|
def factory(cls, global_config, **local_config):
|
||||||
"""Used for paste app factories in paste.deploy config fles."""
|
"""Used for paste app factories in paste.deploy config fles.
|
||||||
rv = cls()
|
|
||||||
for k,v in local_config.iteritems():
|
Any local configuration (that is, values under the [app:APPNAME]
|
||||||
if hasattr(rv, k):
|
section of the paste config) will be passed into the `__init__` method
|
||||||
setattr(rv, k, v)
|
as kwargs.
|
||||||
else:
|
|
||||||
logging.debug(_("Unknown local config option %s for %s"),
|
A hypothetical configuration would look like:
|
||||||
k, cls)
|
|
||||||
return rv
|
[app:wadl]
|
||||||
|
latest_version = 1.3
|
||||||
|
paste.app_factory = nova.api.fancy_api:Wadl.factory
|
||||||
|
|
||||||
|
which would result in a call to the `Wadl` class as
|
||||||
|
|
||||||
|
import nova.api.fancy_api
|
||||||
|
fancy_api.Wadl(latest_version='1.3')
|
||||||
|
|
||||||
|
You could of course re-implement the `factory` method in subclasses,
|
||||||
|
but using the kwarg passing it shouldn't be necessary.
|
||||||
|
|
||||||
|
"""
|
||||||
|
return cls(**local_config)
|
||||||
|
|
||||||
def __call__(self, environ, start_response):
|
def __call__(self, environ, start_response):
|
||||||
r"""Subclasses will probably want to implement __call__ like this:
|
r"""Subclasses will probably want to implement __call__ like this:
|
||||||
@@ -123,16 +136,29 @@ class Middleware(Application):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def factory(cls, global_config, **local_config):
|
def factory(cls, global_config, **local_config):
|
||||||
"""Used for paste app factories in paste.deploy config fles."""
|
"""Used for paste app factories in paste.deploy config fles.
|
||||||
|
|
||||||
|
Any local configuration (that is, values under the [filter:APPNAME]
|
||||||
|
section of the paste config) will be passed into the `__init__` method
|
||||||
|
as kwargs.
|
||||||
|
|
||||||
|
A hypothetical configuration would look like:
|
||||||
|
|
||||||
|
[filter:analytics]
|
||||||
|
redis_host = 127.0.0.1
|
||||||
|
paste.filter_factory = nova.api.analytics:Analytics.factory
|
||||||
|
|
||||||
|
which would result in a call to the `Analytics` class as
|
||||||
|
|
||||||
|
import nova.api.analytics
|
||||||
|
analytics.Analytics(app_from_paste, redis_host='127.0.0.1')
|
||||||
|
|
||||||
|
You could of course re-implement the `factory` method in subclasses,
|
||||||
|
but using the kwarg passing it shouldn't be necessary.
|
||||||
|
|
||||||
|
"""
|
||||||
def _factory(app):
|
def _factory(app):
|
||||||
rv = cls(app)
|
return cls(app, **local_config)
|
||||||
for k,v in local_config.iteritems():
|
|
||||||
if hasattr(rv, k):
|
|
||||||
setattr(rv, k, v)
|
|
||||||
else:
|
|
||||||
logging.debug(_("Unknown local config option %s for %s"),
|
|
||||||
k, cls)
|
|
||||||
return rv
|
|
||||||
return _factory
|
return _factory
|
||||||
|
|
||||||
def __init__(self, application): # pylint: disable-msg=W0231
|
def __init__(self, application): # pylint: disable-msg=W0231
|
||||||
|
|||||||
Reference in New Issue
Block a user