3
zYðiá(  ã               @   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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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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               @   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   ú-/tmp/pip-build-ue6tjl93/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_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_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   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Úrstripr1   r2   r3   )r   ÚdescriptionÚlabelr   r   r   Úformat_descriptionH   s    
z&PrettyHelpFormatter.format_descriptionc             C   s   |r|S dS d 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S r   r   )Ú.0Úline)Úindentr   r   ú
<listcomp>b   s    z4PrettyHelpFormatter.indent_lines.<locals>.<listcomp>r:   )Úsplitr*   )r   ÚtextrF   Ú	new_linesr   )rF   r   r1   a   s    z PrettyHelpFormatter.indent_linesN)r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r0   r6   rA   rC   r1   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)r<   Ú_update_defaultsÚdefaultsr   r   Úexpand_default)r   r   r   r   r   rR   m   s    
z,UpdatingDefaultsHelpFormatter.expand_defaultN)rK   rL   rM   rN   rR   r   r   r   r   rO   f   s   rO   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_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_listrU   Úextend)r   ÚresÚir   r   r   Úoption_list_all~   s    z"CustomOptionParser.option_list_allN)rK   rL   rM   rY   Úpropertyr^   r   r   r   r   rS   s   s   	rS   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   sd   t jƒ | _|jdƒ| _|jddƒ| _| jƒ | _| jrB| jj| jƒ | jsLt	‚t
jj| f|ž|Ž d S )NÚnameÚisolatedF)r   ÚRawConfigParserÚconfigrV   ra   rb   Ú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 S ttƒ}| jsj|rFt jj|ƒrF|j	|ƒ n$|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	   rb   ÚpathÚexistsr"   r   r*   r
   Úuser_config_dirr   r   ÚsysÚprefix)r   Úconfig_filerf   Úvenv_config_filer   r   r   re   ˜   s&    


z#ConfigOptionParser.get_config_filesc             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Úprintrt   Úexit)r   r   ÚkeyÚvalÚexcr   r   r   Úcheck_defaultÀ   s
    z ConfigOptionParser.check_defaultc       	         sj  i }x(dˆj fD ]}|jˆjˆj|ƒƒƒ qW ˆjsH|jˆjˆjƒ ƒƒ tjˆjƒˆ_	t
ƒ }xâ|jƒ D ]Ö\‰ }|stqfˆjˆ ƒ‰ˆdkrˆqfˆjd
krœt|ƒ}n–ˆjdkrÆ|jƒ }‡ ‡‡fdd„|D ƒ}nlˆjd	k�r$|jˆjƒ ˆjƒ }ˆj||ƒ}ˆj� püf }ˆj�pi }ˆjˆ||ˆf|ž|Ž nˆjˆˆ |ƒ}||ˆj< qfW x|D ]‰ tˆj	ˆ ƒ|ˆ < �qFW 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).ÚglobalNÚ
store_trueÚstore_falseÚcountr"   c                s   g | ]}ˆj ˆˆ |ƒ‘qS r   )r€   )rD   Úv)r}   r   r   r   r   rG   é   s    z7ConfigOptionParser._update_defaults.<locals>.<listcomp>Úcallback)r‚   rƒ   r„   )ra   ÚupdateÚnormalize_keysÚget_config_sectionrb   Úget_environ_varsr   ÚValuesrQ   ÚvaluesÚsetÚitemsÚ
get_optionÚactionr   rH   Úaddr(   Úget_opt_stringÚconvert_valueÚcallback_argsÚcallback_kwargsr†   r€   Úgetattr)	r   rQ   rd   ÚsectionÚ	late_evalr~   Úopt_strr   r   r   )r}   r   r   r   rP   Ç   s@    




z#ConfigOptionParser._update_defaultsc             C   s@   i }x6|D ].\}}|j ddƒ}|jdƒs0d| }|||< 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|ƒS g S )z Get a section of a configuration)rd   Úhas_sectionrŽ   )r   ra   r   r   r   r‰     s    z%ConfigOptionParser.get_config_sectionc             c   s<   x6t jjƒ D ](\}}tj|ƒrtjd|ƒjƒ |fV  qW dS )z@Returns a generator with all environmental vars with prefix PIP_r    N)rl   rm   rŽ   Ú_environ_prefix_reÚsearchÚsubr)   )r   r}   r~   r   r   r   rŠ     s    
z#ConfigOptionParser.get_environ_varsc             C   sn   | j stj| jƒS | j| jjƒ ƒ}x@| jƒ D ]4}|j|jƒ}t	|t
ƒr,|jƒ }|j||ƒ||j< q,W t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‹   rQ   rP   ÚcopyÚ_get_all_optionsrn   r(   Ú
isinstancer   r’   ry   )r   rQ   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_usagert   Ústderrr|   )r   r5   r   r   r   Úerror#  s    zConfigOptionParser.errorN)rK   rL   rM   rN   rb   r   re   r€   rP   rˆ   r‰   rŠ   r¨   r«   r   r   r   r   r`   ˆ   s   
(5r`   )rN   Ú
__future__r   rt   r   rl   Úrer2   Údistutils.utilr   Zpip._vendor.sixr   Zpip._vendor.six.movesr   Úpip.locationsr   r   r   r	   Ú	pip.utilsr
   r   ÚcompileÚIr    r   r   rO   ri   rS   r`   r   r   r   r   Ú<module>   s    O