ó
Ð²Ebc           @   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 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 r	d n d Z d Z  e d d d d d d d d d d ƒ Z! e! j" e! j# e! j$ e! j% e! j& f Z' e! j# e! j" e! j$ f Z( e j) e* ƒ Z+ d „  Z, d „  Z- d „  Z. d e/ f d „  ƒ  YZ0 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   Kinds   pip.inis   pip.conft   versiont   helpt   USERt   usert   GLOBALt   globalt   SITEt   sitet   ENVt   envt   ENV_VARs   env-varc         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(    (    s8   /tmp/pip-build-UPPWic/pip/pip/_internal/configuration.pyt   _normalize_name6   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(    (    s8   /tmp/pip-build-UPPWic/pip/pip/_internal/configuration.pyt   _disassemble_key@   s
    	c          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(    (    s8   /tmp/pip-build-UPPWic/pip/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 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 ƒ  | d  k	 rU | t k rU t d j d j t t	 t ƒ ƒ ƒ ƒ ‚ n  | |  _
 | |  _ d „  t Dƒ |  _ d „  t Dƒ |  _ g  |  _ d  S(   Ns5   Got invalid value for load_only - should be one of {}s   , c         S   s   i  |  ] } g  | “ q S(    (    (   t   .0t   variant(    (    s8   /tmp/pip-build-UPPWic/pip/pip/_internal/configuration.pys
   <dictcomp>   s   	 c         S   s   i  |  ] } i  | “ q S(    (    (   R9   R:   (    (    s8   /tmp/pip-build-UPPWic/pip/pip/_internal/configuration.pys
   <dictcomp>‚   s   	 (   t   superR8   t   __init__t   Nonet   VALID_LOAD_ONLYR   R$   R-   t   mapt   reprt   isolatedt	   load_onlyt   OVERRIDE_ORDERt   _parserst   _configt   _modified_parsers(   t   selfRA   RB   (    (    s8   /tmp/pip-build-UPPWic/pip/pip/_internal/configuration.pyR<   p   s    !		c         C   s$   |  j  ƒ  |  j s  |  j ƒ  n  d S(   sE   Loads configuration from configuration files and environment
        N(   t   _load_config_filesRA   t   _load_environment_vars(   RG   (    (    s8   /tmp/pip-build-UPPWic/pip/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(   RB   R=   t   AssertionErrort   _get_parser_to_modifyt
   IndexError(   RG   (    (    s8   /tmp/pip-build-UPPWic/pip/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   (    (    s8   /tmp/pip-build-UPPWic/pip/pip/_internal/configuration.pyRP   š   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(   RO   t   KeyErrorR   R$   (   RG   t   key(    (    s8   /tmp/pip-build-UPPWic/pip/pip/_internal/configuration.pyt	   get_value¡   s    c         C   s¦   |  j  ƒ  |  j s t ‚ |  j ƒ  \ } } | d k	 r~ t | ƒ \ } } | j | ƒ sh | j | ƒ n  | j | | | ƒ n  | |  j	 |  j | <|  j
 | | ƒ d S(   s-   Modify a value in the configuration.
        N(   t   _ensure_have_load_onlyRB   RK   RL   R=   R'   t   has_sectiont   add_sectiont   setRE   t   _mark_as_modified(   RG   RR   t   valuet   fnamet   parsert   sectionR!   (    (    s8   /tmp/pip-build-UPPWic/pip/pip/_internal/configuration.pyt	   set_valueª   s    
c         C   sî   |  j  ƒ  |  j s t ‚ | |  j |  j k rG t d j | ƒ ƒ ‚ n  |  j ƒ  \ } } | d k	 rÙ t | ƒ \ } } | j	 | ƒ o• | j
 | | ƒ s§ t d ƒ ‚ n  | j | ƒ sÆ | j | ƒ n  |  j | | ƒ 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(   RT   RB   RK   RE   R   R$   RL   R=   R'   RU   t   remove_optionRP   t   remove_sectionRX   (   RG   RR   RZ   R[   R\   R!   (    (    s8   /tmp/pip-build-UPPWic/pip/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(
   RT   RF   t   loggert   infoR   R+   R,   t   dirnamet   opent   write(   RG   RZ   R[   t   f(    (    s8   /tmp/pip-build-UPPWic/pip/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(   RB   R=   R   Rb   t   debug(   RG   (    (    s8   /tmp/pip-build-UPPWic/pip/pip/_internal/configuration.pyRT   ì   s    c         C   s/   i  } x" t  D] } | j |  j | ƒ q W| S(   s<   A dictionary representing the loaded configuration.
        (   RC   t   updateRE   (   RG   t   retvalR:   (    (    s8   /tmp/pip-build-UPPWic/pip/pip/_internal/configuration.pyRO   ò   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_filesR2   R   R+   t   devnullRb   Ri   RP   RB   R=   t
   _load_fileRD   t   append(   RG   t   config_filesR:   t   filesRZ   R[   (    (    s8   /tmp/pip-build-UPPWic/pip/pip/_internal/configuration.pyRH      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'(   Rb   Ri   t   _construct_parsert   sectionsRP   RE   Rj   t   _normalized_keys(   RG   R:   RZ   R[   R\   RP   (    (    s8   /tmp/pip-build-UPPWic/pip/pip/_internal/configuration.pyRo     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 {} charactersRZ   t   error(   R    t   RawConfigParserR+   R,   t   existst   readt   UnicodeDecodeErrorR   R$   t   localet   getpreferredencodingt   Falset   Error(   RG   RZ   R[   Rw   (    (    s8   /tmp/pip-build-UPPWic/pip/pip/_internal/configuration.pyRs   &  s    c         C   s-   |  j  t j j |  j d |  j ƒ  ƒ ƒ d S(   s7   Loads configuration from environment variables
        s   :env:N(   RE   R2   R   Rj   Ru   t   get_environ_vars(   RG   (    (    s8   /tmp/pip-build-UPPWic/pip/pip/_internal/configuration.pyRI   =  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\   RP   t
   normalizedR!   t   valRR   (    (    s8   /tmp/pip-build-UPPWic/pip/pip/_internal/configuration.pyRu   E  s
    c         c   s`   xY t  j j ƒ  D]H \ } } | j d ƒ r | d j ƒ  } | t k rX | | f VqX q q Wd S(   s@   Returns a generator with all environmental vars with prefix PIP_t   PIP_i   N(   R+   t   environRP   R    R   t   ENV_NAMES_IGNORED(   RG   RR   R‚   R!   (    (    s8   /tmp/pip-build-UPPWic/pip/pip/_internal/configuration.pyR€   R  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=   R2   R   R7   R   RA   R,   Ry   R   R   (   RG   t   config_fileRq   t   should_load_user_config(    (    s8   /tmp/pip-build-UPPWic/pip/pip/_internal/configuration.pyRm   \  s    		
c         C   s   |  j  | S(   s#   Get values present in a config file(   RE   (   RG   R:   (    (    s8   /tmp/pip-build-UPPWic/pip/pip/_internal/configuration.pyt   get_values_in_config{  s    c         C   s<   |  j  s t ‚ |  j |  j  } | s4 t d ƒ ‚ n  | d S(   Ns4   Fatal Internal error [id=2]. Please report as a bug.iÿÿÿÿ(   RB   RK   RD   R   (   RG   t   parsers(    (    s8   /tmp/pip-build-UPPWic/pip/pip/_internal/configuration.pyRL   €  s    c         C   s2   | | f } | |  j  k r. |  j  j | ƒ n  d  S(   N(   RF   Rp   (   RG   RZ   R[   t   file_parser_tuple(    (    s8   /tmp/pip-build-UPPWic/pip/pip/_internal/configuration.pyRX     s    c         C   s   d j  |  j j |  j ƒ S(   Ns   {}({!r})(   R$   t	   __class__t   __name__RO   (   RG   (    (    s8   /tmp/pip-build-UPPWic/pip/pip/_internal/configuration.pyt   __repr__•  s    N(   RŽ   t
   __module__t   __doc__R=   R<   RJ   RN   RP   RS   R]   R`   Rh   RT   t   propertyRO   RH   Ro   Rs   RI   Ru   R€   Rm   RŠ   RL   RX   R   (    (    (    s8   /tmp/pip-build-UPPWic/pip/pip/_internal/configuration.pyR8   b   s,   															
				(   R   R   (1   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   Rx   t   strR   R.   R…   R2   R   R   R   R   R   RC   R>   t	   getLoggerRŽ   Rb   R"   R'   R7   t   objectR8   (    (    (    s8   /tmp/pip-build-UPPWic/pip/pip/_internal/configuration.pyt   <module>   s:   4		$	
		