
    >                         d Z g dZddlmZmZ ddlmZmZ ddlm	Z
 ddlmZ ddlZ	 dd	lmZ  G d
 de      Z G d de      ZdZdZeefdZeefdZd Zd Z edd      ZddZ y# e$ r	 dd	lmZ Y Gw xY w)zEfunctools.py - Tools for working with functions and callable objects
)	update_wrapperwrapsWRAPPER_ASSIGNMENTSWRAPPER_UPDATEStotal_ordering
cmp_to_key	lru_cachereducepartial    )r
   r	   )MutableMapping
namedtuple   )recursive_repr)proxyN)allocate_lockc                       e Zd ZdZy)_Link)prevnextkey__weakref__N)__name__
__module____qualname__	__slots__     *lib/third_party/functools32/functools32.pyr   r      s    4Ir   r   c                   X   e Zd ZdZd Zej                  eefdZej                  fdZ	d Z
d Zd ZddZdd	Zd
 Zej"                  xZZej&                  Zej(                  Zej*                  Zej,                  Z e       ZefdZddZ e       d        Zd Zd Zedd       Z d Z!y)OrderedDictz)Dictionary that remembers insertion orderc                 *   t        |      dkD  rt        dt        |      z        	 | j                    | j                  |i | y# t        $ rC t	               | _        t        | j
                        x| _        }|x|_        |_        i | _	        Y ^w xY w)zInitialize an ordered dictionary.  The signature is the same as
        regular dictionaries, but keyword arguments are not recommended because
        their insertion order is arbitrary.

        r   z$expected at most 1 arguments, got %dN)len	TypeError_OrderedDict__rootAttributeErrorr   _OrderedDict__hardroot_proxyr   r   _OrderedDict__map_OrderedDict__update)selfargskwdsroots       r   __init__zOrderedDict.__init__.   s     t9q=BSYNOO	KK 	t$t$  	#gDO!'!88DK$$((DI	DJ		s   A A	BBc                     || vrX |       x| j                   |<   }| j                  }|j                  }|||c|_        |_        |_        ||_         ||      |_         || ||       y)z!od.__setitem__(i, y) <==> od[i]=yN)r(   r$   r   r   r   )	r*   r   valuedict_setitemr   Linklinkr-   lasts	            r   __setitem__zOrderedDict.__setitem__?   se    
 d?%)V+DJJsOd;;D99D-14*DIty$(DIdDIT3&r   c                      || |       | j                   j                  |      }|j                  }|j                  }||_        ||_        y)z od.__delitem__(y) <==> del od[y]N)r(   popr   r   )r*   r   dict_delitemr3   	link_prev	link_nexts         r   __delitem__zOrderedDict.__delitem__M   s@     	T3zz~~c"II	II	"	"	r   c              #      K   | j                   }|j                  }||ur |j                   |j                  }||uryyw)zod.__iter__() <==> iter(od)N)r$   r   r   r*   r-   currs      r   __iter__zOrderedDict.__iter__X   >      {{yy$((N99D $
   ;A A c              #      K   | j                   }|j                  }||ur |j                   |j                  }||uryyw)z#od.__reversed__() <==> reversed(od)N)r$   r   r   r=   s      r   __reversed__zOrderedDict.__reversed__a   r@   rA   c                     | j                   }|x|_        |_        | j                  j	                          t
        j	                  |        y)z.od.clear() -> None.  Remove all items from od.N)r$   r   r   r(   cleardict)r*   r-   s     r   rE   zOrderedDict.clearj   s5    {{ $$	DI



4r   c                 8   | st        d      | j                  }|r'|j                  }|j                  }||_        ||_        n&|j                  }|j                  }||_        ||_        |j                  }| j
                  |= t        j                  | |      }||fS )zod.popitem() -> (k, v), return and remove a (key, value) pair.
        Pairs are returned in LIFO order if last is true or FIFO order if false.

        zdictionary is empty)KeyErrorr$   r   r   r   r(   rF   r7   )r*   r4   r-   r3   r9   r:   r   r0   s           r   popitemzOrderedDict.popitemq   s    
 011{{99D		I!IN!DI99D		I!DI!INhhJJsOs#Ezr   c                 ,   | j                   |   }|j                  }|j                  }||_        ||_        | j                  }|r)|j                  }||_        ||_        |x|_        |_        y|j                  }||_        ||_        |x|_        |_        y)zMove an existing element to the end (or beginning if last==False).

        Raises KeyError if the element does not exist.
        When last=True, acts like a fast version of self[key]=self.pop(key).

        N)r(   r   r   r$   )r*   r   r4   r3   r9   r:   r-   firsts           r   move_to_endzOrderedDict.move_to_end   s     zz#II	II	"	"	{{99DDIDI$((DI	IIEDIDI%))DI
r   c                     t         j                  }t        |       dz   } || j                        }| || j                        dz  z  }| || j
                        |z  z  }| || j                        |z  z  }|S )Nr      )_sys	getsizeofr"   __dict__r(   r&   r$   )r*   sizeofnsizes       r   
