B
    ãUvgÃ  ã               @   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mZ ddl	m
Z
 dd„ Zdd	„ Zd
d„ ZdS )zBLogic that powers autocompletion installed by ``pip completion``.
é    N)Úcreate_main_parser)Úcommands_dictÚget_summaries)Úget_installed_distributionsc                 s  dt jkrdS t jd  ¡ dd… } tt jd ƒ}y| |d  ‰ W n tk
rZ   d‰ Y nX dd„ tƒ D ƒ‰g }y‡fd	d„| D ƒd
 }W n tk
r¢   d}Y nX tƒ }|�rX|dkrÂt d¡ |dkoÔˆ  	d¡ }|�rNg }ˆ  
¡ }x<tdd�D ].}|j 	|¡rô|j| dd… krô| |j¡ qôW |�rNx|D ]}t|ƒ �q2W t d¡ t| ƒ }	xH|	jjD ]<}
|
jtjk�rbx&|
j|
j D ]}| ||
jf¡ �q‚W �qbW dd„ | d|d … D ƒ‰‡fdd„|D ƒ}‡ fdd„|D ƒ}t| ||	jjƒ}|�rtˆ |ƒ}dd„ |D ƒ}xæ|D ]>}|d
 }|d �rH|d
 dd… dk�rH|d7 }t|ƒ �qW nždd„ |jD ƒ}| |j¡ dd„ |D ƒ}ˆ  	d¡�r¾xJ|D ]$}
|
jtjk�r”ˆ|
j|
j 7 ‰�q”W nt| ||ƒ}|�rÚtˆ |ƒ‰td ‡ fdd„ˆD ƒ¡ƒ t d¡ dS )z?Entry Point for completion of main and subcommand options.
    ÚPIP_AUTO_COMPLETENÚ
COMP_WORDSé   Ú
COMP_CWORDÚ c             S   s   g | ]\}}|‘qS © r   )Ú.0ÚcmdÚsummaryr   r   úA/tmp/pip-install-r_9ig3yj/pip/pip/_internal/cli/autocompletion.pyú
<listcomp>   s    z autocomplete.<locals>.<listcomp>c                s   g | ]}|ˆ kr|‘qS r   r   )r   Úw)Úsubcommandsr   r   r      s    r   Úhelp)ÚshowÚ	uninstallú-T)Ú
local_onlyc             S   s   g | ]}|  d ¡d ‘qS )ú=r   )Úsplit)r   Úxr   r   r   r   A   s    c                s    g | ]\}}|ˆ kr||f‘qS r   r   )r   r   Úv)Ú	prev_optsr   r   r   B   s    c                s"   g | ]\}}|  ˆ ¡r||f‘qS r   )Ú
startswith)r   Úkr   )Úcurrentr   r   r   D   s    c             s   s   | ]}|d fV  qdS )r   Nr   )r   Úoptr   r   r   ú	<genexpr>M   s    zautocomplete.<locals>.<genexpr>é   z--r   c             S   s   g | ]
}|j ‘qS r   )Úoption_list)r   Úir   r   r   r   W   s    c             s   s   | ]}|D ]
}|V  q
qd S )Nr   )r   ÚitÚor   r   r   r!   Y   s    ú c                s   g | ]}|  ˆ ¡r|‘qS r   )r   )r   r   )r   r   r   r   d   s    )ÚosÚenvironr   ÚintÚ
IndexErrorr   r   ÚsysÚexitr   Úlowerr   ÚkeyÚappendÚprintr   ÚparserÚoption_list_allr   ÚoptparseÚSUPPRESS_HELPÚ
_long_optsÚ_short_optsÚnargsÚget_path_completion_typeÚauto_complete_pathsÚoption_groupsr#   Újoin)ÚcwordsÚcwordÚoptionsÚsubcommand_namer2   Úshould_list_installedÚ	installedÚlcÚdistÚ
subcommandr    Úopt_strÚcompletion_typeÚoptionÚ	opt_labelÚoptsr   )r   r   r   r   Úautocomplete   sx    








 

rK   c             C   s–   |dk s| |d    d¡sdS xr|D ]j}|jtjkr6q$xVt|ƒ d¡D ]D}| |d   d¡d |krF|jr„tdd„ |j d¡D ƒƒrF|jS qFW q$W dS )	aL  Get the type of path completion (``file``, ``dir``, ``path`` or None)

    :param cwords: same as the environmental variable ``COMP_WORDS``
    :param cword: same as the environmental variable ``COMP_CWORD``
    :param opts: The available options to check
    :return: path completion type (``file``, ``dir``, ``path`` or None)
    r"   r   Nú/r   r   c             s   s   | ]}|d kV  qdS ))ÚpathÚfileÚdirNr   )r   r   r   r   r   r!   x   s   z+get_path_completion_type.<locals>.<genexpr>)r   r   r4   r5   Ústrr   ÚmetavarÚany)r=   r>   rJ   r    r&   r   r   r   r9   h   s    
r9   c             #   sÀ   t j | ¡\}‰ t j |¡}t  |t j¡s.dS t j ˆ ¡‰ ‡ fdd„t  |¡D ƒ}xh|D ]`}t j ||¡}t j t j ||¡¡}|dkrœt j 	|¡rœ|V  qXt j 
|¡rXt j |d¡V  qXW dS )ao  If ``completion_type`` is ``file`` or ``path``, list all regular files
    and directories starting with ``current``; otherwise only list directories
    starting with ``current``.

    :param current: The word to be completed
    :param completion_type: path completion type(`file`, `path` or `dir`)i
    :return: A generator of regular files and/or directories
    Nc             3   s$   | ]}t j |¡ ˆ ¡r|V  qd S )N)r(   rM   Únormcaser   )r   r   )Úfilenamer   r   r!   �   s    z&auto_complete_paths.<locals>.<genexpr>rO   r
   )r(   rM   r   ÚabspathÚaccessÚR_OKrS   Úlistdirr<   ÚisfileÚisdir)r   rG   Ú	directoryÚcurrent_pathÚ	file_listÚfr    Ú	comp_filer   )rT   r   r:   }   s    	
r:   )Ú__doc__r4   r(   r,   Úpip._internal.cli.main_parserr   Úpip._internal.commandsr   r   Úpip._internal.utils.miscr   rK   r9   r:   r   r   r   r   Ú<module>   s   [