3
ÀÞL]¢$  ã               @   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G d	d
„ d
ejƒZG dd„ deƒZG dd„ dejƒZG d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               @   sR   e Zd ZdZdd„ Zdd„ Z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   s:   d|d< d|d< t ƒ d d |d< tjj| f|ž|Ž d S )Né   Úmax_help_positioné   Úindent_incrementr   é   Úwidth)r   ÚoptparseÚIndentedHelpFormatterÚ__init__)ÚselfÚargsÚkwargs© r   ú7/tmp/pip-build-69lrc5qx/pip/pip/_internal/cli/parser.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_stringsú <%s>ú, c             C   s|   g }|j r|j|j d ƒ |jr0|j|jd ƒ t|ƒdkrH|jd|ƒ |jƒ rr|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_heading7   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   sV   |rNt | jdƒrd}nd}|jdƒ}|jƒ }| jtj|ƒdƒ}d||f }|S dS d S )NÚmainÚCommandsÚDescriptionÚ
z  z%s:
%s
r   )ÚhasattrÚparserÚlstripÚrstripr.   r/   r0   )r   ÚdescriptionÚlabelr   r   r   Úformat_descriptionD   s    
z&PrettyHelpFormatter.format_descriptionc             C   s   |r|S dS d S )Nr   r   )r   Úepilogr   r   r   Úformat_epilogV   s    z!PrettyHelpFormatter.format_epilogc                s"   ‡ fdd„|j dƒD ƒ}dj|ƒS )Nc                s   g | ]}ˆ | ‘qS r   r   )Ú.0Úline)Úindentr   r   ú
<listcomp>^   s    z4PrettyHelpFormatter.indent_lines.<locals>.<listcomp>r7   )Úsplitr'   )r   ÚtextrC   Ú	new_linesr   )rC   r   r.   ]   s    z PrettyHelpFormatter.indent_linesN)r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r-   r3   r>   r@   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   s(   | j d k	r| j j| j jƒ tjj| |ƒS )N)r9   Ú_update_defaultsÚdefaultsr   r   Úexpand_default)r   r   r   r   r   rO   i   s    
z,UpdatingDefaultsHelpFormatter.expand_defaultN)rH   rI   rJ   rK   rO   r   r   r   r   rL   b   s   rL   c               @   s    e Zd Zdd„ Zedd„ ƒZdS )ÚCustomOptionParserc             O   s(   | 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_groupq   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_listrR   Úextend)r   ÚresÚir   r   r   Úoption_list_allz   s    z"CustomOptionParser.option_list_allN)rH   rI   rJ   rV   Úpropertyr[   r   r   r   r   rP   o   s   	rP   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   sD   |j dƒ| _|j ddƒ}t|ƒ| _| js,t‚tjj| f|ž|Ž d S )NÚnameÚisolatedF)rS   r^   r   ÚconfigÚAssertionErrorr   ÚOptionParserr   )r   r   r   r_   r   r   r   r   ˆ   s
    

zConfigOptionParser.__init__c             C   sL   y|j ||ƒS  tjk
rF } ztd| ƒ tjdƒ W Y d d }~X nX d S )Nz*An error occurred during configuration: %sé   )Úcheck_valuer   ÚOptionValueErrorÚprintÚ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 ƒ}xR| jjƒ D ]D\}}|s@tjd|ƒ q&|jddƒ\}}||kr&|| j||fƒ q&W x,|D ]$}x|| D ]\}}||fV  q‚W qtW d S )NÚglobalz:env:c             S   s   i | ]
}g |“qS r   r   )rA   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ÚdebugrE   r   )r   Zoverride_orderÚsection_itemsZsection_keyrj   Úsectionri   r   r   r   Ú _get_ordered_configuration_items˜   s    
z3ConfigOptionParser._get_ordered_configuration_itemsc                sR  t jˆjƒˆ_tƒ }�xˆjƒ D �]\‰ }ˆjdˆ  ƒ‰ˆdkrBq ˆjd
krˆyt|ƒ}W n, t	k
r„   t
ˆjˆ |ƒ}ˆj|ƒ Y nX n’ˆjdkr²|jƒ }‡ ‡‡fdd„|D ƒ}nhˆjd	k�r|jˆjƒ ˆjƒ }ˆj||ƒ}ˆjpæf }ˆjpði }ˆjˆ||ˆf|ž|Ž nˆjˆˆ |ƒ}||ˆj< q W x|D ]‰ tˆjˆ ƒ|ˆ < �q.W dˆ_|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).z--NÚ
store_trueÚstore_falseÚcountr   c                s   g | ]}ˆj ˆˆ |ƒ‘qS r   )rl   )rA   Úv)ri   r   r   r   r   rD   Î   s    z7ConfigOptionParser._update_defaults.<locals>.<listcomp>Úcallback)rv   rw   rx   )r   ÚValuesrN   ÚvaluesÚsetru   Ú
get_optionÚactionr   Ú
ValueErrorÚinvalid_config_error_messageÚerrorrE   Úaddr%   Úget_opt_stringÚconvert_valueÚcallback_argsÚcallback_kwargsrz   rl   Úgetattr)r   rN   Ú	late_evalrj   Z	error_msgÚopt_strr   r   r   )ri   r   r   r   rM   °   s:    




z#ConfigOptionParser._update_defaultsc             C   s°   | j stj| jƒS y| jjƒ  W n2 tk
rR } z| jtt	|ƒƒ W Y dd}~X nX | j
| jjƒ ƒ}x@| jƒ D ]4}|j|jƒ}t|tƒrn|jƒ }|j||ƒ||j< qnW tj|ƒS )z‹Overriding to make updating the defaults after instantiation of
        the option parser possible, _update_defaults() does the dirty work.N)Úprocess_default_valuesr   r{   rN   r`   Úloadr   rh   r   ÚstrrM   ÚcopyÚ_get_all_optionsÚgetr%   Ú
isinstancer   r„   rd   )r   ÚerrrN   r   ÚdefaultrŠ   r   r   r   Úget_default_valuesá   s    "
z%ConfigOptionParser.get_default_valuesc             C   s    | j tjƒ | jtd| ƒ d S )Nz%s
)Úprint_usagerg   Ústderrrh   r   )r   r2   r   r   r   r‚   ö   s    zConfigOptionParser.errorN)
rH   rI   rJ   rK   r   rl   ru   rM   r”   r‚   r   r   r   r   r]   „   s   	1r]   c             C   s    | dkrdj ||ƒS dj ||ƒS )zQReturns a better error message when invalid configuration option
    is provided.rv   rw   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.)rv   rw   )Úformat)r   ri   rj   r   r   r   r�   û   s
    r�   )rK   Ú
__future__r   Úloggingr   rg   r/   Údistutils.utilr   Zpip._vendor.sixr   Zpip._internal.cli.status_codesr   Zpip._internal.configurationr   r   Zpip._internal.utils.compatr   Ú	getLoggerrH   rq   r   r	   rL   rb   rP   r]   r�   r   r   r   r   Ú<module>   s    
Ow