3 JZEV@s.dZddlmZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl m Z ddl mZddlmZddlmZddlmZydd lmZWnek rdZYnXydd lmZWnek rdZYnXe jdkrddlZndZe jdd kZd ddddddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8d9d:d;dd?d@dAdBdCdDdEdFdGdHdIdJdKdLdMdNdOdPdQdRgFZejdSkZejdTkZe jjdUZe jjdVZe jjdWZ e jjdXZ!e jjdYZ"e pe!pe"Z#e jjdZpe jjd[Z$e jjd\Z%d]Z&d^Z'd_Z(d`Z)daZ*dbZ+dcZ,ddZ-deZ.dfZ/dgZ0dhZ1diZ2djZ3dkZ4dlZ5dmZ6dnZ7doZ8dpZ9dqZ:drZ;dsZedkrdvZ?dwZ@dZAn GdxdydyejBZCeDjEeCjFedkrdZGdZHn Gdzd{d{ejBZIeDjEeIjFe jJZKes d|ZLn2y e jMZLWn$eNk r:er2d}nd|ZLYnXedFd~dddddgZOedBd~dddgZPed@ddddddgZQedAddddgZRedCddddddddgZSedGdddddgZTed>dddddddgZUedDdddddgZVedEddddgZWed?ddddgZXeddddgZYedddddgZZeddddgZ[edddgZ\ed6ddddgZ]ed;ddgZ^ed>> @memoize ... def foo() ... return 1 ... >>> foo() 1 >>> foo.cache_clear() >>> c sH|tt|jf}y|Stk rB||}|<|SXdS)N) frozensetsorteditemsKeyError)argskwargskeyr)cachefunr_r`wrapper"s zmemoize..wrappercs jdS)z Clear cache.N)clearr_)rr_r` cache_clear+szmemoize..cache_clear) functoolswrapsr)rrrr_)rrr`rIs  csNtjfddfdd}fdd}id_|_|_S)aA memoize decorator which is disabled by default. It can be activated and deactivated on request. For efficiency reasons it can be used only against class methods accepting no arguments. >>> class Foo: ... @memoize ... def foo() ... print(1) ... >>> f = Foo() >>> # deactivated (default) >>> foo() 1 >>> foo() 1 >>> >>> # activated >>> foo.cache_activate() >>> foo() 1 >>> foo() >>> foo() >>> c sHjs|Sy }Wn$tk r>|}<YnX|SdS)N)cache_activatedr)selfr)rrrr_r`rNs z'memoize_when_activated..wrappercs d_dS)zActivate cache.TN)rr_)rr_r`cache_activateYsz.memoize_when_activated..cache_activatecsd_jdS)zDeactivate and clear cache.FN)rrr_)rrr_r`cache_deactivate]sz0memoize_when_activated..cache_deactivateF)rrrrr)rrrr_)rrrr`memoize_when_activated4s rcCsTytj|}Wn4tk rB}z|jtjtjfkr4dSd}~XnXtj|jSdS)zSame as os.path.isfile() but does not swallow EACCES / EPERM exceptions, see: http://mail.python.org/pipermail/python-dev/2012-June/120787.html FN)osstatOSErrorerrnoEPERMEACCESS_ISREGst_mode)rysterrr_r_r`rHiscCsLytj|Wn4tk rB}z|jtjtjfkr4dSd}~XnXdSdS)zSame as os.path.exists() but does not swallow EACCES / EPERM exceptions, see: http://mail.python.org/pipermail/python-dev/2012-June/120787.html FNT)rrrrrr)ryrr_r_r`rKxscCsbtj stdkrdSy2tjttj}tj||jdWdQRXdStjk r\dSXdS)z2Return True if IPv6 is supported on this platform.NF::1rT)rr)socketZhas_ipv6rr contextlibclosingZbinderror)Zsockr_r_r`rMs cCszi}d}t}xh|jd|}||kr$P|jd||}||krj|||}||d|}|rb|j}|||<|d}qW|S)zCParse a C environ block of environment variables into a dictionary.r=rZ)rfindupper)datarposZWINDOWS_Znext_posZ equal_posrr|r_r_r`rJs    c Cs4tdkr |Sy tj|Sttfk r.|SXdS)zConvert a numeric socket family value to an IntEnum member. If it's not a known member, return the numeric value itself. N)enumrZ AddressFamily ValueErrorAttributeError)numr_r_r`rNs  c Cs4tdkr |Sy tj|Sttfk r.|SXdS)zConvert a numeric socket type value to an IntEnum member. If it's not a known member, return the numeric value itself. N)rrZ AddressTyperr)rr_r_r`rOs  csfdd}|S)zA decorator which can be used to mark a method as deprecated 'replcement' is the method name which will be called instead. cs:d|jf|jdkr|_tj|fdd}|S)Nz8%s() is deprecated and will be removed; use %s() insteadcs tjtddt|||S)NrY)category stacklevel)warningswarn FutureWarninggetattr)rrr)msg replacementr_r`innersz/deprecated_method..outer..inner)r\__doc__rr)rr)r)rr`outers   z deprecated_method..outerr_)rrr_)rr`rGs c@sBeZdZdZddZddZddZdd Zdd d Zd dZ d S) _WrapNumberszNWatches numbers so that they don't overflow and wrap (reset to zero). cCs tj|_i|_i|_i|_dS)N) threadingZLocklockr reminders reminder_keys)rr_r_r`__init__s z_WrapNumbers.__init__cCsT||jkst||jkst||jks*t||j|<tt|j|<tt|j|<dS)N)rAssertionErrorrrrintset)r input_dictrjr_r_r` _add_dicts  z_WrapNumbers._add_dictcCsd|j|}t|jt|j}x<|D]4}x"|j||D]}|j||=qYnXg}xvtt|D]f} || } || } || f} | | kr|j|| | 7<|j||j | |j | |j|| qWt |||<q>W||j|<|S)zkCache dict and sum numbers which overflow and wrap. Return an updated copy of `input_dict` ) rrrrrrangelenrraddappendtuple) rrrjrZnew_dictrZ input_tupleZ old_tuplebitsiZ input_valueZ old_valuerr_r_r`runs2      z_WrapNumbers.runNc Csh|jX|dkr0|jj|jj|jjn*|jj|d|jj|d|jj|dWdQRXdS)z>Clear the internal cache, optionally only for function 'name'.N)rrrrrpop)rrjr_r_r`r"s   z_WrapNumbers.cache_clearc Cs"|j|j|j|jfSQRXdS)z5Return internal cache dicts as a tuple of 3 elements.N)rrrr)rr_r_r` cache_info.sz_WrapNumbers.cache_info)N) r\r]r^rrrrrrrr_r_r_r`rs ' rc Cstjtj||SQRXdS)zGiven an `input_dict` and a function `name`, adjust the numbers which "wrap" (restart from zero) across different calls by adding "old value" to "new value" and return an updated dict. N)_wnrr)rrjr_r_r`rP4s)r r rbrc)N)wrZ __future__rrrrrrrsysrr collectionsrrrrrr ImportErrorr version_inforZPY3__all__rjrrplatform startswithrrr rrr rZAIXr*r+r'r,r.r2r&r0r1r(r)r/r-rr!r rrr"rrrrrrr#r$r%IntEnumr[globalsupdate __members__rdreragetfilesystemencodingrrgetfilesystemencodeerrorsrrDr@r>r?rArEr<rBrCr=rorrrsrtr4r9r:r;r6r7r8r5r3r}rFrLrIrrHrKrMrJrNrOrGrrPrrrr_r_r_r`sH                                       5   W