ó
[³XMc        
   @   s  d  Z  d d l Z d d l m Z m Z d d l m Z d d l m Z d d l	 m
 Z
 d e f d     YZ e   Z d	 a e e d
 d  d k r” d   Z n	 d   Z d   Z d   Z e j d  Z e j j e  d   Z d   Z d   Z e Z d d% d     YZ d e
 j f d     YZ d e f d     YZ  d d  Z! d e" f d     YZ# d e" f d     YZ$ d d d  d d d d! d" d# d$ g
 Z% d S(&   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.
i’’’’N(   t	   interfacet   declarations(   t   AdapterRegistry(   t   reflect(   t   stylest   ComponentsDeprecationWarningc           B   s   e  Z d  Z RS(   s-   
    Nothing emits this warning anymore.
    (   t   __name__t
   __module__t   __doc__(    (    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/components.pyR   +   s   i    t
   registeredc         C   s%   |  j  |  j j  | i   j  d  S(   s   
        Return the adapter factory for the given parameters in the given
        registry, or None if there is not one.
        t    (   t   gett   selfImplied(   t   registryt   requiredt   provided(    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/components.pyt   _registered;   s    c         C   s   |  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   R   R   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/components.pyR   B   s    c         G   s³   t  } | s t d  t | t j  s9 t j |  } n  xI | D]A } t | | |  } | d k	 r@ t	 r@ t
 d | f   q@ q@ Wx' | D] } | j | g | d |   q Wd S(   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'.
    s   You need to pass an Interfaces'   an adapter (%s) was already registered.R
   N(   t   globalRegistryt   AssertionErrort
   isinstanceR    t   InterfaceClassR   t   implementedByR   t   Nonet   ALLOW_DUPLICATESt
   ValueErrort   register(   t   adapterFactoryt   origInterfacet   interfaceClassest   selft   interfaceClasst   factory(    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/components.pyt   registerAdapterJ   s    c         C   sU   t  } t |  t j  s* t j |   }  n  | j |  |  } | d k rQ | } n  | S(   s²   Return 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   R   R    R   R   R   t   lookup1R   (   t   fromInterfacet   toInterfacet   defaultR   R   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/components.pyt   getAdapterFactoryb   s    	c         C   s6   | t  j |  |   } | d  k r( d  S| |  Sd  S(   N(   R   t
   providedByR   (   t   ifacet   obt   lookupR   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/components.pyt   _hookr   s    c         C   s   t  j d t d d d S(   s°   DEPRECATED.

    Does nothing. Previously handled backwards compat from a
    zope.interface using class to a class wanting old twisted
    components interface behaviors.
    sY   components.backwardsCompatImplements doesn't do anything in Twisted 2.3, stop calling it.t
   stackleveli   N(   t   warningst   warnR   (   t   klass(    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/components.pyt   backwardsCompatImplements}   s    c         C   s   t  j d t d d d S(   sn   DEPRECATED.

    Does nothing. Previously converted class from __implements__ to
    zope implementation.
    sR   components.fixClassImplements doesn't do anything in Twisted 2.3, stop calling it.R+   i   N(   R,   R-   R   (   R.   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/components.pyt   fixClassImplements   s    c           C   s   t  S(   sW   Returns the Twisted global
    C{zope.interface.adapter.AdapterRegistry} instance.
    (   R   (    (    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/components.pyt   getRegistry   s    t   Adapterc           B   s5   e  Z d  Z d Z d Z d   Z d   Z d   Z RS(   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.
    i    i   c         C   s   | |  _  d S(   sD   Set my 'original' attribute to be the object I am adapting.
        N(   t   original(   R   R3   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/components.pyt   __init__®   s    c         C   s&   t  |  j d  r" |  j j |  Sd S(   sn   
        I forward __conform__ to self.original if it has it, otherwise I
        simply return None.
        t   __conform__N(   t   hasattrR3   R5   R   (   R   R    (    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/components.pyR5   ³   s    c         C   s   |  j  j | |  S(   s1   
        Forward isuper to self.original
        (   R3   t   isuper(   R   R'   t   adapter(    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/components.pyR7   ¼   s    (   R   R   R   t   temporaryAdaptert   multiComponentR4   R5   R7   (    (    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/components.pyR2      s   			t   Componentizedc           B   sw   e  Z d  Z d Z d   Z d   Z d   Z d d  Z d   Z d d  Z	 d	   Z
 d
   Z d d  Z d   Z RS(   sį  I 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.
    i   c         C   s   i  |  _  d  S(   N(   t   _adapterCache(   R   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/components.pyR4   Ń   s    c         C   s   t  | | |  S(   N(   R%   (   R   R.   R   R$   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/components.pyt   locateAdapterClassŌ   s    c         C   s   |  j  | | |    d  S(   N(   t   setComponent(   R   R   t   adapterClass(    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/components.pyt
   setAdapter×   s    i    c         C   s    | |   } |  j  | |  | S(   s³   Utility method that calls addComponent.  I take an adapter class and
        instantiate it with myself as the first argument.

        @return: The adapter instantiated.
        (   t   addComponent(   R   R?   t   ignoreClasst   adapt(    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/components.pyt
   addAdapterŚ   s    c         C   s   | |  j  t j |  <d S(   s	   
        N(   R<   R   t   qual(   R   R   t	   component(    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/components.pyR>   ä   s    c         C   s^   xW t  j |  D]F } | s= |  j |  j | d  | j k r | |  j t j |  <q q Wd S(   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(   R   R&   R=   t	   __class__R   R<   R   RE   (   R   RF   RB   R'   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/components.pyRA   é   s
    c         C   s   |  j  t j |  =d S(   s;   Remove my component specified by the given interface class.N(   R<   R   RE   (   R   R   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/components.pyt   unsetComponent’   s    c         C   sY   g  } xL |  j  j   D]; \ } } | | k r |  j  | =| j t j |   q q W| 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.
        (   R<   t   itemst   appendR   t   namedObject(   R   RF   t   lt   kt   v(    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/components.pyt   removeComponent  s    
c         C   s°   t  j |  } |  j j |  r, |  j | S| j |   } | d k	 r t | d  o\ | j r | |  j | <t | d  r | j r |  j	 |  q n  | d k rØ | S| Sd S(   s  Create 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.
        R9   R:   N(
   R   RE   R<   t   has_keyt	   __adapt__R   R6   R9   R:   RA   (   R   R    R$   RM   R8   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/components.pyt   getComponent  s    
	c         C   s   |  j  |  S(   N(   RR   (   R   R    (    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/components.pyR5   2  s    N(   R   R   R   t   persistenceVersionR4   R=   R@   RD   R>   RA   RH   RO   R   RR   R5   (    (    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/components.pyR;   Ć   s   
			
			!t   ReprableComponentizedc           B   s   e  Z d    Z d   Z RS(   c         C   s   t  j |   d  S(   N(   R;   R4   (   R   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/components.pyR4   7  s    c         C   sC   d d l  m } d d l m } |   } | |  j |  | j   S(   Ni’’’’(   t   StringIO(   t   pprint(   t	   cStringIORU   RV   R<   t   getvalue(   R   RU   RV   t   sio(    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/components.pyt   __repr__:  s
    	(   R   R   R4   RZ   (    (    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/components.pyRT   6  s   	R3   c            sy     f d   } i | d 6} x! |  D] } t  |    | | <q# Wt d t j |   f t f |  } t j | |   | 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.
    c            s   t  |    |  d  S(   N(   t   setattr(   R   R3   (   t   originalAttribute(    s=   /usr/lib/python2.7/dist-packages/twisted/python/components.pyR4   [  s    R4   s   (Proxy for %s)(   t   _ProxyDescriptort   typeR   RE   t   objectR   t   classImplements(   R'   R\   R4   t   contentst   namet   proxy(    (   R\   s=   /usr/lib/python2.7/dist-packages/twisted/python/components.pyt   proxyForInterfaceC  s    t   _ProxiedClassMethodc           B   s    e  Z d  Z d   Z d   Z RS(   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}
    c         C   s   | |  _  | |  _ d  S(   N(   t
   methodNameR\   (   R   Rf   R\   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/components.pyR4   s  s    	c         O   s1   t  | |  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.
        (   t   getattrR\   Rf   (   R   t   oselft   argst   kwR3   t   actualMethod(    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/components.pyt   __call__x  s    	(   R   R   R   R4   Rl   (    (    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/components.pyRe   g  s   	R]   c           B   s5   e  Z d  Z d   Z d d  Z d   Z d   Z RS(   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}
    c         C   s   | |  _  | |  _ d  S(   N(   t   attributeNameR\   (   R   Rm   R\   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/components.pyR4     s    	c         C   sA   | d k r t |  j |  j  St | |  j  } t | |  j  S(   sL   
        Retrieve the C{self.attributeName} property from L{oself}.
        N(   R   Re   Rm   R\   Rg   (   R   Rh   R^   R3   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/components.pyt   __get__  s
    	
c         C   s)   t  | |  j  } t | |  j |  d S(   sE   
        Set the C{self.attributeName} property of L{oself}.
        N(   Rg   R\   R[   Rm   (   R   Rh   t   valueR3   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/components.pyt   __set__„  s    c         C   s&   t  | |  j  } t | |  j  d S(   sH   
        Delete the C{self.attributeName} property of L{oself}.
        N(   Rg   R\   t   delattrRm   (   R   Rh   R3   (    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/components.pyt
   __delete__­  s    N(   R   R   R   R4   R   Rn   Rp   Rr   (    (    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/components.pyR]     s
   		R    R%   R1   Rd   R/   R0   (    (&   R   R,   t   zope.interfaceR    R   t   zope.interface.adapterR   t   twisted.pythonR   t   twisted.persistedR   t   DeprecationWarningR   R   R   Rg   R   R   R    R%   R!   R*   t   adapter_hooksRJ   R/   R0   R1   t	   TypeErrort   CannotAdaptR2   t	   VersionedR;   RT   Rd   R_   Re   R]   t   __all__(    (    (    s=   /usr/lib/python2.7/dist-packages/twisted/python/components.pyt   <module>   s>   									+s$ 1