U
    KQh(                     @   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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-tlro1kwj/pip/pip/baseparser.pyr      s    zPrettyHelpFormatter.__init__c                 C   s   |  |ddS )N <%s>, )_format_option_stringsr   optionr   r   r   format_option_strings!   s    z)PrettyHelpFormatter.format_option_stringsr   r   c                 C   s|   g }|j r||j d  |jr0||jd  t|dkrH|d| | rr|jp^|j }|||   d	|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|  t|d }|S )zz
        Ensure there is only one newline between usage and the first heading
        if there is no description.
        z
Usage: %s
  )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}|d}| }| t|d}d||f }|S dS d S )NmainCommandsDescription
r3   z%s:
%s
r"   )hasattrparserlstriprstripr4   r5   r6   )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| dD }d|S )Nc                    s   g | ]} | qS r   r   ).0lineindentr   r   
<listcomp>b   s     z4PrettyHelpFormatter.indent_lines.<locals>.<listcomp>r=   )splitr,   )r   textrJ   	new_linesr   rI   r   r4   a   s    z PrettyHelpFormatter.indent_linesN)r   r   )__name__
__module____qualname____doc__r   r!   r   r2   r9   rD   rF   r4   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 tj| |S )N)r?   _update_defaultsdefaultsr   r   expand_defaultr   r   r   r   rV   m   s    
z,UpdatingDefaultsHelpFormatter.expand_defaultN)rO   rP   rQ   rR   rV   r   r   r   r   rS   f   s   rS   c                   @   s    e Zd Zdd Zedd ZdS )CustomOptionParserc                 O   s(   | 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 }| jD ]}||j  q|S )z<Get a list of all options, including those in option groups.N)option_listrY   extend)r   resir   r   r   option_list_all~   s    
z"CustomOptionParser.option_list_allN)rO   rP   rQ   r]   propertyrb   r   r   r   r   rW   s   s   	rW   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  | _|d| _|dd| _|  | _| jrB| j| j | jsLt	t
jj| f|| d S )NnameisolatedF)r   RawConfigParserconfigrZ   re   rf   get_config_filesfilesreadAssertionErrorr   OptionParserr   r   r   r   r   r      s    


zConfigOptionParser.__init__c                 C   s   t jdd}|t jkrg S tt}| jsj|rFt j|rF|	| n$|	t
 |	t jtdt t rt jtjt}t j|r|	| |S )NPIP_CONFIG_FILEFpip)osenvirongetdevnulllistr	   rf   pathexistsr$   r   r,   r
   user_config_dirr   r   sysprefix)r   config_filerj   venv_config_filer   r   r   ri      s,    


z#ConfigOptionParser.get_config_filesc              
   C   sN   z| ||W S  tjk
rH } ztd|  td W 5 d }~X Y nX d S )Nz*An error occurred during configuration: %s   )check_valuer   OptionValueErrorprintrx   exit)r   r    keyvalexcr   r   r   check_default   s
    z ConfigOptionParser.check_defaultc           	         sZ  i }dj fD ]}|| qjsD|  tj_	t
 }| D ]\ }|snq` dkrq`jdkrt|}njdkr| } fdd|D }nhjdkr|j  }||}jpd}jpi }j||f|| n |}||j< q`|D ] tj	 | < q8d_	|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 | ]}  |qS r   )r   )rG   vr   r    r   r   r   rK      s     z7ConfigOptionParser._update_defaults.<locals>.<listcomp>callbackr   )re   updatenormalize_keysget_config_sectionrf   get_environ_varsr   ValuesrU   valuessetitems
get_optionactionr   rL   addr*   get_opt_stringconvert_valuecallback_argscallback_kwargsr   r   getattr)	r   rU   rh   section	late_evalr   opt_strr   r   r   r   r   rT      sB    





z#ConfigOptionParser._update_defaultsc                 C   s<   i }|D ].\}}| dd}|ds.d| }|||< q|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 |r| j |S g S )z Get a section of a configuration)rh   has_sectionr   )r   re   r   r   r   r     s    z%ConfigOptionParser.get_config_sectionc                 c   s8   t j D ](\}}t|r
td| |fV  q
dS )z@Returns a generator with all environmental vars with prefix PIP_r"   N)rp   rq   r   _environ_prefix_researchsubr+   )r   r   r   r   r   r   r     s    
z#ConfigOptionParser.get_environ_varsc                 C   sj   | j st| jS | | j }|  D ]4}||j}t	|t
r*| }|||||j< q*t|S )zOverriding to make updating the defaults after instantiation of
        the option parser possible, _update_defaults() does the dirty work.)process_default_valuesr   r   rU   rT   copy_get_all_optionsrr   r*   
isinstancer   r   r}   )r   rU   r    defaultr   r   r   r   get_default_values  s    
z%ConfigOptionParser.get_default_valuesc                 C   s    |  tj | dd|  d S )Nr   z%s
)print_usagerx   stderrr   )r   r8   r   r   r   error#  s    zConfigOptionParser.errorN)rO   rP   rQ   rR   rf   r   ri   r   rT   r   r   r   r   r   r   r   r   r   rd      s   
(5rd   )rR   
__future__r   rx   r   rp   rer5   distutils.utilr   Zpip._vendor.sixr   Zpip._vendor.six.movesr   pip.locationsr   r   r   r	   	pip.utilsr
   r   compileIr   r   r   rS   rm   rW   rd   r   r   r   r   <module>   s    O