o
    .&ßaXS  ã                   @   sD  d Z ddlZddlZddlmZ ddlmZmZ ddlm	Z	m
Z
 ddlmZ ddlmZmZmZ ddlmZ e d	¡ZG d
d„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Z d/d#d$„Z!d%d&„ Z"d'd(„ Z#G d)d*„ d*e$ƒZ%G d+d,„ d,e%ƒZ&G d-d.„ d.e%ƒZ'dS )0zModule for processing CLI args.é    N)Úsix)ÚOrderedDictÚjson)ÚSCALAR_TYPESÚCOMPLEX_TYPES)Ú	shorthand)Ú%find_service_and_method_in_event_nameÚis_document_typeÚis_document_type_container)Úis_json_value_headerzawscli.argprocessc                       ó   e Zd Z‡ fdd„Z‡  ZS )Ú
ParamErrorc                    s,   d||f }t t| ƒ |¡ || _|| _dS )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_nameÚmessage)Úselfr   r   Úfull_message©Ú	__class__© ú3/usr/lib/python3/dist-packages/awscli/argprocess.pyr       s   ÿ
zParamError.__init__©Ú__name__Ú
__module__Ú__qualname__r   Ú__classcell__r   r   r   r   r      ó    r   c                   @   ó   e Zd ZdS )ÚParamSyntaxErrorN©r   r   r   r   r   r   r   r   3   ó    r   c                       r   )ÚParamUnknownKeyErrorc                    s*   d  |¡}d||f }tt| ƒ |¡ d S )Nú, z'Unknown key '%s', valid choices are: %s)Újoinr   r"   r   )r   ÚkeyÚ
valid_keysr   r   r   r   r   8   s   
ÿÿzParamUnknownKeyError.__init__r   r   r   r   r   r"   7   r   r"   c                   @   r   )ÚTooComplexErrorNr    r   r   r   r   r'   @   r!   r'   c                 C   s:   t |ddƒ}| jd|||f ||||d�}|dur|}|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Ú	anonymouszload-cli-arg.%s.%s.%s)ÚparamÚvalueÚservice_nameÚoperation_nameN)ÚgetattrÚemit_first_non_none_response)Úsessionr,   r-   Úcli_argumentr+   Ú
param_nameÚvalue_overrider   r   r   Úunpack_argumentD   s   	þûr4   c                 C   s   g }t | |ƒS ©N©Ú_detect_shape_structure)r*   Ústackr   r   r   Údetect_shape_structure\   s   
r9   c                    sH  | j ˆ v rdS ˆ  | j ¡ z‘| jtv rW ˆ  ¡  dS | jdkrg‡ fdd„| j ¡ D ƒ}t|ƒdkrAtdd„ |D ƒƒrAW ˆ  ¡  d	S t|ƒdkrWtd
d„ |D ƒƒrWW ˆ  ¡  dS dd 	t
t|ƒƒ¡ W ˆ  ¡  S | jdkrydt| jˆ ƒ W ˆ  ¡  S | jdkr˜| jjtv r‹W ˆ  ¡  dS dt| jˆ ƒ W ˆ  ¡  S W ˆ  ¡  d S ˆ  ¡  w )NÚ	recursiveÚscalarÚ	structurec                    s   g | ]}t |ˆ ƒ‘qS r   r6   ©Ú.0Úp©r8   r   r   Ú
<listcomp>j   s    ÿz+_detect_shape_structure.<locals>.<listcomp>é   c                 s   ó   � | ]}|d kV  qdS ©r;   Nr   r=   r   r   r   Ú	<genexpr>p   ó   € z*_detect_shape_structure.<locals>.<genexpr>zstructure(scalar)c                 s   rC   rD   r   r=   r   r   r   rE   r   rF   zstructure(scalars)zstructure(%s)r#   Úlistzlist-%sÚmapz
map-scalarzmap-%s)r(   ÚappendÚ	type_namer   ÚpopÚmembersÚvaluesÚlenÚallr$   ÚsortedÚsetr7   Úmemberr+   )r*   r8   Ú	sub_typesr   r@   r   r7   a   s<   


