o
    !d!                     @   s   d Z ddlZddlZddlm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dZdd ZdS )zC
Utility functions to make it easier to work with customizations.

    N)ClientErrorc                 C       | | }|| |< ||_ | |= d S Nname)argument_tableexisting_namenew_namecurrent r   ?usr/lib/python3.10/site-packages/awscli/customizations/utils.pyrename_argument      
r   c                 C   s$   | | }t  |}||_|| |< |S r   )copyr   )r   Zcurrent_nameZ	copy_namer
   copy_argr   r   r   _copy_argument   s
   
r   c                 C   s:   | | }t | ||}d|_|jrd|_d|_d|_dS dS )a{  Create a hidden alias for an existing argument.

    This will copy an existing argument object in an arg table,
    and add a new entry to the arg table with a different name.
    The new argument will also be undocumented.

    This is needed if you want to check an existing argument,
    but you still need the other one to work for backwards
    compatibility reasons.

    TFN)r   _UNDOCUMENTEDrequiredZ_DOCUMENT_AS_REQUIRED)r   r   
alias_namer
   r   r   r   r   make_hidden_alias&   s   
r   c                 C   r   r   r   command_tabler   r	   r
   r   r   r   rename_command?   r   r   c                 C   s   | | }t | || d|_dS )aW  Moves an argument to a new name, keeping the old as a hidden alias.

    :type command_table: dict
    :param command_table: The full command table for the CLI or a service.

    :type existing_name: str
    :param existing_name: The current name of the command.

    :type new_name: str
    :param new_name: The new name for the command.
    TNr   r   r   r   r   r   alias_commandF   s   
r   c                 C   s   t | ||}d|_dS )a  Create a hidden alias for an exiting command.

    This will copy an existing command object in a command table and add a new
    entry to the command table with a different name. The new command will
    be undocumented.

    This is needed if you want to change an existing command, but you still
    need the old name to work for backwards compatibility reasons.

    :type command_table: dict
    :param command_table: The full command table for the CLI or a service.

    :type existing_name: str
    :param existing_name: The current name of the command.

    :type alias_name: str
    :param alias_name: The new name for the command.
    TNr   )r   r   r   newr   r   r   make_hidden_command_aliasW   s   
r   c                     s    fdd}|S )Nc                    s   t | g R  S r   )validate_mutually_exclusive)parsed_argskwargsgroupsr   r   _handlero   s   z5validate_mutually_exclusive_handler.<locals>._handlerr   )r!   r"   r   r    r   #validate_mutually_exclusive_handlern   s   r#   c                    s   t | tdd |D  t fddD sdS d}fddD D ]"}t||}|du r2q&|du r9|}q&||ksHtd|d|f q&dS )	z6Validate mutually exclusive groups in the parsed args.c                 s   s    | ]
}|D ]}|V  qqd S r   r   ).0groupargr   r   r   	<genexpr>w   s    z.validate_mutually_exclusive.<locals>.<genexpr>c                 3   s$    | ]}| d ur| v V  qd S r   r   r$   kall_args	args_dictr   r   r'   x   s   " Nc                    s   g | ]
} | d ur|qS r   r   r(   )r,   r   r   
<listcomp>}   s    z/validate_mutually_exclusive.<locals>.<listcomp>zVThe key "%s" cannot be specified when one of the following keys are also specified: %sz, )varssetany_get_group_for_key
ValueErrorjoin)r   r!   Zcurrent_groupkeyZ	key_groupr   r*   r   r   t   s"   
r   c                 C   s   |D ]
}| |v r|  S qd S r   r   )r4   r!   r%   r   r   r   r1      s
   r1   c              
   C   sj   d}z	| j |d W |S  ty4 } zt|jd d }|dkr)d}W Y d }~|S W Y d }~|S d }~ww )NT)ZBucketErrorZCodei  F)Zhead_bucketr   intresponse)Z	s3_clientZbucket_nameZbucket_existseZ
error_coder   r   r   s3_bucket_exists   s   
r9   c                 C   sZ   i }d|v r|j |d< d|v r|j|d< d|v r|j|d< |r$|| | j|fi |S )a  Creates a service client, taking parsed_globals into account

    Any values specified in overrides will override the returned dict. Note
    that this override occurs after 'region' from parsed_globals has been
    translated into 'region_name' in the resulting dict.
    regionZregion_nameendpoint_url
verify_sslZverify)r:   r;   r<   updateZcreate_client)ZsessionZservice_nameZparsed_globalsZ	overridesZclient_argsr   r   r   !create_client_from_parsed_globals   s   



r>   c                 C   sp   |du rt j}z||  W n# ty1   t|dd}|du r!d}| |d|}|| Y nw |  dS )z
    This function is used to properly write unicode to a file, usually
    stdout or stdderr.  It ensures that the proper encoding is used if the
    statement is not a string type.
    Nencodingasciireplace)sysstdoutwriteUnicodeEncodeErrorgetattrencodedecodeflush)	statementout_fileZnew_encodingZnew_statementr   r   r   	uni_print   s    rL   c                 C   s(   |   }|drdS |drdS dS )z7Method to return region value as expected by policy arnzcn-zaws-cnzus-govz
aws-us-govZaws)lower
startswith)r:   Zregion_stringr   r   r   get_policy_arn_suffix   s   

rO   r   )__doc__r   rB   Zbotocore.exceptionsr   r   r   r   r   r   r   r#   r   r1   r9   r>   rL   rO   r   r   r   r   <module>   s$   

+