from zope.interface.adapter import AdapterRegistry from zope.interface.interface import adapter_hooks import zope.interface registry = AdapterRegistry() def hook(provided, object): adapter = registry.lookup1(zope.interface.providedBy(object), provided, '') if not adapter: return return adapter(object) adapter_hooks.append(hook) def configure_registry(): from jukebox import interfaces from jukebox import storage registry.register( [interfaces.ISearchableStorage], interfaces.IStorage, '', storage.NoOpStorageAdaptor ) registry.register( [interfaces.IStorage], interfaces.ISearchableStorage, '', storage.NoOpSearchableStorage
# Detect FilePath like objects IFilePathExtended.providedBy: lambda obj: DevicePathFilesystem(path=obj), } def _adapt(iface, obj): """ Adapt the ``filesystem`` argument of ``mount`` to ``IMountableFilesystem``. """ for wrapper_test, wrapper in IMOUNTABLE_FILESYSTEM_ADAPTERS.items(): if wrapper_test(obj): return wrapper(obj) zope_interface_adapter_hooks.append(_adapt) @implementer(IMountedFilesystem) class MountedFileSystem(PClass): """ A directory where a filesystem is mounted. """ mountpoint = interface_field((IFilePathExtended, ), mandatory=True) def unmount(self): _unmount(self.mountpoint) def __enter__(self): return self.mountpoint
def adapter_for(orig, *interfaceClasses): def decorator(implementor): register(implementor, orig, *interfaceClasses) return implementor return decorator def deregister(implementor, orig, *interfaceClasses): if not interfaceClasses: interfaceClasses = tuple(implementedBy(implementor)) register(None, orig, *interfaceClasses) def lookup(targetinterface, obj): try: sourceinterface = fakeimplementeds[type(obj)] except KeyError: sourceinterface = providedBy(obj) implementor = registry.lookup1(sourceinterface, targetinterface, '') if implementor == None: return None return implementor(obj) adapter_hooks.append(lookup)
def adapt_obj_to_iconstraint(iface, t): if iface is not IConstraint: return None assert not IConstraint.providedBy(t) # not sure about this c = constraintMap.get(t, None) if c: return c for (typ, constraintMaker) in constraintTypeMap: if isinstance(t, typ): c = constraintMaker(t) if c: return c # RIFoo means accept either a Referenceable that implements RIFoo, or a # RemoteReference that points to just such a Referenceable. This is # hooked in by remoteinterface.py, when it calls addToConstraintTypeMap # we are the only way to make constraints raise UnknownSchemaType("can't make constraint from '%s' (%s)" % (t, type(t))) from zope.interface.interface import adapter_hooks adapter_hooks.append(adapt_obj_to_iconstraint) # how to accept "([(ref0" ? # X = "TupleOf(ListOf(TupleOf(" * infinity # ok, so you can't write a constraint that accepts it. I'm ok with that.
IMOUNTABLE_FILESYSTEM_ADAPTERS = { # Detect FilePath like objects IFilePathExtended.providedBy: lambda obj: DevicePathFilesystem(path=obj), } def _adapt(iface, obj): """ Adapt the ``filesystem`` argument of ``mount`` to ``IMountableFilesystem``. """ for wrapper_test, wrapper in IMOUNTABLE_FILESYSTEM_ADAPTERS.items(): if wrapper_test(obj): return wrapper(obj) zope_interface_adapter_hooks.append(_adapt) @implementer(IMountedFilesystem) class MountedFileSystem(PClass): """ A directory where a filesystem is mounted. """ mountpoint = interface_field((IFilePathExtended,), mandatory=True) def unmount(self): _unmount(self.mountpoint) def __enter__(self): return self.mountpoint
""" def _decorator(implementor): #pylint: disable=C0111 register(implementor, orig, *target_interfaces) return implementor return _decorator def deregister(implementor, orig, *target_interfaces): "exact opposite of register()" if not target_interfaces: target_interfaces = tuple(implementedBy(implementor)) register(None, orig, *target_interfaces) def lookup(targetinterface, obj): "look up to see if there are any adapters to adapt an obj to a targetinterface" try: sourceinterface = fakeimplementeds[type(obj)] except KeyError: sourceinterface = providedBy(obj) if targetinterface == sourceinterface: return obj implementor = registry.lookup1(sourceinterface, targetinterface, '') if implementor == None: return None return implementor(obj) adapter_hooks.append(lookup)
return packages def notify(*event): """ Send event to event listeners """ registry.subscribers(event, None) def objectEventNotify(event): registry.subscribers((event.object, event), None) def adapterHook(iface, obj, name='', default=None): return registry.queryAdapter(obj, iface, name, default) adapter_hooks.append(adapterHook) _cleanups = set() def cleanup(handler): _cleanups.add(handler) return handler def cleanup_system(*modIds): mods.clear() for h in _cleanups: h()