
[Y                 @   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 d d l 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_prog)pkg_resourcesc               @   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   :/home/kubikplay/www/django_dtb/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_heading3   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_usage8   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_epilogR   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>Z   s   	 z4PrettyHelpFormatter.indent_lines.<locals>.<listcomp>r5   )splitr%   )r   textrA   	new_linesr   )rA   r   r,   Y   s    "z PrettyHelpFormatter.indent_linesN)__name__
__module____qualname____doc__r   r   r   r+   r1   r<   r>   r,   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 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)r7   update_defaultsdefaultsr   r   expand_default)r   r   r   r   r   rM   c   s    z,UpdatingDefaultsHelpFormatter.expand_defaultN)rF   rG   rH   rI   rM   r   r   r   r   rJ   ^   s   rJ   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_groupj   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_listrP   extend)r   resir   r   r   option_list_alls   s    z"CustomOptionParser.option_list_allN)rF   rG   rH   rT   propertyrY   r   r   r   r   rN   i   s   	rN   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configrQ   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)
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 Sr   )rv   )r?   v)rs   r   r   r   r   rB      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   rC   rv   r#   )r   rL   r^   sectionrt   r   )rs   r   r   r   rK      s"    #"z"ConfigOptionParser.update_defaultsc             C   sY   i  } xL | D]D \ } } | j  d d  } | j d  sG d | } n  | | | <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   
normalizedrs   rt   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)re   rf   r   r   r   r$   )r   prefixrs   rt   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)zOverridding to make updating the defaults after instantiation of
        the option parser possible, update_defaults() does the dirty work.)process_default_valuesr   ValuesrL   rK   copy_get_all_optionsrg   r#   
isinstancer   get_opt_stringrm   )r   rL   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_usagero   stderrrr   )r   r0   r   r   r   error   s    zConfigOptionParser.errorN)rF   rG   rH   rI   r   r_   rv   rK   r}   r~   r   r   r   r   r   r   r   r[   }   s   	r[   )rI   ro   r   re   r-   distutils.utilr   Zpip.backwardcompatr   r   pip.locationsr   pip.utilr   r   pip._vendorr   r   r	   rJ   rc   rN   r[   r   r   r   r   <module>   s   O