U  W[7@s dZddlmZmZmZddlmZmZddlm Z ddl m Z ddl m Z ddlmZe Zdadd Zd d Zd d ZddZeeddZeZeGdddZeGdddZGdddeZd ddZGdddeZGdddeZd d dddddgZ dS)!a Component architecture for Twisted, based on Zope3 components. Using the Zope3 API directly is strongly recommended. Everything you need is in the top-level of the zope.interface package, e.g.:: from zope.interface import Interface, implementer class IFoo(Interface): pass @implementer(IFoo) class Foo: print(IFoo.implementedBy(Foo)) # True print(IFoo.providedBy(Foo())) # True L{twisted.python.components.registerAdapter} from this module may be used to add to Twisted's global adapter registry. L{twisted.python.components.proxyForInterface} is a factory for classes which allow access to only the parts of another class defined by a specified interface. )divisionabsolute_importprint_function) interface declarations)AdapterRegistry)NativeStringIO)reflect) _oldStylecGsxt}|stdt|tjs&t|}|D],}||g|}|dk r*ts*t d|fq*|D]}| |g|d|q\dS)a Register an adapter class. An adapter class is expected to implement the given interface, by adapting instances implementing 'origInterface'. An adapter class's __init__ method should accept one parameter, an instance implementing 'origInterface'. zYou need to pass an InterfaceNz'an adapter (%s) was already registered.) globalRegistryAssertionError isinstancerInterfaceClassr implementedByZ registeredALLOW_DUPLICATES ValueErrorregister)ZadapterFactoryZ origInterfaceZinterfaceClassesselfinterfaceClassfactoryr;/usr/lib/python3/dist-packages/twisted/python/components.pyregisterAdapter2s    rcCs6t}t|tjst|}|||}|dkr2|}|S)zReturn registered adapter for a given class and interface. Note that is tied to the *Twisted* global registry, and will thus not find adapters registered elsewhere. N)r rrrrrlookup1)Z fromInterfaceZ toInterfacedefaultrrrrrgetAdapterFactoryJs   rcs"|jfdd}tj||S)z Add an adapter hook which will attempt to look up adapters in the given registry. @type registry: L{zope.interface.adapter.AdapterRegistry} @return: The hook which was added, for later use with L{_removeHook}. cs(t||}|dkrdS||SdSN)r providedBy)ifaceobrlookuprr_hookcsz_addHook.._hook)rr adapter_hooksappend)registryr#rr!r_addHookYs   r'cCstj|dS)z Remove a previously added adapter hook. @param hook: An object previously returned by a call to L{_addHook}. This will be removed from the list of adapter hooks. N)rr$remove)hookrrr _removeHookmsr*cCstS)zWReturns the Twisted global C{zope.interface.adapter.AdapterRegistry} instance. )r rrrr getRegistryzsr+c@s0eZdZdZdZdZddZddZdd Zd S) Adaptera/I am the default implementation of an Adapter for some interface. This docstring contains a limerick, by popular demand:: Subclassing made Zope and TR much harder to work with by far. So before you inherit, be sure to declare it Adapter, not PyObject* @cvar temporaryAdapter: If this is True, the adapter will not be persisted on the Componentized. @cvar multiComponent: If this adapter is persistent, should it be automatically registered for all appropriate interfaces. rcCs ||_dS)zDSet my 'original' attribute to be the object I am adapting. N)originalrr.rrr__init__szAdapter.__init__cCst|jdr|j|SdS)zn I forward __conform__ to self.original if it has it, otherwise I simply return None. __conform__N)hasattrr.r1rrrrrr1s  zAdapter.__conform__cCs|j||S)z1 Forward isuper to self.original )r.isuper)rradapterrrrr4szAdapter.isuperN) __name__ __module__ __qualname____doc__temporaryAdaptermultiComponentr0r1r4rrrrr,s  r,c@sjeZdZdZdZddZddZddZdd d Zd d Z dddZ ddZ ddZ dddZ ddZdS) ComponentizedaI am a mixin to allow you to be adapted in various ways persistently. I define a list of persistent adapters. This is to allow adapter classes to store system-specific state, and initialized on demand. The getComponent method implements this. You must also register adapters for this class for the interfaces that you wish to pass to getComponent. Many other classes and utilities listed here are present in Zope3; this one is specific to Twisted. r-cCs i|_dSr) _adapterCacherrrrr0szComponentized.__init__cCs t|||Sr)r)rklassrrrrrlocateAdapterClasssz Componentized.locateAdapterClasscCs||||dS)zv Cache a provider for the given interface, by adapting C{self} using the given adapter class. N) setComponent)rr adapterClassrrr setAdapterszComponentized.setAdapterrcCs||}||||S)zUtility method that calls addComponent. I take an adapter class and instantiate it with myself as the first argument. @return: The adapter instantiated. ) addComponent)rrB ignoreClassZadaptrrr addAdapters zComponentized.addAdaptercCs||jt|<dS)z: Cache a provider of the given interface. Nr=r qual)rr componentrrrrAszComponentized.setComponentcCs>t|D].}|s(||j|d|jkr ||jt|<q dS)a Add a component to me, for all appropriate interfaces. In order to determine which interfaces are appropriate, the component's provided interfaces will be scanned. If the argument 'ignoreClass' is True, then all interfaces are considered appropriate. Otherwise, an 'appropriate' interface is one for which its class has been registered as an adapter for my class according to the rules of getComponent. @return: the list of appropriate interfaces N)rrr@ __class__r=r rH)rrIrErrrrrDszComponentized.addComponentcCs|jt|=dS)z;Remove my component specified by the given interface class.NrG)rrrrrunsetComponentszComponentized.unsetComponentcCs@g}t|jD](\}}||kr|j|=|t|q|S)z Remove the given component from me entirely, for all interfaces for which it has been registered. @return: a list of the interfaces that were removed. )listr=itemsr%r Z namedObject)rrIlkvrrrremoveComponents zComponentized.removeComponentNcCsxt|}||jkr|j|S||}|dk rdt|dr@|jsd||j|<t|drd|jrd|||dkrp|S|SdS)aCreate or retrieve an adapter for the given interface. If such an adapter has already been created, retrieve it from the cache that this instance keeps of all its adapters. Adapters created through this mechanism may safely store system-specific state. If you want to register an adapter that will be created through getComponent, but you don't require (or don't want) your adapter to be cached and kept alive for the lifetime of this Componentized object, set the attribute 'temporaryAdapter' to True on your adapter class. If you want to automatically register an adapter for all appropriate interfaces (with addComponent), set the attribute 'multiComponent' to True on your adapter class. Nr:r;)r rHr=Z __adapt__r2r:r;rD)rrrrOr5rrr getComponents"       zComponentized.getComponentcCs ||Sr)rRr3rrrr1$szComponentized.__conform__)r)r)N)r6r7r8r9ZpersistenceVersionr0r@rCrFrArDrKrQrRr1rrrrr<s    !r<c@seZdZddZddZdS)ReprableComponentizedcCst|dSr)r<r0r>rrrr0)szReprableComponentized.__init__cCs&ddlm}t}||j||S)Nr)pprint)rTrr=getvalue)rrTZsiorrr__repr__,s  zReprableComponentized.__repr__N)r6r7r8r0rVrrrrrS(srSr.csVfdd}d|i}|D]}t|||<qtdt|ftf|}t|||S)a Create a class which proxies all method calls which adhere to an interface to another provider of that interface. This function is intended for creating specialized proxies. The typical way to use it is by subclassing the result:: class MySpecializedProxy(proxyForInterface(IFoo)): def someInterfaceMethod(self, arg): if arg == 3: return 3 return self.original.someInterfaceMethod(arg) @param iface: The Interface to which the resulting object will conform, and which the wrapped object must provide. @param originalAttribute: name of the attribute used to save the original object in the resulting class. Default to C{original}. @type originalAttribute: C{str} @return: A class whose constructor takes the original object as its only argument. Constructing the class creates the proxy. cst||dSr)setattrr/originalAttributerrr0Lsz#proxyForInterface..__init__r0z(Proxy for %s))_ProxyDescriptortyper rHobjectrZclassImplements)rrYr0contentsnameproxyrrXrproxyForInterface4s   r`c@s eZdZdZddZddZdS)_ProxiedClassMethoda A proxied class method. @ivar methodName: the name of the method which this should invoke when called. @type methodName: L{str} @ivar __name__: The name of the method being proxied (the same as C{methodName}). @type __name__: L{str} @ivar originalAttribute: name of the attribute of the proxy where the original object is stored. @type originalAttribute: L{str} cCs||_|_||_dSr) methodNamer6rY)rrbrYrrrr0hs z_ProxiedClassMethod.__init__cOs"t||j}t||j}|||S)z Invoke the specified L{methodName} method of the C{original} attribute for proxyForInterface. @param oself: an instance of a L{proxyForInterface} object. @return: the result of the underlying method. )getattrrYrb)roselfargskwr.Z actualMethodrrr__call__ms  z_ProxiedClassMethod.__call__N)r6r7r8r9r0rgrrrrraXsrac@s2eZdZdZddZd ddZddZd d ZdS) rZa A descriptor which will proxy attribute access, mutation, and deletion to the L{_ProxyDescriptor.originalAttribute} of the object it is being accessed from. @ivar attributeName: the name of the attribute which this descriptor will retrieve from instances' C{original} attribute. @type attributeName: C{str} @ivar originalAttribute: name of the attribute of the proxy where the original object is stored. @type originalAttribute: C{str} cCs||_||_dSr) attributeNamerY)rrhrYrrrr0sz_ProxyDescriptor.__init__NcCs.|dkrt|j|jSt||j}t||jS)zL Retrieve the C{self.attributeName} property from I{oself}. N)rarhrYrc)rrdr[r.rrr__get__s  z_ProxyDescriptor.__get__cCst||j}t||j|dS)zE Set the C{self.attributeName} property of I{oself}. N)rcrYrWrh)rrdvaluer.rrr__set__s z_ProxyDescriptor.__set__cCst||j}t||jdS)zH Delete the C{self.attributeName} property of I{oself}. N)rcrYdelattrrh)rrdr.rrr __delete__s z_ProxyDescriptor.__delete__)N)r6r7r8r9r0rirkrmrrrrrZ|s   rZN)r.)!r9Z __future__rrrZzope.interfacerrZzope.interface.adapterrZtwisted.python.compatrZtwisted.pythonr Ztwisted.python._oldstyler r rrrr'r*r+ TypeErrorZ CannotAdaptr,r<rSr`r\rarZ__all__rrrrs>     +x $$0