o
    !d+                     @   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 e 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G dd deZG dd deZdS )    N)raw_config_parsecompat_shell_quote)
CLICommand) emit_top_level_args_parsed_eventc                   @   s   e Zd ZdS )InvalidAliasExceptionN)__name__
__module____qualname__ r   r   0usr/lib/python3.10/site-packages/awscli/alias.pyr      s    r   c                   @   sN   e Zd ZejejddddfddZdd Zd	d
 Z	dd Z
dd ZdS )AliasLoader~z.awsclialiasc                 C   s   || _ d| _dS )zInterface for loading and interacting with alias file

        :param alias_filename: The name of the file to load aliases from.
            This file must be an INI file.
        N)	_filename_aliases)selfZalias_filenamer   r   r   __init__!   s   
zAliasLoader.__init__c                 C   s"   |   | _| | jdi  d S Ntoplevel)_load_aliasesr   _cleanup_alias_valuesgetr   r   r   r   _build_aliases,   s   
zAliasLoader._build_aliasesc                 C   s$   t j| jrt| jddS di iS )NF)Zparse_subsectionsr   )ospathexistsr   r   r   r   r   r   r   0   s
   zAliasLoader._load_aliasesc                 C   s   |D ]
}||   ||< qd S N)strip)r   aliasesr   r   r   r   r   6   s   z!AliasLoader._cleanup_alias_valuesc                 C   s    | j d u r	|   | j di S r   )r   r   r   r   r   r   r   get_aliases<   s   
zAliasLoader.get_aliasesN)r   r	   r
   r   r   
expanduserjoinr   r   r   r   r"   r   r   r   r   r       s    
r   c                   @   s   e Zd Zdd Zdd ZdS )AliasCommandInjectorc                 C      || _ || _dS )zInjects alias commands for a command table

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

        :type alias_loader: awscli.alias.AliasLoader
        :param alias_loader: The alias loader to use
        N)_session_alias_loader)r   sessionZalias_loaderr   r   r   r   C   s   	
zAliasCommandInjector.__init__c                 C   sh   | j   D ]*\}}|drt||}n||| j||g}||v r)|||  t| }|||< qd S )N!)r(   r"   items
startswithExternalAliasCommandr'   appendServiceAliasCommand)r   command_tableparser
alias_namealias_valueZ	alias_cmdZservice_alias_cmd_argsr   r   r   inject_aliasesO   s   



z#AliasCommandInjector.inject_aliasesN)r   r	   r
   r   r4   r   r   r   r   r%   B   s    r%   c                   @   s:   e Zd ZdZdd Zdd Zedd Zejdd Zd	S )
BaseAliasCommandTc                 C   r&   )a%  Base class for alias command

        :type alias_name: string
        :param alias_name: The name of the alias

        :type alias_value: string
        :param alias_value: The parsed value of the alias. This can be
            retrieved from `AliasLoader.get_aliases()[alias_name]`
        N)_alias_name_alias_value)r   r2   r3   r   r   r   r   g   s   

zBaseAliasCommand.__init__c                 C   s   t d)N__call__)NotImplementedError)r   argsZparsed_argsr   r   r   r8   t   s   zBaseAliasCommand.__call__c                 C   s   | j S r   r6   r   r   r   r   namew   s   zBaseAliasCommand.namec                 C   s
   || _ d S r   r;   )r   valuer   r   r   r<   {   s   
N)	r   r	   r
   Z_UNDOCUMENTEDr   r8   propertyr<   setterr   r   r   r   r5   d   s    
r5   c                       sH   e Zd ZddgZ	d fdd	Zdd Zdd	 Zd
d Zdd Z  Z	S )r/   debugZprofileNc                    s.   t t| || || _|| _|| _|| _dS )a  Command for a `toplevel` subcommand alias

        :type alias_name: string
        :param alias_name: The name of the alias

        :type alias_value: string
        :param alias_value: The parsed value of the alias. This can be
            retrieved from `AliasLoader.get_aliases()[alias_name]`

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

        :type command_table: dict
        :param command_table: The command table containing all of the
            possible service command objects that a particular alias could
            redirect to.

        :type parser: awscli.argparser.MainArgParser
        :param parser: The parser to parse commands provided at the top level
            of a CLI command which includes service commands and global
            parameters. This is used to parse the service command and any
            global parameters from the alias's value.

        :type shadow_proxy_command: CLICommand
        :param shadow_proxy_command: A built-in command that
            potentially shadows the alias in name. If the alias
            references this command in its value, the alias should proxy
            to this command as opposed to proxy to itself in the command
            table
        N)superr/   r   r'   _command_table_parser_shadow_proxy_command)r   r2   r3   r)   r0   r1   Zshadow_proxy_command	__class__r   r   r      s
    
