From 963ece6feac200151b35df2efa0df4b1c75f1763 Mon Sep 17 00:00:00 2001 From: Todd Willey Date: Thu, 6 Jan 2011 13:18:17 -0500 Subject: Add factories into the wsgi classes. --- nova/wsgi.py | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) (limited to 'nova/wsgi.py') diff --git a/nova/wsgi.py b/nova/wsgi.py index b5d6b96c1..5ecc21eed 100644 --- a/nova/wsgi.py +++ b/nova/wsgi.py @@ -63,10 +63,20 @@ class Server(object): class Application(object): -# TODO(gundlach): I think we should toss this class, now that it has no -# purpose. """Base WSGI application wrapper. Subclasses need to implement __call__.""" + @classmethod + def factory(cls, global_config, **local_config): + """Used for paste app factories in paste.deploy config fles.""" + rv = cls() + 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 + def __call__(self, environ, start_response): r"""Subclasses will probably want to implement __call__ like this: @@ -111,6 +121,20 @@ class Middleware(Application): behavior. """ + @classmethod + def factory(cls, global_config, **local_config): + """Used for paste app factories in paste.deploy config fles.""" + def _factory(app): + rv = cls(app) + 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 + def __init__(self, application): # pylint: disable-msg=W0231 self.application = application -- cgit From 4e034f3d69c6aba6920dd7dd38e07aeb495b45db Mon Sep 17 00:00:00 2001 From: Todd Willey Date: Thu, 6 Jan 2011 13:57:48 -0500 Subject: 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. --- nova/wsgi.py | 62 ++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 44 insertions(+), 18 deletions(-) (limited to 'nova/wsgi.py') diff --git a/nova/wsgi.py b/nova/wsgi.py index 5ecc21eed..aa8f315d6 100644 --- a/nova/wsgi.py +++ b/nova/wsgi.py @@ -67,15 +67,28 @@ class Application(object): @classmethod def factory(cls, global_config, **local_config): - """Used for paste app factories in paste.deploy config fles.""" - rv = cls() - 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 + """Used for paste app factories in paste.deploy config fles. + + Any local configuration (that is, values under the [app:APPNAME] + section of the paste config) will be passed into the `__init__` method + as kwargs. + + A hypothetical configuration would look like: + + [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): r"""Subclasses will probably want to implement __call__ like this: @@ -123,16 +136,29 @@ class Middleware(Application): @classmethod 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): - rv = cls(app) - 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 cls(app, **local_config) return _factory def __init__(self, application): # pylint: disable-msg=W0231 -- cgit From 500b268d0ef83b4770f9883690564e458cf94247 Mon Sep 17 00:00:00 2001 From: Todd Willey Date: Thu, 13 Jan 2011 18:57:29 -0500 Subject: pep8. Someday I'll remember 2 blank lines between module methods. --- nova/wsgi.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'nova/wsgi.py') diff --git a/nova/wsgi.py b/nova/wsgi.py index 817cd0478..b4cca9138 100644 --- a/nova/wsgi.py +++ b/nova/wsgi.py @@ -393,9 +393,10 @@ class Serializer(object): result.appendChild(node) return result + def paste_config_file(basename): """Find the best location in the system for a paste config file. - + Search Order ------------ @@ -424,6 +425,7 @@ def paste_config_file(basename): if os.path.exists(configfile): return configfile + def load_paste_configuration(filename, appname): """Returns a paste configuration dict, or None.""" filename = os.path.abspath(filename) @@ -434,6 +436,7 @@ def load_paste_configuration(filename, appname): pass return config + def load_paste_app(filename, appname): """Builds a wsgi app from a paste config, None if app not configured.""" filename = os.path.abspath(filename) @@ -444,8 +447,9 @@ def load_paste_app(filename, appname): pass return app + def paste_config_to_flags(config, mixins): - for k,v in mixins.iteritems(): + for k, v in mixins.iteritems(): value = config.get(k, v) converted_value = FLAGS[k].parser.Parse(value) setattr(FLAGS, k, converted_value) -- cgit