
§|já(  ã               @   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-5c7ija6t/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)ÚUpdatingDefaultsHelpFormatterz®Custom 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)z¢Updates 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)z™Return 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)z‹Overriding 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