Skip to content

Instantly share code, notes, and snippets.

@lonnen
Created December 21, 2011 22:34
Show Gist options
  • Save lonnen/1508027 to your computer and use it in GitHub Desktop.
Save lonnen/1508027 to your computer and use it in GitHub Desktop.
Deps Draft
import settings
class DepsResolver():
"""Resolves low level implementation from the config at the time the class
is instantiated by returning the low level class.
"""
def __new__(cls, name):
cap_name = name.capitalize()
key = "%sImplementationModule" % name
impl_loc = settings[key]
impl_module = __import__(impl_loc, globals(), locals(), [cap_name], -1)
impl_cls = get(cap_name, impl_module)
return impl_cls.__new__(cls)
class LazyDepsResolver():
"""Resolves low level implementation from the config at the time a method
is called by calling the low level method.
"""
def __init__(self, name):
self.name = name
def __getattr__(self, attr):
class Resolver:
"""Delay resolving the implementation until any arguments
potentially being passed to it can be inspected for an
implementation override.
"""
def __init__(self, attr):
self.attr = attr
def __call__(self, *args, **kwargs):
"""Inspects any (k)wargs for overrides, and finally resolves
which component should be used to implement thi attribute.
"""
name = kwargs.get("force_api_impl", self.name)
impl = DepsResolver(name)
impl.__getattribute__(self.attr)(*args, **kwargs)
return Resolver(attr)
class WebService(LazyDepsResolver):
"""Add the URI field so our web framework to knows when to dispatch a
request.
"""
def __init__(self, name, uri):
super(SocorroWebService, self).__init__(name)
self.uri = uri
# internal use example:
# finding current products
product_internal = DepsResolver(name = "product")
# web service example:
# create the search service
search_service = WebService(name = "search", uri = "/search/([^/.]*)/(.*)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment