U C^6@sddlZddlZddlZddlZddlZddlZddlmZmZddlmZ ej dkrdddlm Z nGddde Z dd Z d d Zd d ZddZddZGddde Ze Ze ejjjddZddZiZddZddZd$ddZGdddejZd d!Zd%d"d#ZdS)&N)datetimedate)gettext3)getfullargspecc@seZdZdZddZdS)rz?A quick and dirty replacement for getfullargspec for Python 2.XcCs,t|\|_|_|_|_t|di|_dS)N__annotations__)inspect getargspecargsvarargsvarkwdefaultsgetattr annotations)selffr+/tmp/pip-install-6_kvzl1k/plac/plac_core.py__init__szgetfullargspec.__init__N)__name__ __module__ __qualname____doc__rrrrrr srcCstt|dddS)zReturns year-month-dayz%Y-%m-%dr)rtimestrptimesrrrto_datesrcCstt|dddS)z)Returns year-month-day hour-minute-secondz%Y-%m-%d %H-%M-%Sr)rrrrrrr to_datetimesr cCst|rt|}nt|r0t|}|jd=njt|rh|jtjkrTtdd}n t|j}|jd=n2t|drt|j }|jd=nt t dt ||S)zGiven a callable return an object with attributes .args, .varargs, .varkw, .defaults. It tries to do the "right thing" with functions, methods, classes and generic callables.rcSsdS)Nrrrrr*zgetargspec..__call__z%Could not determine the signature of ) r isfunctionrismethodr isclassrobjecthasattrr$ TypeError_str)Z callableobjargspecrrrr s"           r c sfdd}|S)z Returns a decorator annotating a function with the given annotations. This is a trick to support function annotations in Python 2.X. cs^t|}|j}|jr ||j|jr2||jD]}||kr6ttd|q6|_|S)Nz$Annotating non-existing argument: %s)rr r appendr NameErrorr+r)rZfasr argnameannrrannotate<s   zannotations..annotater)r2r3rr1rr7s rcCs<t|do:t|do:t|do:t|do:t|do:t|dS)zt An object is an annotation object if it has the attributes help, kind, abbrev, type, choices, metavar. helpkindabbrevtypechoicesmetavar)r)objrrr is_annotationLsr<c@s&eZdZdddZddZeeZdS) AnnotationN positionalcCsP|dkst||dkr(|dks(t|||_||_||_||_||_||_dS)N)r>optionflagr>)AssertionErrorr4r5r6r7r8r9)rr4r5r6r7r8r9rrrrWszAnnotation.__init__cCs4t|r |St|dr,t|ttfs,||S||S)z9Helper to convert an object into an annotation, if needed__iter__)r<r) isinstancer,bytes)clsr;rrrfrom_cs zAnnotation.from_)Nr>NNNN)rrrrrF classmethodrrrrr=Vs  r=cCs8t|jtjd}t|D]}|tkrt||||<q|S)zDExtracts the configuration of the underlying ArgumentParser from obj) descriptionformatter_class)dictrargparseRawDescriptionHelpFormatterdir PARSER_CFGr)r;cfgnamerrrpconfss rRcKsz t|WStk rYnXt|}||tf|t|<}||_|dt|dd|_ t |drt |s| |j|dn |||S)zg obj can be a callable or an object with a .commands attribute. Returns an ArgumentParser. case_sensitiveTcommands subcommands)_parser_registryKeyErrorrRcopyupdateArgumentParserr;getrrSr)rr'addsubcommandsrT populate_from)r;Z confparamsconfparserrrr parser_froms      r`cCsXg}i}|D]B}td|}|rD|d}|t|dd||<q ||q ||fS)z3Returns two lists: regular args and name=value argsz([a-zA-Z_]\w*)=rHN)rematchgrouplenr.)r arglistkwargsargrbrQrrr_extract_kwargss   rhTcs|sdd|D}fdd|D}t|dkr@|dSfdd|D}t|}|dkrj|dS|dkrttd|fdS) zBExtract the command name from an abbreviation or raise a NameErrorcSsg|] }|qSr)upper).0crrr sz_match_cmd..csg|]}|kr|qSrrrjrQr6rrrlsrHrcsg|]}|r|qSr) startswithrmrnrrrls z!Ambiguous command %r: matching %sN)rirdr/r+)r6rTrSZperfect_matchesmatchesnrrnr _match_cmds rrc@sVeZdZdZdZddZddZddZdd d Zd dZ ddZ ddZ ddZ d S)rZzk An ArgumentParser with .func and .argspec attributes, and possibly .commands and .subparsers. TcCs|S)z6Can be overridden to preprocess command-line argumentsr)rrgrrraliasszArgumentParser.aliasc sfdd|D}d}tdrX|\}}|dkrL|dk rL||fS|dk rX|tdrzjjrz|\}n|g}tdsttdr:jjr:jj}jj}|j krj |}t |\}} |j |<n.|j krj |}t |\}} |j |<|r>|s> t d|ni} tjjt| @} | rp t dd| fd djjD}tjjpd g} |j|| || fS) z}Call the underlying function with the args. Works also for command containers, by dispatching to the right subparser.csg|]}|qSr)rsrjar!rrrlsz*ArgumentParser.consume..N subparsersr-zUnrecognized arguments: %szcolliding keyword arguments: %s csg|]}t|qSr)rrt)nsrrrls)r)_extract_subparser_cmdmissingr-r parse_known_args parse_args SystemExitr __dict__poprherrorr+setr joinrfunc) rr recmdZsubpZ extraoptsvr lstrfZ collisionr r)rxrrconsumesF           zArgumentParser.consumecCs\|jd}|jj}t|D]<\}}||st|||j}||=|||pN|fSqdS)z>Extract the right subparser from the first recognized argumentr)NN) prefix_charsrv_name_parser_map enumeraterorrrSr[)rreZ optprefixZname_parser_mapirgrrrrrzs  z%ArgumentParser._extract_subparser_cmdNryc Cst||r&|j|jkr&ttd|t|ds@|j|d|_n|rP|j|dtt |dd}t |dd}|D]T}t |||d}|j rt |j nd} |jj|f|| d t||qpdS) zAExtract a list of subcommands from obj and add them to the parserzThe prefix %r is already taken!rv)title cmdprefixryadd_helpTN)rr4)r)rr ValueErrorr+add_subparsersrvadd_argument_grouprdrrtextwrapdedentrstrip add_parserrRr]) rrTr;rr prefixlenrrrdocrrrr\s0   zArgumentParser.addsubcommandscCs||_t||_|t|<dS)zExtracts the signature from a callable object and adds an .argspec attribute to the parser. Also adds a .func reference to the object.N)rr r-rV)rr;rrr_set_func_argspecs z ArgumentParser._set_func_argspecc Cs|||j}|jpd}t|j}t|}tf|||}t|ddd}|_t|j|D]\}} |j |d} t | } | j } | tkrd} np| } | jdkrdt| | _| jdkrt| trt| _n$t| trt| _n| dk rt| | _| s| dkrd} | jd krN| jr:|| j|d |d df}n||d df}nD| tkrt|j|| j| j| j| d n|j|d | j| | j| j| d| jdkr| tk r| pt| } |j|| j| | j| j| dq`| jdkr`| tk r| dk rttd|| f|j|d| jdq`|jrZt |j |jd} |j|jd| jg| j| j d|jrt |j |jd} |j|jd| ji| j| j ddS)z Extract the arguments from the attributes of the passed function and return a populated ArgumentParser instance. rr-rNz[%s]ryz'')r?r@r+)r4r7r8r9?)nargsr4defaultr7r8r9r?)r4rr7r8r9r@Fz#Flag %r wants default False, got %r store_true)actionr4*)rr4rr7r9)rr-r rdr NONErprefixziprr[r=rFr9r4r,r7rCrr rrr5r6replace add_argumentr8r*r+r r )rrrr Zn_argsZ n_defaultsZ alldefaultsrrQrr2rur9ZdfltZ shortlongrrrr]s               zArgumentParser.populate_fromcstjdfdd}||S)zMay raise a SystemExit __missing__csd|S)Nz No command %r)r)rQr!rrr"Qsz(ArgumentParser.missing..)rr;)rrQZmissrr!rr{OszArgumentParser.missingcCs t||jD] }t|qdS)zUseful for debuggingN)print_actions)rrurrr print_actionsUs zArgumentParser.print_actions)Nry) rrrrrSrsrrzr\rr]r{rrrrrrZs) ArZcCst|dot|ttf S)z8Any object with an __iter__ method which is not a stringrB)r)rCr,rDr:rrriterable\srcCsZ|dkrtjdd}t|}|r4|jddd|d||\}}t|rV|rVt|S|S)a If obj is a function or a bound method, parse the given arglist by using the parser inferred from the annotations of obj and call obj with the parsed arguments. If obj is an object with attribute .commands, dispatch to the associated subparser. NrHz --versionz-vversion)rr)sysargvr`rrrlist)r;reeagerrr_rresultrrrcallas r)T)NTN)rarrrrrLrrrr+rrr(rr r rr<r=rrZrr rOrRrVr`rhrrrrrrrrs6     '