__sizeof__zOrderedDict.__sizeof__   ss    IMdmm$tzz"Q&&t'!++t{{#a''r   c                 T    || v r
| |   }| |= |S || j                   u rt        |      |S )zod.pop(k[,d]) -> v, remove specified key and return the corresponding
        value.  If key is not found, d is returned if given, otherwise KeyError
        is raised.

        )_OrderedDict__markerrH   )r*   r   defaultresults       r   r7   zOrderedDict.pop   s;     $;#YFS	Mdmm#3-r   Nc                 "    || v r| |   S || |<   |S )zDod.setdefault(k[,d]) -> od.get(k,d), also set od[k]=d if k not in odr   )r*   r   rX   s      r   
setdefaultzOrderedDict.setdefault   s!    $;9S	r   c                     | s| j                   j                  dS | j                   j                  dt        | j                               dS )zod.__repr__() <==> repr(od)z()())	__class__r   listitemsr*   s    r   __repr__zOrderedDict.__repr__   s:     !^^4466>>22D4FGGr   c                     | D cg c]	  }|| |   g }}t        |       j                         }t        t                     D ]  }|j                  |d        |r| j                  |f|fS | j                  |ffS c c}w )z%Return state information for picklingN)varscopyr    r7   r_   )r*   kra   	inst_dicts       r   
__reduce__zOrderedDict.__reduce__   sy    '+,t!!T!Wt,JOO%	km$AMM!T" %NNUHi88~~x'' -s   A9c                 $    | j                  |       S )z!od.copy() -> a shallow copy of od)r_   rb   s    r   rf   zOrderedDict.copy   s    ~~d##r   c                 ,     |        }|D ]  }|||<   	 |S )zOD.fromkeys(S[, v]) -> New ordered dictionary with keys from S.
        If not specified, the value defaults to None.

        r   )clsiterabler0   r*   r   s        r   fromkeyszOrderedDict.fromkeys   s#     uCDI r   c                     t        |t              rQt        |       t        |      k(  xr8 t        d t	        | j                         |j                               D              S t        j                  | |      S )zod.__eq__(y) <==> od==y.  Comparison to another OD is order-sensitive
        while comparison to a regular mapping is order-insensitive.

        c              3   ,   K   | ]  \  }}||k(    y wNr   ).0pqs      r   	<genexpr>z%OrderedDict.__eq__.<locals>.<genexpr>   s     I(H1q!t(Hs   )
isinstancer    r"   allzipra   rF   __eq__r*   others     r   ry   zOrderedDict.__eq__   s\    
 e[)t9c%j( JIDJJL%++-(HIIJ{{4''r   )Trq   )"r   r   r   __doc__r.   rF   r5   r'   r   r;   r?   rC   rE   rI   rL   rU   r   updater)   keysvaluesra   __ne__objectrW   r7   r[   _recursive_reprrc   ri   rf   classmethodrn   ry   r   r   r   r    r       s    /%$ "&!1!1e' -1,<,< 	#.*0 '---FXD""F  E""FxH'  H H($  (r   r    )r   r   r|   )rQ   c                     || _         |D ]  }	 t        ||      }t        | ||        |D ](  }t        | |      j	                  t        ||i              * | S # t        $ r Y Yw xY w)a  Update a wrapper function to look like the wrapped function

       wrapper is the function to be updated
       wrapped is the original function
       assigned is a tuple naming the attributes assigned directly
       from the wrapped function to the wrapper function (defaults to
       functools.WRAPPER_ASSIGNMENTS)
       updated is a tuple naming the attributes of the wrapper that
       are updated with the corresponding attribute from the wrapped
       function (defaults to functools.WRAPPER_UPDATES)
    )__wrapped__getattrsetattrr%   r}   )wrapperwrappedassignedupdatedattrr0   s         r   r   r      sz     "G	*GT*E GT5)  %%ggtR&@A  N  		s   A	A$#A$c                 (    t        t        | ||      S )a  Decorator factory to apply update_wrapper() to a wrapper function

       Returns a decorator that invokes update_wrapper() with the decorated
       function as the wrapper argument and the arguments to wraps() as the
       remaining arguments. Default arguments are as for update_wrapper().
       This is a convenience function to simplify applying partial() to
       update_wrapper().
    r   r   r   )r
   r   r   s      r   r   r     s     >7$g7 7r   c                 n   dd fdd fdd fgdd fdd	 fdd
 fgdd fdd fdd fgdd fdd fdd fgd}t        t        |             t        |      z  }|st        d      t        |      }||   D ]=  \  }}||vs||_        t        t        |      j                  |_        t        | ||       ? | S )z6Class decorator that fills in missing ordering methods__gt__c                     | |k  xs | |k(   S rq   r   rz   s     r   <lambda>z total_ordering.<locals>.<lambda>"      te|7Ttu}2Ur   __le__c                     | |k  xs | |k(  S rq   r   rz   s     r   r   z total_ordering.<locals>.<lambda>#      $,2O$%-2Or   __ge__c                     | |k   S rq   r   rz   s     r   r   z total_ordering.<locals>.<lambda>$      dUl2Br   c                     | |k   xs | |k(  S rq   r   rz   s     r   r   z total_ordering.<locals>.<lambda>%  s    dem2C2Ttu}2Tr   __lt__c                     | |k  xr | |k(   S rq   r   rz   s     r   r   z total_ordering.<locals>.<lambda>&      $%-2UPUDU2Ur   c                     | |k   S rq   r   rz   s     r   r   z total_ordering.<locals>.<lambda>'      dem2Cr   c                     | |kD  xs | |k(   S rq   r   rz   s     r   r   z total_ordering.<locals>.<lambda>(  r   r   c                     | |kD  xs | |k(  S rq   r   rz   s     r   r   z total_ordering.<locals>.<lambda>)  r   r   c                     | |kD   S rq   r   rz   s     r   r   z total_ordering.<locals>.<lambda>*  r   r   c                     | |k\   xs | |k(  S rq   r   rz   s     r   r   z total_ordering.<locals>.<lambda>+  s    tu}3D2VQV2Vr   c                     | |k\  xr | |k(   S rq   r   rz   s     r   r   z total_ordering.<locals>.<lambda>,  r   r   c                     | |k\   S rq   r   rz   s     r   r   z total_ordering.<locals>.<lambda>-  r   r   )r   r   r   r   z6must define at least one ordering operation: < > <= >=)	setdir
ValueErrormaxr   r   intr|   r   )rl   convertrootsr-   opnameopfuncs         r   r   r     s    UVOPBCE TUUVCDF UVOPBCE VWUVCDFG CMCL(EQRRu:D!$-$FO$S&199FNC(	 (
 Jr   c                 *      G  fddt               }|S )z,Convert a cmp= function into a key= functionc                   Z    e Zd ZdgZd Z fdZ fdZ fdZ fdZ fdZ	 fdZ
d	Zy	)
cmp_to_key.<locals>.Kobjc                     || _         y rq   r   )r*   r   s     r   r.   zcmp_to_key.<locals>.K.__init__>  s	    DHr   c                 D     | j                   |j                         dk  S Nr   r   r*   r{   mycmps     r   r   zcmp_to_key.<locals>.K.__lt__@      599-11r   c                 D     | j                   |j                         dkD  S r   r   r   s     r   r   zcmp_to_key.<locals>.K.__gt__B  r   r   c                 D     | j                   |j                         dk(  S r   r   r   s     r   ry   zcmp_to_key.<locals>.K.__eq__D      599-22r   c                 D     | j                   |j                         dk  S r   r   r   s     r   r   zcmp_to_key.<locals>.K.__le__F  r   r   c                 D     | j                   |j                         dk\  S r   r   r   s     r   r   zcmp_to_key.<locals>.K.__ge__H  r   r   c                 D     | j                   |j                         dk7  S r   r   r   s     r   r   zcmp_to_key.<locals>.K.__ne__J  r   r   N)r   r   r   r   r.   r   r   ry   r   r   r   __hash__)r   s   r   Kr   <  s2    G			2	2	3	3	3	3r   r   )r   )r   r   s   ` r   r   r   :  s    F " Hr   	CacheInfozhits misses maxsize currsizec                 <     t         t        t        t        f fd	}|S )a  Least-recently-used cache decorator.

    If *maxsize* is set to None, the LRU features are disabled and the cache
    can grow without bound.

    Arguments to the cached function must be hashable.

    View the cache statistics named tuple (hits, misses, maxsize, currsize) with
    f.cache_info().  Clear the cache and statistics with f.cache_clear().
    Access the underlying function with f.__wrapped__.

    See:  http://en.wikipedia.org/wiki/Cache_algorithms#Least_Recently_Used

    c                 h   	
 dgdgct               ft               %t               t                fd       }nAt	               j
                  	j                  
t               	
 fd       }fd}fd}||_        ||_        |S )Nr   c                      | }|r"| 
 	|j                                     z   z  }	 |   }dxx   dz  cc<   |S # $ r Y nw xY w | i |}||<   dxx   dz  cc<   |S Nr   r   ra   )r+   r,   r   rY   rH   cachehitskwd_markmissessortedtupleuser_functions       r   r   z7lru_cache.<locals>.decorating_function.<locals>.wrappero  s    8eF4::<,@&AAAC"3ZFGqLG!M &55#c
q	Q	s   = AAc                  r   | }|r"|	  |j                                     z   z  }5  	 |   } |       dxx   dz  cc<   |cd d d        S # $ r Y nw xY w	 d d d        n# 1 sw Y   nxY w | i |}5  ||<   dxx   dz  cc<    
      kD  r d       d d d        |S # 1 sw Y   |S xY wr   r   )r+   r,   r   rY   rH   r   cache_popitemcache_renewr   r   r"   lockmaxsizer   r   r   r   s       r   r   z7lru_cache.<locals>.decorating_function.<locals>.wrapper  s    8eF4::<,@&AAAC!&s#C(Q1% T $  TT '55!'E#J1INI5zG+%a(	 
  
 s4   A&AAA&AA&&A/<&B,,B6c            	      h    5  t        d   d                 cddd       S # 1 sw Y   yxY w)zReport cache statisticsr   N)
_CacheInfo)r   r   r"   r   r   r   s   r   
cache_infoz:lru_cache.<locals>.decorating_function.<locals>.cache_info  s*    !$q'6!9gs5zJ s   (1c                  h    5   j                          dxd<   d<   ddd       y# 1 sw Y   yxY w)z$Clear the cache and cache statisticsr   N)rE   )r   r   r   r   s   r   cache_clearz;lru_cache.<locals>.decorating_function.<locals>.cache_clear  s+    &''Q&) s   (1)	r   LockrF   r   r    rI   rL   r   r   )r   r   r   r"   rH   r   r   r   r   r   r   r   r   r   r   r   s   `````   @@@@@@@r   decorating_functionz&lru_cache.<locals>.decorating_functione  s     sQCfH;v?FE=!  "  ME!MMM++K=!  "(	K 	K
	( ()r   )r   r   r"   rH   )r   r   s   ` r   r   r   Q  s    * Fh@D r   )d   )!r|   __all__
_functoolsr
   r	   collectionsr   r   	reprlib32r   r   weakrefr   r'   sysrO   threadr   r   ImportError_dummy_thread32r   r   rF   r    r   r   r   r   r   r   r   r   r   r   r   <module>r      s   M ' 2 8 # 7,5F 5O($ O(h <  2,: )#76* %CD
Vy	  767s   A( (A65A6