U
    P^                     @   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create_command)get_installed_distributionsc                     s  dt jkrdS t jd  dd } tt jd }z| |d   W n tk
rZ   d Y nX ttg }zfdd| D d	 }W n tk
r   d}Y nX t }|r:|d
krt	d |dkö 
d }|r>g }  }tddD ].}|j
|r|j| dd kr||j q|r>|D ]}t| q$t	d t|}	|	jjD ]8}
|
jtjkrN|
j|
j D ]}|||
jf qlqNdd | d|d  D fdd|D } fdd|D }t| ||	jj}|rt |}dd |D }|D ]>}|d	 }|d r,|d	 dd dkr,|d7 }t| qndd |jD }||j dd |D } 
dr|D ]$}
|
jtjkrt|
j|
j 7 qtn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   g | ]}| kr|qS  r   ).0w)subcommandsr   A/tmp/pip-install-220asx0h/pip/pip/_internal/cli/autocompletion.py
<listcomp>!   s      z autocomplete.<locals>.<listcomp>r   help)show	uninstall-T)
local_onlyc                 S   s   g | ]}| d d qS )=r   )splitr   xr   r   r   r   D   s     c                    s    g | ]\}}| kr||fqS r   r   )r   r   v)	prev_optsr   r   r   E   s      c                    s"   g | ]\}}|  r||fqS r   
startswith)r   kr   currentr   r   r   G   s     
 c                 s   s   | ]}|d fV  qdS )r   Nr   )r   optr   r   r   	<genexpr>P   s     zautocomplete.<locals>.<genexpr>   z--r   c                 S   s   g | ]
}|j qS r   )option_list)r   ir   r   r   r   Z   s     c                 s   s   | ]}|D ]
}|V  q
qd S Nr   )r   itor   r   r   r"   \   s        c                    s   g | ]}|  r|qS r   r   r   r   r   r   r   g   s     
 )osenvironr   int
IndexErrorlistr   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_namer5   should_list_installed	installedlcdist
subcommandr!   opt_strcompletion_typeoption	opt_labeloptsr   )r    r   r   r   autocomplete   s    





  
 
rN   c                 C   s   |dk s| |d   dsdS |D ]n}|jtjkr4q"t|dD ]L}| |d  dd |krB|jrtdd |jdD rB|j    S qBq"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"   z   s   z+get_path_completion_type.<locals>.<genexpr>)r   r   r7   r8   strr   metavarany)r@   rA   rM   r!   r(   r   r   r   r<   k   s    
r<   c                 #   s   t j| \} t j|}t |t js.dS t j   fddt |D }|D ]`}t j||}t jt j||}|dkrt j	|r|V  qVt j
|rVt j|dV  qVd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 r&   )r*   rP   normcaser   r   filenamer   r   r"      s    z&auto_complete_paths.<locals>.<genexpr>rR   r
   )r*   rP   r   abspathaccessR_OKrV   listdirr?   isfileisdir)r    rJ   	directorycurrent_path	file_listfr!   	comp_filer   rW   r   r=      s    	r=   )__doc__r7   r*   r/   pip._internal.cli.main_parserr   pip._internal.commandsr   r   pip._internal.utils.miscr   rN   r<   r=   r   r   r   r   <module>   s   [