Ϫfw7dZddlmZddlmZddlmZmZddlm Z ddl m Z e Z da dZdZd Zd Zee d ZeZGd d ZGddZGddeZddZGddZGddZgdZy)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: pass 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. )StringIO)Dict) declarations interface)AdapterRegistry)reflectct}|sJdt|tjst j |}|D]-}|j |g|}|tr td|d|D]}|j|g|d|y)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 (z) was already registered.) globalRegistry isinstancerInterfaceClassr implementedBy registeredALLOW_DUPLICATES ValueErrorregister)adapterFactory origInterfaceinterfaceClassesselfinterfaceClassfactorys ;/usr/lib/python3/dist-packages/twisted/python/components.pyregisterAdapterr2s D <<<  mY%=%= >$22=A *P//=/>B  '7|G94MNO OP+K }o~r>JKct}t|tjst j |}|j ||}||}|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. )r r rr rrlookup1) fromInterface toInterfacedefaultrrs rgetAdapterFactoryr!JsH D mY%=%= >$22=A ll=+6G Nrch|jfd}tjj||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}. cRtj||}|y||SN)r providedBy)ifaceobrlookups r_hookz_addHook.._hookds,004e< ?2; r)rr adapter_hooksappend)registryr)r(s @r_addHookr-Ys0  F""5) LrcBtjj|y)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)hooks r _removeHookr1os""4(rctS)zWReturns the Twisted global C{zope.interface.adapter.AdapterRegistry} instance. )r rr getRegistryr4}s  rc*eZdZdZdZdZdZdZdZy)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. rc||_y)z;Set my 'original' attribute to be the object I am adapting.Noriginal)rr:s r__init__zAdapter.__init__s   rcft|jdr|jj|Sy)zn I forward __conform__ to self.original if it has it, otherwise I simply return None. __conform__N)hasattrr:r=rrs rr=zAdapter.__conform__s* 4==- 0==,,Y7 7rc:|jj||S)z1 Forward isuper to self.original )r:isuper)rr&adapters rrAzAdapter.isupers}}##E733rN) __name__ __module__ __qualname____doc__temporaryAdaptermultiComponentr;r=rAr3rrr6r6s#$N!4rr6cVeZdZdZdZdZdZdZddZdZ ddZ d Z d Z dd Z d Zy ) 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. r7ci|_yr$) _adapterCachers rr;zComponentized.__init__s rct|||Sr$)r!)rklassrr s rlocateAdapterClassz Componentized.locateAdapterClasss @@rc4|j|||y)zv Cache a provider for the given interface, by adapting C{self} using the given adapter class. N) setComponent)rr adapterClasss r setAdapterzComponentized.setAdapters .,t*<=rc:||}|j|||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)rrS ignoreClassadapts r addAdapterzComponentized.addAdapters# T" %- rcH||jtj|<y)z: Cache a provider of the given interface. NrLrqual)rr components rrRzComponentized.setComponentsrGrHrV)rrr rgrBs r getComponentzComponentized.getComponents LL # "" "%%a( ()i))$/G"!349Q9Q(/""1%7$45':P:P%%g.Nrc$|j|Sr$)rlr?s rr=zComponentized.__conform__#s  ++r)rr$)rCrDrErFpersistenceVersionr;rPrTrYrRrVrarirlr=r3rrrJrJsD  A>E D*= >,rrJceZdZdZdefdZy)ReprableComponentizedc.tj|yr$)rJr;rMs rr;zReprableComponentized.__init__(st$rreturnchddlm}t}||j||jS)Nr)pprint)rtrrLgetvalue)rrtsios r__repr__zReprableComponentized.__repr__+s(!jt!!3'||~rN)rCrDrEr;strrwr3rrrprp's%#rrpcfd}d|i}|D]}t|||<tdtj|dtf|}t j |||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. c t||yr$)setattr)rr:originalAttributes rr;z#proxyForInterface..__init__Ls'2rr;z (Proxy for ))_ProxyDescriptortyperr\objectrclassImplements)r&r|r;contentsnameproxys ` rproxyForInterfacer3sp23$.x"8HC)$0ABC ;w||E2315y( KE  . LrceZdZdZdZdZy)_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} c.|x|_|_||_yr$) methodNamerCr|)rrr|s rr;z_ProxiedClassMethod.__init__js*44$-!2rcjt||j}t||j}||i|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. )getattrr|r)roselfargskwr: actualMethods r__call__z_ProxiedClassMethod.__call__ns65$"8"89x9 T(R((rN)rCrDrErFr;rr3rrrrYs 3 )rrc*eZdZdZdZddZdZdZy)r~a 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} c ||_||_yr$) attributeNamer|)rrr|s rr;z_ProxyDescriptor.__init__s*!2rNc| t|j|jSt||j}t||jS)zL Retrieve the C{self.attributeName} property from I{oself}. )rrr|r)rrrr:s r__get__z_ProxyDescriptor.__get__sG =&t'9'94;Q;QR R5$"8"89x!3!344rc^t||j}t||j|y)zE Set the C{self.attributeName} property of I{oself}. N)rr|r{r)rrvaluer:s r__set__z_ProxyDescriptor.__set__s(5$"8"89$,,e4rc\t||j}t||jy)zH Delete the C{self.attributeName} property of I{oself}. N)rr|delattrr)rrr:s r __delete__z_ProxyDescriptor.__delete__s&5$"8"89$,,-rr$)rCrDrErFr;rrrr3rrr~r~|s 355.rr~)rr!r6rJrpr4rNr9)rFiortypingrzope.interfacerrzope.interface.adapterrtwisted.pythonrr rrr!r-r1r4 TypeError CannotAdaptr6rJrprrr~__all__r3rrrs 632#!"K0 ,)  '4'4Tr,r,j M #L ) )F(.(.V r