o .&aXS@sDdZddlZddlZddlmZddlmZmZddlm Z m Z ddlm Z ddl m Z mZmZddlmZed ZGd d d eZGd d d eZGdddeZGdddeZddZddZddZddZddZddZddZd d!Z d/d#d$Z!d%d&Z"d'd(Z#Gd)d*d*e$Z%Gd+d,d,e%Z&Gd-d.d.e%Z'dS)0zModule for processing CLI args.N)six) OrderedDictjson) SCALAR_TYPES COMPLEX_TYPES) shorthand)%find_service_and_method_in_event_nameis_document_typeis_document_type_container)is_json_value_headerzawscli.argprocessceZdZfddZZS) ParamErrorcs,d||f}tt||||_||_dS)aB :type cli_name: string :param cli_name: The complete cli argument name, e.g. "--foo-bar". It should include the leading hyphens if that's how a user would specify the name. :type message: string :param message: The error message to display to the user. z Error parsing parameter '%s': %sN)superr __init__cli_namemessage)selfrr full_message __class__3/usr/lib/python3/dist-packages/awscli/argprocess.pyr s   zParamError.__init____name__ __module__ __qualname__r __classcell__rrrrr r c@ eZdZdS)ParamSyntaxErrorNrrrrrrrr3rcr )ParamUnknownKeyErrorcs*d|}d||f}tt||dS)N, z'Unknown key '%s', valid choices are: %s)joinrr"r)rkey valid_keysrrrrr8s zParamUnknownKeyError.__init__rrrrrr"7rr"c@r)TooComplexErrorNr rrrrr'@r!r'cCs:t|dd}|jd|||f||||d}|dur|}|S)a Unpack an argument's value from the commandline. This is part one of a two step process in handling commandline arguments. Emits the load-cli-arg event with service, operation, and parameter names. Example:: load-cli-arg.ec2.describe-instances.foo name anonymouszload-cli-arg.%s.%s.%s)paramvalue service_nameoperation_nameN)getattremit_first_non_none_response)sessionr,r- cli_argumentr+ param_namevalue_overriderrrunpack_argumentDs r4cCsg}t||SN_detect_shape_structure)r*stackrrrdetect_shape_structure\s r9csH|jvrdS|jz|jtvrWdS|jdkrgfdd|jD}t|dkrAtdd|DrAWd St|dkrWtd d|DrWWd Sd d t t |WS|jdkrydt |j WS|jdkr|jjtvrWdSdt |jWSWdSw)N recursivescalar structurecsg|]}t|qSrr6.0pr8rr jsz+_detect_shape_structure..cs|]}|dkVqdSr;Nrr=rrr pz*_detect_shape_structure..zstructure(scalar)csrCrDrr=rrrrErrFzstructure(scalars)z structure(%s)r#listzlist-%smapz map-scalarzmap-%s)r(append type_namerpopmembersvalueslenallr$sortedsetr7memberr+)r*r8 sub_typesrr@rr7as<            r7cCst|j||jS)a1 Parses and unpacks the encoded string command line parameter and returns native Python data structures that can be passed to the Operation. :type cli_argument: :class:`awscli.arguments.BaseCLIArgument` :param cli_argument: The CLI argument object. :param value: The value of the parameter. This can be a number of different python types (str, list, etc). This is the value as it's specified on the command line. :return: The "unpacked" argument than can be sent to the `Operation` object in python. )_unpack_cli_argargument_modelr)r1r+rrrunpack_cli_argsrVcCs.|jdr|jddkr|jdkrdSdS)N jsonvaluelocationheaderstringTF) serializationgetrJmodelrrr _special_types  r_cCsRt|st|rt|||S|jtvrt|||S|jtvr$t|||St |Sr5) r r _unpack_json_cli_argrJrunpack_scalar_cli_argr_unpack_complex_cli_argr text_type)rUr+rrrrrTs    rTc Cs<ztj|tdWSty}z t|d||fd}~ww)N)object_pairs_hookz"Invalid JSON: %s JSON received: %s)rloadsr ValueErrorr )rUr+rerrrr`sr`c s|j}|dks |dkr |ddkrt||Std||dkr~t|tjr9|ddkr8t||Sn!t|trZt|dkrZ|d }|rZ|ddkrZt||dSz|j fd d |DWSt t fy}}zt|dd}~wwdS) Nr<rHr{zInvalid JSON: %srG[rBcsg|]}t|qSr)rTr>vrmember_shape_modelrrrAsz+_unpack_complex_cli_arg..) rJlstripr`r isinstancer string_typesrGrNstriprRrf TypeError)rUr+rrJ single_valuergrrlrrbs0     rbcCs|jdks |jdkrt|S|jdks|jdkrt|S|jdkrE|jdrEtj|}tj|}tj |s@d}t ||t |dS|jd kr\t |t jrX|d krXd St|S|S) Nintegerlongfloatdoubleblob streamingz%Blob values must be a path to a file.rbbooleanfalseF)rJintrwr[r\ospath expandvars expanduserisfiler openrorrplowerbool)rUr+r file_pathmsgrrrras$        racCst|rdSt|S)NF)r _is_complex_shaper]rrr_supports_shorthand_syntaxs rcCs,|jdvrdS|jdkr|jjdvrdSdS)N)r<rGrHFrGT)rJrRr]rrrrs   rc@seZdZddZdS)ParamShorthandcCsNddgidgdgdgddgdgdgdgd d }||i|g}||vS) z Determines whether a given operation for a service needs to use the deprecated shorthand parsing case for lists of structures that only have a single member. zput-record-batchrecordszreboot-workspace-requestszrebuild-workspace-requestszterminate-workspace-requests)zreboot-workspaceszrebuild-workspaceszterminate-workspacestags instances)z remove-tagszdescribe-instance-healthz'deregister-instances-from-load-balancerz%register-instances-with-load-balancer)firehose workspaceszelastic-load-balancing)r\)r service_idr- argument_namecasesrrr_uses_old_list_casesz"ParamShorthand._uses_old_list_caseN)rrrrrrrrrs rc@s4eZdZddZddZddZddZd d Zd S) ParamShorthandParsercCst|_t|_dSr5)rShorthandParser_parserBackCompatVisitor_visitorrrrrr"s zParamShorthandParser.__init__cKs,|||sdSt|\}}|||||S)aAttempt to parse shorthand syntax for values. This is intended to be hooked up as an event handler (hence the **kwargs). Given ``param`` object and its string ``value``, figure out if we can parse it. If we can parse it, we return the parsed value (typically some sort of python dict). :type cli_argument: :class:`awscli.arguments.BaseCLIArgument` :param cli_argument: The CLI argument object. :type param: :class:`botocore.parameters.Parameter` :param param: The parameter object (includes various metadata about the parameter). :type value: str :param value: The value for the parameter type on the command line, e.g ``--foo this_value``, value would be ``"this_value"``. :returns: If we can parse the value we return the parsed value. If it looks like JSON, we return None (which tells the event emitter to use the default ``unpack_cli_arg`` provided that no other event handlers can parsed the value). If we run into an error parsing the value, a ``ParamError`` will be raised. N)_should_parse_as_shorthandr_parse_as_shorthand)rr1r+ event_namekwargsrr-rrr__call__&s zParamShorthandParser.__call__c sz@td|j||||}|dur|WSt|tr0fdd|D}j||jW|Sj |}j||jW|St j yU}zt |jt|d}~wt tfyj}zt |jt|d}~ww)NzParsing param %s as shorthandcsg|]}j|qSr)rparserjrrrrAYsz.)LOGdebugr_handle_special_casesrorGrvisitrUrrrShorthandParseErrorr strr")rr1r+rr- handled_valueparsedrgrrrrJs.   z(ParamShorthandParser._parse_as_shorthandcs|j}|jdkr3|jjdkr3t|jjdkr3||||jr3t|jjdfdd|D}|S|jdkrTt|jdkrVd|jvrX|jdjdkrZd |vr\d|iSdSdSdSdSdS) NrGr<rBrcsg|]}|iqSrrrjkey_namerrrA|sz>ParamShorthandParser._handle_special_cases..ValuerZ=) rUrJrRrNrLrr(rGkeys)rr1r+rr-r^ new_valuesrrrris(     z*ParamShorthandParser._handle_special_casescCsV|r t|tr |d}n|}t|tjr$|dr$td|jdS|j }t |S)Nr)rirhz=Param %s looks like JSON, not considered for param shorthand.F) rorGrrprq startswithrrpy_namerUr)rr1r+ check_valr^rrrrs z/ParamShorthandParser._should_parse_as_shorthandN)rrrrrrrrrrrrr s $ #rc@sZeZdZdZeZdZddZddZddZ d d Z d d Z d dZ ddZ ddZdS)ParamShorthandDocGenz3Documentation generator for param shorthand syntax.cCs|durt|SdS)z3Checks if a CLI argument supports shorthand syntax.NF)r)rrUrrrsupports_shorthandsz'ParamShorthandDocGen.supports_shorthandcCst||||}||jurdS|r|Sg}z|jjdkr(|jj}|||dWS||j|WSty9YdSw)aGenerate documentation for a CLI argument. :type cli_argument: awscli.arguments.BaseCLIArgument :param cli_argument: The CLI argument which to generate documentation for. :return: Returns either a string or ``None``. If a string is returned, it is the generated shorthand example. If a value of ``None`` is returned then this indicates that no shorthand syntax is available for the provided ``argument_model``. NrGz ...rt)r _DONT_DOCrUrJrR_shorthand_docsr')rr1rr- docstringr8rUrrrgenerate_shorthand_examples    z/ParamShorthandDocGen.generate_shorthand_examplecCs|j}|jdkrG|jjdkrGt|jjdkrG||||jrGt|jjd}|j}| ddkr>| ddkr>d|j |fSd|j |||fS|jdkrct|jdkrcd |jvrc|jd jd krc|j Sd S) NrGr<rBrminmaxz%s %s1z%s %s1 %s2 %s3rrZrt) rUrJrRrNrLrr(rGmetadatar\rr)rr1rr-r^ member_namerrrrrs*    z*ParamShorthandDocGen._handle_special_casescCs\t||jkr t|jdkr|||S|jdkr |||S|jdkr+|||S|jS)Nr<rGrH)rN _MAX_STACKr'rJ_structure_docs _list_docs _map_docs)rrUr8rrrrs      z$ParamShorthandDocGen._shorthand_docscCsf|j}||jz ||j|}W|n|w|jtvs't|dkr-d||fSd||fS)NrBz[%s,%s]z%s,%s)rRrIr(rrKrJrrN)rrUr8 list_member element_docsrrrrs   zParamShorthandDocGen._list_docscCs^|j}||j|}d||f}|jr'|s'|d7}|jD]}|d|7}q|S|r-d|}|S)NzKeyName1=%s,KeyName2=%sz Where valid key names are: z %s {%s})r%rr+enum)rrUr8k value_docsstartrrrrrs   zParamShorthandDocGen._map_docscCsPg}|jD]\}}t|rq|||||qd|}|s$|Sd|S)N,r)rLitemsr rI _member_docsr$)rrUr8partsr( member_shape inner_partrrrrs z$ParamShorthandDocGen._structure_docscCsP||jdkr dS||jz |||}W|n|wd||fS)Nrz( ... recursive ... )z%s=%s)countr(rIrrK)rr(shaper8 value_docrrrrs  z!ParamShorthandDocGen._member_docsN)rrr__doc__objectrrrrrrrrrrrrrrrs"   r)rt)(rrlogging awscli.compatrbotocore.compatrrawsclirrr awscli.utilsrr r botocore.utilsr getLoggerr Exceptionr rr"r'r4r9r7rVr_rTr`rbrarrrrrrrrrrs8          }