ó
b]c           @   sz  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 m Z d d l m Z e rý d d	 l m Z m Z m Z m Z m Z m Z m Z e j Z e d
 e ƒ Z n  e j e  ƒ Z! d „  Z" d „  Z# e d d d d d d d d d d ƒ Z$ e rQd n d Z% d „  Z& d e' f d „  ƒ  YZ( d S(   s  Configuration management setup

Some terminology:
- name
  As written in config files.
- value
  Value associated with a name
- key
  Name combined with it's section (section.name)
- variant
  A single word describing where the configuration key-value pair came from
iÿÿÿÿN(   t   configparser(   t   ConfigurationErrort!   ConfigurationFileCouldNotBeLoaded(   t   appdirs(   t   WINDOWSt
   expanduser(   t
   ensure_dirt   enum(   t   MYPY_CHECK_RUNNING(   t   Anyt   Dictt   Iterablet   Listt   NewTypet   Optionalt   Tuplet   Kindc         C   s8   |  j  ƒ  j d d ƒ }  |  j d ƒ r4 |  d }  n  |  S(   sF   Make a name consistent regardless of source (environment or file)
    t   _t   -s   --i   (   t   lowert   replacet
   startswith(   t   name(    (    s:   lib/python2.7/site-packages/pip/_internal/configuration.pyt   _normalize_name)   s    c         C   s:   d |  k r* d j  |  ƒ } t | ƒ ‚ n  |  j d d ƒ S(   Nt   .sb   Key does not contain dot separated section and key. Perhaps you wanted to use 'global.{}' instead?i   (   t   formatR   t   split(   R   t   error_message(    (    s:   lib/python2.7/site-packages/pip/_internal/configuration.pyt   _disassemble_key3   s
    	t   USERt   usert   GLOBALt   globalt   SITEt   sitet   ENVt   envt   ENV_VARs   env-vars   pip.inis   pip.confc          C   s¼   g  t  j d ƒ D] }  t j j |  t ƒ ^ q } t j j t j t ƒ } t j j t d ƒ t	 rg d n d t ƒ } t j j t  j
 d ƒ t ƒ } i | t j 6| g t j 6| | g t j 6S(   Nt   pipt   ~s   .pip(   R   t   site_config_dirst   ost   patht   joint   CONFIG_BASENAMEt   syst   prefixR   R   t   user_config_dirt   kindsR   R!   R   (   R*   t   global_config_filest   site_config_filet   legacy_config_filet   new_config_file(    (    s:   lib/python2.7/site-packages/pip/_internal/configuration.pyt   get_configuration_filesK   s    .				
t   Configurationc           B   sÂ   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d	 „  Z e d
 „  ƒ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   sâ  Handles management of configuration.

    Provides an interface to accessing and managing configuration files.

    This class converts provides an API that takes "section.key-name" style
    keys and stores the value associated with it as "key-name" under the
    section "section".

    This allows for a clean interface wherein the both the section and the
    key-name are preserved in an easy to manage form in the configuration files
    and the data stored is also nice.
    c         C   sé   t  t |  ƒ j ƒ  t j t j t j d  g } | | k rh t d j	 d j
 t t | d  ƒ ƒ ƒ ƒ ‚ n  | |  _ | |  _ t j t j t j t j t j g |  _ d d g |  _ d „  |  j Dƒ |  _ d „  |  j Dƒ |  _ g  |  _ d  S(   Ns5   Got invalid value for load_only - should be one of {}s   , iÿÿÿÿt   versiont   helpc         S   s   i  |  ] } g  | “ q S(    (    (   t   .0t   variant(    (    s:   lib/python2.7/site-packages/pip/_internal/configuration.pys
   <dictcomp>†   s   	 c         S   s   i  |  ] } i  | “ q S(    (    (   R9   R:   (    (    s:   lib/python2.7/site-packages/pip/_internal/configuration.pys
   <dictcomp>‰   s   	 (   t   superR6   t   __init__R0   R   R   R!   t   NoneR   R   R+   t   mapt   reprt   isolatedt	   load_onlyR#   R%   t   _override_ordert   _ignore_env_namest   _parserst   _configt   _modified_parsers(   t   selfR@   RA   t   _valid_load_only(    (    s:   lib/python2.7/site-packages/pip/_internal/configuration.pyR<   o   s    %		'c         C   s$   |  j  ƒ  |  j s  |  j ƒ  n  d S(   sE   Loads configuration from configuration files and environment
        N(   t   _load_config_filesR@   t   _load_environment_vars(   RG   (    (    s:   lib/python2.7/site-packages/pip/_internal/configuration.pyt   load   s    
	c         C   sF   |  j  d k	 s t d ƒ ‚ y |  j ƒ  d SWn t k
 rA d SXd S(   s@   Returns the file with highest priority in configuration
        s)   Need to be specified a file to be editingi    N(   RA   R=   t   AssertionErrort   _get_parser_to_modifyt
   IndexError(   RG   (    (    s:   lib/python2.7/site-packages/pip/_internal/configuration.pyt   get_file_to_edit•   s    	c         C   s   |  j  j ƒ  S(   s`   Returns key-value pairs like dict.items() representing the loaded
        configuration
        (   t   _dictionaryt   items(   RG   (    (    s:   lib/python2.7/site-packages/pip/_internal/configuration.pyRQ   ¡   s    c         C   s<   y |  j  | SWn& t k
 r7 t d j | ƒ ƒ ‚ n Xd S(   s,   Get a value from the configuration.
        s   No such key - {}N(   RP   t   KeyErrorR   R   (   RG   t   key(    (    s:   lib/python2.7/site-packages/pip/_internal/configuration.pyt	   get_value¨   s    c         C   s—   |  j  ƒ  |  j ƒ  \ } } | d k	 ro t | ƒ \ } } | j | ƒ sY | j | ƒ n  | j | | | ƒ n  | |  j |  j | <|  j	 | | ƒ d S(   s-   Modify a value in the configuration.
        N(
   t   _ensure_have_load_onlyRM   R=   R   t   has_sectiont   add_sectiont   setRE   RA   t   _mark_as_modified(   RG   RS   t   valuet   fnamet   parsert   sectionR   (    (    s:   lib/python2.7/site-packages/pip/_internal/configuration.pyt	   set_value±   s    
c   	      C   s*  |  j  ƒ  | |  j |  j k r8 t d j | ƒ ƒ ‚ n  |  j ƒ  \ } } | d k	 rt | ƒ \ } } t } | j	 | ƒ r’ | j
 | | ƒ } n  | rt | j | ƒ ƒ } y t | ƒ } Wn t k
 rÖ d } n X| d k ró | j | ƒ n  |  j | | ƒ qt d ƒ ‚ n  |  j |  j | =d S(   s,   Unset a value in the configuration.
        s   No such key - {}s4   Fatal Internal error [id=1]. Please report as a bug.N(   RU   RE   RA   R   R   RM   R=   R   t   FalseRV   t   remove_optiont   iterRQ   t   nextt   StopIterationt   remove_sectionRY   (	   RG   RS   R[   R\   R]   R   t   modified_somethingt   section_itert   val(    (    s:   lib/python2.7/site-packages/pip/_internal/configuration.pyt   unset_valueÄ   s*    

c         C   ss   |  j  ƒ  xb |  j D]W \ } } t j d | ƒ t t j j | ƒ ƒ t | d ƒ  } | j	 | ƒ Wd QXq Wd S(   s*   Save the current in-memory state.
        s   Writing to %st   wN(
   RU   RF   t   loggert   infoR   R)   R*   t   dirnamet   opent   write(   RG   R[   R\   t   f(    (    s:   lib/python2.7/site-packages/pip/_internal/configuration.pyt   saveë   s    
c         C   s5   |  j  d  k r t d ƒ ‚ n  t j d |  j  ƒ d  S(   Ns'   Needed a specific file to be modifying.s$   Will be working with %s variant only(   RA   R=   R   Rj   t   debug(   RG   (    (    s:   lib/python2.7/site-packages/pip/_internal/configuration.pyRU   þ   s    c         C   s2   i  } x% |  j  D] } | j |  j | ƒ q W| S(   s<   A dictionary representing the loaded configuration.
        (   RB   t   updateRE   (   RG   t   retvalR:   (    (    s:   lib/python2.7/site-packages/pip/_internal/configuration.pyRP     s    c         C   sØ   t  |  j ƒ  ƒ } | t j d d !t j g k rC t j d ƒ d SxŽ | j ƒ  D]€ \ } } xq | D]i } |  j	 d k	 r  | |  j	 k r  t j d | | ƒ qc n  |  j | | ƒ } |  j | j | | f ƒ qc WqP Wd S(   s5   Loads configuration from configuration files
        i    i   sZ   Skipping loading configuration files due to environment's PIP_CONFIG_FILE being os.devnullNs    Skipping file '%s' (variant: %s)(   t   dictt   _iter_config_filesR0   R#   R)   t   devnullRj   Rq   RQ   RA   R=   t
   _load_fileRD   t   append(   RG   t   config_filesR:   t   filesR[   R\   (    (    s:   lib/python2.7/site-packages/pip/_internal/configuration.pyRI     s     c         C   sl   t  j d | | ƒ |  j | ƒ } xC | j ƒ  D]5 } | j | ƒ } |  j | j |  j | | ƒ ƒ q/ W| S(   Ns'   For variant '%s', will try loading '%s'(   Rj   Rq   t   _construct_parsert   sectionsRQ   RE   Rr   t   _normalized_keys(   RG   R:   R[   R\   R]   RQ   (    (    s:   lib/python2.7/site-packages/pip/_internal/configuration.pyRw   -  s    $c         C   s•   t  j ƒ  } t j j | ƒ r‘ y | j | ƒ Wq‘ t k
 ri t d d j t	 j
 t ƒ ƒ d | ƒ ‚ q‘ t  j k
 r } t d | ƒ ‚ q‘ Xn  | S(   Nt   reasons   contains invalid {} charactersR[   t   error(   R    t   RawConfigParserR)   R*   t   existst   readt   UnicodeDecodeErrorR   R   t   localet   getpreferredencodingR_   t   Error(   RG   R[   R\   R   (    (    s:   lib/python2.7/site-packages/pip/_internal/configuration.pyR{   8  s    c         C   s-   |  j  t j j |  j d |  j ƒ  ƒ ƒ d S(   s7   Loads configuration from environment variables
        s   :env:N(   RE   R0   R%   Rr   R}   t   _get_environ_vars(   RG   (    (    s:   lib/python2.7/site-packages/pip/_internal/configuration.pyRJ   O  s    c         C   s?   i  } x2 | D]* \ } } | d t  | ƒ } | | | <q W| S(   s×   Normalizes items to construct a dictionary with normalized keys.

        This routine is where the names become keys and are made the same
        regardless of source - configuration files or environment.
        R   (   R   (   RG   R]   RQ   t
   normalizedR   Rg   RS   (    (    s:   lib/python2.7/site-packages/pip/_internal/configuration.pyR}   W  s
    c         c   sj   xc t  j j ƒ  D]R \ } } | j d ƒ oA | d j ƒ  |  j k } | r | d j ƒ  | f Vq q Wd S(   s@   Returns a generator with all environmental vars with prefix PIP_t   PIP_i   N(   R)   t   environRQ   R   R   RC   (   RG   RS   Rg   t   should_be_yielded(    (    s:   lib/python2.7/site-packages/pip/_internal/configuration.pyR‡   d  s
    c         c   s»   t  j j d d ƒ } | d k	 r5 t j | g f Vn t j g  f Vt ƒ  } t j | t j f V|  j o | o€ t  j	 j
 | ƒ } | r¢ t j | t j f Vn  t j | t j f Vd S(   s   Yields variant and configuration files associated with it.

        This should be treated like items of a dictionary.
        t   PIP_CONFIG_FILEN(   R)   RŠ   t   getR=   R0   R#   R5   R   R@   R*   R   R   R!   (   RG   t   config_fileRy   t   should_load_user_config(    (    s:   lib/python2.7/site-packages/pip/_internal/configuration.pyRu   p  s    		
c         C   s-   |  j  |  j } | s% t d ƒ ‚ n  | d S(   Ns4   Fatal Internal error [id=2]. Please report as a bug.iÿÿÿÿ(   RD   RA   R   (   RG   t   parsers(    (    s:   lib/python2.7/site-packages/pip/_internal/configuration.pyRM     s
    c         C   s2   | | f } | |  j  k r. |  j  j | ƒ n  d  S(   N(   RF   Rx   (   RG   R[   R\   t   file_parser_tuple(    (    s:   lib/python2.7/site-packages/pip/_internal/configuration.pyRY     s    N(   t   __name__t
   __module__t   __doc__R=   R<   RK   RO   RQ   RT   R^   Rh   Rp   RU   t   propertyRP   RI   Rw   R{   RJ   R}   R‡   Ru   RM   RY   (    (    (    s:   lib/python2.7/site-packages/pip/_internal/configuration.pyR6   a   s(   							'										()   R”   R„   t   loggingR)   R-   t   pip._vendor.six.movesR    t   pip._internal.exceptionsR   R   t   pip._internal.utilsR   t   pip._internal.utils.compatR   R   t   pip._internal.utils.miscR   R   t   pip._internal.utils.typingR   t   typingR	   R
   R   R   R   R   R   R€   t   strR   t	   getLoggerR’   Rj   R   R   R0   R,   R5   t   objectR6   (    (    (    s:   lib/python2.7/site-packages/pip/_internal/configuration.pyt   <module>   s4   4		
			