Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding a settings function

  • Loading branch information...
commit f5aafa3e560d3349adc7a7a9af51ee86db1ee24c 1 parent 8f4778a
Jason Baker authored
Showing with 30 additions and 16 deletions.
  1. +30 −16 shrapnel/config.py
View
46 shrapnel/config.py
@@ -9,21 +9,23 @@
import threading
-def instance(name, *args, **kwargs):
+def _split_name(name):
if '.' not in name:
name += ".__default__"
+ return name.split('.')
- cls_name, method_name = name.split('.')
-
- try:
- MyProvider = ProviderMetaclass._subclasses[cls_name]
- except KeyError:
- raise LookupError("Couldn't find provider class for {0}, tried {1}{2}ConfigurationProvider.".format(name, cls_name[0].upper(), cls_name[1:].lower()))
+def instance(name, *args, **kwargs):
+ cls_name, method_name = _split_name(name)
+ provider = ProviderMetaclass.find(cls_name)
+ if provider._instance == None:
+ provider._instance = provider()
- if MyProvider._instance == None:
- MyProvider._instance = MyProvider()
+ return provider._instance.__provide__(method_name)
- return MyProvider._instance.__provide__(method_name)
+def settings(name):
+ cls_name, method_name = _split_name(name)
+ provider = ProviderMetaclass.find(cls_name)()
+ return provider.get_config(method_name)
def list_instances():
"""
@@ -33,6 +35,8 @@ def list_instances():
"""
return ProviderMetaclass._subclasses.keys()
+
+
class ProviderMetaclass(type):
_subclasses = {}
@@ -55,6 +59,13 @@ def __new__(cls, name, bases, attrs):
return new_cls
+ @classmethod
+ def find(cls, cls_name):
+ try:
+ return cls._subclasses[cls_name]
+ except KeyError:
+ raise LookupError, "Couldn't find provider class for {0}".format(cls_name)
+
class SingletonProvider(object):
def __init__(self, *args, **kwargs):
@@ -69,18 +80,14 @@ def __provide__(self, method_name):
try:
result = self._instances[method_name]
except KeyError:
- config_method = getattr(self, method_name)
- config = dict(self.__defaults__().items() + config_method().items())
- result = self._instances[method_name] = self.construct(config)
+ result = self.get_config(method_name)
return result
class InstanceProvider(object):
def __provide__(self, method_name):
- # pymongo will do the appropriate connection pooling.
- config_method = getattr(self, method_name)
- config = dict(self.__defaults__().items() + config_method().items())
+ config = self. get_config(method_name)
return self.construct(config)
@@ -95,6 +102,13 @@ def __defaults__(self):
def construct(self, configuration):
return configuration
+ def get_config(self, method_name):
+ config_method = getattr(self, method_name)
+ config = {}
+ config.update(self.__defaults__())
+ config.update(config_method())
+ return config
+
def __init__(self):
self._instances = {}
super(Provider, self).__init__()
Please sign in to comment.
Something went wrong with that request. Please try again.