14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""LazyFactory lets you register objects to be loaded by request
19
Basically, formats are registered by some sort of key. In the future
20
that object can be requested, and the factory will import the appropriate
21
module, and return the requested member object.
25
class LazyFactory(object):
26
"""A factory which registers objects which will be load on request."""
17
"""Classes to provide name-to-object registry-like support."""
20
class Registry(object):
21
"""A class that registers objects to a name."""
28
23
def __init__(self, first_is_default=False):
29
"""Create a new Lazy Factory.
24
"""Create a new Registry.
31
:param first_is_default: If True, then the first object to be registered
32
will also be registered against the key None, and will be returned
33
by default from .get()
26
:param first_is_default: If True, then the first key to be registered
27
will be set as the default key for get() to use.
35
29
self._first_is_default = first_is_default
30
self._default_key = None
33
def register(self, key, object):
34
"""Register a new object to a name.
36
:param key: This is the key to use to request the object later.
37
:param object: The object to register.
39
if self._first_is_default and not self._dict:
40
self._default_key = key
41
self._dict[key] = object
43
def get(self, key=None):
44
"""Return the object register()'ed to the given key.
46
:param key: The key to obtain the object for. If no object has been
47
registered to that key, the object registered for self.default_key
48
will be returned instead, if it exists. Otherwise KeyError will be
50
:return: The previously registered object.
53
return self._dict[key]
55
if self.default_key is not None:
56
return self._dict[self.default_key]
61
"""Get a list of registered entries"""
62
return sorted(self._dict.keys())
64
def _set_default_key(self, key):
65
if not self._dict.has_key(key):
66
raise KeyError('No object registered under key %s.' % key)
68
self._default_key = key
70
def _get_default_key(self):
71
return self._default_key
73
default_key = property(_get_default_key, _set_default_key)
74
"""Current value of the default key. Can be set to any existing key."""
77
class LazyImportRegistry(Registry):
78
"""A class to register modules/members to be loaded on request."""
38
80
def register(self, key, module_name, member_name):
39
81
"""Register a new object to be loaded on request.
41
:param key: This is the key to use to request the object later.
42
:param module_name: The python path to the module. Such as 'os.path'
83
:param module_name: The python path to the module. Such as 'os.path'.
43
84
:param member_name: The member of the module to return, if empty or None
44
85
get() will return the module itself.
45
:return: if something used to be registered, its information will be
48
old_info = self._dict.get(key)
49
if self._first_is_default and not self._dict:
50
self._dict[None] = (module_name, member_name)
51
self._dict[key] = (module_name, member_name)
87
Registry.register(self, key, (module_name, member_name))
53
89
def get(self, key=None):
54
90
"""Load the module and return the object specified by the given key.
56
This may raise KeyError if the key is not present.
57
May also raise ImportError if there are any problems
58
Or AttributeError if the module does not have the supplied member
60
:param key: The key registered by register()
61
:return: The module/klass/function/object specified earlier
92
May raise ImportError if there are any problems, or AttributeError if
93
the module does not have the supplied member.
63
module_name, member_name = self._dict[key]
95
module_name, member_name = Registry.get(self, key)
64
96
module = __import__(module_name, globals(), locals(), [member_name])
66
98
return getattr(module, member_name)
69
def keys(self, include_none=False):
70
"""Get a list of registered entries"""
71
keys = self._dict.keys()
72
if not include_none and None in self._dict: