o
    !di                     @   s  d dl Z d dlZd dl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m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m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 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$ 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* d dl'm+Z+ d dl'm,Z, d dl-m.Z. d dl/m0Z0 d dl/m1Z1 d dl2m3Z3 d d l2m4Z4 e5d!Z6d"Z7e Z8d#9d$ d%d& Z:d'd( Z;d)d* Z<G d+d, d,e=Z>G d-d. d.eZ?G d/d0 d0e=Z@G d1d2 d2e=ZAdS )3    N)__version__)HierarchicalEmitter)
xform_name)copy_kwargsOrderedDict)NoCredentialsError)NoRegionError)ProfileNotFound)get_global_history_recorder)EnvironmentVariablesr   )get_stderr_text_writer)get_formatter)load_plugins)
CLICommand)six)MainArgParser)ServiceArgParserArgTableArgParser)USAGE)ProviderHelpCommand)ServiceHelpCommand)OperationHelpCommand)CustomArgument)ListArgument)BooleanArgument)CLIArgument)UnknownArgumentError)unpack_argument)AliasLoader)AliasCommandInjector) emit_top_level_args_parsed_event)write_exceptionzawscli.clidriverzE%(asctime)s - %(threadName)s - %(name)s - %(levelname)s - %(message)s Zidnac                  C   s    t  } |  }td|d |S )NZCLI_RCCLI)create_clidrivermainHISTORY_RECORDERrecord)driverrc r+   4usr/lib/python3.10/site-packages/awscli/clidriver.pyr&   D   s   r&   c                  C   s>   t jt} t|  t| jdi | dd t	| d}|S )NZpluginsevent_emitter)Zevent_hookssession)
botocorer/   ZSessionr   _set_user_agent_for_sessionr   Zfull_configgetget_component	CLIDriver)r/   r)   r+   r+   r,   r%   K   s   
r%   c                 C   s   d| _ t| _dt | _d S )Nzaws-clizbotocore/%s)Zuser_agent_namer   Zuser_agent_versionbotocore_versionZuser_agent_extrar.   r+   r+   r,   r1   T   s   r1   c                   @   s   e Zd Zd 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 Zdd Zdd Zd ddZdd Zdd Zdd ZdS )!r4   Nc                 C   sF   |d u rt jt| _t| j n|| _d | _d | _d | _t | _	d S N)
r0   r/   Zget_sessionr   r1   	_cli_data_command_table_argument_tabler   alias_loaderselfr/   r+   r+   r,   __init__\   s   zCLIDriver.__init__c                 C   s   | j d u r| jd| _ | j S )Ncli)r7   r/   get_datar<   r+   r+   r,   _get_cli_datag   s   
zCLIDriver._get_cli_datac                 C      | j d u r
|  | _ | j S r6   )r8   _build_command_tabler@   r+   r+   r,   _get_command_tableo      

zCLIDriver._get_command_tablec                 C   rB   r6   )r9   _build_argument_tabler@   r+   r+   r,   _get_argument_tablet   rE   zCLIDriver._get_argument_tablec                 C   s&   |  | j}| jjd|| j| d |S )z
        Create the main parser to handle the global arguments.

        :rtype: ``argparser.ArgumentParser``
        :return: The parser object

        zbuilding-command-table.maincommand_tabler/   Zcommand_object)_build_builtin_commandsr/   emitr<   rI   r+   r+   r,   rC   y   s   zCLIDriver._build_command_tablec                 C   s0   t  }| }|D ]}t|| j|d||< q	|S )N)cli_namer/   service_name)r   Zget_available_servicesServiceCommandr/   )r<   r/   commandsZservicesrN   r+   r+   r,   rJ      s   z!CLIDriver._build_builtin_commandsc                 C   s   t | j| j}||| d S r6   )r    r/   r:   Zinject_aliases)r<   rI   parserZinjectorr+   r+   r,   _add_aliases   s   zCLIDriver._add_aliasesc                 C   sZ   t  }|  }|dd }|D ]}t|| }| ||}|| q| jjd|d |S )Noptionszbuilding-top-level-params)argument_table)r   rA   r2   r   _create_cli_argumentadd_to_arg_tabler/   rK   )r<   rT   cli_dataZcli_argumentsoptionoption_paramscli_argumentr+   r+   r,   rF      s   zCLIDriver._build_argument_tablec                 C   sD   t ||dd|d|d|d|d|d|dd	S )
Nhelpr#   destdefaultactionrequiredchoicestype)Z	help_textr\   r]   r^   r_   r`   Zcli_type_name)r   r2   )r<   option_namerY   r+   r+   r,   rU      s   zCLIDriver._create_cli_argumentc              
   C   s<   |   }t| j|  |  |dd |dd |dd S )NdescriptionZsynopsisZ
help_usage)rA   r   r/   rD   rG   r2   )r<   rW   r+   r+   r,   create_help_command   s   


zCLIDriver.create_help_commandc                 C   s<   |   |d< |  }t|| j |dd |  dd}|S )Nr[   rc   Zaws)prog)rd   rA   r   r/   
user_agentr2   rG   )r<   rI   rW   rQ   r+   r+   r,   _create_parser   s   

zCLIDriver._create_parserc              
   C   s  |du rt jdd }|  }| |}| || ||\}}z$| | | | t	d| j
 d t	d|d ||j ||W S  tyn } zt jdt  t jt| t jd W Y d}~dS d}~w ty } zd	| }| | W Y d}~dS d}~w ty } zd
| }| | W Y d}~dS d}~w ty   t jd dtj  Y S  ty } ztjddd td t|t d W Y d}~dS d}~ww )z

        :param args: List of arguments, with the 'aws' removed.  For example,
            the command "aws s3 list-objects --bucket foo" will have an
            args list of ``['s3', 'list-objects', '--bucket', 'foo']``.

        N   ZCLI_VERSIONr$   ZCLI_ARGUMENTSz
usage: %s

   zA%s You can also configure your region by running "aws configure".z=%s. You can configure credentials by running "aws configure".   zException caught in main()Texc_infozExiting with rc 255)outfile)sysargvrD   rg   rR   parse_known_args_handle_top_level_args_emit_session_eventr'   r(   r/   rf   commandr   stderrwriter   strr   _show_errorr   KeyboardInterruptstdoutsignalSIGINT	ExceptionLOGdebugr"   r   )r<   argsrI   rQ   parsed_args	remainingemsgr+   r+   r,   r&      sX   





zCLIDriver.mainc                 C   s   | j jd| j |d d S )Nzsession-initialized)r/   r   )r/   rK   )r<   r   r+   r+   r,   rs      s   
zCLIDriver._emit_session_eventc                 C   s*   t j|dd tj| tjd d S )NTrl   ri   )r~   r   ro   ru   rv   )r<   r   r+   r+   r,   rx      s   zCLIDriver._show_errorc                 C   s   t | j| |jr| jd|j |jr| jd|j |jr]| jjdtjt	d | jjdtjt	d | jjdtjt	d | jjdtjt	d t
d| j  t
d	tjd
d   d S | jjdtjd d S )Nprofileregionr0   )format_stringawscliZ
s3transferZurllib3zCLI version: %szArguments entered to CLI: %srh   )Zlogger_nameZ	log_level)r!   r/   r   Zset_config_variabler   r   Zset_stream_loggerloggingDEBUG
LOG_FORMATr~   rf   ro   rp   ERROR)r<   r   r+   r+   r,   rr     s.   
z CLIDriver._handle_top_level_argsr6   )__name__
__module____qualname__r=   rA   rD   rG   rC   rJ   rR   rF   rU   rd   rg   r&   rs   rx   rr   r+   r+   r+   r,   r4   Z   s     
	

4
r4   c                   @   s   e Zd ZdZdddZedd Zejdd Zedd	 Zed
d Z	e	j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 )rO   zA service command for the CLI.

    For example, ``aws ec2 ...`` we'd create a ServiceCommand
    object that represents the ec2 service.

    Nc                 C   s:   || _ || _d | _|d u r|| _n|| _| g| _d | _d S r6   )_namer/   r8   _service_name_lineage_service_model)r<   rM   r/   rN   r+   r+   r,   r=   (  s   
zServiceCommand.__init__c                 C      | j S r6   r   r@   r+   r+   r,   name>     zServiceCommand.namec                 C   
   || _ d S r6   r   r<   valuer+   r+   r,   r   B     
c                 C   s   |   S r6   )_get_service_modelr@   r+   r+   r,   service_modelF  s   zServiceCommand.service_modelc                 C   r   r6   r   r@   r+   r+   r,   lineageJ  r   zServiceCommand.lineagec                 C   r   r6   r   r   r+   r+   r,   r   N  r   c                 C   rB   r6   )r8   _create_command_tabler@   r+   r+   r,   rD   R  rE   z!ServiceCommand._get_command_tablec                 C   sV   | j d u r(z| jd| jd }W n ty   d }Y nw | jj| j|d| _ | j S )NZapi_versions)api_version)r   r/   get_config_variabler2   r   r	   Zget_service_model)r<   r   r+   r+   r,   r   W  s   

