î
É X¼  ã               @   sä   d  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 m	 Z	 d d l
 m Z d d l m Z m Z Gd d „  d e j ƒ Z Gd	 d
 „  d
 e ƒ Z Gd d „  d e j ƒ Z Gd d „  d e ƒ Z d S)zBase option parser setupé    N)Ú	strtobool)ÚConfigParserÚstring_types)Údefault_config_file)Úget_terminal_sizeÚget_progc               @   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   ú9/home/ubuntu/projects/ifolica/build/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_stringsz <%s>z, c             C   s¶   g  } | j  r& | j | j  d ƒ n  | j rF | j | j d ƒ n  t | ƒ d k rk | j d | ƒ n  | j ƒ  r© | j pŒ | j j ƒ  } | j | | j ƒ  ƒ n  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_heading2   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_usage7   s    z PrettyHelpFormatter.format_usagec             C   sy   | rq t  |  j d ƒ r! d } n d } | j d ƒ } | j ƒ  } |  j t j | ƒ d ƒ } d | | f } | Sd Sd  S)NÚmainÚCommandsÚDescriptionÚ
z  z%s:
%s
r   )ÚhasattrÚparserÚlstripÚrstripr+   r,   r-   )r   ÚdescriptionÚlabelr   r   r   Úformat_description?   s    	z&PrettyHelpFormatter.format_descriptionc             C   s   | r
 | Sd Sd  S)Nr   r   )r   Úepilogr   r   r   Úformat_epilogQ   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>Y   s   	 z4PrettyHelpFormatter.indent_lines.<locals>.<listcomp>r4   )Úsplitr$   )r   Útextr@   Ú	new_linesr   )r@   r   r+   X   s    "z PrettyHelpFormatter.indent_linesN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r*   r0   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 that updates
    the defaults before expanding them, allowing them to show up correctly
    in the help listingc             C   s;   |  j  d  k	 r( |  j  j |  j  j ƒ n  t j j |  | ƒ S)N)r6   Úupdate_defaultsÚdefaultsr   r   Úexpand_default)r   r   r   r   r   rL   b   s    z,UpdatingDefaultsHelpFormatter.expand_defaultN)rE   rF   rG   rH   rL   r   r   r   r   rI   ]   s   rI   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_groupi   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_listrO   Úextend)r   ÚresÚir   r   r   Úoption_list_allr   s    z"CustomOptionParser.option_list_allN)rE   rF   rG   rS   ÚpropertyrX   r   r   r   r   rM   h   s   	rM   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 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   sx   t  j ƒ  |  _ | j d ƒ |  _ |  j ƒ  |  _ |  j rO |  j j |  j ƒ n  |  j s^ t ‚ t	 j
 j |  | | Ž d  S)NÚname)r   ÚRawConfigParserÚconfigrP   r[   Úget_config_filesÚfilesÚreadÚAssertionErrorr   ÚOptionParserr   )r   r   r   r   r   r   r   €   s    	zConfigOptionParser.__init__c             C   sN   t  j j d d ƒ } | t  j k r( g  S| rG t  j j | ƒ rG | g St g S)NÚPIP_CONFIG_FILEF)ÚosÚenvironÚgetÚdevnullÚpathÚexistsr   )r   Úconfig_filer   r   r   r^   ‰   s    z#ConfigOptionParser.get_config_filesc             C   s[   y | j  | | ƒ SWn@ t j k
 rV t j ƒ  d } t d | ƒ t j d ƒ Yn Xd  S)Nr   z*An error occurred during configuration: %sé   )Úcheck_valuer   ÚOptionValueErrorÚsysÚexc_infoÚprintÚexit)r   r   ÚkeyÚvalÚer   r   r   Úcheck_default‘   s    z ConfigOptionParser.check_defaultc                s#  i  } x6 d ˆ j  f D]% } | j ˆ j ˆ j | ƒ ƒ ƒ q W| j ˆ j ˆ j ƒ  ƒ ƒ xÁ | j ƒ  D]³ \ ‰  } ˆ j ˆ  ƒ ‰ ˆ d k	 rh | s› qh n  ˆ j d	 k r¹ t | ƒ } n  ˆ j d k rö | j	 ƒ  } ‡  ‡ ‡ f d d †  | Dƒ } n ˆ j
 ˆ ˆ  | ƒ } | | ˆ j <qh qh W| 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 Sr   )ru   )r>   Úv)rr   r   r   r   r   rA   ¯   s   	 z6ConfigOptionParser.update_defaults.<locals>.<listcomp>)z
store_truezstore_falsezcount)r[   ÚupdateÚnormalize_keysÚget_config_sectionÚget_environ_varsÚitemsÚ
get_optionÚactionr   rB   ru   r"   )r   rK   r]   Úsectionrs   r   )rr   r   r   r   rJ   ™   s"    #"z"ConfigOptionParser.update_defaultsc             C   sY   i  } xL | D]D \ } } | j  d d ƒ } | j d ƒ sG d | } n  | | | <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   Ú
normalizedrr   rs   r   r   r   r|   ¶   s    z!ConfigOptionParser.normalize_keysc             C   s&   |  j  j | ƒ r" |  j  j | ƒ Sg  S)z Get a section of a configuration)r]   Úhas_sectionr   )r   r[   r   r   r   r}   Â   s    z%ConfigOptionParser.get_config_sectionÚPIP_c             c   sS   xL t  j j ƒ  D]; \ } } | j | ƒ r | j | d ƒ j ƒ  | f Vq q Wd S)z@Returns a generator with all environmental vars with prefix PIP_r   N)rd   re   r   r†   r…   r#   )r   Úprefixrr   rs   r   r   r   r~   È   s    z#ConfigOptionParser.get_environ_varsc             C   sž   |  j  s t j |  j ƒ S|  j |  j j ƒ  ƒ } x] |  j ƒ  D]O } | j | j ƒ } t	 | t
 ƒ r> | j ƒ  } | j | | ƒ | | j <q> q> Wt j | ƒ S)z‹Overridding to make updating the defaults after instantiation of
        the option parser possible, update_defaults() does the dirty work.)Úprocess_default_valuesr   ÚValuesrK   rJ   ÚcopyÚ_get_all_optionsrf   r"   Ú
isinstancer   Úget_opt_stringrl   )r   rK   r   ÚdefaultÚopt_strr   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_usagern   Ústderrrq   )r   r/   r   r   r   ÚerrorÝ   s    zConfigOptionParser.errorN)rE   rF   rG   rH   r   r^   ru   rJ   r|   r}   r~   r“   r–   r   r   r   r   rZ   |   s   	rZ   )rH   rn   r   rd   r,   Údistutils.utilr   Zpip.backwardcompatr   r   Úpip.locationsr   Úpip.utilr   r   r   r   rI   rb   rM   rZ   r   r   r   r   Ú<module>   s   O