ë
ÿô÷
	
ø

úþ
úr7   c                 C   s   t | j|| jƒS )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_argÚargument_modelr   )r1   r+   r   r   r   Úunpack_cli_arg�   s   ÿrV   c                 C   s.   | j  d¡r| j  d¡dkr| jdkrdS dS )NÚ	jsonvalueÚlocationÚheaderÚstringTF)ÚserializationÚgetrJ   ©Úmodelr   r   r   Ú_special_type•   s
   
r_   c                 C   sR   t | ƒst| ƒrt| ||ƒS | jtv rt| ||ƒS | jtv r$t| ||ƒS t 	|¡S r5   )
r   r	   Ú_unpack_json_cli_argrJ   r   Úunpack_scalar_cli_argr   Ú_unpack_complex_cli_argr   Ú	text_type)rU   r+   r   r   r   r   rT   ž   s   ÿ
ÿ
ÿ
rT   c              
   C   s<   zt j|td�W S  ty } z	t|d||f ƒ‚d }~ww )N)Úobject_pairs_hookz"Invalid JSON: %s
JSON received: %s)r   Úloadsr   Ú
ValueErrorr   )rU   r+   r   Úer   r   r   r`   ¬   s   ÿÿ€ÿr`   c              
      s   | j }|dks|dkr | ¡ d dkrt| |ˆ ƒS tˆ d| ƒ‚|dkr~t|tjƒr9| ¡ d dkr8t| |ˆ ƒS n!t|tƒrZt|ƒdkrZ|d  	¡ }|rZ|d dkrZt| |d ˆ ƒS z| j
