3

g                 @   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 ddlmZ erxddlmZmZmZmZ d	d
 Zdd Zdd ZdS )zBLogic that powers autocompletion installed by ``pip completion``.
    N)chain)create_main_parser)commands_dictcreate_command)get_installed_distributions)MYPY_CHECK_RUNNING)AnyIterableListOptionalc                 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rtj	d |dko j
d
 }|r6g } j }	x<tddD ].}
|
jj
|	r|
j| dd kr|j|
j qW |r6x|D ]}
t|
 qW tj	d t|}xH|jjD ]<}|jtjkrHx&|j|j D ]}|j||jf qhW qHW dd | d|d  D fdd|D } fdd|D }t| ||jj}|rt |}dd |D }x|D ]>}|d }|d r.|d dd dkr.|d7 }t| qW ndd |jD }|j|j tj|} j
d
rxN|D ]$}|jtjkrv||j|j 7 }qvW n t| ||}|rt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	uninstall-T)
local_onlyc             S   s   g | ]}|j d d qS )=r   )split).0x r   D/tmp/pip-unpacked-wheel-v59g9nu1/pip/_internal/cli/autocompletion.py
<listcomp>H   s    z autocomplete.<locals>.<listcomp>c                s    g | ]\}}| kr||fqS r   r   )r   r   v)	prev_optsr   r   r   I   s    c                s"   g | ]\}}|j  r||fqS r   )
startswith)r   kr   )currentr   r   r   K   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   m   s    )r   r   ) osenvironr   int
IndexErrorr   listr   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$   r   from_iterablejoin)cwordscwordr2   subcommandsoptionssubcommand_namewordshould_list_installed	installedlcdist
subcommandoptopt_strcompletion_typepathsoption	opt_labeloptsflattened_optsr   )r!   r   r   autocomplete   s~    








 

rQ   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 st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"   rS   rT   r   )r   r   r   r   r   	<genexpr>   s   z+get_path_completion_type.<locals>.<genexpr>)r   r   r4   r5   strr   metavarany)r>   r?   rO   rI   or   r   r   r9   q   s    	

r9   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rt 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   rU      s    z&auto_complete_paths.<locals>.<genexpr>rT   r   )r'   r"   r   abspathaccessR_OKrZ   listdirr=   isfileisdir)r!   rK   	directorycurrent_path	file_listfrI   	comp_filer   )r[   r   r:      s    

r:   )__doc__r4   r'   r,   	itertoolsr   pip._internal.cli.main_parserr   pip._internal.commandsr   r   pip._internal.utils.miscr   pip._internal.utils.typingr   typingr   r	   r
   r   rQ   r9   r:   r   r   r   r   <module>   s   _