13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
"""Classes to provide name-to-object registry-like support."""
62
62
return super(_LazyObjectGetter, self).get_obj()
64
64
def _do_import(self):
65
obj = __import__(self._module_name, globals(), locals(),
68
obj = getattr(obj, self._member_name)
66
segments = self._member_name.split('.')
69
names = [self._member_name]
70
obj = __import__(self._module_name, globals(), locals(), names)
72
for segment in segments:
73
obj = getattr(obj, segment)
70
75
self._imported = True
107
112
:param obj: The object to register.
108
113
:param help: Help text for this entry. This may be a string or
109
114
a callable. If it is a callable, it should take two
110
parameters (registry, key): this registry and the key that
115
parameters (registry, key): this registry and the key that
111
116
the help was registered under.
112
117
:param info: More information for this entry. Registry.get_info()
113
118
can be used to get this information. Registry treats this as an
128
133
"""Register a new object to be loaded on request.
130
135
:param module_name: The python path to the module. Such as 'os.path'.
131
:param member_name: The member of the module to return. If empty or
136
:param member_name: The member of the module to return. If empty or
132
137
None, get() will return the module itself.
133
138
:param help: Help text for this entry. This may be a string or
135
:param info: More information for this entry. Registry
140
:param info: More information for this entry. Registry
136
141
:param override_existing: If True, replace the existing object
137
142
with the new one. If False, if there is already something
138
143
registered with the same key, raise a KeyError
152
157
"""Return the object register()'ed to the given key.
154
159
May raise ImportError if the object was registered lazily and
155
there are any problems, or AttributeError if the module does not
160
there are any problems, or AttributeError if the module does not
156
161
have the supplied member.
158
163
:param key: The key to obtain the object for. If no object has been
231
236
default_key = property(_get_default_key, _set_default_key,
232
237
doc="Current value of the default key."
233
238
" Can be set to any existing key.")
241
class FormatRegistry(Registry):
242
"""Registry specialised for handling formats."""
244
def __init__(self, other_registry=None):
245
Registry.__init__(self)
246
self._other_registry = other_registry
248
def register_lazy(self, key, module_name, member_name,
249
help=None, info=None,
250
override_existing=False):
251
# Overridden to allow capturing registrations to two seperate
252
# registries in a single call.
253
Registry.register_lazy(self, key, module_name, member_name,
254
help=help, info=info, override_existing=override_existing)
255
if self._other_registry is not None:
256
self._other_registry.register_lazy(key, module_name, member_name,
257
help=help, info=info, override_existing=override_existing)
259
def get(self, format_string):
260
r = Registry.get(self, format_string)