
0c@_%                 @   s  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 m Z d d l	 m
 Z
 d d l m Z d d l m Z m Z d d l m Z e j e  Z Gd	 d
   d
 e j  Z Gd d   d e  Z Gd d   d e j  Z Gd d   d e  Z d d   Z d S)zBase option parser setup    )absolute_importN)	strtobool)string_types)UNKNOWN_ERROR)ConfigurationConfigurationError)get_terminal_sizec               @   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   7/tmp/pip-build-jynh7p1z/pip/pip/_internal/cli/parser.pyr      s    

zPrettyHelpFormatter.__init__c             C   s   |  j  |  S)N)_format_option_strings)r   optionr   r   r   format_option_strings!   s    z)PrettyHelpFormatter.format_option_stringsz <{}>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	 | j     d j
 |  S)z
        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
        :param optsep:  separator
        r   r    )_short_optsappend
_long_optsleninserttakes_valuemetavardestlowerformat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  |  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: {}
z  )r%   indent_linestextwrapdedent)r   usagemsgr   r   r   format_usage@   s    z PrettyHelpFormatter.format_usagec             C   s{   | rs t  |  j d  r! d } n d } | j d  } | j   } |  j t j |  d  } d j | |  } | Sd Sd  S)NmainCommandsDescription
z  z{}:
{}
r   )hasattrparserlstriprstripr-   r.   r/   r%   )r   descriptionlabelr   r   r   format_descriptionI   s    	z&PrettyHelpFormatter.format_descriptionc             C   s   | r
 | Sd Sd  S)Nr   r   )r   epilogr   r   r   format_epilog[   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>c   s   	 z4PrettyHelpFormatter.indent_lines.<locals>.<listcomp>r6   )splitr&   )r   textrB   	new_linesr   )rB   r   r-   b   s    "z PrettyHelpFormatter.indent_linesN)__name__
__module____qualname____doc__r   r   r   r,   r2   r=   r?   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)r8   _update_defaultsdefaultsr   r   expand_default)r   r   r   r   r   rN   n   s    z,UpdatingDefaultsHelpFormatter.expand_defaultN)rG   rH   rI   rJ   rN   r   r   r   r   rK   g   s   rK   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_groupv   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_listrQ   extend)r   resir   r   r   option_list_all   s    z"CustomOptionParser.option_list_allN)rG   rH   rI   rU   propertyrZ   r   r   r   r   rO   t   s   	rO   c               @   s^   e  Z d  Z 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 variablesc             O   s\   | j  d  |  _ | j  d d  } t |  |  _ |  j sB t  t j j |  | |  d  S)NnameisolatedF)rR   r]   r   configAssertionErrorr   OptionParserr   )r   r   r   r^   r   r   r   r      s
    zConfigOptionParser.__init__c             C   sc   y | j  | |  SWnH t j k
 r^ } z% t d j |   t j d  WYd  d  } ~ Xn Xd  S)Nz*An error occurred during configuration: {}   )check_valuer   OptionValueErrorprintr%   sysexit)r   r   keyvalexcr   r   r   check_default   s
    z ConfigOptionParser.check_defaultc             c   s   d |  j  d g } d d   | D } xq |  j j   D]` \ } } | sZ t j d |  q5 | j d d  \ } } | | k r5 | | j | | f  q5 Wx4 | D], } x# | | D] \ } } | | f Vq Wq Wd  S)Nglobalz:env:c             S   s   i  |  ] } g  |  q Sr   r   )r@   r]   r   r   r   
<dictcomp>   s   	 zGConfigOptionParser._get_ordered_configuration_items.<locals>.<dictcomp>z7Ignoring configuration key '%s' as it's value is empty..r   )r]   r_   itemsloggerdebugrD   r   )r   Zoverride_ordersection_itemsZsection_keyri   sectionrh   r   r   r    _get_ordered_configuration_items   s    z3ConfigOptionParser._get_ordered_configuration_itemsc                s  t  j  j   _ t   } xn j   D]`\   }  j d      d k rY q+  j d
 k r y t |  } Wq~t	 k
 r t
  j   |  }  j |  Yq~Xn  j d k r | j   }     f d d   | D } n  j d	 k ri| j  j   j   }  j | |  }  j p8f  }  j pG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).z--N
store_truestore_falsecountr   c                s%   g  |  ] }  j     |   q Sr   )rk   )r@   v)rh   r   r   r   r   rC      s   	 z7ConfigOptionParser._update_defaults.<locals>.<listcomp>callback)z
store_truezstore_falsezcount)r   ValuesrM   valuessetrt   
get_optionactionr   
ValueErrorinvalid_config_error_messageerrorrD   addr#   get_opt_stringconvert_valuecallback_argscallback_kwargsry   rk   getattr)r   rM   	late_evalri   Z	error_msgopt_strr   r   r   )rh   r   r   r   rL      s:    	"	z#ConfigOptionParser._update_defaultsc             C   s   |  j  s t j |  j  Sy |  j j   Wn; t k
 rg } z |  j t t	 |   WYd d } ~ Xn X|  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.N)process_default_valuesr   rz   rM   r_   loadr   rg   r   strrL   copy_get_all_optionsgetr#   
isinstancer   r   rc   )r   errrM   r   defaultr   r   r   r   get_default_values   s    	)z%ConfigOptionParser.get_default_valuesc             C   s-   |  j  t j  |  j t d j |   d  S)Nz{}
)print_usagerf   stderrrg   r   r%   )r   r1   r   r   r   r      s    zConfigOptionParser.errorN)
rG   rH   rI   rJ   r   rk   rt   rL   r   r   r   r   r   r   r\      s   	1r\   c             C   s,   |  d k r d j  | |  Sd j  | |  S)zQReturns a better error message when invalid configuration option
    is provided.ru   rv   zo{0} is not a valid value for {1} option, please specify a boolean value like yes/no, true/false or 1/0 instead.z[{0} is not a valid value for {1} option, please specify a numerical value like 1/0 instead.)z
store_truezstore_false)r%   )r~   rh   ri   r   r   r   r      s
    
r   )rJ   
__future__r   loggingr   rf   r.   distutils.utilr   Zpip._vendor.sixr   Zpip._internal.cli.status_codesr   Zpip._internal.configurationr   r   Zpip._internal.utils.compatr   	getLoggerrG   rp   r   r	   rK   ra   rO   r\   r   r   r   r   r   <module>   s    Pw