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rtd |dkoԈ 	d }|rNg } 
 }x<tddD ].}|j	|r|j| dd kr||j qW |rNx|D ]}t| q2W td t|  }	xH|	jjD ]<}
|
jtjkrbx&|
j|
j D ]}|||
jf qW 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 ndd |jD }||j dd |D } 	drxJ|D ]$}
|
jtjkr|
j|
j 7 qW nt| ||}|rt |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rt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rt 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   [