zServiceAliasCommand.__init__c                 C   s   |   }| j|\}}| || || td| j||j | j	|j }| j
r?| j
j}||jkr?td| j
| j | j
}|||S )Nz/Alias %r passing on arguments: %r to %r commandz/Using shadowed command object: %s for alias: %s)_get_alias_argsrC   Zparse_known_args_update_parsed_globalsextendLOGr@   r6   commandrB   rD   r<   )r   r:   parsed_globals
alias_argsparsed_alias_args	remainingrK   Zshadow_namer   r   r   r8      s*   



zServiceAliasCommand.__call__c              
   C   sf   zt | j}W n ty } ztd| j|| jf d }~ww dd |D }td| j| j| |S )NzLValue of alias "%s" could not be parsed. Received error: %s when parsing:
%sc                 S   s   g | ]}| tjqS r   )r    r   linesep).0argr   r   r   
<listcomp>   s    z7ServiceAliasCommand._get_alias_args.<locals>.<listcomp>z2Expanded subcommand alias %r with value: %r to: %r)shlexsplitr7   
ValueErrorr   r6   rJ   r@   )r   rM   er   r   r   rG      s"   

z#ServiceAliasCommand._get_alias_argsc                 C   s:   |  |}t| j| |D ]}t||}t||| qd S r   ) _get_global_parameters_to_updater   r'   getattrsetattr)r   rN   rL   global_params_to_update
param_nameZupdated_param_valuer   r   r   rH      s   
z*ServiceAliasCommand._update_parsed_globalsc                 C   sT   g }t | D ]\}}| j||kr'|| jv r"td|| jf || q|S )NzZGlobal parameter "--%s" detected in alias "%s" which is not support in subcommand aliases.)varsr+   rC   Zget_defaultUNSUPPORTED_GLOBAL_PARAMETERSr   r6   r.   )r   rN   r[   Zparsed_paramr=   r   r   r   rX      s   

z4ServiceAliasCommand._get_global_parameters_to_updater   )
r   r	   r
   r^   r   r8   rG   rH   rX   __classcell__r   r   rE   r   r/      s    &r/   c                   @   s"   e Zd ZejfddZdd ZdS )r-   c                 C   s   || _ || _|| _dS )a0  Command for external aliases

        Executes command external of CLI as opposed to being a proxy
        to another command.

        :type alias_name: string
        :param alias_name: The name of the alias

        :type alias_value: string
        :param alias_value: The parsed value of the alias. This can be
            retrieved from `AliasLoader.get_aliases()[alias_name]`

        :type invoker: callable
        :param invoker: Callable to run arguments of external alias. The
            signature should match that of ``subprocess.call``
        N)r6   r7   _invoker)r   r2   r3   Zinvokerr   r   r   r      s   
zExternalAliasCommand.__init__c                 C   sP   | j dd  g}|dd |D  d|}td| j| j | | j|ddS )N   c                 s   s    | ]}t |V  qd S r   r   )rQ   ar   r   r   	<genexpr>  s    z0ExternalAliasCommand.__call__.<locals>.<genexpr> z1Using external alias %r with value: %r to run: %rT)shell)r7   rI   r$   rJ   r@   r6   r`   )r   r:   rL   Zcommand_componentsrK   r   r   r   r8     s   

zExternalAliasCommand.__call__N)r   r	   r
   
subprocesscallr   r8   r   r   r   r   r-      s    r-   )loggingr   rT   rf   Zbotocore.configloaderr   Zawscli.compatr   Zawscli.commandsr   Zawscli.utilsr   	getLoggerr   rJ   	Exceptionr   objectr   r%   r5   r/   r-   r   r   r   r   <module>   s   
""|