Commit b440b594 authored by Eliot Berriot's avatar Eliot Berriot

renamed obj argument to data, which seems more adequate

parent e82bb3f9
Changelog
=========
0.2.0
*****
26/07/2014:
- Added prepare_data hook for manipulating data before registration
- Added prepare_name hook for manipulating name before registration
- Added post_register hook
0.1.2
*****
......
......@@ -159,6 +159,53 @@ By default, a registry will accept any registered value. Sometimes, it's not wha
# will fail and raise ValueError
r.register("not so awesome day", name="not_so_awesome_day")
Registry.prepare_data()
***********************
If you want to manipulate your data before registering it, override this method. In this example, we add :
from persisting_theory import Registry
class HelloRegistry(Registry):
def prepare_data(self, obj):
if isinstance(obj, str):
return obj.startswith("awesome")
return False
r = HelloRegistry()
class Somebody()
# will pass registration
r.register("awesome day", name="awesome_day")
# will fail and raise ValueError
r.register("not so awesome day", name="not_so_awesome_day")
Registry.prepare_name()
***********************
If you want a dynamic name for the data you register, you can override this method:
from persisting_theory import Registry
class HelloRegistry(Registry):
def prepare_name(self, obj, name=None):
if isinstance(obj, str):
return obj.startswith("awesome")
return False
r = HelloRegistry()
class Somebody()
# will pass registration
r.register("awesome day", name="awesome_day")
# will fail and raise ValueError
r.register("not so awesome day", name="not_so_awesome_day")
Going meta
**********
......
from registries import Registry, meta_registry
__version__ = "0.1.2"
\ No newline at end of file
__version__ = "0.2.0"
\ No newline at end of file
......@@ -17,11 +17,11 @@ class Registry(OrderedDict):
"""
name = kwargs.get('name')
def decorator(decorated):
self.register_func(obj=decorated, name=name)
self.register_func(data=decorated, name=name)
return decorated
return decorator
def register(self, obj=None, name=None, **kwargs):
def register(self, data=None, name=None, **kwargs):
"""
Use this method as a decorator on class/function you want to register:
......@@ -29,67 +29,68 @@ class Registry(OrderedDict):
class Test:
pass
:param:obj: An object to register in the registry
:param:name: The name of the object to register. If none, the obj class name will be used
:param:data: Something to register in the registry
:param:name: The unique name that will identify registered data.
If None, by default, registry will try to deduce name from class name (if object is a class or an object).
You can change this behaviour by overriding :py::method:`prepare_name`
"""
if obj is None:
return self.register_decorator_factory(obj=obj, name=name, **kwargs)
if data is None:
return self.register_decorator_factory(data=data, name=name, **kwargs)
else:
self.register_func(obj=obj, name=name, **kwargs)
return obj
self.register_func(data=data, name=name, **kwargs)
return data
def get_object_name(self, obj):
def get_object_name(self, data):
"""
Return a name from an element (object, class, function...)
"""
if callable(obj):
return obj.__name__
if callable(data):
return data.__name__
elif inspect.isclass(obj):
return obj.__class__.__name__
elif inspect.isclass(data):
return data.__class__.__name__
else:
raise ValueError("Cannot deduce name from given object ({0}). Please user registry.register() with a 'name' argument.".format(obj))
raise ValueError("Cannot deduce name from given object ({0}). Please user registry.register() with a 'name' argument.".format(data))
def validate(self, obj):
def validate(self, data):
"""
Called before registering a new value into the registry
Override this method if you want to restrict what type of data cna be registered
"""
return True
def prepare_name(self, obj, name=None):
def prepare_name(self, data, name=None):
if name is None:
return self.get_object_name(obj)
return self.get_object_name(data)
return name
def register_func(self, obj, name=None, **kwargs):
def register_func(self, data, name=None, **kwargs):
"""
Register an object, class, function... into the registry
Register abritrary data into the registry
"""
if self.validate(obj):
o = self.prepare_data(obj)
n = self.prepare_name(obj, name)
if self.validate(data):
o = self.prepare_data(data)
n = self.prepare_name(data, name)
self[n] = o
self.post_register(obj=0, name=n)
self.post_register(data=0, name=n)
else:
raise ValueError("{0} (type: {0.__class__}) is not a valid value for {1} registry".format(obj, self.__class__))
raise ValueError("{0} (type: {0.__class__}) is not a valid value for {1} registry".format(data, self.__class__))
def post_register(self, obj, name):
def post_register(self, data, name):
"""
Will be triggered each time a new element is successfully registered.
Feel free to override this method
"""
pass
def prepare_data(self, obj):
def prepare_data(self, data):
"""
Override this methode if you want to manipulate data before registering it
You MUST return a value to register
"""
return obj
return data
def autodiscover(self, apps, force_reload=True):
......
......@@ -113,8 +113,8 @@ class RegistryTest(unittest.TestCase):
def test_can_manipulate_data_before_registering(self):
class ModifyData(registries.Registry):
def prepare_data(self, obj):
return "hello " + obj
def prepare_data(self, data):
return "hello " + data
r = ModifyData()
......@@ -127,17 +127,17 @@ class RegistryTest(unittest.TestCase):
def test_can_manipulate_key_before_registering(self):
class ModifyKey(registries.Registry):
def prepare_name(self, obj, key=None):
return "custom_key " + obj.name
def prepare_name(self, data, key=None):
return "custom_key " + data.first_name
r = ModifyKey()
class N:
def __init__(self, name):
self.name = name
def __init__(self, first_name):
self.first_name = first_name
n1 = N(name="eliot")
n2 = N(name="alain")
n1 = N(first_name="eliot")
n2 = N(first_name="alain")
r.register(n1)
r.register(n2)
......@@ -150,7 +150,7 @@ class RegistryTest(unittest.TestCase):
pass
class PostRegister(registries.Registry):
def post_register(self, obj, name):
def post_register(self, data, name):
raise PostRegisterException('Post register triggered')
r = PostRegister()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment