3
ûâdeÿ  ã               @   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m	Z	m
Z
 ddlmZ ddlmZmZ ddlmZ ddœd	d
„Ze	e eee e
e dœdd„Zeeee dœdd„ZdS )zBLogic that powers autocompletion installed by ``pip completion``.
é    N)Úchain)ÚAnyÚIterableÚListÚOptional)Úcreate_main_parser)Úcommands_dictÚcreate_command)Úget_default_environment)Úreturnc                 sÈ  dt jkrdS t jd jƒ dd… ‰tt jd ƒ} yˆ| d  ‰ W n tk
rZ   d‰ Y nX tƒ }ttƒ}g }d}xˆD ]}||krx|}P qxW |dk	�r|dkrªtj	dƒ ˆ j
dƒ o¼|dk}|�rtƒ }ˆ jƒ ‰‡‡fdd„|jdd�D ƒ}|�rx|D ]}	t|	ƒ qúW tj	dƒ t|ƒ}
xH|
jjD ]<}|jtjk�r&x&|j|j D ]}|j||jfƒ �qFW �q&W 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 �r|d dd… dk�r|d7 }t|ƒ �qØW nždd„ |jD ƒ}|j|jƒ tj|ƒ}ˆ j
dƒ�r~xN|D ]$}|jtjk�rT||j|j 7 }�qTW n tˆ| |ƒ}|�ržttˆ |ƒƒ}tdj‡ fdd„|D ƒƒƒ tj	dƒ dS )z:Entry Point for completion of main and subcommand options.ÚPIP_AUTO_COMPLETENÚ
COMP_WORDSé   Ú
COMP_CWORDÚ Úhelpú-ÚshowÚ	uninstallc                s0   g | ](}|j jˆƒr|j ˆ d d… kr|j ‘qS )r   N)Úcanonical_nameÚ
startswith)Ú.0Údist)ÚcwordsÚlc© ú?/tmp/pip-build-red6nood/pip/pip/_internal/cli/autocompletion.pyú
<listcomp>3   s   z autocomplete.<locals>.<listcomp>T)Ú
local_onlyc             S   s   g | ]}|j d ƒd ‘qS )ú=r   )Úsplit)r   Úxr   r   r   r   F   s    c                s    g | ]\}}|ˆ kr||f‘qS r   r   )r   r!   Úv)Ú	prev_optsr   r   r   G   s    c                s"   g | ]\}}|j ˆ ƒr||f‘qS r   )r   )r   Úkr"   )Úcurrentr   r   r   I   s    c             S   s   g | ]}|d f‘qS )r   r   )r   Úpathr   r   r   r   T   s    r   é   z--r   c             S   s   g | ]
}|j ‘qS r   )Úoption_list)r   Úir   r   r   r   ^   s    ú c                s   g | ]}|j ˆ ƒr|‘qS r   )r   )r   r!   )r%   r   r   r   k   s    )r   r   ) ÚosÚenvironr    ÚintÚ
IndexErrorr   Úlistr   ÚsysÚexitr   r
   ÚlowerZiter_installed_distributionsÚprintr	   ÚparserÚoption_list_allr   ÚoptparseÚSUPPRESS_HELPÚ
_long_optsÚ_short_optsÚappendÚnargsÚget_path_completion_typeÚauto_complete_pathsÚoption_groupsr(   r   Úfrom_iterableÚjoin)Úcwordr4   ÚsubcommandsÚoptionsÚsubcommand_nameÚwordZshould_list_installedÚenvÚ	installedr   Ú
subcommandÚoptÚopt_strÚcompletion_typeÚpathsÚoptionÚ	opt_labelÚoptsZflattened_optsr   )r%   r   r   r#   r   Úautocomplete   s~    




 




 

rP   )r   rA   rO   r   c             C   sš   |dk s| |d  j dƒ r dS xt|D ]l}|jtjkr8q&xXt|ƒjdƒD ]F}| |d  jdƒd |krH|j sˆtdd„ |jjdƒD ƒƒrH|jS qHW 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 )r&   ÚfileÚdirN)r&   rR   rS   r   )r   r!   r   r   r   ú	<genexpr>�   s    z+get_path_completion_type.<locals>.<genexpr>)r   r   r6   r7   Ústrr    ÚmetavarÚany)r   rA   rO   rI   Úor   r   r   r<   o   s    


r<   )r%   rK   r   c             #   sÀ   t jj| ƒ\}‰ t jj|ƒ}t j|t jƒs.dS t jjˆ ƒ‰ ‡ fdd„t j|ƒD ƒ}xh|D ]`}t jj||ƒ}t jjt jj||ƒƒ}|dkrœt jj	|ƒrœ|V  qXt jj
|ƒrXt j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j|ƒjˆ ƒr|V  qd S )N)r+   r&   Únormcaser   )r   r!   )Úfilenamer   r   rT   ˜   s    z&auto_complete_paths.<locals>.<genexpr>rS   r   )r+   r&   r    ÚabspathÚaccessÚR_OKrY   Úlistdirr@   ÚisfileÚisdir)r%   rK   Ú	directoryZcurrent_pathÚ	file_listÚfrI   Z	comp_filer   )rZ   r   r=   ‡   s    	
r=   )Ú__doc__r6   r+   r0   Ú	itertoolsr   Útypingr   r   r   r   Zpip._internal.cli.main_parserr   Zpip._internal.commandsr   r	   Zpip._internal.metadatar
   rP   rU   r-   r<   r=   r   r   r   r   Ú<module>   s   a