o
    !dI                     @   s   d Z ddlZddlmZ ddlmZ ddlmZ ddlm	Z	 ddlm
Z
 edZG d	d
 d
eZd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G dd deZdS )a  Abstractions for CLI arguments.

This module contains abstractions for representing CLI arguments.
This includes how the CLI argument parser is created, how arguments
are serialized, and how arguments are bound (if at all) to operation
arguments.

The BaseCLIArgument is the interface for all arguments.  This is the interface
expected by objects that work with arguments.  If you want to implement your
own argument subclass, make sure it implements everything in BaseCLIArgument.

Arguments generally fall into one of several categories:

* global argument.  These arguments may influence what the CLI does,
  but aren't part of the input parameters needed to make an API call.  For
  example, the ``--region`` argument specifies which region to send the request
  to.  The ``--output`` argument specifies how to display the response to the
  user.  The ``--query`` argument specifies how to select specific elements
  from a response.
* operation argument.  These are arguments that influence the parameters we
  send to a service when making an API call.  Some of these arguments are
  automatically created directly from introspecting the JSON service model.
  Sometimes customizations may provide a pseudo-argument that takes the
  user input and maps the input value to several API parameters.

    N)
xform_name)first_non_none_response)unpack_cli_arg)SchemaTransformer)modelzawscli.argumentsc                   @   s   e Zd ZdS )UnknownArgumentErrorN)__name__
__module____qualname__ r   r   4usr/lib/python3.10/site-packages/awscli/arguments.pyr   4   s    r   c                 C   s(   t  }|| }t|}|d}|S )NZ
InputShape)r   	transformr   ZShapeResolverZget_shape_by_name)ZschemaZtransformerZ
shapes_mapZshape_resolverZ	arg_shaper   r   r   !create_argument_model_from_schema8   s
   


r   c                   @   s   e Zd ZdZdd Zdd Zdd Zdd	 Zed
d Z	edd Z
edd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Ze	jd d Z	ed!d" Zd#S )$BaseCLIArgumentzsInterface for CLI argument.

    This class represents the interface used for representing CLI
    arguments.

    c                 C   
   || _ d S N_name)selfnamer   r   r   __init__M   s   
zBaseCLIArgument.__init__c                 C   s   | || j < dS )a  Add this object to the argument_table.

        The ``argument_table`` represents the argument for the operation.
        This is called by the ``ServiceOperation`` object to create the
        arguments associated with the operation.

        :type argument_table: dict
        :param argument_table: The argument table.  The key is the argument
            name, and the value is an object implementing this interface.
        N)r   )r   argument_tabler   r   r   add_to_arg_tableP   s   z BaseCLIArgument.add_to_arg_tablec                 C      dS )am  Add this object to the parser instance.

        This method is called by the associated ``ArgumentParser``
        instance.  This method should make the relevant calls
        to ``add_argument`` to add itself to the argparser.

        :type parser: ``argparse.ArgumentParser``.
        :param parser: The argument parser associated with the operation.

        Nr   r   parserr   r   r   add_to_parser]   s   zBaseCLIArgument.add_to_parserc                 C   r   )a  Add this object to the parameters dict.

        This method is responsible for taking the value specified
        on the command line, and deciding how that corresponds to
        parameters used by the service/operation.

        :type parameters: dict
        :param parameters: The parameters dictionary that will be
            given to ``botocore``.  This should match up to the
            parameters associated with the particular operation.

        :param value: The value associated with the CLI option.

        Nr   r   
parametersvaluer   r   r   add_to_paramsj   s   zBaseCLIArgument.add_to_paramsc                 C      | j S r   r   r   r   r   r   r   {      zBaseCLIArgument.namec                 C   s
   d| j  S Nz--r   r"   r   r   r   cli_name      
zBaseCLIArgument.cli_namec                 C      t d)Ncli_type_nameNotImplementedErrorr"   r   r   r   r(         zBaseCLIArgument.cli_type_namec                 C   r'   )Nrequiredr)   r"   r   r   r   r,      r+   zBaseCLIArgument.requiredc                 C   r'   )Ndocumentationr)   r"   r   r   r   r-      r+   zBaseCLIArgument.documentationc                 C   r'   )Ncli_typer)   r"   r   r   r   r.      r+   zBaseCLIArgument.cli_typec                 C      | j ddS N-_r   replacer"   r   r   r   py_name      zBaseCLIArgument.py_namec                 C   r   )zList valid choices for argument value.

        If this value is not None then this should return a list of valid
        values for the argument.

        Nr   r"   r   r   r   choices   s   zBaseCLIArgument.choicesc                 C   r   )N r   r"   r   r   r   synopsis      zBaseCLIArgument.synopsisc                 C   r   NFr   r"   r   r   r   positional_arg   r:   zBaseCLIArgument.positional_argc                 C   s   d S r   r   r"   r   r   r   nargs   r:   zBaseCLIArgument.nargsc                 C   r   r   r   r   r   r   r   r   r      r&   c                 C   r   )a  Get the group name associated with the argument.

        An argument can be part of a group.  This property will
        return the name of that group.

        This base class has no default behavior for groups, code
        that consumes argument objects can use them for whatever
        purposes they like (documentation, mutually exclusive group
        validation, etc.).

        Nr   r"   r   r   r   