z!ServiceCommand._get_service_modelc                 C   s.   |   }||\}}|  }||j ||S r6   )rg   rq   rD   Z	operation)r<   r   parsed_globalsZservice_parserr   r   rI   r+   r+   r,   __call__b  s   zServiceCommand.__call__c                 C   sx   t  }|  }|jD ]}t|d}||}t|| j| j|t| jd||< q
| jj	d| j || j| d | 
| |S )N-)r   parent_namer/   operation_modeloperation_callerzbuilding-command-table.%srH   )r   r   Zoperation_namesr   r   ServiceOperationr   r/   CLIOperationCallerrK   _add_lineage)r<   rI   r   operation_namerM   r   r+   r+   r,   r   k  s&   



z$ServiceCommand._create_command_tablec                 C   s$   |D ]}|| }| j |g |_ qd S r6   r   )r<   rI   rt   command_objr+   r+   r,   r     s   zServiceCommand._add_lineagec                 C   s,   |   }t| j|  |d d| j| jdS )N.)r/   objrI   	arg_tableevent_classr   )rD   r   r/   r   joinlineage_namesr   rL   r+   r+   r,   rd     s   
z"ServiceCommand.create_help_commandc                 C   s"   |   }|  |d< t|| jdS )Nr[   )Zoperations_tablerN   )rD   rd   r   r   rL   r+   r+   r,   rg     s
   zServiceCommand._create_parserr6   )r   r   r   __doc__r=   propertyr   setterr   r   rD   r   r   r   r   rd   rg   r+   r+   r+   r,   rO     s(    





		rO   c                   @   s   e Zd ZdZeedZeZ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d Zdd Zdd Zdd  Zd!S )"r   zA single operation of a service.

    This class represents a single operation for a service, for
    example ``ec2.DescribeInstances``.

    )listZbooleanc                 C   s@   d| _ || _|| _|| _| g| _|| _|| _|jrd| _dS dS )aT  

        :type name: str
        :param name: The name of the operation/subcommand.

        :type parent_name: str
        :param parent_name: The name of the parent command.

        :type operation_model: ``botocore.model.OperationModel``
        :param operation_object: The operation model
            associated with this subcommand.

        :type operation_caller: ``CLIOperationCaller``
        :param operation_caller: An object that can properly call the
            operation.

        :type session: ``botocore.session.Session``
        :param session: The session object.

        NT)	
_arg_tabler   _parent_name_operation_callerr   _operation_model_sessionZ
deprecatedZ_UNDOCUMENTED)r<   r   r   r   r   r/   r+   r+   r,   r=     s   
zServiceOperation.__init__c                 C   r   r6   r   r@   r+   r+   r,   r     r   zServiceOperation.namec                 C   r   r6   r   r   r+   r+   r,   r     r   c                 C   r   r6   r   r@   r+   r+   r,   r     r   zServiceOperation.lineagec                 C   r   r6   r   r   r+   r+   r,   r     r   c                 C   s   dd | j D S )Nc                 S   s   g | ]}|j qS r+   )r   ).0cmdr+   r+   r,   
<listcomp>  s    z2ServiceOperation.lineage_names.<locals>.<listcomp>r   r@   r+   r+   r,   r     s   zServiceOperation.lineage_namesc                 C   rB   r6   )r   _create_argument_tabler@   r+   r+   r,   r     s   

zServiceOperation.arg_tablec           
      C   s  d| j | jf }| j|| j|| j|d | | j}| | ||\}}|jdkr4| 	 }|||S |jr=|
|j |rHtdd| d| j | jf }| j|||d | || j}d| j | jf }| j||||d	}	|	d ur}t|	tr{|	|	S | j| jjj| jj||S )
Nz+before-building-argument-table-parser.%s.%s)rT   r   r/   r   r[   zUnknown options: %sz, zoperation-args-parsed.%s.%s)r   r   zcalling-command.%s.%s)call_parametersr   r   )r   r   _emitr   r   _create_operation_parser	_add_helprq   r[   rd   appendr   r   _build_call_parameters_emit_first_non_none_response
isinstancer}   r   invoker   r   rN   r   )
r<   r   r   eventZoperation_parserr   r   Zop_helpr   overrider+   r+   r,   r     sZ   





