ó
ÔËÞKc           @   sø  d  Z  d Z d d l Z d d l Z d d l m Z m Z d d l m Z d d l m	 Z	 m
 Z
 m Z d d l m Z i  Z d e f d	 „  ƒ  YZ d
 e f d „  ƒ  YZ d „  Z e Z d „  Z d „  Z d „  Z d d2 d „  ƒ  YZ d d3 d „  ƒ  YZ d „  Z d „  Z d „  Z d e f d „  ƒ  YZ e Z e j ƒ  Z d „  Z e e _  y  d d l m! Z! e! e" f Z# Wn e$ k
 r‰e" f Z# n Xd „  Z% d „  Z& d „  Z' d e( f d „  ƒ  YZ) e) Z* y d d l+ Z+ Wn e$ k
 rän Xd d  l+ m* Z* d! e e* f d" „  ƒ  YZ, d# „  Z- d$ „  Z. d% „  Z/ d& d4 d' „  ƒ  YZ0 d( „  Z1 d) „  Z2 d* „  Z3 d+ „  Z4 d, e( f d- „  ƒ  YZ5 e5 Z6 d d. „ Z8 e ƒ  Z9 y d d l+ Z+ Wn e$ k
 r´n7 Xd d/ l+ m Z m4 Z4 d d0 l+ m3 Z3 d d1 l+ m6 Z6 e6 ƒ  Z: d S(5   së  Implementation of interface declarations

There are three flavors of declarations:

  - Declarations are used to simply name declared interfaces.

  - ImplementsDeclarations are used to express the interfaces that a
    class implements (that instances of the class provides).

    Implements specifications support inheriting interfaces.

  - ProvidesDeclarations are used to express interfaces directly
    provided by objects.


$Id: declarations.py 110736 2010-04-11 10:59:30Z regebro $
t   restructuredtextiÿÿÿÿN(   t   InterfaceClasst   Specification(   t   SpecificationBase(   t
   ModuleTypet
   MethodTypet   FunctionType(   t   addClassAdvisort   Declarationc           B   sS   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 e	 Z
 RS(   s   Interface declarationsc         G   s   t  j |  t | ƒ ƒ d  S(   N(   R   t   __init__t   _normalizeargs(   t   selft
   interfaces(    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyR	   ,   s    c         C   s2   t  j |  | ƒ y
 |  ` Wn t k
 r- n Xd  S(   N(   R   t   changedt   _v_attrst   AttributeError(   R   t   originally_changed(    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyR   /   s
    
c         C   s   |  j  | ƒ o | |  j ƒ  k S(   sJ  Test whether an interface is in the specification

        for example:

          >>> from zope.interface import Interface
          >>> class I1(Interface): pass
          ...
          >>> class I2(I1): pass
          ...
          >>> class I3(Interface): pass
          ...
          >>> class I4(I3): pass
          ...
          >>> spec = Declaration(I2, I3)
          >>> spec = Declaration(I4, spec)
          >>> int(I1 in spec)
          0
          >>> int(I2 in spec)
          1
          >>> int(I3 in spec)
          1
          >>> int(I4 in spec)
          1
        (   t   extendsR   (   R   t	   interface(    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyt   __contains__6   s    c         C   s
   |  j  ƒ  S(   s/  Return an iterator for the interfaces in the specification

        for example:

          >>> from zope.interface import Interface
          >>> class I1(Interface): pass
          ...
          >>> class I2(I1): pass
          ...
          >>> class I3(Interface): pass
          ...
          >>> class I4(I3): pass
          ...
          >>> spec = Declaration(I2, I3)
          >>> spec = Declaration(I4, spec)
          >>> i = iter(spec)
          >>> [x.getName() for x in i]
          ['I4', 'I2', 'I3']
          >>> list(i)
          []
        (   R   (   R   (    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyt   __iter__Q   s    c         C   s   t  |  j ƒ S(   sI  Return an iterator of all included and extended interfaces

        for example:

          >>> from zope.interface import Interface
          >>> class I1(Interface): pass
          ...
          >>> class I2(I1): pass
          ...
          >>> class I3(Interface): pass
          ...
          >>> class I4(I3): pass
          ...
          >>> spec = Declaration(I2, I3)
          >>> spec = Declaration(I4, spec)
          >>> i = spec.flattened()
          >>> [x.getName() for x in i]
          ['I4', 'I2', 'I1', 'I3', 'Interface']
          >>> list(i)
          []

        (   t   itert   __iro__(   R   (    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyt	   flattenedi   s    c         C   sT   t  g  |  j ƒ  D]= } g  | j ƒ  D] } | j | d ƒ r# | ^ q# s | ^ q Œ  S(   sÎ  Remove interfaces from a specification

        Examples:

          >>> from zope.interface import Interface
          >>> class I1(Interface): pass
          ...
          >>> class I2(I1): pass
          ...
          >>> class I3(Interface): pass
          ...
          >>> class I4(I3): pass
          ...
          >>> spec = Declaration()
          >>> [iface.getName() for iface in spec]
          []
          >>> spec -= I1
          >>> [iface.getName() for iface in spec]
          []
          >>> spec -= Declaration(I1, I2)
          >>> [iface.getName() for iface in spec]
          []
          >>> spec = Declaration(I2, I4)
          >>> [iface.getName() for iface in spec]
          ['I2', 'I4']
          >>> [iface.getName() for iface in spec - I4]
          ['I2']
          >>> [iface.getName() for iface in spec - I1]
          ['I4']
          >>> [iface.getName() for iface
          ...  in spec - Declaration(I3, I4)]
          ['I2']

        i    (   R   R   R   (   R   t   othert   it   j(    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyt   __sub__‚   s    $c         C   s   i  } g  } x: |  j  ƒ  D], } | | k r d | | <| j | ƒ q q Wx: | j  ƒ  D], } | | k rV d | | <| j | ƒ qV qV Wt | Œ  S(   s  Add two specifications or a specification and an interface

        Examples:

          >>> from zope.interface import Interface
          >>> class I1(Interface): pass
          ...
          >>> class I2(I1): pass
          ...
          >>> class I3(Interface): pass
          ...
          >>> class I4(I3): pass
          ...
          >>> spec = Declaration()
          >>> [iface.getName() for iface in spec]
          []
          >>> [iface.getName() for iface in spec+I1]
          ['I1']
          >>> [iface.getName() for iface in I1+spec]
          ['I1']
          >>> spec2 = spec
          >>> spec += I1
          >>> [iface.getName() for iface in spec]
          ['I1']
          >>> [iface.getName() for iface in spec2]
          []
          >>> spec2 += Declaration(I3, I4)
          >>> [iface.getName() for iface in spec2]
          ['I3', 'I4']
          >>> [iface.getName() for iface in spec+spec2]
          ['I1', 'I3', 'I4']
          >>> [iface.getName() for iface in spec2+spec]
          ['I3', 'I4', 'I1']

        i   (   R   t   appendR   (   R   R   t   seent   resultR   (    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyt   __add__­   s    %

(   t   __name__t
   __module__t   __doc__R	   R   R   R   R   R   R   t   __radd__(    (    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyR   )   s   						+	2t
   Implementsc           B   s,   e  Z d Z d Z d  Z  d „  Z d „  Z RS(   t   ?c         C   s   d |  j  S(   Ns   <implementedBy %s>(   R    (   R   (    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyt   __repr__ò   s    c         C   s   t  |  j f f S(   N(   t   implementedByt   inherit(   R   (    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyt
   __reduce__õ   s    N(    (   R    R!   t   NoneR(   t   declaredR&   R)   (    (    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyR$   è   s
   	c      
   C   s`  y |  j  j d ƒ } Wnu t k
 r t |  d d ƒ } | d k rg t j |  ƒ } | d k	 rc | St S| j t k rz | St	 t
 | f ƒ Œ  SXt | t ƒ r¡ | S| d k rÏ t j |  ƒ } | d k	 rÏ | Sn  | d k	 r| f } t t
 | ƒ Œ  } d | _ |  ` ns y |  j } Wn5 t k
 rLt |  ƒ sCt d |  ƒ ‚ n  d } n Xt g  | D] } t | ƒ ^ qWŒ  } |  | _ t |  d d ƒ pd d t |  d d ƒ p©d | _ ym | |  _ t |  d ƒ s×t |  _ n  t |  t ƒ rd |  j  k rt |  t |  d	 t |  ƒ ƒ ƒ |  _ n  Wn< t k
 r[t |  t ƒ sNt d
 |  ƒ ‚ n  | t |  <n X| S(   sÓ  Return the interfaces implemented for a class' instances

      The value returned is an IDeclaration.

      for example:

        >>> from zope.interface import Interface
        >>> class I1(Interface): pass
        ...
        >>> class I2(I1): pass
        ...
        >>> class I3(Interface): pass
        ...
        >>> class I4(I3): pass
        ...
        >>> class C1(object):
        ...   implements(I2)
        >>> class C2(C1):
        ...   implements(I3)
        >>> [i.getName() for i in implementedBy(C2)]
        ['I3', 'I2']

      Really, any object should be able to receive a successful answer, even
      an instance:

        >>> class Callable(object):
        ...     def __call__(self):
        ...         return self

        >>> implementedBy(Callable())
        <implementedBy zope.interface.declarations.?>

      Note that the name of the spec ends with a '?', because the `Callable`
      instance does not have a `__name__` attribute.
      t   __implemented__s$   ImplementedBy called for non-factoryR!   R%   t   .R    t   __providedBy__t   __provides__t	   __class__s!   ImplementedBy called for non-typeN(    (   t   __dict__t   getR   t   getattrR*   t#   BuiltinImplementationSpecificationst   _emptyR0   R$   R   R
   t
   isinstanceR(   R,   t	   __bases__t   callablet	   TypeErrorR'   R    t   hasattrt   objectSpecificationDescriptorR.   t   DescriptorAwareMetaClassest   ClassProvidest   typeR/   (   t   clst   spect   basest   c(    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyt   implementedByFallbackø   s\    &			
%	5	%c         G   s/   t  |  ƒ } d | _ d | _ t |  | Œ d S(   sü  Declare the only interfaces implemented by instances of a class

      The arguments after the class are one or more interfaces or interface
      specifications (``IDeclaration`` objects).

      The interfaces given (including the interfaces in the specifications)
      replace any previous declarations.

      Consider the following example:

        >>> from zope.interface import Interface
        >>> class I1(Interface): pass
        ...
        >>> class I2(Interface): pass
        ...
        >>> class I3(Interface): pass
        ...
        >>> class I4(Interface): pass
        ...
        >>> class A(object):
        ...   implements(I3)
        >>> class B(object):
        ...   implements(I4)
        >>> class C(A, B):
        ...   pass
        >>> classImplementsOnly(C, I1, I2)
        >>> [i.getName() for i in implementedBy(C)]
        ['I1', 'I2']

      Instances of ``C`` provide only ``I1``, ``I2``, and regardless of
      whatever interfaces instances of ``A`` and ``B`` implement.
      N(    (   R'   R+   R*   R(   t   classImplements(   R?   R   R@   (    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyt   classImplementsOnlyr  s    !		c         G   sÛ   t  |  ƒ } | j t t | ƒ ƒ 7_ g  } i  } x7 | j D], } | | k r= d | | <| j | ƒ q= q= W| j d k	 rÈ xI | j j D]8 } t  | ƒ } | | k r‰ d | | <| j | ƒ q‰ q‰ Wn  t | ƒ | _ d S(   s¸  Declare additional interfaces implemented for instances of a class

      The arguments after the class are one or more interfaces or
      interface specifications (``IDeclaration`` objects).

      The interfaces given (including the interfaces in the specifications)
      are added to any interfaces previously declared.

      Consider the following example:

        >>> from zope.interface import Interface
        >>> class I1(Interface): pass
        ...
        >>> class I2(Interface): pass
        ...
        >>> class I3(Interface): pass
        ...
        >>> class I4(Interface): pass
        ...
        >>> class I5(Interface): pass
        ...
        >>> class A(object):
        ...   implements(I3)
        >>> class B(object):
        ...   implements(I4)
        >>> class C(A, B):
        ...   pass
        >>> classImplements(C, I1, I2)
        >>> [i.getName() for i in implementedBy(C)]
        ['I1', 'I2', 'I3', 'I4']
        >>> classImplements(C, I5)
        >>> [i.getName() for i in implementedBy(C)]
        ['I1', 'I2', 'I5', 'I3', 'I4']

      Instances of ``C`` provide ``I1``, ``I2``, ``I5``, and whatever
      interfaces instances of ``A`` and ``B`` provide.
      i   N(   R'   R+   t   tupleR
   R   R(   R*   R7   (   R?   R   R@   RA   R   t   bRB   (    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyRD   ˜  s    '

c         C   s*   |  j  d \ } } |  ` | |  | Œ |  S(   Nt   __implements_advice_data__(   R1   RH   (   R?   R   RD   (    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyt   _implements_adviceÔ  s    t   implementerc           B   s   e  Z d  „  Z d „  Z RS(   c         G   s   | |  _  d  S(   N(   R   (   R   R   (    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyR	   Ý  s    c         C   sf   t  | t ƒ r# t | |  j Œ | St |  j Œ  } y | | _ Wn  t k
 ra t d | ƒ ‚ n X| S(   Ns   Can't declare implements(   R6   R<   RD   R   R$   R,   R   R9   (   R   t   obR@   (    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyt   __call__à  s    (   R    R!   R	   RL   (    (    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyRJ   Û  s   	t   implementer_onlyc           B   s   e  Z d  „  Z d „  Z RS(   c         G   s   | |  _  d  S(   N(   R   (   R   R   (    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyR	   î  s    c         C   s<   t  | t t f ƒ r$ t d ƒ ‚ n t | |  j Œ | Sd  S(   NsI   The implementor_only decorator is not supported for methods or functions.(   R6   R   R   t
   ValueErrorRE   R   (   R   RK   (    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyRL   ñ  s    (   R    R!   R	   RL   (    (    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyRM   ì  s   	c         C   sœ   t  j d ƒ } | j } | | j k sF d | k rY t  j d  d	 k rY t |  d ƒ ‚ n  d | k rx t |  d ƒ ‚ n  | | f | d <t t d d ƒd  S(
   Ni   R!   i   i    s*    can be used only from a class definition.RH   s-    can be used only once in a class definition.t   depth(   i   i   i    (   t   syst	   _getframet   f_localst	   f_globalst   version_infoR9   R   RI   (   t   nameR   RD   t   framet   locals(    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyt   _implementsý  s    	c          G   s   t  d |  t ƒ d S(   s  Declare interfaces implemented by instances of a class

      This function is called in a class definition.

      The arguments are one or more interfaces or interface
      specifications (IDeclaration objects).

      The interfaces given (including the interfaces in the
      specifications) are added to any interfaces previously
      declared.

      Previous declarations include declarations for base classes
      unless implementsOnly was used.

      This function is provided for convenience. It provides a more
      convenient way to call classImplements. For example::

        implements(I1)

      is equivalent to calling::

        classImplements(C, I1)

      after the class has been created.

      Consider the following example::


        >>> from zope.interface import Interface
        >>> class IA1(Interface): pass
        ...
        >>> class IA2(Interface): pass
        ...
        >>> class IB(Interface): pass
        ...
        >>> class IC(Interface): pass
        ...
        >>> class A(object):
        ...     implements(IA1, IA2)
        >>> class B(object):
        ...     implements(IB)

        >>> class C(A, B):
        ...    implements(IC)

        >>> ob = C()
        >>> int(IA1 in providedBy(ob))
        1
        >>> int(IA2 in providedBy(ob))
        1
        >>> int(IB in providedBy(ob))
        1
        >>> int(IC in providedBy(ob))
        1

      Instances of ``C`` implement ``I1``, ``I2``, and whatever interfaces
      instances of ``A`` and ``B`` implement.

      t
   implementsN(   RX   RD   (   R   (    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyRY     s    <c          G   s   t  d |  t ƒ d S(   su  Declare the only interfaces implemented by instances of a class

      This function is called in a class definition.

      The arguments are one or more interfaces or interface
      specifications (IDeclaration objects).

      Previous declarations including declarations for base classes
      are overridden.

      This function is provided for convenience. It provides a more
      convenient way to call classImplementsOnly. For example::

        implementsOnly(I1)

      is equivalent to calling::

        classImplementsOnly(I1)

      after the class has been created.

      Consider the following example::

        >>> from zope.interface import Interface
        >>> class IA1(Interface): pass
        ...
        >>> class IA2(Interface): pass
        ...
        >>> class IB(Interface): pass
        ...
        >>> class IC(Interface): pass
        ...
        >>> class A(object):
        ...     implements(IA1, IA2)
        >>> class B(object):
        ...     implements(IB)

        >>> class C(A, B):
        ...    implementsOnly(IC)

        >>> ob = C()
        >>> int(IA1 in providedBy(ob))
        0
        >>> int(IA2 in providedBy(ob))
        0
        >>> int(IB in providedBy(ob))
        0
        >>> int(IC in providedBy(ob))
        1


      Instances of ``C`` implement ``IC``, regardless of what
      instances of ``A`` and ``B`` implement.

      t   implementsOnlyN(   RX   RE   (   R   (    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyRZ   L  s    8t   Providesc           B   s/   e  Z d  Z d „  Z d „  Z d Z d „  Z RS(   s   Implement __provides__, the instance-specific specification

    When an object is pickled, we pickle the interfaces that it implements.
    c         G   s:   | f | |  _  | |  _ t j |  | t | ƒ f Œ d  S(   N(   t   _Provides__argst   _clsR   R	   R'   (   R   R?   R   (    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyR	     s    	c         C   s   t  |  j f S(   N(   R[   R\   (   R   (    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyR)   •  s    s   zope.interfacec         C   s/   | d k r | |  j k r |  St d ƒ ‚ d S(   sÍ  Make sure that a class __provides__ doesn't leak to an instance

        For example:

          >>> from zope.interface import Interface
          >>> class IFooFactory(Interface): pass
          ...

          >>> class C(object):
          ...   pass

          >>> C.__provides__ = ProvidesClass(C, IFooFactory)
          >>> [i.getName() for i in C.__provides__]
          ['IFooFactory']
          >>> getattr(C(), '__provides__', 0)
          0

        R/   N(   R*   R]   R   (   R   t   instR?   (    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyt   __get__š  s    (   R    R!   R"   R	   R)   R_   (    (    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyR[   Š  s
   		c          G   s8   t  j |  ƒ } | d k r4 t |  Œ  } | t  |  <n  | S(   s~  Cache instance declarations

      Instance declarations are shared among instances that have the same
      declaration. The declarations are cached in a weak value dictionary.

      (Note that, in the examples below, we are going to make assertions about
       the size of the weakvalue dictionary.  For the assertions to be
       meaningful, we need to force garbage collection to make sure garbage
       objects are, indeed, removed from the system. Depending on how Python
       is run, we may need to make multiple calls to be sure.  We provide a
       collect function to help with this:

       >>> import gc
       >>> def collect():
       ...     for i in range(4):
       ...         gc.collect()

      )

      >>> collect()
      >>> before = len(InstanceDeclarations)

      >>> class C(object):
      ...    pass

      >>> from zope.interface import Interface
      >>> class I(Interface):
      ...    pass

      >>> c1 = C()
      >>> c2 = C()

      >>> len(InstanceDeclarations) == before
      1

      >>> directlyProvides(c1, I)
      >>> len(InstanceDeclarations) == before + 1
      1

      >>> directlyProvides(c2, I)
      >>> len(InstanceDeclarations) == before + 1
      1

      >>> del c1
      >>> collect()
      >>> len(InstanceDeclarations) == before + 1
      1

      >>> del c2
      >>> collect()
      >>> len(InstanceDeclarations) == before
      1
      N(   t   InstanceDeclarationsR2   R*   t   ProvidesClass(   R   R@   (    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyR[   »  s
    7(   t	   ClassTypec         G   sâ   t  |  d d ƒ } | d k	 rW t  | d d ƒ | k rW t |  t ƒ sW t d ƒ ‚ qW n  t | ƒ } | d k r~ t |  ƒ } n  t } x' t D] } t | | ƒ r‹ t	 } Pq‹ q‹ W| rÌ t
 |  | | Œ |  _ n t | | Œ |  _ d S(   s³  Declare interfaces declared directly for an object

      The arguments after the object are one or more interfaces or interface
      specifications (``IDeclaration`` objects).

      The interfaces given (including the interfaces in the specifications)
      replace interfaces previously declared for the object.

      Consider the following example:

        >>> from zope.interface import Interface
        >>> class I1(Interface): pass
        ...
        >>> class I2(Interface): pass
        ...
        >>> class IA1(Interface): pass
        ...
        >>> class IA2(Interface): pass
        ...
        >>> class IB(Interface): pass
        ...
        >>> class IC(Interface): pass
        ...
        >>> class A(object):
        ...     implements(IA1, IA2)
        >>> class B(object):
        ...     implements(IB)

        >>> class C(A, B):
        ...    implements(IC)

        >>> ob = C()
        >>> directlyProvides(ob, I1, I2)
        >>> int(I1 in providedBy(ob))
        1
        >>> int(I2 in providedBy(ob))
        1
        >>> int(IA1 in providedBy(ob))
        1
        >>> int(IA2 in providedBy(ob))
        1
        >>> int(IB in providedBy(ob))
        1
        >>> int(IC in providedBy(ob))
        1

      The object, ``ob`` provides ``I1``, ``I2``, and whatever interfaces
      instances have been declared for instances of ``C``.

      To remove directly provided interfaces, use ``directlyProvidedBy`` and
      subtract the unwanted interfaces. For example:

        >>> directlyProvides(ob, directlyProvidedBy(ob)-I2)
        >>> int(I1 in providedBy(ob))
        1
        >>> int(I2 in providedBy(ob))
        0

      removes I2 from the interfaces directly provided by ``ob``. The object,
      ``ob`` no longer directly provides ``I2``, although it might still
      provide ``I2`` if it's class implements ``I2``.

      To add directly provided interfaces, use ``directlyProvidedBy`` and
      include additional interfaces.  For example:

        >>> int(I2 in providedBy(ob))
        0
        >>> directlyProvides(ob, directlyProvidedBy(ob), I2)

      adds ``I2`` to the interfaces directly provided by ob::

        >>> int(I2 in providedBy(ob))
        1

      R0   sH   Attempt to make an interface declaration on a non-descriptor-aware classN(   R3   R*   R6   R<   R9   R
   R>   t   Falset
   issubclasst   TrueR=   R/   R[   (   t   objectR   R?   t   issubt   damc(    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyt   directlyProvides   s    P$c         G   s   t  |  t |  ƒ | Œ d S(   sŒ  Declare interfaces declared directly for an object

    The arguments after the object are one or more interfaces or interface
    specifications (``IDeclaration`` objects).

    The interfaces given (including the interfaces in the specifications) are
    added to the interfaces previously declared for the object.

    Consider the following example:

      >>> from zope.interface import Interface
      >>> class I1(Interface): pass
      ...
      >>> class I2(Interface): pass
      ...
      >>> class IA1(Interface): pass
      ...
      >>> class IA2(Interface): pass
      ...
      >>> class IB(Interface): pass
      ...
      >>> class IC(Interface): pass
      ...
      >>> class A(object):
      ...     implements(IA1, IA2)
      >>> class B(object):
      ...     implements(IB)

      >>> class C(A, B):
      ...    implements(IC)

      >>> ob = C()
      >>> directlyProvides(ob, I1)
      >>> int(I1 in providedBy(ob))
      1
      >>> int(I2 in providedBy(ob))
      0
      >>> int(IA1 in providedBy(ob))
      1
      >>> int(IA2 in providedBy(ob))
      1
      >>> int(IB in providedBy(ob))
      1
      >>> int(IC in providedBy(ob))
      1

      >>> alsoProvides(ob, I2)
      >>> int(I1 in providedBy(ob))
      1
      >>> int(I2 in providedBy(ob))
      1
      >>> int(IA1 in providedBy(ob))
      1
      >>> int(IA2 in providedBy(ob))
      1
      >>> int(IB in providedBy(ob))
      1
      >>> int(IC in providedBy(ob))
      1

    The object, ``ob`` provides ``I1``, ``I2``, and whatever interfaces
    instances have been declared for instances of ``C``. Notice that the
    alsoProvides just extends the provided interfaces.
    N(   Ri   t   directlyProvidedBy(   Rf   R   (    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyt   alsoProvidesh  s    Ac         C   s9   t  |  t |  ƒ | ƒ | j |  ƒ r5 t d ƒ ‚ n  d S(   s:  
    This removes a directly provided interface from an object.
    Consider the following two interfaces:

      >>> from zope.interface import Interface
      >>> class I1(Interface): pass
      ...
      >>> class I2(Interface): pass
      ...

    ``I1`` is provided through the class, ``I2`` is directly provided
    by the object:
    
      >>> class C(object):
      ...    implements(I1)
      >>> c = C()
      >>> alsoProvides(c, I2)
      >>> I2.providedBy(c)
      True

    Remove I2 from c again:
      
      >>> noLongerProvides(c, I2)
      >>> I2.providedBy(c)
      False

    Removing an interface that is provided through the class is not possible:

      >>> noLongerProvides(c, I1)
      Traceback (most recent call last):
      ...
      ValueError: Can only remove directly provided interfaces.

    s-   Can only remove directly provided interfaces.N(   Ri   Rj   t
   providedByRN   (   Rf   R   (    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyt   noLongerProvides«  s    #t   ClassProvidesBasePyc           B   s   e  Z d  „  Z RS(   c         C   s6   | |  j  k r& | d  k r |  S|  j St d ƒ ‚ d  S(   NR/   (   R]   R*   RX   R   (   R   R^   R?   (    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyR_   Ô  s
    (   R    R!   R_   (    (    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyRn   Ò  s   (   t   ClassProvidesBaseR=   c           B   s)   e  Z d  Z d „  Z d „  Z e j Z RS(   se  Special descriptor for class __provides__

    The descriptor caches the implementedBy info, so that
    we can get declarations for objects without instance-specific
    interfaces a bit quicker.

    For example:

      >>> from zope.interface import Interface
      >>> class IFooFactory(Interface):
      ...     pass
      >>> class IFoo(Interface):
      ...     pass
      >>> class C(object):
      ...     implements(IFoo)
      ...     classProvides(IFooFactory)
      >>> [i.getName() for i in C.__provides__]
      ['IFooFactory']

      >>> [i.getName() for i in C().__provides__]
      ['IFoo']
    c         G   sL   | |  _  t | ƒ |  _ | | f | |  _ t j |  | t | ƒ f Œ d  S(   N(   R]   R'   RX   t   _ClassProvides__argsR   R	   (   R   R?   t   metaclsR   (    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyR	     s    	c         C   s   |  j  |  j f S(   N(   R0   Rp   (   R   (    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyR)   
  s    (   R    R!   R"   R	   R)   Ro   R_   (    (    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyR=   ì  s   		c         C   sB   t  |  d d ƒ } | d k s- t | t ƒ r1 t St | j d  ƒ S(   sp   Return the interfaces directly provided by the given object

    The value returned is an ``IDeclaration``.
    R/   iÿÿÿÿN(   R3   R*   R6   R$   R5   R   R7   (   Rf   t   provides(    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyRj     s
    c          G   s   t  j d ƒ } | j } | | j k s3 d | k rB t d ƒ ‚ n  d | k r] t d ƒ ‚ n  t |  ƒ | d <t t d d ƒd S(	   s-  Declare interfaces provided directly by a class

      This function is called in a class definition.

      The arguments are one or more interfaces or interface specifications
      (``IDeclaration`` objects).

      The given interfaces (including the interfaces in the specifications)
      are used to create the class's direct-object interface specification.
      An error will be raised if the module class has an direct interface
      specification. In other words, it is an error to call this function more
      than once in a class definition.

      Note that the given interfaces have nothing to do with the interfaces
      implemented by instances of the class.

      This function is provided for convenience. It provides a more convenient
      way to call directlyProvides for a class. For example::

        classProvides(I1)

      is equivalent to calling::

        directlyProvides(theclass, I1)

      after the class has been created.

      For example:

        >>> from zope.interface import Interface
        >>> class IFoo(Interface): pass
        ...
        >>> class IFooFactory(Interface): pass
        ...
        >>> class C(object):
        ...   implements(IFoo)
        ...   classProvides(IFooFactory)
        >>> [i.getName() for i in C.__providedBy__]
        ['IFooFactory']
        >>> [i.getName() for i in C().__providedBy__]
        ['IFoo']

      if equivalent to:

        >>> from zope.interface import Interface
        >>> class IFoo(Interface): pass
        ...
        >>> class IFooFactory(Interface): pass
        ...
        >>> class C(object):
        ...   implements(IFoo)
        >>> directlyProvides(C, IFooFactory)
        >>> [i.getName() for i in C.__providedBy__]
        ['IFooFactory']
        >>> [i.getName() for i in C().__providedBy__]
        ['IFoo']

      i   R!   s7   classProvides can be used only from a class definition.R/   s:   classProvides can only be used once in a class definition.RO   i   N(   RP   RQ   RR   RS   R9   R
   R   t   _classProvides_advice(   R   RV   RW   (    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyt   classProvides"  s    ;	c         C   s$   |  j  d } |  ` t |  | Œ |  S(   NR/   (   R1   R/   Ri   (   R?   R   (    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyRs   l  s    t   providerc           B   s    e  Z d  Z d „  Z d „  Z RS(   s(   Class decorator version of classProvidesc         G   s   | |  _  d  S(   N(   R   (   R   R   (    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyR	   u  s    c         C   s   t  | |  j Œ | S(   N(   Ri   R   (   R   RK   (    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyRL   x  s    (   R    R!   R"   R	   RL   (    (    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyRu   r  s   	c          G   sz   t  j d ƒ } | j } | | j k	 s3 d | k rB t d ƒ ‚ n  d | k r] t d ƒ ‚ n  t t t |  ƒ Œ | d <d S(   sû  Declare interfaces provided by a module

    This function is used in a module definition.

    The arguments are one or more interfaces or interface specifications
    (``IDeclaration`` objects).

    The given interfaces (including the interfaces in the specifications) are
    used to create the module's direct-object interface specification.  An
    error will be raised if the module already has an interface specification.
    In other words, it is an error to call this function more than once in a
    module definition.

    This function is provided for convenience. It provides a more convenient
    way to call directlyProvides. For example::

      moduleImplements(I1)

    is equivalent to::

      directlyProvides(sys.modules[__name__], I1)
    i   R    s9   moduleProvides can only be used from a module definition.R/   s<   moduleProvides can only be used once in a module definition.N(   RP   RQ   RR   RS   R9   R[   R   R
   (   R   RV   RW   (    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyt   moduleProvides|  s    	c         C   s   t  | |  ƒ S(   s°  Provide object specifications

    These combine information for the object and for it's classes.

    For example:

      >>> from zope.interface import Interface
      >>> class I1(Interface): pass
      ...
      >>> class I2(Interface): pass
      ...
      >>> class I3(Interface): pass
      ...
      >>> class I31(I3): pass
      ...
      >>> class I4(Interface): pass
      ...
      >>> class I5(Interface): pass
      ...
      >>> class A(object):
      ...     implements(I1)
      >>> class B(object): __implemented__ = I2
      ...
      >>> class C(A, B):
      ...     implements(I31)
      >>> c = C()
      >>> directlyProvides(c, I4)
      >>> [i.getName() for i in providedBy(c)]
      ['I4', 'I31', 'I1', 'I2']
      >>> [i.getName() for i in providedBy(c).flattened()]
      ['I4', 'I31', 'I3', 'I1', 'I2', 'Interface']
      >>> int(I1 in providedBy(c))
      1
      >>> int(I3 in providedBy(c))
      0
      >>> int(providedBy(c).extends(I3))
      1
      >>> int(providedBy(c).extends(I31))
      1
      >>> int(providedBy(c).extends(I5))
      0
      >>> class COnly(A, B):
      ...     implementsOnly(I31)
      >>> class D(COnly):
      ...     implements(I5)
      >>> c = D()
      >>> directlyProvides(c, I4)
      >>> [i.getName() for i in providedBy(c)]
      ['I4', 'I5', 'I31']
      >>> [i.getName() for i in providedBy(c).flattened()]
      ['I4', 'I5', 'I31', 'I3', 'Interface']
      >>> int(I1 in providedBy(c))
      0
      >>> int(I3 in providedBy(c))
      0
      >>> int(providedBy(c).extends(I3))
      1
      >>> int(providedBy(c).extends(I1))
      0
      >>> int(providedBy(c).extends(I31))
      1
      >>> int(providedBy(c).extends(I5))
      1
    (   R[   (   t   directR?   (    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyt   ObjectSpecification¦  s    Bc         C   s`   t  |  d d  ƒ } | d  k	 r4 t | t ƒ r4 | Sn  y |  j } Wn t k
 rU t SXt | ƒ S(   NR/   (   R3   R*   R6   R   R0   R   R5   R'   (   RK   Rr   R?   (    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyt   getObjectSpecificationê  s    c         C   s´   y |  j  } Wn t k
 r' t |  ƒ SXy | j Wnz t k
 r¯ y |  j } Wn t k
 rm t |  j ƒ SXy |  j j } Wn t k
 r’ | SX| | k r° t |  j ƒ Sn X| S(   N(   R.   R   Ry   R   R/   R'   R0   (   RK   t   rt   cp(    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyRl   ù  s$    t   ObjectSpecificationDescriptorPyc           B   s   e  Z d  Z d „  Z RS(   s„   Implement the `__providedBy__` attribute

    The `__providedBy__` attribute computes the interfaces peovided by
    an object.
    c         C   sB   | d k r t | ƒ St | d d ƒ } | d k	 r8 | St | ƒ S(   sú  Get an object specification for an object

        For example:

          >>> from zope.interface import Interface
          >>> class IFoo(Interface): pass
          ...
          >>> class IFooFactory(Interface): pass
          ...
          >>> class C(object):
          ...   implements(IFoo)
          ...   classProvides(IFooFactory)
          >>> [i.getName() for i in C.__providedBy__]
          ['IFooFactory']
          >>> [i.getName() for i in C().__providedBy__]
          ['IFoo']

        R/   N(   R*   Ry   R3   R'   (   R   R^   R?   Rr   (    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyR_   2  s    
(   R    R!   R"   R_   (    (    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyR|   +  s   c         C   sn   | d k r g  } n  |  j } t | j k s< t | j k rL | j |  ƒ n x |  D] } t | | ƒ qS W| S(   sË   Normalize declaration arguments

    Normalization arguments might contain Declarions, tuples, or single
    interfaces.

    Anything but individial interfaces or implements specs will be expanded.
    N(   R*   R0   R   t   __mro__R$   R   R
   (   t   sequencet   outputR?   t   v(    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyR
   V  s    		(   R'   Rl   (   Ry   (   t   ObjectSpecificationDescriptor(    (    (    (;   R"   t   __docformat__RP   t   weakreft   zope.interface.interfaceR   R   R   t   typesR   R   R   t   zope.interface.adviceR   R4   R   R$   RC   R'   RE   RD   RI   RJ   RM   RX   RY   RZ   R[   Ra   t   WeakValueDictionaryR`   Re   t   __safe_for_unpickling__Rb   R>   R<   t   ImportErrorRi   Rk   Rm   Rf   Rn   Ro   t   _zope_interface_coptimizationsR=   Rj   Rt   Rs   Ru   Rv   Rx   Ry   Rl   R|   R   R*   R
   R5   R;   (    (    (    s?   /usr/lib/python2.7/dist-packages/zope/interface/declarations.pyt   <module>   sz   ¿	x	&	<			>	>+	=		h	C	'$		J	
	*	D		2'	