
}_(                 @   s1  d  Z  d d l m Z d d l Z d d l 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 d d l m Z m Z m Z m Z d d l m Z m Z e j d	 e j  Z Gd
 d   d e j  Z Gd d   d e  Z Gd d   d e j  Z Gd d   d e  Z d S)zBase option parser setup    )absolute_importN)	strtobool)string_types)configparser)legacy_config_fileconfig_basenamerunning_under_virtualenvsite_config_files)appdirsget_terminal_sizez^PIP_c               @   s|   e  Z d  Z d Z d d   Z d d   Z d d d d	  Z d
 d   Z d d   Z d d   Z	 d d   Z
 d d   Z d S)PrettyHelpFormatterz4A prettier/less verbose help formatter for optparse.c             O   sC   d | d <d | d <t    d d | d <t j j |  | |  d  S)N   max_help_position   indent_incrementr      width)r   optparseIndentedHelpFormatter__init__)selfargskwargs r   -/tmp/pip-build-3_sjtvrs/pip/pip/baseparser.pyr      s    

zPrettyHelpFormatter.__init__c             C   s   |  j  | d d  S)Nz <%s>z, )_format_option_strings)r   optionr   r   r   format_option_strings!   s    z)PrettyHelpFormatter.format_option_stringsz <%s>z, c             C   s   g  } | j  r# | j | j  d  | j r@ | j | j d  t |  d k rb | j d |  | j   r | j p | j j   } | j | | j    d j	 |  S)a  
        Return a comma-separated list of option strings and metavars.

        :param option:  tuple of (short opt, long opt), e.g: ('-f', '--format')
        :param mvarfmt: metavar format string - evaluated as mvarfmt % metavar
        :param optsep:  separator
        r   r    )
_short_optsappend
_long_optsleninserttakes_valuemetavardestlowerjoin)r   r   mvarfmtoptsepoptsr%   r   r   r   r   $   s    		z*PrettyHelpFormatter._format_option_stringsc             C   s   | d k r d S| d S)NOptionsr   z:
r   )r   headingr   r   r   format_heading;   s    z"PrettyHelpFormatter.format_headingc             C   s#   d |  j  t j |  d  } | S)zz
        Ensure there is only one newline between usage and the first heading
        if there is no description.
        z
Usage: %s
z  )indent_linestextwrapdedent)r   usagemsgr   r   r   format_usage@   s    z PrettyHelpFormatter.format_usagec             C   sy   | rq t  |  j d  r! d } n d } | j d  } | j   } |  j t j |  d  } d | | f } | Sd Sd  S)NmainCommandsDescription
z  z%s:
%s
r   )hasattrparserlstriprstripr/   r0   r1   )r   descriptionlabelr   r   r   format_descriptionH   s    	z&PrettyHelpFormatter.format_descriptionc             C   s   | r
 | Sd Sd  S)Nr   r   )r   epilogr   r   r   format_epilogZ   s    z!PrettyHelpFormatter.format_epilogc                s/     f d d   | j  d  D } d j |  S)Nc                s   g  |  ] }   |  q Sr   r   ).0line)indentr   r   
