Your IP : 172.28.240.42


Current Path : /usr/lib/python2.7/dist-packages/twisted/python/
Upload File :
Current File : //usr/lib/python2.7/dist-packages/twisted/python/components.pyc


lONc
@sdZddlZddlmZmZddlmZddlmZddl	m
Z
defdYZeZ
d	aeed
ddkrdZn	dZd
ZdZdZdZee
dZdZdZeZdd&dYZde
jfdYZdefdYZddZde fdYZ!de fdYZ"dd d!dddd"d#d$d%g
Z#dS('s
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, implements

   class IFoo(Interface):
       pass

   class Foo:
       implements(IFoo)

   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.
iN(t	interfacetdeclarations(tAdapterRegistry(treflect(tstylestComponentsDeprecationWarningcBseZdZRS(s-
    Nothing emits this warning anymore.
    (t__name__t
__module__t__doc__(((s=/usr/lib/python2.7/dist-packages/twisted/python/components.pyR+sit
registeredcCs%|j|jj|ijdS(s
        Return the adapter factory for the given parameters in the given
        registry, or None if there is not one.
        t(tgettselfImplied(tregistrytrequiredtprovided((s=/usr/lib/python2.7/dist-packages/twisted/python/components.pyt_registered;scCs|j|g|S(s
        Return the adapter factory for the given parameters in the given
        registry, or None if there is not one.
        (R	(R
RR((s=/usr/lib/python2.7/dist-packages/twisted/python/components.pyRBscGst}|stdt|tjs<tj|}nxI|D]A}t|||}|dk	rCt	rCt
d|fqCqCWx'|D]}|j|g|d|qWdS(s
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'.
    sYou need to pass an Interfaces'an adapter (%s) was already registered.R
N(tglobalRegistrytAssertionErrort
isinstanceRtInterfaceClassRt
implementedByRtNonetALLOW_DUPLICATESt
ValueErrortregister(tadapterFactoryt
origInterfacetinterfaceClassestselftinterfaceClasstfactory((s=/usr/lib/python2.7/dist-packages/twisted/python/components.pytregisterAdapterJs

cCsUt}t|tjs*tj|}n|j||}|dkrQ|}n|S(sReturn 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(RRRRRRtlookup1R(t
fromInterfacettoInterfacetdefaultRR((s=/usr/lib/python2.7/dist-packages/twisted/python/components.pytgetAdapterFactorybs	cs,|jfd}tjj||S(s
    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}.
    cs6tj||}|dkr(dS||SdS(N(Rt
providedByR(tifacetobR(tlookup(s=/usr/lib/python2.7/dist-packages/twisted/python/components.pyt_hook{s(R!Rt
adapter_hookstappend(R
R*((R)s=/usr/lib/python2.7/dist-packages/twisted/python/components.pyt_addHookqs		cCstjj|dS(s
    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+tremove(thook((s=/usr/lib/python2.7/dist-packages/twisted/python/components.pyt_removeHookscCstjdtdddS(sDEPRECATED.

    Does nothing. Previously handled backwards compat from a
    zope.interface using class to a class wanting old twisted
    components interface behaviors.
    sYcomponents.backwardsCompatImplements doesn't do anything in Twisted 2.3, stop calling it.t
stackleveliN(twarningstwarnR(tklass((s=/usr/lib/python2.7/dist-packages/twisted/python/components.pytbackwardsCompatImplementsscCstjdtdddS(snDEPRECATED.

    Does nothing. Previously converted class from __implements__ to
    zope implementation.
    sRcomponents.fixClassImplements doesn't do anything in Twisted 2.3, stop calling it.R1iN(R2R3R(R4((s=/usr/lib/python2.7/dist-packages/twisted/python/components.pytfixClassImplementsscCstS(sWReturns the Twisted global
    C{zope.interface.adapter.AdapterRegistry} instance.
    (R(((s=/usr/lib/python2.7/dist-packages/twisted/python/components.pytgetRegistrystAdaptercBs5eZdZdZdZdZdZdZRS(s/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.
    iicCs
||_dS(sDSet my 'original' attribute to be the object I am adapting.
        N(toriginal(RR9((s=/usr/lib/python2.7/dist-packages/twisted/python/components.pyt__init__scCs&t|jdr"|jj|SdS(sn
        I forward __conform__ to self.original if it has it, otherwise I
        simply return None.
        t__conform__N(thasattrR9R;R(RR((s=/usr/lib/python2.7/dist-packages/twisted/python/components.pyR;scCs|jj||S(s1
        Forward isuper to self.original
        (R9tisuper(RR'tadapter((s=/usr/lib/python2.7/dist-packages/twisted/python/components.pyR=s(RRRttemporaryAdaptertmultiComponentR:R;R=(((s=/usr/lib/python2.7/dist-packages/twisted/python/components.pyR8s			t
ComponentizedcBsweZdZdZdZdZdZddZdZddZ	d	Z
d
Zd
dZ
dZRS(sI 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.
    icCs
i|_dS(N(t
_adapterCache(R((s=/usr/lib/python2.7/dist-packages/twisted/python/components.pyR:scCst|||S(N(R%(RR4RR$((s=/usr/lib/python2.7/dist-packages/twisted/python/components.pytlocateAdapterClassscCs|j|||dS(N(tsetComponent(RRtadapterClass((s=/usr/lib/python2.7/dist-packages/twisted/python/components.pyt
setAdaptersicCs ||}|j|||S(sUtility method that calls addComponent.  I take an adapter class and
        instantiate it with myself as the first argument.

        @return: The adapter instantiated.
        (taddComponent(RREtignoreClasstadapt((s=/usr/lib/python2.7/dist-packages/twisted/python/components.pyt
addAdapterscCs||jtj|<dS(s	
        N(RBRtqual(RRt	component((s=/usr/lib/python2.7/dist-packages/twisted/python/components.pyRDscCs^xWtj|D]F}|s=|j|j|d|jkr||jtj|<qqWdS(s
        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(RR&RCt	__class__RRBRRK(RRLRHR'((s=/usr/lib/python2.7/dist-packages/twisted/python/components.pyRGs
cCs|jtj|=dS(s;Remove my component specified by the given interface class.N(RBRRK(RR((s=/usr/lib/python2.7/dist-packages/twisted/python/components.pytunsetComponentscCsYg}xL|jjD];\}}||kr|j|=|jtj|qqW|S(s
        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.
        (RBtitemsR,RtnamedObject(RRLtltktv((s=/usr/lib/python2.7/dist-packages/twisted/python/components.pytremoveComponents
cCstj|}|jj|r,|j|S|j|}|dk	rt|do\|jr||j|<t|dr|jr|j	|qn|dkr|S|SdS(sCreate 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.
        R?R@N(
RRKRBthas_keyt	__adapt__RR<R?R@RG(RRR$RRR>((s=/usr/lib/python2.7/dist-packages/twisted/python/components.pytgetComponent)s

	cCs
|j|S(N(RW(RR((s=/usr/lib/python2.7/dist-packages/twisted/python/components.pyR;JsN(RRRtpersistenceVersionR:RCRFRJRDRGRNRTRRWR;(((s=/usr/lib/python2.7/dist-packages/twisted/python/components.pyRAs
			
			!tReprableComponentizedcBseZdZdZRS(cCstj|dS(N(RAR:(R((s=/usr/lib/python2.7/dist-packages/twisted/python/components.pyR:OscCsCddlm}ddlm}|}||j||jS(Ni(tStringIO(tpprint(t	cStringIORZR[RBtgetvalue(RRZR[tsio((s=/usr/lib/python2.7/dist-packages/twisted/python/components.pyt__repr__Rs
	(RRR:R_(((s=/usr/lib/python2.7/dist-packages/twisted/python/components.pyRYNs	R9csyfd}i|d6}x!|D]}t|||<q#Wtdtj|ftf|}tj|||S(s
    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||dS(N(tsetattr(RR9(toriginalAttribute(s=/usr/lib/python2.7/dist-packages/twisted/python/components.pyR:ssR:s(Proxy for %s)(t_ProxyDescriptorttypeRRKtobjectRtclassImplements(R'RaR:tcontentstnametproxy((Ras=/usr/lib/python2.7/dist-packages/twisted/python/components.pytproxyForInterface[s

t_ProxiedClassMethodcBs eZdZdZdZRS(s+
    A proxied class method.

    @ivar methodName: the name of the method which this should invoke when
        called.
    @type methodName: C{str}

    @ivar originalAttribute: name of the attribute of the proxy where the
        original object is stored.
    @type orginalAttribute: C{str}
    cCs||_||_dS(N(t
methodNameRa(RRkRa((s=/usr/lib/python2.7/dist-packages/twisted/python/components.pyR:s	cOs1t||j}t||j}|||S(s
        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.
        (tgetattrRaRk(RtoselftargstkwR9tactualMethod((s=/usr/lib/python2.7/dist-packages/twisted/python/components.pyt__call__s	(RRRR:Rq(((s=/usr/lib/python2.7/dist-packages/twisted/python/components.pyRjs	RbcBs5eZdZdZddZdZdZRS(s
    A descriptor which will proxy attribute access, mutation, and
    deletion to the L{original} attribute 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 orginalAttribute: C{str}
    cCs||_||_dS(N(t
attributeNameRa(RRrRa((s=/usr/lib/python2.7/dist-packages/twisted/python/components.pyR:s	cCsA|dkrt|j|jSt||j}t||jS(sL
        Retrieve the C{self.attributeName} property from L{oself}.
        N(RRjRrRaRl(RRmRcR9((s=/usr/lib/python2.7/dist-packages/twisted/python/components.pyt__get__s
	
cCs)t||j}t||j|dS(sE
        Set the C{self.attributeName} property of L{oself}.
        N(RlRaR`Rr(RRmtvalueR9((s=/usr/lib/python2.7/dist-packages/twisted/python/components.pyt__set__scCs&t||j}t||jdS(sH
        Delete the C{self.attributeName} property of L{oself}.
        N(RlRatdelattrRr(RRmR9((s=/usr/lib/python2.7/dist-packages/twisted/python/components.pyt
__delete__sN(RRRR:RRsRuRw(((s=/usr/lib/python2.7/dist-packages/twisted/python/components.pyRbs

		R R%R7RiR5R6(($RR2tzope.interfaceRRtzope.interface.adapterRttwisted.pythonRttwisted.persistedRtDeprecationWarningRRRRlRRR R%R-R0R5R6R7t	TypeErrortCannotAdaptR8t	VersionedRARYRiRdRjRbt__all__(((s=/usr/lib/python2.7/dist-packages/twisted/python/components.pyt<module>s@						

					+s
$ 1