o
    !dXS                     @   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__ 5usr/lib/python3.10/site-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

    nameZ	anonymouszload-cli-arg.%s.%s.%s)paramvalueservice_nameoperation_nameN)getattrZemit_first_non_none_response)Zsessionr+   r,   cli_argumentr*   
param_nameZvalue_overrider   r   r   unpack_argumentD   s   	r0   c                 C   s   g }t | |S N_detect_shape_structure)r)   stackr   r   r   detect_shape_structure\   s   
r5   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 rW    dS dt| j  W    S W    d S    w )N	recursivescalar	structurec                    s   g | ]}t | qS r   r2   .0pr4   r   r   
<listcomp>j   s    z+_detect_shape_structure.<locals>.<listcomp>   c                 s       | ]}|d kV  qdS r7   Nr   r9   r   r   r   	<genexpr>p       z*_detect_shape_structure.<locals>.<genexpr>zstructure(scalar)c                 s   r?   r@   r   r9   r   r   r   rA   r   rB   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setr3   memberr*   )r)   r4   Z	sub_typesr   r<   r   r3   a   s<   




	



r3   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   )r.   r*   r   r   r   unpack_cli_arg   s   rQ   c                 C   s.   | j dr| j ddkr| jdkrdS dS )NZ	jsonvaluelocationheaderstringTF)serializationgetrF   modelr   r   r   _special_type   s
   
rY   c                 C   sR   t | st| rt| ||S | jtv rt| ||S | jtv r$t| ||S t	|S r1   )
r   r	   _unpack_json_cli_argrF   r   unpack_scalar_cli_argr   _unpack_complex_cli_argr   Z	text_type)rP   r*   r   r   r   r   rO      s   


rO   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   )rP   r*   r   er   r   r   rZ      s   rZ   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 )Nr8   rD   r   {zInvalid JSON:
%srC   [r>   c                    s   g | ]}t | qS r   )rO   r:   vr   Zmember_shape_modelr   r   r=      s    z+_unpack_complex_cli_arg.<locals>.<listcomp>)rF   lstriprZ   r   
isinstancer   string_typesrC   rJ   striprN   r_   	TypeError)rP   r*   r   rF   Zsingle_valuer`   r   re   r   r\      s0   r\    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floatZdoubleZblobZ	streamingz%Blob values must be a path to a file.rbZbooleanfalseF)rF   intrn   rU   rV   ospath
expandvars
expanduserisfiler   openrg   r   rh   lowerbool)rP   r*   r   	file_pathmsgr   r   r   r[      s$   




r[   c                 C   s   t | rdS t| S )NF)r
   _is_complex_shaperW   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)r8   rC   rD   FrC   T)rF   rN   rW   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tagsZ	instances)zremove-tagszdescribe-instance-healthz'deregister-instances-from-load-balancerz%register-instances-with-load-balancer)ZfirehoseZ
workspaceszelastic-load-balancing)rV   )r   
service_idr,   Z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 r1   )r   ZShorthandParser_parserZ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   r.   r*   Z
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   parserc   r   r   r   r=   Y  s    z<ParamShorthandParser._parse_as_shorthand.<locals>.<listcomp>)LOGdebugr   _handle_special_casesrg   rC   r   visitrP   r   r   r   ZShorthandParseErrorr   strr"   )r   r.   r*   r   r,   Zhandled_valueparsedr`   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 )
NrC   r8   r>   r   c                    s   g | ]} |iqS r   r   rc   Zkey_namer   r   r=   |  s    z>ParamShorthandParser._handle_special_cases.<locals>.<listcomp>ValuerT   =)	rP   rF   rN   rJ   rH   r   r(   rC   keys)r   r.   r*   r   r,   rX   
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   )rb   ra   z=Param %s looks like JSON, not considered for param shorthand.F)rg   rC   r   rh   ri   
startswithr   r   Zpy_namerP   r}   )r   r.   r*   Z	check_valrX   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   rP   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``.

        NrC   z ...rk   )r   	_DONT_DOCrP   rF   rN   _shorthand_docsr'   )r   r.   r   r,   	docstringr4   rP   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 )NrC   r8   r>   r   minmaxz%s %s1z%s %s1 %s2 %s3r   rT   rk   )rP   rF   rN   rJ   rH   r   r(   rC   metadatarV   r   r   )r   r.   r   r,   rX   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 )Nr8   rC   rD   )rJ   
_MAX_STACKr'   rF   _structure_docs
_list_docs	_map_docs)r   rP   r4   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 )Nr>   z[%s,%s]z%s,%s)rN   rE   r(   r   rG   rF   r   rJ   )r   rP   r4   Zlist_memberZ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   rP   r4   kZ
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   )rH   itemsr
   rE   _member_docsr$   )r   rP   r4   partsr(   Zmember_shapeZ
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(   rE   r   rG   )r   r(   shaper4   Z	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   )rk   )(r   rr   loggingZawscli.compatr   Zbotocore.compatr   r   awsclir   r   r   Zawscli.utilsr   r	   r
   Zbotocore.utilsr   	getLoggerr   	Exceptionr   r   r"   r'   r0   r5   r3   rQ   rY   rO   rZ   r\   r[   r}   r|   r   r~   r   r   r   r   r   r   <module>   s8   
	 		
	}