a
    Fb2'                     @   s   d 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	m
Z
 ddlmZ ed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dS )zE
System command aliases.

Authors:

* Fernando Perez
* Brian Granger
    N)Configurable   )
UsageError)ListInstance)errorz^(\s*)()(\S+)(.*$)c                  C   sx   t jdkr\g d} tjdr(g d}n*tjds@tjdrJg d}ng d}| | } nt jd	v rpg d
} ng } | S )z1Return list of shell aliases to auto-define.
    posix)mkdirr
   rmdirr   )mvr   )rmr   )cpr   )catr   Zlinux))lszls -F --color)llzls -F -o --color)lfzls -F -o --color %l | grep ^-)lkzls -F -o --color %l | grep ^l)ldirzls -F -o --color %l | grep /$)lxz ls -F -o --color %l | grep ^-..xZopenbsdZnetbsd))r   zls -F)r   zls -F -l)r   zls -F -l %l | grep ^-)r   zls -F -l %l | grep ^l)r   zls -F -l %l | grep /$)r   zls -F -l %l | grep ^-..x))r   zls -F -G)r   zls -F -l -G)r   zls -F -l -G %l | grep ^-)r   zls -F -l -G %l | grep ^l)r   zls -F -G -l %l | grep /$)r   zls -F -l -G %l | grep ^-..x)ntZdos))r   zdir /on)Zddirdir /ad /on)r   r   r	   r   )echor   )renr   )copyr   )osnamesysplatform
startswith)default_aliasesZ
ls_aliases r"   1lib/python3.9/site-packages/IPython/core/alias.pyr!   (   s    






r!   c                   @   s   e Zd ZdS )
AliasErrorN__name__
__module____qualname__r"   r"   r"   r#   r$   q   s   r$   c                   @   s   e Zd ZdS )InvalidAliasErrorNr%   r"   r"   r"   r#   r)   u   s   r)   c                   @   s:   e Zd ZdZh dZdd Zdd Zdd Zdd
dZdS )AliaszCallable object storing the details of one alias.

    Instances are registered as magic functions to allow use of aliases.
    >   ZcdaliasZdhistZpopdZunaliasZpushdc                 C   s,   || _ || _|| _d|| _|  | _d S )NzAlias for `!{}`)shellr   cmdformat__doc__validatenargs)selfr,   r   r-   r"   r"   r#   __init__   s
    zAlias.__init__c                 C   s   | j | jv rtd| j  z| jjjd | j  }W n tyD   Y n0 t|ts^td| j  t| j	t
sxtd| j	 | j	d| j	d }|dkr| j	ddkrtd	|S )
z7Validate the alias, and return the number of arguments.z@The name %s can't be aliased because it is a keyword or builtin.linezAThe name %s can't be aliased because it is another magic command.z*An alias command must be a string, got: %r%s%%sr   %lzEThe %s and %l specifiers are mutually exclusive in alias definitions.)r   	blacklistr)   r,   magics_managermagicsKeyError
isinstancer*   r-   strcountfind)r2   callerr1   r"   r"   r#   r0      s(    
zAlias.validatec                 C   s   d | j| jS )Nz<alias {} for {!r}>)r.   r   r-   r2   r"   r"   r#   __repr__   s    zAlias.__repr__ c                 C   s   | j }| j}|ddkr*|d|}d}|dkrZ|ddkrL|dd}d||f }nZ|d |}t||k rtd| j|t|f d|t|d |  d		||d  f }| j
| d S )
Nr7   r   rC   r6   r   r5   z%s %sz+Alias <%s> requires %s arguments, %s given. )r-   r1   r?   replacesplitlenr   r   tuplejoinr,   system)r2   restr-   r1   argsr"   r"   r#   __call__   s     *zAlias.__call__N)rC   )	r&   r'   r(   r/   r8   r3   r0   rB   rM   r"   r"   r"   r#   r*   x   s   r*   c                       s   e Zd Zee jddZeg djddZedddZd fdd	Z	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  ZS )AliasManagerT)Zconfig)Zdefault_valuez1IPython.core.interactiveshell.InteractiveShellABC)Z
allow_noneNc                    s6   t t| jf d|i| | jjjd | _|   d S )Nr,   r4   )superrN   r3   r,   r9   r:   
linemagicsinit_aliases)r2   r,   kwargs	__class__r"   r#   r3      s    zAliasManager.__init__c                 C   sH   | j | j D ]6\}}|dr6| jjdkr6|dd}| || qd S )Nzls ZNoColorz --colorrC   )r!   user_aliasesr    r,   ZcolorsrE   soft_define_aliasr2   r   r-   r"   r"   r#   rQ      s    zAliasManager.init_aliasesc                 C   s   dd | j  D S )Nc                 S   s$   g | ]\}}t |tr||jfqS r"   )r<   r*   r-   ).0nfuncr"   r"   r#   
<listcomp>   s   
z(AliasManager.aliases.<locals>.<listcomp>)rP   itemsrA   r"   r"   r#   aliases   s    zAliasManager.aliasesc              
   C   sF   z|  || W n0 ty@ } ztd|  W Y d}~n
d}~0 0 dS )z2Define an alias, but don't raise on an AliasError.zInvalid alias: %sN)define_aliasr$   r   )r2   r   r-   er"   r"   r#   rV      s    zAliasManager.soft_define_aliasc                 C   s(   t | j||d}| jjj|d|d dS )zDefine a new alias after validating it.

        This will raise an :exc:`AliasError` if there are validation
        problems.
        )r,   r   r-   r4   )Z
magic_kindZ
magic_nameN)r*   r,   r9   Zregister_function)r2   r   r-   r@   r"   r"   r#   r^      s    zAliasManager.define_aliasc                 C   s    | j |d}t|tr|S dS )z9Return an alias, or None if no alias by that name exists.N)rP   getr<   r*   )r2   r   Zanamer"   r"   r#   	get_alias   s    zAliasManager.get_aliasc                 C   s   |  |duS )z?Return whether or not a given name has been defined as an aliasN)ra   r2   r   r"   r"   r#   is_alias   s    zAliasManager.is_aliasc                 C   s$   |  |r| j|= ntd| d S )N%s is not an alias)rc   rP   
ValueErrorrb   r"   r"   r#   undefine_alias   s    

zAliasManager.undefine_aliasc                 C   s   | j D ]\}}| | qd S )N)r]   rf   rW   r"   r"   r#   clear_aliases   s    zAliasManager.clear_aliasesc                 C   s$   |  |}|r|jS td| dS )z/Retrieve the command to which an alias expands.rd   N)ra   r-   re   )r2   r   r@   r"   r"   r#   retrieve_alias   s    
zAliasManager.retrieve_alias)N)r&   r'   r(   r   r!   tagrU   r   r,   r3   rQ   propertyr]   rV   r^   ra   rc   rf   rg   rh   __classcell__r"   r"   rS   r#   rN      s   

rN   )r/   r   rer   Ztraitlets.config.configurabler   r   r   Z	traitletsr   r   ZloggingcompileZshell_line_splitr!   	Exceptionr$   r)   objectr*   rN   r"   r"   r"   r#   <module>   s   
IH