1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
# Copyright (C) 2006 by Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""Classes to provide name-to-object registry-like support."""
class Registry(object):
"""A class that registers objects to a name.
This is designed such that you can register objects in a lazy fashion,
so that they can be imported later. While still having the help text
available right away.
"""
def __init__(self):
"""Create a new Registry."""
self._default_key = None
# Map from key => (is_lazy, info)
self._dict = {}
self._help_dict = {}
self._info_dict = {}
def register(self, key, object, help=None, info=None,
override_existing=False):
"""Register a new object to a name.
:param key: This is the key to use to request the object later.
:param object: The object to register.
:param help: Help text for this entry. This may be a string or
a callable. If it is a callable, it should take a
single parameter, which is the key that the help was
registered under.
:param info: More information for this entry. Registry.get_info()
can be used to get this information. It is meant as an
opaque storage location.
:param override_existing: If True, replace the existing object
with the new one. If False, if there is already something
registered with the same key, raise a KeyError
"""
if not override_existing:
if key in self._dict:
raise KeyError('Key %r already registered' % key)
self._dict[key] = (False, object)
self._add_help_and_info(key, help=help, info=info)
def register_lazy(self, key, module_name, member_name,
help=None, info=None,
override_existing=False):
"""Register a new object to be loaded on request.
:param module_name: The python path to the module. Such as 'os.path'.
:param member_name: The member of the module to return, if empty or
None get() will return the module itself.
:param help: Help text for this entry. This may be a string or
a callable.
:param info: More information for this entry. Registry
:param override_existing: If True, replace the existing object
with the new one. If False, if there is already something
registered with the same key, raise a KeyError
"""
if not override_existing:
if key in self._dict:
raise KeyError('Key %r already registered' % key)
self._dict[key] = (True, (module_name, member_name))
self._add_help_and_info(key, help=help, info=info)
def _add_help_and_info(self, key, help=None, info=None):
"""Add the help and information about this key"""
self._help_dict[key] = help
self._info_dict[key] = info
def get(self, key=None):
"""Return the object register()'ed to the given key.
May raise ImportError if the object was registered lazily and
there are any problems, or AttributeError if the module does not
have the supplied member.
:param key: The key to obtain the object for. If no object has been
registered to that key, the object registered for self.default_key
will be returned instead, if it exists. Otherwise KeyError will be
raised.
:return: The previously registered object.
"""
return self._get_one(self._get_key_or_default(key))
def _get_key_or_default(self, key=None):
"""Return either 'key' or the default key if key is None"""
if key is not None:
return key
if self.default_key is None:
raise KeyError('Key is None, and no default key is set')
else:
return self.default_key
__getitem__ = get
def _get_one(self, key):
"""Attempt to return a single entry.
This will import the entry if it is lazy, and replace the registry
with the imported object.
This may raise KeyError if the given key doesn't exist, or ImportError
or AttributeError.
"""
is_lazy, info_or_object = self._dict[key]
if not is_lazy:
# We have a real object to return
return info_or_object
module_name, member_name = info_or_object
obj = __import__(module_name, globals(), locals(), [member_name])
if member_name:
obj = getattr(obj, member_name)
self._dict[key] = (False, obj)
return obj
def get_help(self, key=None):
"""Get the help text associated with the given key"""
the_help = self._help_dict[self._get_key_or_default(key)]
if callable(the_help):
return the_help(key)
return the_help
def get_info(self, key=None):
"""Get the extra information associated with the given key"""
return self._info_dict[self._get_key_or_default(key)]
def remove(self, key):
"""Remove a registered entry.
This is mostly for the test suite, but it can be used by others
"""
del self._dict[key]
__delitem__ = remove
def __contains__(self, key):
return key in self._dict
def __len__(self):
return len(self._dict)
def keys(self):
"""Get a list of registered entries"""
return sorted(self._dict.keys())
def iterkeys(self):
return self._dict.iterkeys()
def iteritems(self):
for key in self._dict.iterkeys():
yield key, self._get_one(key)
def items(self):
return list(self.iteritems())
def itervalues(self):
for key in self._dict.iterkeys():
yield self._get_one(key)
def values(self):
return list(self.itervalues())
def _set_default_key(self, key):
if not self._dict.has_key(key):
raise KeyError('No object registered under key %s.' % key)
else:
self._default_key = key
def _get_default_key(self):
return self._default_key
default_key = property(_get_default_key, _set_default_key,
doc="Current value of the default key."
"Can be set to any existing key.")
|