zServiceOperation.__call__c                 C   s"   t | j| j| j| jd| jdS )Nr   )r   r   r   r   )r   r   r   r   r   r   r   r@   r+   r+   r,   rd     s   z$ServiceOperation.create_help_commandc                 C   s   |j ddd d S )Nr[   ?)nargs)add_argument)r<   rQ   r+   r+   r,   r     s   zServiceOperation._add_helpc                 C   sL   i }t |}| D ]}|j}||v r#|| }| ||}||| q
|S r6   )varsvaluespy_name_unpack_argZadd_to_params)r<   r   r   Zservice_paramsr   
arg_objectr   r   r+   r+   r,   r   $  s   z'ServiceOperation._build_call_parametersc                 C   s,   | j }| jjj}t| jd}t|||||S )Nr   )r   r   r   Zendpoint_prefixr   r   r   )r<   rZ   r   r/   rN   r   r+   r+   r,   r   3  s   
zServiceOperation._unpack_argc              	   C   s   t  }| jj}g }i }|d ur|j}|j}| D ]6\}}t|d}| j|j	| j
}|jdd}	||v o8|	 }
| jd}||||
| j||d}|| qt| | jd| j| jf | j| j| |d |S )Nr   ZidempotencyTokenFr-   )r   Zargument_modelis_requiredr   Zserialized_namer-   zbuilding-argument-table.%s.%s)r   r/   rt   rT   )r   r   input_shapeZrequired_membersmembersitemsr   	ARG_TYPESr2   	type_nameDEFAULT_ARG_CLASSmetadatar   r3   rV   r~   r   r   r   r   )r<   rT   r   Zrequired_argumentsZarg_dictZarg_nameZ	arg_shapeZcli_arg_nameZ	arg_classZis_tokenr   r-   r   r+   r+   r,   r   =  sD   



z'ServiceOperation._create_argument_tablec                 K      | j j|fi |S r6   )r   rK   r<   r   kwargsr+   r+   r,   r   ]  s   zServiceOperation._emitc                 K   r   r6   )r   Zemit_first_non_none_responser   r+   r+   r,   r   `  s
   z.ServiceOperation._emit_first_non_none_responsec                 C   s   t |}|S r6   r   )r<   r   rQ   r+   r+   r,   r   d  s   z)ServiceOperation._create_operation_parserN)r   r   r   r   r   r   r   r   r   r=   r   r   r   r   r   r   r   rd   r   r   r   r   r   r   r   r+   r+   r+   r,   r     s8    "





6
 r   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )r   z.Call an AWS operation and format the response.c                 C   r   r6   )r   r;   r+   r+   r,   r=   m  s   
zCLIOperationCaller.__init__c                 C   s<   | j j||j|j|jd}| ||||}| ||| dS )a  Invoke an operation and format the response.

        :type service_name: str
        :param service_name: The name of the service.  Note this is the service name,
            not the endpoint prefix (e.g. ``ses`` not ``email``).

        :type operation_name: str
        :param operation_name: The operation name of the service.  The casing
            of the operation name should match the exact casing used by the service,
            e.g. ``DescribeInstances``, not ``describe-instances`` or
            ``describe_instances``.

        :type parameters: dict
        :param parameters: The parameters for the operation call.  Again, these values
            have the same casing used by the service.

        :type parsed_globals: Namespace
        :param parsed_globals: The parsed globals from the command line.

        :return: None, the result is displayed through a formatter, but no
            value is returned.

        )Zregion_nameendpoint_urlZverifyr   )r   Zcreate_clientr   r   Z
verify_ssl_make_client_call_display_response)r<   rN   r   
parametersr   clientresponser+   r+   r,   r   p  s   zCLIOperationCaller.invokec                 C   sR   t |}||r|jr||}|jdi |}|S t|t |di |}|S )Nr+   )r   Zcan_paginateZpaginateZget_paginatorgetattr)r<   r   r   r   r   Zpy_operation_nameZ	paginatorr   r+   r+   r,   r     s   
z$CLIOperationCaller._make_client_callc                 C   s2   |j }|d u r| jd}t||}||| d S )Noutput)r   r   r   r   )r<   command_namer   r   r   	formatterr+   r+   r,   r     s
   
z$CLIOperationCaller._display_responseN)r   r   r   r   r=   r   r   r   r+   r+   r+   r,   r   i  s    !r   )Bro   r{   r   Zbotocore.sessionr0   r   r5   Zbotocore.hooksr   r   Zbotocore.compatr   r   Zbotocore.exceptionsr   r   r	   Zbotocore.historyr
   r   r   Zawscli.compatr   Zawscli.formatterr   Zawscli.pluginr   Zawscli.commandsr   r   Zawscli.argparserr   r   r   r   Zawscli.helpr   r   r   Zawscli.argumentsr   r   r   r   r   Zawscli.argprocessr   Zawscli.aliasr   r    Zawscli.utilsr!   r"   	getLoggerr~   r   r'   encoder&   r%   r1   objectr4   rO   r   r   r+   r+   r+   r,   <module>   sb   


	 Fv U