80 cls.__regid__ = cls.id |
80 cls.__regid__ = cls.id |
81 if hasattr(cls, 'id') and not isinstance(cls.id, property): |
81 if hasattr(cls, 'id') and not isinstance(cls.id, property): |
82 return cls.id |
82 return cls.id |
83 return cls.__regid__ |
83 return cls.__regid__ |
84 |
84 |
|
85 def class_registries(cls, registryname): |
|
86 if registryname: |
|
87 return (registryname,) |
|
88 return cls.__registries__ |
|
89 |
85 |
90 |
86 class Registry(dict): |
91 class Registry(dict): |
87 |
92 |
88 def __init__(self, config): |
93 def __init__(self, config): |
89 super(Registry, self).__init__() |
94 super(Registry, self).__init__() |
316 for obj in objects: |
321 for obj in objects: |
317 try: |
322 try: |
318 if obj.__module__ != modname or obj in butclasses: |
323 if obj.__module__ != modname or obj in butclasses: |
319 continue |
324 continue |
320 oid = class_regid(obj) |
325 oid = class_regid(obj) |
321 registryname = obj.__registry__ |
|
322 except AttributeError: |
326 except AttributeError: |
323 continue |
327 continue |
324 if oid and not '__abstract__' in obj.__dict__: |
328 if oid and not '__abstract__' in obj.__dict__: |
325 self.register(obj, registryname) |
329 self.register(obj, oid=oid) |
326 |
330 |
327 def register(self, obj, registryname=None, oid=None, clear=False): |
331 def register(self, obj, registryname=None, oid=None, clear=False): |
328 """base method to add an object in the registry""" |
332 """base method to add an object in the registry""" |
329 assert not '__abstract__' in obj.__dict__ |
333 assert not '__abstract__' in obj.__dict__ |
330 registryname = registryname or obj.__registry__ |
|
331 registry = self.setdefault(registryname) |
|
332 registry.register(obj, oid=oid, clear=clear) |
|
333 try: |
334 try: |
334 vname = obj.__name__ |
335 vname = obj.__name__ |
335 except AttributeError: |
336 except AttributeError: |
336 vname = obj.__class__.__name__ |
337 vname = obj.__class__.__name__ |
337 self.debug('registered appobject %s in registry %s with id %s', |
338 for registryname in class_registries(obj, registryname): |
338 vname, registryname, oid or class_regid(obj)) |
339 registry = self.setdefault(registryname) |
|
340 registry.register(obj, oid=oid, clear=clear) |
|
341 self.debug('registered appobject %s in registry %s with id %s', |
|
342 vname, registryname, oid or class_regid(obj)) |
339 self._loadedmods[obj.__module__][classid(obj)] = obj |
343 self._loadedmods[obj.__module__][classid(obj)] = obj |
340 |
344 |
341 def unregister(self, obj, registryname=None): |
345 def unregister(self, obj, registryname=None): |
342 self[registryname or obj.__registry__].unregister(obj) |
346 for registryname in class_registries(obj, registryname): |
|
347 self[registryname].unregister(obj) |
343 |
348 |
344 def register_and_replace(self, obj, replaced, registryname=None): |
349 def register_and_replace(self, obj, replaced, registryname=None): |
345 self[registryname or obj.__registry__].register_and_replace(obj, replaced) |
350 for registryname in class_registries(obj, registryname): |
|
351 self[registryname].register_and_replace(obj, replaced) |
346 |
352 |
347 # initialization methods ################################################### |
353 # initialization methods ################################################### |
348 |
354 |
349 def init_registration(self, path, extrapath=None): |
355 def init_registration(self, path, extrapath=None): |
350 # compute list of all modules that have to be loaded |
356 # compute list of all modules that have to be loaded |
449 self._loadedmods[modname][clsid] = appobjectcls |
455 self._loadedmods[modname][clsid] = appobjectcls |
450 for parent in appobjectcls.__bases__: |
456 for parent in appobjectcls.__bases__: |
451 self._load_ancestors_then_object(modname, parent) |
457 self._load_ancestors_then_object(modname, parent) |
452 if (appobjectcls.__dict__.get('__abstract__') |
458 if (appobjectcls.__dict__.get('__abstract__') |
453 or appobjectcls.__name__[0] == '_' |
459 or appobjectcls.__name__[0] == '_' |
454 or not appobjectcls.__registry__ |
460 or not appobjectcls.__registries__ |
455 or not class_regid(appobjectcls)): |
461 or not class_regid(appobjectcls)): |
456 return |
462 return |
457 try: |
463 try: |
458 self.register(appobjectcls) |
464 self.register(appobjectcls) |
459 except Exception, ex: |
465 except Exception, ex: |