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|dstd|dS)Nz_lambda___annotations__rrrr kwonlyargskwonlydefaultszarg%d*z%s=Nonez%s=%sz**z, signaturez%You are decorating a non function: %s)rrrrrr)shortsignature__name__name__doc__doc __module__moduler isfunctionrgetattr annotationssetattr enumeraterlistrappendrrjoinr__dict__copydictrhasattr TypeError)selffuncr rrr"r$ZfuncdictZargspecaiargZallargsZ allshortargsrrrrasZ           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'?rr$)r rr&r!r-r __defaults__r__kwdefaults__rsys _getframeAttributeError f_globalsgetr#update)r2r3kwframeZ callermodulerrrr@s  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).0r6rrr sz&FunctionMaker.make..,_func__call_z%s is overridden in %s zZsinglezError in generated code:)fileZ __source__)rHrI)varsDEFsearch SyntaxErrorgroupsetrsplit NameErrorendswithnext_compile_countcompileexecprintr;stderrr@) r2Z src_templevaldict addsourceattrssrcmor namesnfilenamecoder3rrrmakes4        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. (rCNrJcss|]}d|VqdS)z Nr)rElinerrr sz'FunctionMaker.create..rIz#async def %(name)s(%(signature)s): returnz return awaitzdef %(name)s(%(signature)s): ) isinstancestrrDrRr, splitlinesr?rreplacerd)robjZbodyr[rr"r$r\r]r restrr3r2Zibodycallerrrrcreates     zFunctionMaker.create)NNNNNNN)NF)NNNT)rr# __qualname__r! itertoolscountrVrrrrrrrr@rd classmethodrqrrrrrTs 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. )rIrHz_e%d_z, z,return _call_(_func_, %s%%(shortsignature)s)) __wrapped__rr)r/r)rrqr0rr) r3rpZextrasr[Zesr5ZextraZexZfunrrrdecorates   rxc 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 decoratorNrvzHdecorator(%s) converts functions/generators into factories of %s objectszrz, rG)_callZ _decorate_z %s(func, %s)zhif func is None: return lambda func: _decorate_(func, _call, (%s)) return _decorate_(func, _call, (%s)))r"r$rw)N)rxr Zisclassrlowerr%r!__code__ co_argcountlenr9r, co_varnames __class____call__r/rrqr#) rpZ_funcZ defaultargsrr r"nargsZndefsr[decrrr decorators:          r)_GeneratorContextManager)GeneratorContextManagerc@seZdZddZdS)ContextManagercCstj|dt||d|dS)zContext manager decoratorz.with _self_: return _func_(%(shortsignature)s))Z_self_rH)rw)rrqr/)r2r3rrrr#szContextManager.__call__N)rr#rrrrrrrr"srcOstj||||S)N)rr)r2gr4krrrr-srcOstj||||S)N)rr)r2rr4rrrrr3scCst|S)N)_contextmanager)r3rrrcontextmanager: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+)r4 vancestorsaddjvarrrr+As  r+cs@ddjtjdffdd fdd}d|_|S) zr Factory of decorators turning a function into a generic function dispatching on the given arguments. z(%s,)z, rvcs0|t|tr,tdtt||fdS)z5Make sure one passes the expected number of argumentszExpected %d arguments, got %d%sN)r}r1)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)rE_rrrrFpszIdispatch_on..gen_func_dec..vancestors..cSsg|] }t|qSr)rQ)rErarrrrFus)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 rCzAmbiguous dispatch for %s: %srNri)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)rEr4rrrrgszKdispatch_on..gen_func_dec..dispatch_info..)rsproductr+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)rEr6rrrrgszGdispatch_on..gen_func_dec.._dispatch..)rKeyErrorrsrrUr?)rrrArr combinationsr)rr3rrr _dispatchs    z4dispatch_on..gen_func_dec.._dispatchz#return _f_(%s, %%(shortsignature)s))Z_f_)rdefaultrrrrrw)rQrrrSrrqr/)r3Zargsetrrr)rr dispatch_str)rr3rrr gen_func_decas      z!dispatch_on..gen_func_dec dispatch_on)r,rner)rrr)rrrrrSs W r)N)(r!Z __future__rrer;r rrs collections __version__versionrr namedtupler rr=rr rWrMobjectrrxr contextlibr ImportErrorrrrZinitr}rZn_argsrrrr+rrrrr!s\        &