ó
j·Ã^c           @   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 r« d d l m Z m Z m Z m Z n  d	 „  Z d
 „  Z d „  Z d S(   sB   Logic that powers autocompletion installed by ``pip completion``.
iÿÿÿÿN(   t   chain(   t   create_main_parser(   t   commands_dictt   create_command(   t   get_installed_distributions(   t   MYPY_CHECK_RUNNING(   t   Anyt   Iterablet   Listt   Optionalc          C   s%  d t  j k r d St  j d j ƒ  d }  t t  j d ƒ } y |  | d } Wn t k
 rh d } n Xt ƒ  } t t ƒ } g  } d } x$ |  D] } | | k r‘ | } Pq‘ q‘ W| d k	 r*| d k rÙ t	 j
 d ƒ n  | d k oò | j d
 ƒ } | rŽg  }	 | j ƒ  }
 xO t d t ƒ D]> } | j j |
 ƒ r| j |  d k r|	 j | j ƒ qqW|	 rŽx |	 D] } | GHqlWt	 j
 d ƒ qŽn  t | ƒ } xZ | j j D]L } | j t j k r§x1 | j | j D] } | j | | j f ƒ qÐWq§q§Wg  |  d | d !D] } | j d ƒ d ^ q	} g  | D]$ \ } } | | k r/| | f ^ q/} g  | D]' \ } } | j | ƒ r`| | f ^ q`} t |  | | j j ƒ } | rÜt | | ƒ } g  | D] } | d f ^ qÁ} n  x5| D]@ } | d } | d r| d d  d k r| d 7} n  | GHqãWnê g  | j D] } | j ^ q4} | j | j ƒ t j | ƒ } | j d
 ƒ r´xj | D]/ } | j t j k r~| | j | j 7} q~q~Wn0 t |  | | ƒ } | rät t | | ƒ ƒ } n  d j  g  | D] } | j | ƒ rñ| ^ qñƒ GHt	 j
 d ƒ d S(   s?   Entry Point for completion of main and subcommand options.
    t   PIP_AUTO_COMPLETENt
   COMP_WORDSi   t
   COMP_CWORDt    t   helpt   showt	   uninstallt   -t
   local_onlyt   =i    i   s   --t    (   R   R   (!   t   ost   environt   splitt   intt
   IndexErrorR   t   listR   t   Nonet   syst   exitt
   startswitht   lowerR   t   Truet   keyt   appendR   t   parsert   option_list_allR   t   optparset   SUPPRESS_HELPt
   _long_optst   _short_optst   nargst   get_path_completion_typet   auto_complete_pathst   option_groupst   option_listR    t   from_iterablet   join(   t   cwordst   cwordt   currentR#   t   subcommandst   optionst   subcommand_namet   wordt   should_list_installedt	   installedt   lct   distt
   subcommandt   optt   opt_strt   xt	   prev_optst   vt   kt   completion_typet   pathst   patht   optiont	   opt_labelt   it   optst   flattened_opts(    (    s1   site-packages/pip/_internal/cli/autocompletion.pyt   autocomplete   s~    
	%	!114"
			0c         C   sÌ   | d k  s$ |  | d j  d ƒ r( d Sx | D]• } | j t j k rM q/ n  xt t | ƒ j d ƒ D]] } |  | d j d ƒ d | k rc | j s¶ t d „  | j j d ƒ Dƒ ƒ rÀ | j Sqc qc Wq/ Wd S(   sL  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)
    i   R   t   /R   i    c         s   s   |  ] } | d k Vq d S(   RD   t   filet   dirN(   RD   RL   RM   (    (   t   .0R>   (    (    s1   site-packages/pip/_internal/cli/autocompletion.pys	   <genexpr>‚   s   N(	   R   R   R   R%   R&   t   strR   t   metavart   any(   R0   R1   RH   R<   t   o(    (    s1   site-packages/pip/_internal/cli/autocompletion.pyR*   q   s    	$!c         #   s  t  j j |  ƒ \ } ‰  t  j j | ƒ } t  j | t  j ƒ sC d St  j j ˆ  ƒ ‰  ‡  f d †  t  j | ƒ Dƒ } x“ | D]‹ } t  j j | | ƒ } t  j j t  j j | | ƒ ƒ } | d k rÝ t  j j	 | ƒ rÝ | Vq{ t  j j
 | ƒ r{ t  j j | d ƒ Vq{ q{ Wd S(   so  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   s0   |  ]& } t  j j | ƒ j ˆ  ƒ r | Vq d  S(   N(   R   RD   t   normcaseR   (   RN   R>   (   t   filename(    s1   site-packages/pip/_internal/cli/autocompletion.pys	   <genexpr>™   s    RM   R   (   R   RD   R   t   abspatht   accesst   R_OKRS   t   listdirR/   t   isfilet   isdir(   R2   RB   t	   directoryt   current_patht	   file_listt   fR<   t	   comp_file(    (   RT   s1   site-packages/pip/_internal/cli/autocompletion.pyR+   ˆ   s    
!(   t   __doc__R%   R   R   t	   itertoolsR    t   pip._internal.cli.main_parserR   t   pip._internal.commandsR   R   t   pip._internal.utils.miscR   t   pip._internal.utils.typingR   t   typingR   R   R   R	   RJ   R*   R+   (    (    (    s1   site-packages/pip/_internal/cli/autocompletion.pyt   <module>   s   %	_	