3 5*[Z@@sdZddlmZddlZddlZddlZddlZddlZddlZdZ ej dkrdddlm Z ddZ nej d d Zd d Z d dZ y ejZWnek rddZYnXej ddZddZejdZGdddeZffddZd*ddZyddlmZWn"ek rddlmZYnXGdddeZe ejZe ej!Z"e"dkrhej# rhd d!Zee_n,e"dkr|ej#r|ne"d"krd#d!Zee_eeZ$d$d%Z%d&d'Z&d(d)Z'dS)+zT Decorator module, see http://pypi.python.org/pypi/decorator for the documentation. )print_functionNz4.2.13)getfullargspeccCs|jS)N)__init__)clsr/usr/lib/python3.6/decorator.pyget_init0sr FullArgSpecz5args varargs varkw defaults kwonlyargs kwonlydefaultscCstjtj|gdfS)z?A quick and dirty replacement for getfullargspec for Python 2.XN)r _makeinspect getargspec)frrrr7srcCs|jjS)N)r__func__)rrrrr ;scCsdS)NFr)rrrriscoroutinefunctionBsrArgSpeczargs varargs varkw defaultscCst|}t|j|j|j|jS)z$A replacement for inspect.getargspec)rrargsvarargsvarkwdefaults)rspecrrrr Jsr z\s*def\s*([_\w][_\w\d]*)\s*\(c@sZeZdZdZejZfZZZ Z Z Z d ddZ ddZddd Zedd d ZdS) FunctionMakerz An object with the ability to create functions with a given signature. It has attributes name, doc, module, signature, defaults, dict and methods update and make. NcCs||_|rf|j|_|jdkr$d|_|j|_|j|_tj|rft |}t |di|_ xdD]} t || t || q\Wx&t |jD]\} } t |d | | qWt|j} t|j} |jr| jd |j| jd |jn|jr| jd x.|jD]$} | jd | | jd | | fqW|jrB| jd|j| jd|jdj| |_dj| |_|jj|_|rr||_|dk r||_|r||_|r||_|r||_|r||_t|dstt|dstd|dS)Nz_lambda___annotations__rrrr kwonlyargskwonlydefaultszarg%d*z%s=Nonez%s=%sz**z, name signaturez%You are decorating a non function: %s)rrrrrr)shortsignature__name__r__doc__doc __module__moduler isfunctionrgetattr annotationssetattr enumeraterlistrappendrrjoinr__dict__copydictrhasattrAssertionError TypeError)selffuncrrrr"r$ZfuncdictZargspecaiargZallargsZ allshortargsrrrras\           zFunctionMaker.__init__c Ks|j|_t|dd|_t|di|_|j|_|jp4d|_t|dd|_ yt j d}Wnt k rld}YnX|j jdd}t|d||_|jj|dS) z2Update the signature of func with the data in selfr"Nr/r'?r r$)rr r&r!r-r __defaults__r__kwdefaults__rsys _getframeAttributeError f_globalsgetr#update)r3r4kwframeZ callermodulerrrrAs  zFunctionMaker.updateFc Ks|t|}|pi}tj|}|dkr2td||jd}t|gdd|jjdD}x$|D]} | dkrbtd | |fqbW|j d s|d 7}d t |j f} yt || d } t | |Wn*td tjdt|tjdYnX||} |r||d<|j| f|| S)zBMake a new function from a given template and update the signatureNz not a valid function template %scSsg|]}|jdqS)z *)strip).0r7rrr sz&FunctionMaker.make..,_func__call_z%s is overridden in %s zZsinglezError in generated code:)fileZ __source__)rIrJ)varsDEFsearch SyntaxErrorgroupsetrsplit NameErrorendswithnext_compile_countcompileexecprintr<stderrrA) r3Z src_templevaldict addsourceattrssrcmornamesnfilenamecoder4rrrmakes4        zFunctionMaker.makeTcKst|tr0|jjdd\} } | dd } d} n d} d} |} || | | |||} djdd|jD}|jd}|rt|rd|jd d }nd |}| j |||f|S) z Create a function from the strings name, signature and body. evaldict is the evaluation dictionary. If addsource is true an attribute __source__ is added to the result. The attributes attrs are added, if any. (rDNrKcss|]}d|VqdS)z Nr)rFlinerrr sz'FunctionMaker.create..rJz#async def %(name)s(%(signature)s): returnz return awaitzdef %(name)s(%(signature)s): ) isinstancestrrErSr, splitlinesr@rreplacere)robjZbodyr\rr"r$r]r^rrestrr4r3Zibodycallerrrrcreates     zFunctionMaker.create)NNNNNNN)NF)NNNT)r r# __qualname__r! itertoolscountrWrrrrrrrrAre classmethodrrrrrrrTs 3 "rc Csnt||d}d}x0t|D]$\}}d|}|||<||d7}qWtj|d|||d}t|drj|j|_|S)zE decorate(func, caller) decorates a function using a caller. )rJrIz_e%d_z, z,return _call_(_func_, %s%%(shortsignature)s)) __wrapped__rs)r/r)rrrr0rs) r4rqZextrasr\Zesr6ZextraZexZfunrrrdecorates   ryc Cs|dk rt||Sdf}}tj|rB|jj}d|j|jf}n~tj|r|jdkr\d}n|j}|j}|jj}t |j pzf}dj |jj |||}|r|d7}|j }n|j jj}|jj}t|td}tjd ||fd ||f|||j|d } |rd || _ | S) z=decorator(caller) converts a caller function into a decoratorNrwzHdecorator(%s) converts functions/generators into factories of %s objectszrz, rH)_callZ _decorate_z %s(func, %s)zhif func is None: return lambda func: _decorate_(func, _call, (%s)) return _decorate_(func, _call, (%s)))r"r$rx)N)ryr Zisclassr lowerr%r!__code__ co_argcountlenr:r, co_varnames __class____call__r/rrrr#) rqZ_funcZ defaultargsrrr"nargsZndefsr\decrrr decorators:          r)_GeneratorContextManager)GeneratorContextManagerc@seZdZddZdS)ContextManagercCstj|dt||d|dS)zContext manager decoratorz.with _self_: return _func_(%(shortsignature)s))Z_self_rI)rx)rrrr/)r3r4rrrr#szContextManager.__call__N)r r#rsrrrrrr"srcOstj||||S)N)rr)r3gr5krrrr-srcOstj||||S)N)rr)r3rr5rrrrr3scCst|S)N)_contextmanager)r4rrrcontextmanager:srcCsRd}x:t|D].\}}t||r&d}Pt||r|||<d}qW|rN|j|dS)z_ Append ``a`` to the list of the virtual ancestors, unless it is already included. TFN)r) issubclassr+)r5 vancestorsaddjvarrrr+As  r+csLs tdddjtjdffdd fdd}d |_|S) zr Factory of decorators turning a function into a generic function dispatching on the given arguments. zNo dispatch args passedz(%s,)z, rwcs0|t|tr,tdtt||fdS)z5Make sure one passes the expected number of argumentszExpected %d arguments, got %d%sN)r~r2)Z argumentsZwrongmsg) dispatch_argsrrcheck[szdispatch_on..checkc sttj}t|ks&tdifddfddfdd}fdd }fd d }tjd t|d ||d S)z4Decorator turning a function into a generic functionzUnknown dispatch arguments %scsv|ddttD}xHD]@}x:t|||D]*\}}}t||r6||jkr6t||q6Wq$Wdd|DS)zU Get a list of sets of virtual ancestors for the given types cSsg|]}gqSrr)rF_rrrrGpszIdispatch_on..gen_func_dec..vancestors..cSsg|] }t|qSr)rR)rFrarrrrGus)ranger~ziprmror+)typesZrastypes_tZtype_r)rrtypemaprrrks z5dispatch_on..gen_func_dec..vancestorscs|g}xt||D]p\}}t|}|dkrFtd||fn4|dkrr|\}td||fijdd}n|j}|j|ddqW|S)zG Get a list of virtual MROs, one for each type rDzAmbiguous dispatch for %s: %srNrj)rr~ RuntimeErrortyperr+)rZlistsrZvasZn_vasrr)rrrr ancestorswsz4dispatch_on..gen_func_dec..ancestorscsfdd}|S)zU Decorator to register an implementation for the given types cs&t|jtjd|j|<|S)Nz in )rroperatorltr )r)rrrrrrsz@dispatch_on..gen_func_dec..register..decr)rr)rr)rrregistersz3dispatch_on..gen_func_dec..registercs@|g}x.tj|D]}|jtdd|DqW|S)zI An utility to introspect the dispatch algorithm css|] }|jVqdS)N)r )rFr5rrrrhszKdispatch_on..gen_func_dec..dispatch_info..)rtproductr+tuple)rZlstZanc)rrrr dispatch_infos z8dispatch_on..gen_func_dec..dispatch_infoc stdd|D}y |}Wntk r2Yn X|||Stj|}t|x(|D] }j|}|dk rZ|||SqZW||S)Ncss|]}t|VqdS)N)r)rFr7rrrrhszGdispatch_on..gen_func_dec.._dispatch..)rKeyErrorrtrrVr@)rrrBrr combinationsr)rr4rrr _dispatchs    z4dispatch_on..gen_func_dec.._dispatchz#return _f_(%s, %%(shortsignature)s))Z_f_)rdefaultrrrrrx)rRrrrTrrrr/)r4Zargsetrrr)rr dispatch_str)rr4rrr gen_func_decas      z!dispatch_on..gen_func_dec dispatch_on)r1r,rner )rrr)rrrrrSs  W r)N)(r!Z __future__rrer<r rrt collections __version__versionrr namedtupler rr>rr rXrNobjectrryr contextlibr ImportErrorrrrZinitr~rZn_argsrrrr+rrrrr!s\        &