σ
Ψ(Sc           @` s³   d  d l  m Z d  d l  m Z d  d l  m Z d  d l  m Z d g Z d  d l m Z d  d l m	 Z	 d  d l
 m Z m Z d	   Z d d
  Z d   Z d   Z d   Z d S(   i    (   t   absolute_import(   t   division(   t   print_function(   t   unicode_literalsu   singledispatch(   t   update_wrapper(   t   WeakKeyDictionary(   t   MappingProxyTypet   get_cache_tokenc         C` sΤ   g  } xΗ t  rΟ g  |  D] } | r | ^ q }  |  s8 | SxD |  D]< } | d } x) |  D]  } | | d k rV d } PqV qV WPq? W| s t d   n  | j |  x( |  D]  } | d | k r¨ | d =q¨ q¨ Wq	 Wd S(   u   Merges MROs in *sequences* to a single MRO using the C3 algorithm.

    Adapted from http://www.python.org/download/releases/2.3/mro/.

    i    i   u   Inconsistent hierarchyN(   t   Truet   Nonet   RuntimeErrort   append(   t	   sequencest   resultt   st   s1t	   candidatet   s2t   seq(    (    s-   lib/python2.7/site-packages/singledispatch.pyt	   _c3_merge   s$    	
c   
      ` s€  xO t  t |  j   D]2 \ }   t   d  r t |  j  | } Pq q Wd } | rd t |  n g  } t |  j |   } g  } t |  j |  } xM | D]E   t |     r t   f d   |  j D  r | j    q q Wx | D]   | j	    qν Wg  | D]   t
   d | ^ q} g  | D]   t
   d | ^ q0} g  | D]   t
   d | ^ qU}	 t |  g g | | |	 | g | g | g  S(   u  Computes the method resolution order using extended C3 linearization.

    If no *abcs* are given, the algorithm works exactly like the built-in C3
    linearization used for method resolution.

    If given, *abcs* is a list of abstract base classes that should be inserted
    into the resulting MRO. Unrelated ABCs are ignored and don't end up in the
    result. The algorithm inserts ABCs where their functionality is introduced,
    i.e. issubclass(cls, abc) returns True for the class itself but returns
    False for all its direct base classes. Implicit ABCs for a given class
    (either registered or inferred from the presence of a special method like
    __len__) are inserted directly after the last ABC explicitly listed in the
    MRO of said class. If two implicit ABCs end up next to each other in the
    resulting MRO, their ordering depends on the order of types in *abcs*.

    u   __abstractmethods__i    c         3` s   |  ] } t  |    Vq d  S(   N(   t
   issubclass(   t   .0t   b(   t   base(    s-   lib/python2.7/site-packages/singledispatch.pys	   <genexpr>K   s    t   abcs(   t	   enumeratet   reversedt	   __bases__t   hasattrt   lent   listR   t   anyR   t   removet   _c3_mroR   (
   t   clsR   t   it   boundaryt   explicit_basest   abstract_basest   other_basest   explicit_c3_mrost   abstract_c3_mrost   other_c3_mros(    (   R   s-   lib/python2.7/site-packages/singledispatch.pyR!   .   s(    "%%%c         ` s  t   j       f d   } g   D] } | |  r( | ^ q(   f d   } g   D] } | |  s\ | ^ q\  t    } g  } xε  D]έ } g  } xa | j   D]S }	 |	   k r¬ t  |	  r¬ | j g  |	 j D] }
 |
 | k rέ |
 ^ qέ  q¬ q¬ W| s| j |  q n  | j d t d t  x; | D]3 }	 x* |	 D]" } | | k rF| j |  qFqFWq9Wq Wt  d | S(   uΫ   Calculates the method resolution order for a given class *cls*.

    Includes relevant abstract base classes (with their respective bases) from
    the *types* iterable. Uses a modified C3 linearization algorithm.

    c         ` s(   |    k o' t  |  d  o' t  |   S(   Nu   __mro__(   R   R   (   t   typ(   t   basesR"   (    s-   lib/python2.7/site-packages/singledispatch.pyt
   is_relatedd   s    c         ` s4   x-   D]% } |  | k r |  | j  k r t Sq Wt S(   N(   t   __mro__R   t   False(   R+   t   other(   t   types(    s-   lib/python2.7/site-packages/singledispatch.pyt   is_strict_basej   s    t   keyt   reverseR   (	   t   setR.   t   __subclasses__R   R   t   sortR   R   R!   (   R"   R1   R-   t   nR2   t   type_sett   mroR+   t   foundt   subR   t   subcls(    (   R,   R"   R1   s-   lib/python2.7/site-packages/singledispatch.pyt   _compose_mro[   s*    %%6c         C` s³   t  |  | j    } d } x | D] } | d k	 r | | k r | |  j k r | |  j k r t | |  r t d j | |    n  Pn  | | k r" | } q" q" W| j |  S(   u^  Returns the best matching implementation from *registry* for type *cls*.

    Where there is no registered implementation for a specific type, its method
    resolution order is used to find a more generic implementation.

    Note: if *registry* does not contain an implementation for the base
    *object* type, this function may return None.

    u   Ambiguous dispatch: {0} or {1}N(   R>   t   keysR	   R.   R   R
   t   formatt   get(   R"   t   registryR:   t   matcht   t(    (    s-   lib/python2.7/site-packages/singledispatch.pyt
   _find_impl   s    
	c         ` s¨   i   t     d    d  _    f d     d     f d     f d   } |   t < | _   | _ t   | _  j | _	 t
 | |   | S(   ul  Single-dispatch generic function decorator.

    Transforms a function into a generic function, which can have different
    behaviours depending upon the type of its first argument. The decorated
    function acts as the default implementation, and additional
    implementations can be registered using the register() attribute of the
    generic function.

    c           S` s   d  S(   N(    (    (    (    s-   lib/python2.7/site-packages/singledispatch.pyt   nsͺ   s    c         ` s‘    j  d k	 r@ t   }  j  | k r@   j   |  _  q@ n  y   |  } WnL t k
 r y  |  } Wn  t k
 r t |    } n X|   |  <n X| S(   uΜ   generic_func.dispatch(cls) -> <function implementation>

        Runs the dispatch algorithm to return the best available implementation
        for the given *cls* registered on *generic_func*.

        N(   t   cache_tokenR	   R   t   cleart   KeyErrorRE   (   R"   t   current_tokent   impl(   t   dispatch_cacheRF   RB   (    s-   lib/python2.7/site-packages/singledispatch.pyt   dispatch­   s    	
c         ` sa   | d k r    f d   S|    < j d k rS t   d  rS t    _ n   j   | S(   u   generic_func.register(cls, func) -> func

        Registers a new implementation for the given *cls* on a *generic_func*.

        c         ` s      |   S(   N(    (   t   f(   R"   t   register(    s-   lib/python2.7/site-packages/singledispatch.pyt   <lambda>Κ   s    u   __abstractmethods__N(   R	   RG   R   R   RH   (   R"   t   func(   RL   RF   RO   RB   (   R"   s-   lib/python2.7/site-packages/singledispatch.pyRO   Γ   s    

c          ` s     |  d j   |  |   S(   Ni    (   t	   __class__(   t   argst   kw(   RM   (    s-   lib/python2.7/site-packages/singledispatch.pyt   wrapperΡ   s    N(   R   R	   RG   t   objectRO   RM   R   RB   RH   t   _clear_cacheR   (   RQ   RU   (    (   RM   RL   RF   RO   RB   s-   lib/python2.7/site-packages/singledispatch.pyt   singledispatch   s    
			
		N(   t
   __future__R    R   R   R   t   __all__t	   functoolsR   t   weakrefR   t   singledispatch_helpersR   R   R   R	   R!   R>   RE   RX   (    (    (    s-   lib/python2.7/site-packages/singledispatch.pyt   <module>   s   		-	)	