class Components(object): def __init__(self, name='', bases=()): assert isinstance(name, STRING_TYPES) self.__name__ = name self._init_registries() self._init_registrations() self.__bases__ = tuple(bases) def __repr__(self): return "<%s %s>" % (self.__class__.__name__, self.__name__) def _init_registries(self): self.adapters = AdapterRegistry() self.utilities = AdapterRegistry() def _init_registrations(self): self._utility_registrations = {} self._adapter_registrations = {} self._subscription_registrations = [] self._handler_registrations = [] def _getBases(self): # Subclasses might override return self.__dict__.get('__bases__', ()) def _setBases(self, bases): # Subclasses might override self.adapters.__bases__ = tuple([base.adapters for base in bases]) self.utilities.__bases__ = tuple([base.utilities for base in bases]) self.__dict__['__bases__'] = tuple(bases) __bases__ = property( lambda self: self._getBases(), lambda self, bases: self._setBases(bases), ) def registerUtility(self, component=None, provided=None, name=_u(''), info=_u(''), event=True, factory=None): if factory: if component: raise TypeError("Can't specify factory and component.") component = factory() if provided is None: provided = _getUtilityProvided(component) reg = self._utility_registrations.get((provided, name)) if reg is not None: if reg[:2] == (component, info): # already registered return self.unregisterUtility(reg[0], provided, name) subscribed = False for ((p, _), data) in iter(self._utility_registrations.items()): if p == provided and data[0] == component: subscribed = True break self._utility_registrations[(provided, name)] = component, info, factory self.utilities.register((), provided, name, component) if not subscribed: self.utilities.subscribe((), provided, component) if event: notify( Registered( UtilityRegistration(self, provided, name, component, info, factory))) def unregisterUtility(self, component=None, provided=None, name=_u(''), factory=None): if factory: if component: raise TypeError("Can't specify factory and component.") component = factory() if provided is None: if component is None: raise TypeError("Must specify one of component, factory and " "provided") provided = _getUtilityProvided(component) old = self._utility_registrations.get((provided, name)) if (old is None) or ((component is not None) and (component != old[0])): return False if component is None: component = old[0] # Note that component is now the old thing registered del self._utility_registrations[(provided, name)] self.utilities.unregister((), provided, name) subscribed = False for ((p, _), data) in iter(self._utility_registrations.items()): if p == provided and data[0] == component: subscribed = True break if not subscribed: self.utilities.unsubscribe((), provided, component) notify( Unregistered( UtilityRegistration(self, provided, name, component, *old[1:]))) return True def registeredUtilities(self): for ((provided, name), data) in iter(self._utility_registrations.items()): yield UtilityRegistration(self, provided, name, *data) def queryUtility(self, provided, name=_u(''), default=None): return self.utilities.lookup((), provided, name, default) def getUtility(self, provided, name=_u('')): utility = self.utilities.lookup((), provided, name) if utility is None: raise ComponentLookupError(provided, name) return utility def getUtilitiesFor(self, interface): for name, utility in self.utilities.lookupAll((), interface): yield name, utility def getAllUtilitiesRegisteredFor(self, interface): return self.utilities.subscriptions((), interface) def registerAdapter(self, factory, required=None, provided=None, name=_u(''), info=_u(''), event=True): if provided is None: provided = _getAdapterProvided(factory) required = _getAdapterRequired(factory, required) self._adapter_registrations[(required, provided, name)] = factory, info self.adapters.register(required, provided, name, factory) if event: notify( Registered( AdapterRegistration(self, required, provided, name, factory, info))) def unregisterAdapter( self, factory=None, required=None, provided=None, name=_u(''), ): if provided is None: if factory is None: raise TypeError("Must specify one of factory and provided") provided = _getAdapterProvided(factory) if (required is None) and (factory is None): raise TypeError("Must specify one of factory and required") required = _getAdapterRequired(factory, required) old = self._adapter_registrations.get((required, provided, name)) if (old is None) or ((factory is not None) and (factory != old[0])): return False del self._adapter_registrations[(required, provided, name)] self.adapters.unregister(required, provided, name) notify( Unregistered( AdapterRegistration(self, required, provided, name, *old))) return True def registeredAdapters(self): for ((required, provided, name), (component, info)) in iter(self._adapter_registrations.items()): yield AdapterRegistration(self, required, provided, name, component, info) def queryAdapter(self, object, interface, name=_u(''), default=None): return self.adapters.queryAdapter(object, interface, name, default) def getAdapter(self, object, interface, name=_u('')): adapter = self.adapters.queryAdapter(object, interface, name) if adapter is None: raise ComponentLookupError(object, interface, name) return adapter def queryMultiAdapter(self, objects, interface, name=_u(''), default=None): return self.adapters.queryMultiAdapter(objects, interface, name, default) def getMultiAdapter(self, objects, interface, name=_u('')): adapter = self.adapters.queryMultiAdapter(objects, interface, name) if adapter is None: raise ComponentLookupError(objects, interface, name) return adapter def getAdapters(self, objects, provided): for name, factory in self.adapters.lookupAll( list(map(providedBy, objects)), provided): adapter = factory(*objects) if adapter is not None: yield name, adapter def registerSubscriptionAdapter(self, factory, required=None, provided=None, name=_u(''), info=_u(''), event=True): if name: raise TypeError("Named subscribers are not yet supported") if provided is None: provided = _getAdapterProvided(factory) required = _getAdapterRequired(factory, required) self._subscription_registrations.append( (required, provided, name, factory, info)) self.adapters.subscribe(required, provided, factory) if event: notify( Registered( SubscriptionRegistration(self, required, provided, name, factory, info))) def registeredSubscriptionAdapters(self): for data in self._subscription_registrations: yield SubscriptionRegistration(self, *data) def unregisterSubscriptionAdapter( self, factory=None, required=None, provided=None, name=_u(''), ): if name: raise TypeError("Named subscribers are not yet supported") if provided is None: if factory is None: raise TypeError("Must specify one of factory and provided") provided = _getAdapterProvided(factory) if (required is None) and (factory is None): raise TypeError("Must specify one of factory and required") required = _getAdapterRequired(factory, required) if factory is None: new = [(r, p, n, f, i) for (r, p, n, f, i) in self._subscription_registrations if not (r == required and p == provided)] else: new = [(r, p, n, f, i) for (r, p, n, f, i) in self._subscription_registrations if not (r == required and p == provided and f == factory)] if len(new) == len(self._subscription_registrations): return False self._subscription_registrations[:] = new self.adapters.unsubscribe(required, provided, factory) notify( Unregistered( SubscriptionRegistration(self, required, provided, name, factory, ''))) return True def subscribers(self, objects, provided): return self.adapters.subscribers(objects, provided) def registerHandler(self, factory, required=None, name=_u(''), info=_u(''), event=True): if name: raise TypeError("Named handlers are not yet supported") required = _getAdapterRequired(factory, required) self._handler_registrations.append((required, name, factory, info)) self.adapters.subscribe(required, None, factory) if event: notify( Registered( HandlerRegistration(self, required, name, factory, info))) def registeredHandlers(self): for data in self._handler_registrations: yield HandlerRegistration(self, *data) def unregisterHandler(self, factory=None, required=None, name=_u('')): if name: raise TypeError("Named subscribers are not yet supported") if (required is None) and (factory is None): raise TypeError("Must specify one of factory and required") required = _getAdapterRequired(factory, required) if factory is None: new = [(r, n, f, i) for (r, n, f, i) in self._handler_registrations if r != required] else: new = [(r, n, f, i) for (r, n, f, i) in self._handler_registrations if not (r == required and f == factory)] if len(new) == len(self._handler_registrations): return False self._handler_registrations[:] = new self.adapters.unsubscribe(required, None, factory) notify( Unregistered(HandlerRegistration(self, required, name, factory, ''))) return True def handle(self, *objects): self.adapters.subscribers(objects, None)
class Components(object): def __init__(self, name='', bases=()): assert isinstance(name, STRING_TYPES) self.__name__ = name self._init_registries() self._init_registrations() self.__bases__ = tuple(bases) def __repr__(self): return "<%s %s>" % (self.__class__.__name__, self.__name__) def _init_registries(self): self.adapters = AdapterRegistry() self.utilities = AdapterRegistry() def _init_registrations(self): self._utility_registrations = {} self._adapter_registrations = {} self._subscription_registrations = [] self._handler_registrations = [] def _getBases(self): # Subclasses might override return self.__dict__.get('__bases__', ()) def _setBases(self, bases): # Subclasses might override self.adapters.__bases__ = tuple([ base.adapters for base in bases]) self.utilities.__bases__ = tuple([ base.utilities for base in bases]) self.__dict__['__bases__'] = tuple(bases) __bases__ = property( lambda self: self._getBases(), lambda self, bases: self._setBases(bases), ) def registerUtility(self, component=None, provided=None, name=_u(''), info=_u(''), event=True, factory=None): if factory: if component: raise TypeError("Can't specify factory and component.") component = factory() if provided is None: provided = _getUtilityProvided(component) reg = self._utility_registrations.get((provided, name)) if reg is not None: if reg[:2] == (component, info): # already registered return self.unregisterUtility(reg[0], provided, name) subscribed = False for ((p, _), data) in iter(self._utility_registrations.items()): if p == provided and data[0] == component: subscribed = True break self._utility_registrations[(provided, name)] = component, info, factory self.utilities.register((), provided, name, component) if not subscribed: self.utilities.subscribe((), provided, component) if event: notify(Registered( UtilityRegistration(self, provided, name, component, info, factory) )) def unregisterUtility(self, component=None, provided=None, name=_u(''), factory=None): if factory: if component: raise TypeError("Can't specify factory and component.") component = factory() if provided is None: if component is None: raise TypeError("Must specify one of component, factory and " "provided") provided = _getUtilityProvided(component) old = self._utility_registrations.get((provided, name)) if (old is None) or ((component is not None) and (component != old[0])): return False if component is None: component = old[0] # Note that component is now the old thing registered del self._utility_registrations[(provided, name)] self.utilities.unregister((), provided, name) subscribed = False for ((p, _), data) in iter(self._utility_registrations.items()): if p == provided and data[0] == component: subscribed = True break if not subscribed: self.utilities.unsubscribe((), provided, component) notify(Unregistered( UtilityRegistration(self, provided, name, component, *old[1:]) )) return True def registeredUtilities(self): for ((provided, name), data ) in iter(self._utility_registrations.items()): yield UtilityRegistration(self, provided, name, *data) def queryUtility(self, provided, name=_u(''), default=None): return self.utilities.lookup((), provided, name, default) def getUtility(self, provided, name=_u('')): utility = self.utilities.lookup((), provided, name) if utility is None: raise ComponentLookupError(provided, name) return utility def getUtilitiesFor(self, interface): for name, utility in self.utilities.lookupAll((), interface): yield name, utility def getAllUtilitiesRegisteredFor(self, interface): return self.utilities.subscriptions((), interface) def registerAdapter(self, factory, required=None, provided=None, name=_u(''), info=_u(''), event=True): if provided is None: provided = _getAdapterProvided(factory) required = _getAdapterRequired(factory, required) self._adapter_registrations[(required, provided, name) ] = factory, info self.adapters.register(required, provided, name, factory) if event: notify(Registered( AdapterRegistration(self, required, provided, name, factory, info) )) def unregisterAdapter(self, factory=None, required=None, provided=None, name=_u(''), ): if provided is None: if factory is None: raise TypeError("Must specify one of factory and provided") provided = _getAdapterProvided(factory) if (required is None) and (factory is None): raise TypeError("Must specify one of factory and required") required = _getAdapterRequired(factory, required) old = self._adapter_registrations.get((required, provided, name)) if (old is None) or ((factory is not None) and (factory != old[0])): return False del self._adapter_registrations[(required, provided, name)] self.adapters.unregister(required, provided, name) notify(Unregistered( AdapterRegistration(self, required, provided, name, *old) )) return True def registeredAdapters(self): for ((required, provided, name), (component, info) ) in iter(self._adapter_registrations.items()): yield AdapterRegistration(self, required, provided, name, component, info) def queryAdapter(self, object, interface, name=_u(''), default=None): return self.adapters.queryAdapter(object, interface, name, default) def getAdapter(self, object, interface, name=_u('')): adapter = self.adapters.queryAdapter(object, interface, name) if adapter is None: raise ComponentLookupError(object, interface, name) return adapter def queryMultiAdapter(self, objects, interface, name=_u(''), default=None): return self.adapters.queryMultiAdapter( objects, interface, name, default) def getMultiAdapter(self, objects, interface, name=_u('')): adapter = self.adapters.queryMultiAdapter(objects, interface, name) if adapter is None: raise ComponentLookupError(objects, interface, name) return adapter def getAdapters(self, objects, provided): for name, factory in self.adapters.lookupAll( list(map(providedBy, objects)), provided): adapter = factory(*objects) if adapter is not None: yield name, adapter def registerSubscriptionAdapter(self, factory, required=None, provided=None, name=_u(''), info=_u(''), event=True): if name: raise TypeError("Named subscribers are not yet supported") if provided is None: provided = _getAdapterProvided(factory) required = _getAdapterRequired(factory, required) self._subscription_registrations.append( (required, provided, name, factory, info) ) self.adapters.subscribe(required, provided, factory) if event: notify(Registered( SubscriptionRegistration(self, required, provided, name, factory, info) )) def registeredSubscriptionAdapters(self): for data in self._subscription_registrations: yield SubscriptionRegistration(self, *data) def unregisterSubscriptionAdapter(self, factory=None, required=None, provided=None, name=_u(''), ): if name: raise TypeError("Named subscribers are not yet supported") if provided is None: if factory is None: raise TypeError("Must specify one of factory and provided") provided = _getAdapterProvided(factory) if (required is None) and (factory is None): raise TypeError("Must specify one of factory and required") required = _getAdapterRequired(factory, required) if factory is None: new = [(r, p, n, f, i) for (r, p, n, f, i) in self._subscription_registrations if not (r == required and p == provided) ] else: new = [(r, p, n, f, i) for (r, p, n, f, i) in self._subscription_registrations if not (r == required and p == provided and f == factory) ] if len(new) == len(self._subscription_registrations): return False self._subscription_registrations[:] = new self.adapters.unsubscribe(required, provided, factory) notify(Unregistered( SubscriptionRegistration(self, required, provided, name, factory, '') )) return True def subscribers(self, objects, provided): return self.adapters.subscribers(objects, provided) def registerHandler(self, factory, required=None, name=_u(''), info=_u(''), event=True): if name: raise TypeError("Named handlers are not yet supported") required = _getAdapterRequired(factory, required) self._handler_registrations.append( (required, name, factory, info) ) self.adapters.subscribe(required, None, factory) if event: notify(Registered( HandlerRegistration(self, required, name, factory, info) )) def registeredHandlers(self): for data in self._handler_registrations: yield HandlerRegistration(self, *data) def unregisterHandler(self, factory=None, required=None, name=_u('')): if name: raise TypeError("Named subscribers are not yet supported") if (required is None) and (factory is None): raise TypeError("Must specify one of factory and required") required = _getAdapterRequired(factory, required) if factory is None: new = [(r, n, f, i) for (r, n, f, i) in self._handler_registrations if r != required ] else: new = [(r, n, f, i) for (r, n, f, i) in self._handler_registrations if not (r == required and f == factory) ] if len(new) == len(self._handler_registrations): return False self._handler_registrations[:] = new self.adapters.unsubscribe(required, None, factory) notify(Unregistered( HandlerRegistration(self, required, name, factory, '') )) return True def handle(self, *objects): self.adapters.subscribers(objects, None)
class Components(object): _v_utility_registrations_cache = None def __init__(self, name='', bases=()): # __init__ is used for test cleanup as well as initialization. # XXX add a separate API for test cleanup. assert isinstance(name, STRING_TYPES) self.__name__ = name self._init_registries() self._init_registrations() self.__bases__ = tuple(bases) self._v_utility_registrations_cache = None def __repr__(self): return "<%s %s>" % (self.__class__.__name__, self.__name__) def __reduce__(self): # Mimic what a persistent.Persistent object does and elide # _v_ attributes so that they don't get saved in ZODB. # This allows us to store things that cannot be pickled in such # attributes. reduction = super(Components, self).__reduce__() # (callable, args, state, listiter, dictiter) # We assume the state is always a dict; the last three items # are technically optional and can be missing or None. filtered_state = { k: v for k, v in reduction[2].items() if not k.startswith('_v_') } reduction = list(reduction) reduction[2] = filtered_state return tuple(reduction) def _init_registries(self): # Subclasses have never been required to call this method # if they override it, merely to fill in these two attributes. self.adapters = AdapterRegistry() self.utilities = AdapterRegistry() def _init_registrations(self): self._utility_registrations = {} self._adapter_registrations = {} self._subscription_registrations = [] self._handler_registrations = [] @property def _utility_registrations_cache(self): # We use a _v_ attribute internally so that data aren't saved in ZODB, # because this object cannot be pickled. cache = self._v_utility_registrations_cache if (cache is None or cache._utilities is not self.utilities or cache._utility_registrations is not self._utility_registrations): cache = self._v_utility_registrations_cache = _UtilityRegistrations( self.utilities, self._utility_registrations) return cache def _getBases(self): # Subclasses might override return self.__dict__.get('__bases__', ()) def _setBases(self, bases): # Subclasses might override self.adapters.__bases__ = tuple([base.adapters for base in bases]) self.utilities.__bases__ = tuple([base.utilities for base in bases]) self.__dict__['__bases__'] = tuple(bases) __bases__ = property( lambda self: self._getBases(), lambda self, bases: self._setBases(bases), ) def registerUtility(self, component=None, provided=None, name=u'', info=u'', event=True, factory=None): if factory: if component: raise TypeError("Can't specify factory and component.") component = factory() if provided is None: provided = _getUtilityProvided(component) if name == u'': name = _getName(component) reg = self._utility_registrations.get((provided, name)) if reg is not None: if reg[:2] == (component, info): # already registered return self.unregisterUtility(reg[0], provided, name) self._utility_registrations_cache.registerUtility( provided, name, component, info, factory) if event: notify( Registered( UtilityRegistration(self, provided, name, component, info, factory))) def unregisterUtility(self, component=None, provided=None, name=u'', factory=None): if factory: if component: raise TypeError("Can't specify factory and component.") component = factory() if provided is None: if component is None: raise TypeError("Must specify one of component, factory and " "provided") provided = _getUtilityProvided(component) old = self._utility_registrations.get((provided, name)) if (old is None) or ((component is not None) and (component != old[0])): return False if component is None: component = old[0] # Note that component is now the old thing registered self._utility_registrations_cache.unregisterUtility( provided, name, component) notify( Unregistered( UtilityRegistration(self, provided, name, component, *old[1:]))) return True def registeredUtilities(self): for ((provided, name), data) in iter(self._utility_registrations.items()): yield UtilityRegistration(self, provided, name, *data) def queryUtility(self, provided, name=u'', default=None): return self.utilities.lookup((), provided, name, default) def getUtility(self, provided, name=u''): utility = self.utilities.lookup((), provided, name) if utility is None: raise ComponentLookupError(provided, name) return utility def getUtilitiesFor(self, interface): for name, utility in self.utilities.lookupAll((), interface): yield name, utility def getAllUtilitiesRegisteredFor(self, interface): return self.utilities.subscriptions((), interface) def registerAdapter(self, factory, required=None, provided=None, name=u'', info=u'', event=True): if provided is None: provided = _getAdapterProvided(factory) required = _getAdapterRequired(factory, required) if name == u'': name = _getName(factory) self._adapter_registrations[(required, provided, name)] = factory, info self.adapters.register(required, provided, name, factory) if event: notify( Registered( AdapterRegistration(self, required, provided, name, factory, info))) def unregisterAdapter( self, factory=None, required=None, provided=None, name=u'', ): if provided is None: if factory is None: raise TypeError("Must specify one of factory and provided") provided = _getAdapterProvided(factory) if (required is None) and (factory is None): raise TypeError("Must specify one of factory and required") required = _getAdapterRequired(factory, required) old = self._adapter_registrations.get((required, provided, name)) if (old is None) or ((factory is not None) and (factory != old[0])): return False del self._adapter_registrations[(required, provided, name)] self.adapters.unregister(required, provided, name) notify( Unregistered( AdapterRegistration(self, required, provided, name, *old))) return True def registeredAdapters(self): for ((required, provided, name), (component, info)) in iter(self._adapter_registrations.items()): yield AdapterRegistration(self, required, provided, name, component, info) def queryAdapter(self, object, interface, name=u'', default=None): return self.adapters.queryAdapter(object, interface, name, default) def getAdapter(self, object, interface, name=u''): adapter = self.adapters.queryAdapter(object, interface, name) if adapter is None: raise ComponentLookupError(object, interface, name) return adapter def queryMultiAdapter(self, objects, interface, name=u'', default=None): return self.adapters.queryMultiAdapter(objects, interface, name, default) def getMultiAdapter(self, objects, interface, name=u''): adapter = self.adapters.queryMultiAdapter(objects, interface, name) if adapter is None: raise ComponentLookupError(objects, interface, name) return adapter def getAdapters(self, objects, provided): for name, factory in self.adapters.lookupAll( list(map(providedBy, objects)), provided): adapter = factory(*objects) if adapter is not None: yield name, adapter def registerSubscriptionAdapter(self, factory, required=None, provided=None, name=u'', info=u'', event=True): if name: raise TypeError("Named subscribers are not yet supported") if provided is None: provided = _getAdapterProvided(factory) required = _getAdapterRequired(factory, required) self._subscription_registrations.append( (required, provided, name, factory, info)) self.adapters.subscribe(required, provided, factory) if event: notify( Registered( SubscriptionRegistration(self, required, provided, name, factory, info))) def registeredSubscriptionAdapters(self): for data in self._subscription_registrations: yield SubscriptionRegistration(self, *data) def unregisterSubscriptionAdapter( self, factory=None, required=None, provided=None, name=u'', ): if name: raise TypeError("Named subscribers are not yet supported") if provided is None: if factory is None: raise TypeError("Must specify one of factory and provided") provided = _getAdapterProvided(factory) if (required is None) and (factory is None): raise TypeError("Must specify one of factory and required") required = _getAdapterRequired(factory, required) if factory is None: new = [(r, p, n, f, i) for (r, p, n, f, i) in self._subscription_registrations if not (r == required and p == provided)] else: new = [(r, p, n, f, i) for (r, p, n, f, i) in self._subscription_registrations if not (r == required and p == provided and f == factory)] if len(new) == len(self._subscription_registrations): return False self._subscription_registrations[:] = new self.adapters.unsubscribe(required, provided, factory) notify( Unregistered( SubscriptionRegistration(self, required, provided, name, factory, ''))) return True def subscribers(self, objects, provided): return self.adapters.subscribers(objects, provided) def registerHandler(self, factory, required=None, name=u'', info=u'', event=True): if name: raise TypeError("Named handlers are not yet supported") required = _getAdapterRequired(factory, required) self._handler_registrations.append((required, name, factory, info)) self.adapters.subscribe(required, None, factory) if event: notify( Registered( HandlerRegistration(self, required, name, factory, info))) def registeredHandlers(self): for data in self._handler_registrations: yield HandlerRegistration(self, *data) def unregisterHandler(self, factory=None, required=None, name=u''): if name: raise TypeError("Named subscribers are not yet supported") if (required is None) and (factory is None): raise TypeError("Must specify one of factory and required") required = _getAdapterRequired(factory, required) if factory is None: new = [(r, n, f, i) for (r, n, f, i) in self._handler_registrations if r != required] else: new = [(r, n, f, i) for (r, n, f, i) in self._handler_registrations if not (r == required and f == factory)] if len(new) == len(self._handler_registrations): return False self._handler_registrations[:] = new self.adapters.unsubscribe(required, None, factory) notify( Unregistered(HandlerRegistration(self, required, name, factory, ''))) return True def handle(self, *objects): self.adapters.subscribers(objects, None) def rebuildUtilityRegistryFromLocalCache(self, rebuild=False): """ Emergency maintenance method to rebuild the ``.utilities`` registry from the local copy maintained in this object, or detect the need to do so. Most users will never need to call this, but it can be helpful in the event of suspected corruption. By default, this method only checks for corruption. To make it actually rebuild the registry, pass `True` for *rebuild*. :param bool rebuild: If set to `True` (not the default), this method will actually register and subscribe utilities in the registry as needed to synchronize with the local cache. :return: A dictionary that's meant as diagnostic data. The keys and values may change over time. When called with a false *rebuild*, the keys ``"needed_registered"`` and ``"needed_subscribed"`` will be non-zero if any corruption was detected, but that will not be corrected. .. versionadded:: 5.3.0 """ regs = dict(self._utility_registrations) utils = self.utilities needed_registered = 0 did_not_register = 0 needed_subscribed = 0 did_not_subscribe = 0 # Avoid the expensive change process during this; we'll call # it once at the end if needed. assert 'changed' not in utils.__dict__ utils.changed = lambda _: None if rebuild: register = utils.register subscribe = utils.subscribe else: register = subscribe = lambda *args: None try: for (provided, name), (value, _info, _factory) in regs.items(): if utils.registered((), provided, name) != value: register((), provided, name, value) needed_registered += 1 else: did_not_register += 1 if utils.subscribed((), provided, value) is None: needed_subscribed += 1 subscribe((), provided, value) else: did_not_subscribe += 1 finally: del utils.changed if rebuild and (needed_subscribed or needed_registered): utils.changed(utils) return { 'needed_registered': needed_registered, 'did_not_register': did_not_register, 'needed_subscribed': needed_subscribed, 'did_not_subscribe': did_not_subscribe }
class Components(object): def __init__(self, name='', bases=()): assert isinstance(name, STRING_TYPES) self.__name__ = name self._init_registries() self._init_registrations() self.__bases__ = tuple(bases) # __init__ is used for test cleanup as well as initialization. # XXX add a separate API for test cleanup. # See _utility_registrations below. if hasattr(self, '_v_utility_registrations_cache'): del self._v_utility_registrations_cache def __repr__(self): return "<%s %s>" % (self.__class__.__name__, self.__name__) def _init_registries(self): self.adapters = AdapterRegistry() self.utilities = AdapterRegistry() def _init_registrations(self): self._utility_registrations = {} self._adapter_registrations = {} self._subscription_registrations = [] self._handler_registrations = [] @property def _utility_registrations_cache(self): # We use a _v_ attribute internally so that data aren't saved in ZODB. # If data are pickled in other contexts, the data will be carried along. # There's no harm in pickling the extra data othr than that it would # be somewhat wasteful. It's doubtful that that's an issue anyway. try: return self._v_utility_registrations_cache except AttributeError: self._v_utility_registrations_cache = _UtilityRegistrations( self.utilities, self._utility_registrations) return self._v_utility_registrations_cache def _getBases(self): # Subclasses might override return self.__dict__.get('__bases__', ()) def _setBases(self, bases): # Subclasses might override self.adapters.__bases__ = tuple([base.adapters for base in bases]) self.utilities.__bases__ = tuple([base.utilities for base in bases]) self.__dict__['__bases__'] = tuple(bases) __bases__ = property( lambda self: self._getBases(), lambda self, bases: self._setBases(bases), ) def registerUtility(self, component=None, provided=None, name=u'', info=u'', event=True, factory=None): if factory: if component: raise TypeError("Can't specify factory and component.") component = factory() if provided is None: provided = _getUtilityProvided(component) if name == u'': name = _getName(component) reg = self._utility_registrations.get((provided, name)) if reg is not None: if reg[:2] == (component, info): # already registered return self.unregisterUtility(reg[0], provided, name) self._utility_registrations_cache.registerUtility( provided, name, component, info, factory) if event: notify( Registered( UtilityRegistration(self, provided, name, component, info, factory))) def unregisterUtility(self, component=None, provided=None, name=u'', factory=None): if factory: if component: raise TypeError("Can't specify factory and component.") component = factory() if provided is None: if component is None: raise TypeError("Must specify one of component, factory and " "provided") provided = _getUtilityProvided(component) old = self._utility_registrations.get((provided, name)) if (old is None) or ((component is not None) and (component != old[0])): return False if component is None: component = old[0] # Note that component is now the old thing registered self._utility_registrations_cache.unregisterUtility( provided, name, component) notify( Unregistered( UtilityRegistration(self, provided, name, component, *old[1:]))) return True def registeredUtilities(self): for ((provided, name), data) in iter(self._utility_registrations.items()): yield UtilityRegistration(self, provided, name, *data) def queryUtility(self, provided, name=u'', default=None): return self.utilities.lookup((), provided, name, default) def getUtility(self, provided, name=u''): utility = self.utilities.lookup((), provided, name) if utility is None: raise ComponentLookupError(provided, name) return utility def getUtilitiesFor(self, interface): for name, utility in self.utilities.lookupAll((), interface): yield name, utility def getAllUtilitiesRegisteredFor(self, interface): return self.utilities.subscriptions((), interface) def registerAdapter(self, factory, required=None, provided=None, name=u'', info=u'', event=True): if provided is None: provided = _getAdapterProvided(factory) required = _getAdapterRequired(factory, required) if name == u'': name = _getName(factory) self._adapter_registrations[(required, provided, name)] = factory, info self.adapters.register(required, provided, name, factory) if event: notify( Registered( AdapterRegistration(self, required, provided, name, factory, info))) def unregisterAdapter( self, factory=None, required=None, provided=None, name=u'', ): if provided is None: if factory is None: raise TypeError("Must specify one of factory and provided") provided = _getAdapterProvided(factory) if (required is None) and (factory is None): raise TypeError("Must specify one of factory and required") required = _getAdapterRequired(factory, required) old = self._adapter_registrations.get((required, provided, name)) if (old is None) or ((factory is not None) and (factory != old[0])): return False del self._adapter_registrations[(required, provided, name)] self.adapters.unregister(required, provided, name) notify( Unregistered( AdapterRegistration(self, required, provided, name, *old))) return True def registeredAdapters(self): for ((required, provided, name), (component, info)) in iter(self._adapter_registrations.items()): yield AdapterRegistration(self, required, provided, name, component, info) def queryAdapter(self, object, interface, name=u'', default=None): return self.adapters.queryAdapter(object, interface, name, default) def getAdapter(self, object, interface, name=u''): adapter = self.adapters.queryAdapter(object, interface, name) if adapter is None: raise ComponentLookupError(object, interface, name) return adapter def queryMultiAdapter(self, objects, interface, name=u'', default=None): return self.adapters.queryMultiAdapter(objects, interface, name, default) def getMultiAdapter(self, objects, interface, name=u''): adapter = self.adapters.queryMultiAdapter(objects, interface, name) if adapter is None: raise ComponentLookupError(objects, interface, name) return adapter def getAdapters(self, objects, provided): for name, factory in self.adapters.lookupAll( list(map(providedBy, objects)), provided): adapter = factory(*objects) if adapter is not None: yield name, adapter def registerSubscriptionAdapter(self, factory, required=None, provided=None, name=u'', info=u'', event=True): if name: raise TypeError("Named subscribers are not yet supported") if provided is None: provided = _getAdapterProvided(factory) required = _getAdapterRequired(factory, required) self._subscription_registrations.append( (required, provided, name, factory, info)) self.adapters.subscribe(required, provided, factory) if event: notify( Registered( SubscriptionRegistration(self, required, provided, name, factory, info))) def registeredSubscriptionAdapters(self): for data in self._subscription_registrations: yield SubscriptionRegistration(self, *data) def unregisterSubscriptionAdapter( self, factory=None, required=None, provided=None, name=u'', ): if name: raise TypeError("Named subscribers are not yet supported") if provided is None: if factory is None: raise TypeError("Must specify one of factory and provided") provided = _getAdapterProvided(factory) if (required is None) and (factory is None): raise TypeError("Must specify one of factory and required") required = _getAdapterRequired(factory, required) if factory is None: new = [(r, p, n, f, i) for (r, p, n, f, i) in self._subscription_registrations if not (r == required and p == provided)] else: new = [(r, p, n, f, i) for (r, p, n, f, i) in self._subscription_registrations if not (r == required and p == provided and f == factory)] if len(new) == len(self._subscription_registrations): return False self._subscription_registrations[:] = new self.adapters.unsubscribe(required, provided, factory) notify( Unregistered( SubscriptionRegistration(self, required, provided, name, factory, ''))) return True def subscribers(self, objects, provided): return self.adapters.subscribers(objects, provided) def registerHandler(self, factory, required=None, name=u'', info=u'', event=True): if name: raise TypeError("Named handlers are not yet supported") required = _getAdapterRequired(factory, required) self._handler_registrations.append((required, name, factory, info)) self.adapters.subscribe(required, None, factory) if event: notify( Registered( HandlerRegistration(self, required, name, factory, info))) def registeredHandlers(self): for data in self._handler_registrations: yield HandlerRegistration(self, *data) def unregisterHandler(self, factory=None, required=None, name=u''): if name: raise TypeError("Named subscribers are not yet supported") if (required is None) and (factory is None): raise TypeError("Must specify one of factory and required") required = _getAdapterRequired(factory, required) if factory is None: new = [(r, n, f, i) for (r, n, f, i) in self._handler_registrations if r != required] else: new = [(r, n, f, i) for (r, n, f, i) in self._handler_registrations if not (r == required and f == factory)] if len(new) == len(self._handler_registrations): return False self._handler_registrations[:] = new self.adapters.unsubscribe(required, None, factory) notify( Unregistered(HandlerRegistration(self, required, name, factory, ''))) return True def handle(self, *objects): self.adapters.subscribers(objects, None)
class Components(object): _v_utility_registrations_cache = None def __init__(self, name='', bases=()): # __init__ is used for test cleanup as well as initialization. # XXX add a separate API for test cleanup. assert isinstance(name, STRING_TYPES) self.__name__ = name self._init_registries() self._init_registrations() self.__bases__ = tuple(bases) self._v_utility_registrations_cache = None def __repr__(self): return "<%s %s>" % (self.__class__.__name__, self.__name__) def __reduce__(self): # Mimic what a persistent.Persistent object does and elide # _v_ attributes so that they don't get saved in ZODB. # This allows us to store things that cannot be pickled in such # attributes. reduction = super(Components, self).__reduce__() # (callable, args, state, listiter, dictiter) # We assume the state is always a dict; the last three items # are technically optional and can be missing or None. filtered_state = { k: v for k, v in reduction[2].items() if not k.startswith('_v_') } reduction = list(reduction) reduction[2] = filtered_state return tuple(reduction) def _init_registries(self): # Subclasses have never been required to call this method # if they override it, merely to fill in these two attributes. self.adapters = AdapterRegistry() self.utilities = AdapterRegistry() def _init_registrations(self): self._utility_registrations = {} self._adapter_registrations = {} self._subscription_registrations = [] self._handler_registrations = [] @property def _utility_registrations_cache(self): # We use a _v_ attribute internally so that data aren't saved in ZODB, # because this object cannot be pickled. cache = self._v_utility_registrations_cache if (cache is None or cache._utilities is not self.utilities or cache._utility_registrations is not self._utility_registrations): cache = self._v_utility_registrations_cache = _UtilityRegistrations( self.utilities, self._utility_registrations) return cache def _getBases(self): # Subclasses might override return self.__dict__.get('__bases__', ()) def _setBases(self, bases): # Subclasses might override self.adapters.__bases__ = tuple([base.adapters for base in bases]) self.utilities.__bases__ = tuple([base.utilities for base in bases]) self.__dict__['__bases__'] = tuple(bases) __bases__ = property( lambda self: self._getBases(), lambda self, bases: self._setBases(bases), ) def registerUtility(self, component=None, provided=None, name=u'', info=u'', event=True, factory=None): if factory: if component: raise TypeError("Can't specify factory and component.") component = factory() if provided is None: provided = _getUtilityProvided(component) if name == u'': name = _getName(component) reg = self._utility_registrations.get((provided, name)) if reg is not None: if reg[:2] == (component, info): # already registered return self.unregisterUtility(reg[0], provided, name) self._utility_registrations_cache.registerUtility( provided, name, component, info, factory) if event: notify( Registered( UtilityRegistration(self, provided, name, component, info, factory))) def unregisterUtility(self, component=None, provided=None, name=u'', factory=None): if factory: if component: raise TypeError("Can't specify factory and component.") component = factory() if provided is None: if component is None: raise TypeError("Must specify one of component, factory and " "provided") provided = _getUtilityProvided(component) old = self._utility_registrations.get((provided, name)) if (old is None) or ((component is not None) and (component != old[0])): return False if component is None: component = old[0] # Note that component is now the old thing registered self._utility_registrations_cache.unregisterUtility( provided, name, component) notify( Unregistered( UtilityRegistration(self, provided, name, component, *old[1:]))) return True def registeredUtilities(self): for ((provided, name), data) in iter(self._utility_registrations.items()): yield UtilityRegistration(self, provided, name, *data) def queryUtility(self, provided, name=u'', default=None): return self.utilities.lookup((), provided, name, default) def getUtility(self, provided, name=u''): utility = self.utilities.lookup((), provided, name) if utility is None: raise ComponentLookupError(provided, name) return utility def getUtilitiesFor(self, interface): for name, utility in self.utilities.lookupAll((), interface): yield name, utility def getAllUtilitiesRegisteredFor(self, interface): return self.utilities.subscriptions((), interface) def registerAdapter(self, factory, required=None, provided=None, name=u'', info=u'', event=True): if provided is None: provided = _getAdapterProvided(factory) required = _getAdapterRequired(factory, required) if name == u'': name = _getName(factory) self._adapter_registrations[(required, provided, name)] = factory, info self.adapters.register(required, provided, name, factory) if event: notify( Registered( AdapterRegistration(self, required, provided, name, factory, info))) def unregisterAdapter( self, factory=None, required=None, provided=None, name=u'', ): if provided is None: if factory is None: raise TypeError("Must specify one of factory and provided") provided = _getAdapterProvided(factory) if (required is None) and (factory is None): raise TypeError("Must specify one of factory and required") required = _getAdapterRequired(factory, required) old = self._adapter_registrations.get((required, provided, name)) if (old is None) or ((factory is not None) and (factory != old[0])): return False del self._adapter_registrations[(required, provided, name)] self.adapters.unregister(required, provided, name) notify( Unregistered( AdapterRegistration(self, required, provided, name, *old))) return True def registeredAdapters(self): for ((required, provided, name), (component, info)) in iter(self._adapter_registrations.items()): yield AdapterRegistration(self, required, provided, name, component, info) def queryAdapter(self, object, interface, name=u'', default=None): return self.adapters.queryAdapter(object, interface, name, default) def getAdapter(self, object, interface, name=u''): adapter = self.adapters.queryAdapter(object, interface, name) if adapter is None: raise ComponentLookupError(object, interface, name) return adapter def queryMultiAdapter(self, objects, interface, name=u'', default=None): return self.adapters.queryMultiAdapter(objects, interface, name, default) def getMultiAdapter(self, objects, interface, name=u''): adapter = self.adapters.queryMultiAdapter(objects, interface, name) if adapter is None: raise ComponentLookupError(objects, interface, name) return adapter def getAdapters(self, objects, provided): for name, factory in self.adapters.lookupAll( list(map(providedBy, objects)), provided): adapter = factory(*objects) if adapter is not None: yield name, adapter def registerSubscriptionAdapter(self, factory, required=None, provided=None, name=u'', info=u'', event=True): if name: raise TypeError("Named subscribers are not yet supported") if provided is None: provided = _getAdapterProvided(factory) required = _getAdapterRequired(factory, required) self._subscription_registrations.append( (required, provided, name, factory, info)) self.adapters.subscribe(required, provided, factory) if event: notify( Registered( SubscriptionRegistration(self, required, provided, name, factory, info))) def registeredSubscriptionAdapters(self): for data in self._subscription_registrations: yield SubscriptionRegistration(self, *data) def unregisterSubscriptionAdapter( self, factory=None, required=None, provided=None, name=u'', ): if name: raise TypeError("Named subscribers are not yet supported") if provided is None: if factory is None: raise TypeError("Must specify one of factory and provided") provided = _getAdapterProvided(factory) if (required is None) and (factory is None): raise TypeError("Must specify one of factory and required") required = _getAdapterRequired(factory, required) if factory is None: new = [(r, p, n, f, i) for (r, p, n, f, i) in self._subscription_registrations if not (r == required and p == provided)] else: new = [(r, p, n, f, i) for (r, p, n, f, i) in self._subscription_registrations if not (r == required and p == provided and f == factory)] if len(new) == len(self._subscription_registrations): return False self._subscription_registrations[:] = new self.adapters.unsubscribe(required, provided, factory) notify( Unregistered( SubscriptionRegistration(self, required, provided, name, factory, ''))) return True def subscribers(self, objects, provided): return self.adapters.subscribers(objects, provided) def registerHandler(self, factory, required=None, name=u'', info=u'', event=True): if name: raise TypeError("Named handlers are not yet supported") required = _getAdapterRequired(factory, required) self._handler_registrations.append((required, name, factory, info)) self.adapters.subscribe(required, None, factory) if event: notify( Registered( HandlerRegistration(self, required, name, factory, info))) def registeredHandlers(self): for data in self._handler_registrations: yield HandlerRegistration(self, *data) def unregisterHandler(self, factory=None, required=None, name=u''): if name: raise TypeError("Named subscribers are not yet supported") if (required is None) and (factory is None): raise TypeError("Must specify one of factory and required") required = _getAdapterRequired(factory, required) if factory is None: new = [(r, n, f, i) for (r, n, f, i) in self._handler_registrations if r != required] else: new = [(r, n, f, i) for (r, n, f, i) in self._handler_registrations if not (r == required and f == factory)] if len(new) == len(self._handler_registrations): return False self._handler_registrations[:] = new self.adapters.unsubscribe(required, None, factory) notify( Unregistered(HandlerRegistration(self, required, name, factory, ''))) return True def handle(self, *objects): self.adapters.subscribers(objects, None)
class Components(object): _v_utility_registrations_cache = None def __init__(self, name='', bases=()): # __init__ is used for test cleanup as well as initialization. # XXX add a separate API for test cleanup. assert isinstance(name, STRING_TYPES) self.__name__ = name self._init_registries() self._init_registrations() self.__bases__ = tuple(bases) self._v_utility_registrations_cache = None def __repr__(self): return "<%s %s>" % (self.__class__.__name__, self.__name__) def __reduce__(self): # Mimic what a persistent.Persistent object does and elide # _v_ attributes so that they don't get saved in ZODB. # This allows us to store things that cannot be pickled in such # attributes. reduction = super(Components, self).__reduce__() # (callable, args, state, listiter, dictiter) # We assume the state is always a dict; the last three items # are technically optional and can be missing or None. filtered_state = {k: v for k, v in reduction[2].items() if not k.startswith('_v_')} reduction = list(reduction) reduction[2] = filtered_state return tuple(reduction) def _init_registries(self): # Subclasses have never been required to call this method # if they override it, merely to fill in these two attributes. self.adapters = AdapterRegistry() self.utilities = AdapterRegistry() def _init_registrations(self): self._utility_registrations = {} self._adapter_registrations = {} self._subscription_registrations = [] self._handler_registrations = [] @property def _utility_registrations_cache(self): # We use a _v_ attribute internally so that data aren't saved in ZODB, # because this object cannot be pickled. cache = self._v_utility_registrations_cache if (cache is None or cache._utilities is not self.utilities or cache._utility_registrations is not self._utility_registrations): cache = self._v_utility_registrations_cache = _UtilityRegistrations( self.utilities, self._utility_registrations) return cache def _getBases(self): # Subclasses might override return self.__dict__.get('__bases__', ()) def _setBases(self, bases): # Subclasses might override self.adapters.__bases__ = tuple([ base.adapters for base in bases]) self.utilities.__bases__ = tuple([ base.utilities for base in bases]) self.__dict__['__bases__'] = tuple(bases) __bases__ = property( lambda self: self._getBases(), lambda self, bases: self._setBases(bases), ) def registerUtility(self, component=None, provided=None, name=u'', info=u'', event=True, factory=None): if factory: if component: raise TypeError("Can't specify factory and component.") component = factory() if provided is None: provided = _getUtilityProvided(component) if name == u'': name = _getName(component) reg = self._utility_registrations.get((provided, name)) if reg is not None: if reg[:2] == (component, info): # already registered return self.unregisterUtility(reg[0], provided, name) self._utility_registrations_cache.registerUtility( provided, name, component, info, factory) if event: notify(Registered( UtilityRegistration(self, provided, name, component, info, factory) )) def unregisterUtility(self, component=None, provided=None, name=u'', factory=None): if factory: if component: raise TypeError("Can't specify factory and component.") component = factory() if provided is None: if component is None: raise TypeError("Must specify one of component, factory and " "provided") provided = _getUtilityProvided(component) old = self._utility_registrations.get((provided, name)) if (old is None) or ((component is not None) and (component != old[0])): return False if component is None: component = old[0] # Note that component is now the old thing registered self._utility_registrations_cache.unregisterUtility( provided, name, component) notify(Unregistered( UtilityRegistration(self, provided, name, component, *old[1:]) )) return True def registeredUtilities(self): for ((provided, name), data ) in iter(self._utility_registrations.items()): yield UtilityRegistration(self, provided, name, *data) def queryUtility(self, provided, name=u'', default=None): return self.utilities.lookup((), provided, name, default) def getUtility(self, provided, name=u''): utility = self.utilities.lookup((), provided, name) if utility is None: raise ComponentLookupError(provided, name) return utility def getUtilitiesFor(self, interface): for name, utility in self.utilities.lookupAll((), interface): yield name, utility def getAllUtilitiesRegisteredFor(self, interface): return self.utilities.subscriptions((), interface) def registerAdapter(self, factory, required=None, provided=None, name=u'', info=u'', event=True): if provided is None: provided = _getAdapterProvided(factory) required = _getAdapterRequired(factory, required) if name == u'': name = _getName(factory) self._adapter_registrations[(required, provided, name) ] = factory, info self.adapters.register(required, provided, name, factory) if event: notify(Registered( AdapterRegistration(self, required, provided, name, factory, info) )) def unregisterAdapter(self, factory=None, required=None, provided=None, name=u'', ): if provided is None: if factory is None: raise TypeError("Must specify one of factory and provided") provided = _getAdapterProvided(factory) if (required is None) and (factory is None): raise TypeError("Must specify one of factory and required") required = _getAdapterRequired(factory, required) old = self._adapter_registrations.get((required, provided, name)) if (old is None) or ((factory is not None) and (factory != old[0])): return False del self._adapter_registrations[(required, provided, name)] self.adapters.unregister(required, provided, name) notify(Unregistered( AdapterRegistration(self, required, provided, name, *old) )) return True def registeredAdapters(self): for ((required, provided, name), (component, info) ) in iter(self._adapter_registrations.items()): yield AdapterRegistration(self, required, provided, name, component, info) def queryAdapter(self, object, interface, name=u'', default=None): return self.adapters.queryAdapter(object, interface, name, default) def getAdapter(self, object, interface, name=u''): adapter = self.adapters.queryAdapter(object, interface, name) if adapter is None: raise ComponentLookupError(object, interface, name) return adapter def queryMultiAdapter(self, objects, interface, name=u'', default=None): return self.adapters.queryMultiAdapter( objects, interface, name, default) def getMultiAdapter(self, objects, interface, name=u''): adapter = self.adapters.queryMultiAdapter(objects, interface, name) if adapter is None: raise ComponentLookupError(objects, interface, name) return adapter def getAdapters(self, objects, provided): for name, factory in self.adapters.lookupAll( list(map(providedBy, objects)), provided): adapter = factory(*objects) if adapter is not None: yield name, adapter def registerSubscriptionAdapter(self, factory, required=None, provided=None, name=u'', info=u'', event=True): if name: raise TypeError("Named subscribers are not yet supported") if provided is None: provided = _getAdapterProvided(factory) required = _getAdapterRequired(factory, required) self._subscription_registrations.append( (required, provided, name, factory, info) ) self.adapters.subscribe(required, provided, factory) if event: notify(Registered( SubscriptionRegistration(self, required, provided, name, factory, info) )) def registeredSubscriptionAdapters(self): for data in self._subscription_registrations: yield SubscriptionRegistration(self, *data) def unregisterSubscriptionAdapter(self, factory=None, required=None, provided=None, name=u'', ): if name: raise TypeError("Named subscribers are not yet supported") if provided is None: if factory is None: raise TypeError("Must specify one of factory and provided") provided = _getAdapterProvided(factory) if (required is None) and (factory is None): raise TypeError("Must specify one of factory and required") required = _getAdapterRequired(factory, required) if factory is None: new = [(r, p, n, f, i) for (r, p, n, f, i) in self._subscription_registrations if not (r == required and p == provided) ] else: new = [(r, p, n, f, i) for (r, p, n, f, i) in self._subscription_registrations if not (r == required and p == provided and f == factory) ] if len(new) == len(self._subscription_registrations): return False self._subscription_registrations[:] = new self.adapters.unsubscribe(required, provided, factory) notify(Unregistered( SubscriptionRegistration(self, required, provided, name, factory, '') )) return True def subscribers(self, objects, provided): return self.adapters.subscribers(objects, provided) def registerHandler(self, factory, required=None, name=u'', info=u'', event=True): if name: raise TypeError("Named handlers are not yet supported") required = _getAdapterRequired(factory, required) self._handler_registrations.append( (required, name, factory, info) ) self.adapters.subscribe(required, None, factory) if event: notify(Registered( HandlerRegistration(self, required, name, factory, info) )) def registeredHandlers(self): for data in self._handler_registrations: yield HandlerRegistration(self, *data) def unregisterHandler(self, factory=None, required=None, name=u''): if name: raise TypeError("Named subscribers are not yet supported") if (required is None) and (factory is None): raise TypeError("Must specify one of factory and required") required = _getAdapterRequired(factory, required) if factory is None: new = [(r, n, f, i) for (r, n, f, i) in self._handler_registrations if r != required ] else: new = [(r, n, f, i) for (r, n, f, i) in self._handler_registrations if not (r == required and f == factory) ] if len(new) == len(self._handler_registrations): return False self._handler_registrations[:] = new self.adapters.unsubscribe(required, None, factory) notify(Unregistered( HandlerRegistration(self, required, name, factory, '') )) return True def handle(self, *objects): self.adapters.subscribers(objects, None)
# 10) Errors should never pass silently. # Register an object that depends on IErrors and provides ISilence registry = AdapterRegistry() # XXX Logic below is not quite right registry.register([IErrors], ISilence, 'should not', 'pass') if (registry.lookup([IErrors], ISilence, 'should not') == 'pass' and registry.lookup([Interface], ISilence) is None): print ("Errors should never require a specification that doesn’t extend " "the specification of silence.") # 11) Unless explicitly silenced. errors = Errors() silence = Silence() registry.register([IErrors, ISilence], IPass, '', Explicit) explicit = registry.queryMultiAdapter((errors, silence), IPass) if (explicit.__class__.__name__ == "Explicit" and explicit.errors is errors and explicit.silence is silence): print "Unless explicit is a multi-adapter." # 12) In the face of ambiguity, refuse the temptation to guess. registry.subscribe([IAmbiguity], IGuess, 'refuse the temptation to guess') if 'refuse the temptation to guess' in [sub for sub in registry.subscriptions([IAmbiguity], IGuess)]: print ("In subscribing to ambiguity, return all the objects that refuse " "the temptation to guess.") # 13) There should be one-- and preferably only one --obvious way to do it. registry.subscribe([IErrors], None, handler)