3
e(                 @   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-argcdawy/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 )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   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 rt 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rqfjd
krt|}njdkr|j } fdd|D }nljd	kr$|jj j }j||}j pf }jpi }j||f|| nj |}||j< qfW x|D ] tj	 | < qFW d_	|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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 )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|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 )zOverriding 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