<listcomp>b   s   	 z4PrettyHelpFormatter.indent_lines.<locals>.<listcomp>r8   )splitr(   )r   textrD   	new_linesr   )rD   r   r/   a   s    "z PrettyHelpFormatter.indent_linesN)__name__
__module____qualname____doc__r   r   r   r.   r4   r?   rA   r/   r   r   r   r   r      s   r   c               @   s"   e  Z d  Z d Z d d   Z d S)UpdatingDefaultsHelpFormatterzCustom help formatter for use in ConfigOptionParser.

    This is updates the defaults before expanding them, allowing
    them to show up correctly in the help listing.
    c             C   s8   |  j  d  k	 r% |  j  j |  j  j  t j j |  |  S)N)r:   _update_defaultsdefaultsr   r   expand_default)r   r   r   r   r   rP   m   s    z,UpdatingDefaultsHelpFormatter.expand_defaultN)rI   rJ   rK   rL   rP   r   r   r   r   rM   f   s   rM   c               @   s.   e  Z d  Z d d   Z e d d    Z d S)CustomOptionParserc             O   s6   |  j  | |   } |  j j   |  j j | |  | S)z*Insert an OptionGroup at a given position.)add_option_groupoption_groupspopr#   )r   idxr   r   groupr   r   r   insert_option_groupu   s    z&CustomOptionParser.insert_option_groupc             C   s;   |  j  d d  } x! |  j D] } | j | j   q W| S)z<Get a list of all options, including those in option groups.N)option_listrS   extend)r   resir   r   r   option_list_all~   s    z"CustomOptionParser.option_list_allN)rI   rJ   rK   rW   propertyr\   r   r   r   r   rQ   s   s   	rQ   c               @   s   e  Z d  Z d Z d Z d d   Z d d   Z d d   Z d	 d
   Z d d   Z	 d d   Z
 d d   Z d d   Z d d   Z d S)ConfigOptionParserzsCustom option parser which updates its defaults by checking the
    configuration files and environmental variablesFc             O   s   t  j   |  _ | j d  |  _ | j d d  |  _ |  j   |  _ |  j ra |  j j |  j  |  j sp t	  t
 j j |  | |  d  S)NnameisolatedF)r   RawConfigParserconfigrT   r_   r`   get_config_filesfilesreadAssertionErrorr   OptionParserr   )r   r   r   r   r   r   r      s    	zConfigOptionParser.__init__c             C   s   t  j j d d  } | t  j k r( g  St t  } |  j s | re t  j j |  re | j	 |  n2 | j	 t
  | j	 t  j j t j d  t   t   r t  j j t j t  } t  j j |  r | j	 |  | S)NPIP_CONFIG_FILEFpip)osenvirongetdevnulllistr	   r`   pathexistsr    r   r(   r
   user_config_dirr   r   sysprefix)r   config_filerd   venv_config_filer   r   r   rc      s&    		
			z#ConfigOptionParser.get_config_filesc             C   s^   y | j  | |  SWnC t j k
 rY } z  t d |  t j d  WYd  d  } ~ Xn Xd  S)Nz*An error occurred during configuration: %s   )check_valuer   OptionValueErrorprintrr   exit)r   r   keyvalexcr   r   r   check_default   s
    z ConfigOptionParser.check_defaultc       	         s  i  } x6 d  j  f D]% } | j  j  j |    q W j sd | j  j  j     t j  j   _	 t
   } x8| j   D]*\   } | s q  j      d k r q  j d
 k r t |  } n  j d k r| j   }     f d d   | D } n  j d	 k r| j  j   j   }  j | |  }  j pff  }  j pui  }  j  | |  | |  n  j    |  } | |  j <q Wx$ | D]   t  j	    |   <qWd  _	 | S)zUpdates the given defaults with values from the config files and
        the environ. Does a little special handling for certain types of
        options (lists).globalN
store_truestore_falsecountr    c                s%   g  |  ] }  j     |   q Sr   )r~   )rB   v)r{   r   r   r   r   rE      s   	 z7ConfigOptionParser._update_defaults.<locals>.<listcomp>callback)r   r   r   )r_   updatenormalize_keysget_config_sectionr`   get_environ_varsr   ValuesrO   valuessetitems
get_optionactionr   rF   addr&   get_opt_stringconvert_valuecallback_argscallback_kwargsr   r~   getattr)	r   rO   rb   section	late_evalr|   opt_strr   r   r   )r{   r   r   r   rN      s@    		"	z#ConfigOptionParser._update_defaultsc             C   sV   i  } xI | D]A \ } } | j  d d  } | j d  sD d | } | | | <q W| S)zReturn a config dictionary with normalized keys regardless of
        whether the keys were specified in environment variables or in config
        files_-z--z--%s)replace
startswith)r   r   
normalizedr{   r|   r   r   r   r      s    
z!ConfigOptionParser.normalize_keysc             C   s&   |  j  j |  r" |  j  j |  Sg  S)z Get a section of a configuration)rb   has_sectionr   )r   r_   r   r   r   r     s    z%ConfigOptionParser.get_config_sectionc             c   sP   xI t  j j   D]8 \ } } t j |  r t j d |  j   | f Vq Wd S)z@Returns a generator with all environmental vars with prefix PIP_r   N)rj   rk   r   _environ_prefix_researchsubr'   )r   r{   r|   r   r   r   r     s    z#ConfigOptionParser.get_environ_varsc             C   s   |  j  s t j |  j  S|  j |  j j    } xZ |  j   D]L } | j | j  } t	 | t
  r> | j   } | j | |  | | j <q> Wt j |  S)zOverriding to make updating the defaults after instantiation of
        the option parser possible, _update_defaults() does the dirty work.)process_default_valuesr   r   rO   rN   copy_get_all_optionsrl   r&   
isinstancer   r   rw   )r   rO   r   defaultr   r   r   r   get_default_values  s    	z%ConfigOptionParser.get_default_valuesc             C   s(   |  j  t j  |  j d d |  d  S)Nr   z%s
)print_usagerr   stderrrz   )r   r3   r   r   r   error#  s    zConfigOptionParser.errorN)rI   rJ   rK   rL   r`   r   rc   r~   rN   r   r   r   r   r   r   r   r   r   r^      s   
(5r^   )rL   
__future__r   rr   r   rj   rer0   distutils.utilr   Zpip._vendor.sixr   Zpip._vendor.six.movesr   pip.locationsr   r   r   r	   	pip.utilsr
   r   compileIr   r   r   rM   rg   rQ   r^   r   r   r   r   <module>   s    "O