group_name   s   zBaseCLIArgument.group_nameN)r   r	   r
   __doc__r   r   r   r    propertyr   r%   r(   r,   r-   r.   r5   r7   r9   r<   r=   setterr?   r   r   r   r   r   E   s@    







	



r   c                   @   s   e Zd ZdZ					d ddZdd Zed	d
 Zdd Zedd Z	e	j
dd Z	edd Zedd Zedd Zedd Zedd Zedd Zedd Zedd ZdS )!CustomArgumentad  
    Represents a CLI argument that is configured from a dictionary.

    For example, the "top level" arguments used for the CLI
    (--region, --output) can use a CustomArgument argument,
    as these are described in the cli.json file as dictionaries.

    This class is also useful for plugins/customizations that want to
    add additional args.

    r8   NFc                 C   s   || _ || _|| _|| _|| _|| _|| _|| _|	| _|
| _	|| _
|d u r'g }|| _|| _|| _d | _|d u r;|  }|| _| jd urN| jjdkrPd| _d S d S d S )Nlist+)r   _help_dest_default_action	_required_nargs_const_cli_type_name_group_name_positional_arg_choices	_synopsisno_paramfileargument_model_create_scalar_argument_model	type_name)r   r   Z	help_textdestdefaultactionr,   r7   r=   r(   r?   r<   rR   rS   r9   constr   r   r   r      s2   

zCustomArgument.__init__c                 C   s    | j d urd S | j}td|iS )Ntype)rK   r(   r   )r   rU   r   r   r   rT      s   
z,CustomArgument._create_scalar_argument_modelc                 C   s   | j r| jS d| j S r$   )rO   r   r"   r   r   r   r%      s   
zCustomArgument.cli_namec                 C   s   | j }i }| jdur| j|d< | jdur| j|d< | jdur#| j|d< | jr+| j|d< | jdur5| j|d< | jdur?| j|d< | jdurI| j|d< |j|fi | dS )	X

        See the ``BaseCLIArgument.add_to_parser`` docs for more information.

        NrV   rX   rW   r7   r,   r=   rY   )	r%   rG   rI   rH   rP   rJ   rK   rL   add_argument)r   r   r%   kwargsr   r   r   r     s"   












zCustomArgument.add_to_parserc                 C   s   | j d u rdS | j S r;   rJ   r"   r   r   r   r,     s   
zCustomArgument.requiredc                 C   r   r   r^   r>   r   r   r   r,   "  r&   c                 C   r!   r   )rF   r"   r   r   r   r-   &  r#   zCustomArgument.documentationc                 C   s4   | j d ur| j S | jdv rdS | jd ur| jjS dS )N
store_truestore_falsebooleanstring)rM   rI   rS   rU   r"   r   r   r   r(   *  s   


zCustomArgument.cli_type_namec                 C   s   t }| jdv r	t}|S )Nr_   )strrI   bool)r   r.   r   r   r   r.   7  s   
zCustomArgument.cli_typec                 C   r!   r   )rP   r"   r   r   r   r7   >  r#   zCustomArgument.choicesc                 C   r!   r   rN   r"   r   r   r   r?   B  r#   zCustomArgument.group_namec                 C   r!   r   )rQ   r"   r   r   r   r9   F  r#   zCustomArgument.synopsisc                 C   r!   r   )rO   r"   r   r   r   r<   J  r#   zCustomArgument.positional_argc                 C   r!   r   )rK   r"   r   r   r   r=   N  r#   zCustomArgument.nargs)r8   NNNNNNNNFFNr8   N)r   r	   r
   r@   r   rT   rA   r%   r   r,   rB   r-   r(   r.   r7   r?   r9   r<   r=   r   r   r   r   rC      s@    
$









rC   c                   @   s   e Zd ZdZeeeeeeeeeeedZ		dddZ	e
dd Ze
d	d
 Zejdd
 Ze
dd Zejdd Ze
dd Ze
dd Zdd Zdd Zdd Zdd Zdd ZdS )CLIArgumentzARepresents a CLI argument that maps to a service parameter.

    )Z	structuremap	timestamprD   rc   floatintegerlongrb   ZdoubleZblobFNc                 C   s<   || _ |du r	|}|| _|| _|| _|| _|| _|j| _dS )a=  

        :type name: str
        :param name: The name of the argument in "cli" form
            (e.g.  ``min-instances``).

        :type argument_model: ``botocore.model.Shape``
        :param argument_model: The shape object that models the argument.

        :type argument_model: ``botocore.model.OperationModel``
        :param argument_model: The object that models the associated operation.

        :type event_emitter: ``botocore.hooks.BaseEventHooks``
        :param event_emitter: The event emitter to use when emitting events.
            This class will emit events during parts of the argument
            parsing process.  This event emitter is what is used to emit
            such events.

        :type is_required: boolean
        :param is_required: Indicates if this parameter is required or not.

        N)r   _serialized_namerS   rJ   _operation_model_event_emitterr-   _documentation)r   r   rS   operation_modelevent_emitteris_requiredserialized_namer   r   r   r   f  s   zCLIArgument.__init__c                 C   r/   r0   r3   r"   r   r   r   r5     r6   zCLIArgument.py_namec                 C   r!   r   r^   r"   r   r   r   r,     r#   zCLIArgument.requiredc                 C   r   r   r^   r>   r   r   r   r,     r&   c                 C   r!   r   rp   r"   r   r   r   r-     r#   zCLIArgument.documentationc                 C   r   r   ru   r>   r   r   r   r-     r&   c                 C   s   | j jS r   )rS   rU   r"   r   r   r   r(     r+   zCLIArgument.cli_type_namec                 C   s   | j | jjtS r   )TYPE_MAPgetrS   rU   rd   r"   r   r   r   r.     s   zCLIArgument.cli_typec                 C   s"   | j }|j|| j| j| jd dS )r[   )helprZ   r,   N)r%   r\   r-   r.   r,   r   r   r%   r   r   r   r     s   
