U
    .e                  
   @   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mZ ddddd	d
ddddg
Z	ej
dkrpd dlmZmZ nd dlmZmZ d#dd	Zi Zd$ddZd%dd
Zd&ddZdd Zdd Zedddged edddged d'ddZd d Zd!d Zd"d ZdS )(    N)defaultdict   )CommandLineParserErrorExitReplException_execute_internal_and_sys_cmds_exit_internal_get_registered_target_help_internal_resolve_context_register_internal_commanddispatch_repl_commandshandle_internal_commandssplit_arg_stringexit)   r   )IterableMappingc                 C   s   | r|j }t|tjr|jsZ||| \}}} |dkr:|S |j|| |dd}|j|j } q| r||| \}}} |dkr||S |j|| |dddd}|j} qZ|}|j|j} q qq |S )a;  Produce the context hierarchy starting with the command and
    traversing the complete arguments. This only follows the commands,
    it doesn't trigger input prompts or callbacks.

    :param args: List of complete args before the incomplete value.
    :param cli_ctx: `click.Context` object of the CLI group
    NT)parentresilient_parsingF)r   Zallow_extra_argsallow_interspersed_argsr   )	command
isinstanceclickZMultiCommandchainZresolve_commandZmake_contextZprotected_argsargs)r   ctxr   namecmdZsub_ctx r   4/tmp/pip-unpacked-wheel-rnw4k0lb/click_repl/utils.pyr
       s4    	Tc                 C   s\   t j | |d}d|_d|_g }z|D ]}|| q$W n  tk
rV   ||j Y nX |S )a  Split an argument string as with :func:`shlex.split`, but don't
    fail if the string is incomplete. Ignores a missing closing quote or
    incomplete escape sequence and uses the partial token as-is.
    .. code-block:: python
        split_arg_string("example 'my file")
        ["example", "my file"]
        split_arg_string("example my\")
        ["example", "my"]
    :param string: String to split.
    )posixT )shlexwhitespace_split
commentersappend
ValueErrortoken)stringr    lexoutr'   r   r   r   r   Q   s    c                 C   sf   t |dstdt| tr$| g} n(t| ts8t| tsLtdt| j| D ]}||ft	|< qPd S )N__call__z#Internal command must be a callablez="names" must be a string, or an iterable object, but got "{}")
hasattrr&   r   strr   r   formattype__name___internal_commands)namestargetdescriptionr   r   r   r   r   n   s    

c                 C   s   t | }|r|d S |S )Nr   )r1   get)r   defaulttarget_infor   r   r   r      s    
c                   C   s
   t  d S )N)r   r   r   r   r   r      s    c               	   C   s   t  } | d |   | d | d W 5 Q R X | dT | d tt}t	 D ]\}}||d  
| q`| dd |	 D  W 5 Q R X |  }|S )	Nz	REPL helpzExternal Commandsz!prefix external commands with "!"zInternal Commandsz!prefix internal commands with ":"r   c                 s   s,   | ]$\}}d  tdjt||fV  qdS )z, z:{}N)joinmapr.   sorted).0r4   Z	mnemonicsr   r   r   	<genexpr>   s   z!_help_internal.<locals>.<genexpr>)r   HelpFormatterZwrite_headingindentsection
write_textr   listr1   itemsr%   Zwrite_dlgetvalue)	formatterZ
info_tableZmnemonicr7   valr   r   r   r	      s    


qquitzexits the repl?hhelpz!displays general help informationc              
   C   st   |rt | rdS |r4t| }t|tr4t| dS z
t| W S  tk
rn } ztd	|W 5 d}~X Y nX dS )z_
    Executes internal, system, and all the other registered click commands from the input
    Nz{})
r   r   r   r-   r   Zechor   r&   r   r.   )r   Zallow_internal_commandsZallow_system_commandsresulter   r   r   r      s    


c                   C   s
   t   dS )zExit the replN)r   r   r   r   r   r      s    c                 C   s$   |  dr t| dd  dS dS )zo
    Execute system commands entered in the repl.

    System commands are all commands starting with "!".
    !r   NTF)
startswithossystem)r   r   r   r   r      s    
c                 C   s,   |  dr(t| dd dd}|r(| S dS )ze
    Run repl-internal commands.

    Repl-internal commands are all commands starting with ":".
    :r   N)r6   )rN   r   )r   r3   r   r   r   r      s    
)N)T)N)N)TT)r   rO   r"   syscollectionsr   
exceptionsr   r   __all__version_infocollections.abcr   r   r
   r1   r   r   r   r   r	   r   r   r   r   r   r   r   r   <module>   sL   

.


    