‰‡ ‡fd	d
„|D ƒW S  ttfy} } ztˆ |d ƒ‚d }~ww d S )Nr<   rH   r   ú{zInvalid JSON:
%srG   ú[rB   c                    s   g | ]}t ˆ|ˆ ƒ‘qS r   )rT   ©r>   Úv©r   Úmember_shape_modelr   r   rA   Ê   s    ÿz+_unpack_complex_cli_arg.<locals>.<listcomp>)rJ   Úlstripr`   r   Ú
isinstancer   Ústring_typesrG   rN   ÚstriprR   rf   Ú	TypeError)rU   r+   r   rJ   Úsingle_valuerg   r   rl   r   rb   µ   s0   ÿÿ€ûïrb   Ú c                 C   s¼   | j dks
| j dkrt|ƒS | j dks| j dkrt|ƒS | j dkrE| j d¡rEtj |¡}tj |¡}tj 	|¡s@d}t
||ƒ‚t|dƒS | j d	kr\t|tjƒrX| ¡ d
krXdS t|ƒS |S )NÚintegerÚlongÚfloatÚdoubleÚblobÚ	streamingz%Blob values must be a path to a file.ÚrbÚbooleanÚfalseF)rJ   Úintrw   r[   r\   ÚosÚpathÚ
expandvarsÚ
expanduserÚisfiler   Úopenro   r   rp   ÚlowerÚbool)rU   r+   r   Ú	file_pathÚmsgr   r   r   ra   Ô   s$   

ÿ


ra   c                 C   s   t | ƒrdS t| ƒS )NF)r
   Ú_is_complex_shaper]   r   r   r   Ú_supports_shorthand_syntaxì   s   
rŠ   c                 C   s,   | j dvrdS | j dkr| jj dvrdS dS )N)r<   rG   rH   FrG   T)rJ   rR   r]   r   r   r   r‰   û   s   

r‰   c                   @   s   e Zd Zdd„ ZdS )ÚParamShorthandc                 C   sN   ddgidgdgdgdœdgdgdgdgd	œd
œ}|  |i ¡  |g ¡}||v S )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.
        zput-record-batchÚrecordszreboot-workspace-requestszrebuild-workspace-requestszterminate-workspace-requests)zreboot-workspaceszrebuild-workspaceszterminate-workspacesÚtagsÚ	instances)zremove-tagszdescribe-instance-healthz'deregister-instances-from-load-balancerz%register-instances-with-load-balancer)ÚfirehoseÚ
workspaceszelastic-load-balancing)r\   )r   Ú
service_idr-   Úargument_nameÚcasesr   r   r   Ú_uses_old_list_case  s   ÿýü÷z"ParamShorthand._uses_old_list_caseN)r   r   r   r”   r   r   r   r   r‹     s    r‹   c                   @   s4   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdS )ÚParamShorthandParserc                 C   s   t  ¡ | _t  ¡ | _d S r5   )r   ÚShorthandParserÚ_parserÚBackCompatVisitorÚ_visitor©r   r   r   r   r   "  s   
zParamShorthandParser.__init__c                 K   s,   |   ||¡sdS t|ƒ\}}|  ||||¡S )a“  Attempt 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)r   r1   r+   Ú
event_nameÚkwargsr‘   r-   r   r   r   Ú__call__&  s   ÿÿzParamShorthandParser.__call__c              
      sÖ   z@t  d|j¡ ˆ  ||||¡}|d ur|W S t|tƒr0‡ fdd„|D ƒ}ˆ j ||j¡ W |S ˆ j	 
|¡}ˆ j ||j¡ W |S  tjyU } zt|jt|ƒƒ‚d }~w ttfyj } zt|jt|ƒƒ‚d }~ww )NzParsing param %s as shorthandc                    s   g | ]}ˆ j  |¡‘qS r   )r—   Úparserj   rš   r   r   rA   Y  s    z<ParamShorthandParser._parse_as_shorthand.<locals>.<listcomp>)ÚLOGÚdebugr   Ú_handle_special_casesro   rG   r™   ÚvisitrU   r—   r    r   ÚShorthandParseErrorr   Ústrr"   )r   r1   r+   r‘   r-   Úhandled_valueÚparsedrg   r   rš   r   rœ   J  s.   ÿÿ
ö	ø€€ûz(ParamShorthandParser._parse_as_shorthandc                    s¼   |j }|jdkr3|jjdkr3t|jjƒdkr3|  |||j¡r3t|jj ¡ ƒd ‰ ‡ fdd„|D ƒ}|S |jdkrTt|jƒdkrVd|jv rX|jd jdkrZd	|vr\d|iS d S d S d S d S d S )
NrG   r<   rB   r   c                    s   g | ]}ˆ |i‘qS r   r   rj   ©Úkey_namer   r   rA   |  s    z>ParamShorthandParser._handle_special_cases.<locals>.<listcomp>ÚValuerZ   ú=)	rU   rJ   rR   rN   rL   r”   r(   rG   Úkeys)r   r1   r+   r‘   r-   r^   Ú
new_valuesr   r©   r   r£   i  s(   
ÿ

ôz*ParamShorthandParser._handle_special_casesc                 C   sV   |rt |tƒr|d }n|}t |tjƒr$| ¡  d¡r$t d|j¡ dS |j	}t
|ƒS )Nr   )ri   rh   z=Param %s looks like JSON, not considered for param shorthand.F)ro   rG   r   rp   rq   Ú
startswithr¡   r¢   Úpy_namerU   rŠ   )r   r1   r+   Ú	check_valr^   r   r   r   r›   Œ  s   
ÿÿz/ParamShorthandParser._should_parse_as_shorthandN)r   r   r   r   rŸ   rœ   r£   r›   r   r   r   r   r•      s    $#r•   c                   @   sZ   e Zd ZdZeƒ ZdZdd„ Zdd„ Zdd„ Z	d	d
„ Z
dd„ Zdd„ Zdd„ Zdd„ ZdS )ÚParamShorthandDocGenz3Documentation generator for param shorthand syntax.é   c                 C   s   |durt |ƒS dS )z3Checks if a CLI argument supports shorthand syntax.NF)rŠ   )r   rU   r   r   r   Úsupports_shorthand£  s   z'ParamShorthandDocGen.supports_shorthandc                 C   st   |   |||¡}|| ju rdS |r|S g }z|jjdkr(|jj}|  ||¡d W S |  |j|¡W S  ty9   Y dS w )aû  Generate 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``.

        NrG   z ...rt   )r£   Ú	_DONT_DOCrU   rJ   rR   Ú_shorthand_docsr'   )r   r1   r‘   r-   Ú	docstringr8   rU   r   r   r   Úgenerate_shorthand_example©  s    ÿ
ÿz/ParamShorthandDocGen.generate_shorthand_examplec                 C   sÊ   |j }|jdkrG|jjdkrGt|jjƒdkrG|  |||j¡rGt|jjƒd }|j}| 	d¡dkr>| 	d¡dkr>d|j
|f S d|j
|||f S |jdkrct|jƒdkrcd	|jv rc|jd	 jd
krc| jS dS )NrG   r<   rB   r   ÚminÚmaxz%s %s1z%s %s1 %s2 %s3r«   rZ   rt   )rU   rJ   rR   rN   rL   r”   r(   rG   Úmetadatar\   r   rµ   )r   r1   r‘   r-   r^   Úmember_namer»   r   r   r   r£   Ë  s*   
ÿÿÿ

z*ParamShorthandDocGen._handle_special_casesc                 C   s\   t |ƒ| jkr
tƒ ‚|jdkr|  ||¡S |jdkr |  ||¡S |jdkr+|  ||¡S |jS )Nr<   rG   rH   )rN   Ú
_MAX_STACKr'   rJ   Ú_structure_docsÚ
_list_docsÚ	_map_docs)r   rU   r8   r   r   r   r¶   à  s   


z$ParamShorthandDocGen._shorthand_docsc                 C   sf   |j }| |j¡ z|  |j |¡}W | ¡  n| ¡  w |jtv s't|ƒdkr-d||f S d||f S )NrB   z[%s,%s]z%s,%s)rR   rI   r(   r¶   rK   rJ   r   rN   )r   rU   r8   Úlist_memberÚelement_docsr   r   r   r¿   ì  s   zParamShorthandDocGen._list_docsc                 C   s^   |j }|  |j|¡}d||f }|jr'|s'|d7 }|jD ]}|d| 7 }q|S |r-d| }|S )NzKeyName1=%s,KeyName2=%sz

Where valid key names are:
z  %s
ú{%s})r%   r¶   r+   Úenum)r   rU   r8   ÚkÚ
value_docsÚstartrÄ   r   r   r   rÀ   ø  s   

þzParamShorthandDocGen._map_docsc                 C   sP   g }|j  ¡ D ]\}}t|ƒrq| |  |||¡¡ qd |¡}|s$|S d| S )Nú,rÃ   )rL   Úitemsr
   rI   Ú_member_docsr$   )r   rU   r8   Úpartsr(   Úmember_shapeÚ
inner_partr   r   r   r¾     s   
z$ParamShorthandDocGen._structure_docsc                 C   sP   |  |j¡dkr
dS | |j¡ z|  ||¡}W | ¡  n| ¡  w d||f S )Nr   z( ... recursive ... )z%s=%s)Úcountr(   rI   r¶   rK   )r   r(   Úshaper8   Ú	value_docr   r   r   rÊ     s   z!ParamShorthandDocGen._member_docsN)r   r   r   Ú__doc__Úobjectrµ   r½   r´   r¸   r£   r¶   r¿   rÀ   r¾   rÊ   r   r   r   r   r²   �  s    "r²   )rt   )(rÑ   r   ÚloggingÚawscli.compatr   Úbotocore.compatr   r   Úawsclir   r   r   Úawscli.utilsr   r	   r
   Úbotocore.utilsr   Ú	getLoggerr¡   Ú	Exceptionr   r   r"   r'   r4   r9   r7   rV   r_   rT   r`   rb   ra   rŠ   r‰   rÒ   r‹   r•   r²   r   r   r   r   Ú<module>   s8   
	 		
	}