zCLIArgument.add_to_parserc                 C   s6   |d u rd S |  |}td|| j| ||| j< d S )Nz+Unpacked value of %r for parameter "%s": %r)_unpack_argumentLOGdebugr5   rm   )r   r   r   Zunpackedr   r   r   r      s   
zCLIArgument.add_to_paramsc                 C   sJ   | j jj}t| j jd}| jd||f | j| |d}|d ur |S t| |S )Nr1   zprocess-cli-arg.%s.%s)paramZcli_argumentr   )rn   Zservice_modelservice_namer   r   _emit_first_responserS   r   )r   r   r~   Zoperation_nameoverrider   r   r   rz     s   

zCLIArgument._unpack_argumentc                 K   s   | j j|fi |S r   )ro   emit)r   r   r]   r   r   r   _emit  s   zCLIArgument._emitc                 K   s   | j |fi |}t|S r   )r   r   )r   r   r]   Z	responsesr   r   r   r     s   z CLIArgument._emit_first_response)FN)r   r	   r
   r@   rd   rj   intre   rv   r   rA   r5   r,   rB   r-   r(   r.   r   r    rz   r   r   r   r   r   r   rg   S  sH    
'






rg   c                   @   s   e Zd Zdd ZdS )ListArgumentc                 C   s    | j }|j|d| j| jd d S )N*)r=   rZ   r,   )r%   r\   r.   r,   ry   r   r   r   r     s   
zListArgument.add_to_parserN)r   r	   r
   r   r   r   r   r   r     s    r   c                       sL   e Zd ZdZ			d fdd	Zdd Zd	d
 Zdd Zedd Z	  Z
S )BooleanArgumenta_  Represent a boolean CLI argument.

    A boolean parameter is specified without a value::

        aws foo bar --enabled

    For cases where the boolean parameter is required we need to add
    two parameters::

        aws foo bar --enabled
        aws foo bar --no-enabled

    We use the capabilities of the CLIArgument to help achieve this.

    Fr`   Nc                    sb   t t| j||||||
d d | _|| _|d u r| j| _n|| _|d u r)| j| _n|| _|	| _	d S )N)rt   )
superr   r   Z_mutex_grouprI   r5   _destinationr   rN   rH   )r   r   rS   rq   rr   rs   rX   rV   r?   rW   rt   	__class__r   r   r     s    


zBooleanArgument.__init__c                 C   s   |d ur||| j < d S d S r   )rm   r   r   r   r   r      s   zBooleanArgument.add_to_paramsc              
   C   sF   | || j < d| j  }| j|| j| j| jd| j| j| jd}|||< d S )Nzno-%sra   )rX   rV   r?   rt   )r   r   rS   rn   ro   r   r?   rm   )r   r   Znegative_nameZnegative_versionr   r   r   r     s   

z BooleanArgument.add_to_arg_tablec                 C   s"   |j | j| j| j| j| jd d S )N)rx   rX   rW   rV   )r\   r%   r-   rI   rH   r   r   r   r   r   r   *  s   
zBooleanArgument.add_to_parserc                 C   r!   r   rf   r"   r   r   r   r?   1  r#   zBooleanArgument.group_name)Fr`   NNNN)r   r	   r
   r@   r   r    r   r   rA   r?   __classcell__r   r   r   r   r     s    r   )r@   loggingZbotocorer   Zbotocore.hooksr   Zawscli.argprocessr   Zawscli.schemar   r   	getLoggerr{   	Exceptionr   r   objectr   rC   rg   r   r   r   r   r   r   <module>   s"   
|  
