U
    @vg7                     @   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rdd	lmZmZmZmZmZmZmZ ejZed
eZee Z!dd Z"dd Z#eddddddZ$erdndZ%dd Z&G dd de'Z(dS )a  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
    N)configparser)ConfigurationError!ConfigurationFileCouldNotBeLoaded)appdirs)WINDOWS
expanduser)
ensure_direnum)MYPY_CHECK_RUNNING)AnyDictIterableListNewTypeOptionalTupleKindc                 C   s*   |   dd} | dr&| dd } | S )zFMake a name consistent regardless of source (environment or file)
    _-z--   N)lowerreplace
startswith)name r   ?/tmp/pip-unpacked-wheel-gw11q0wt/pip/_internal/configuration.py_normalize_name-   s    
r   c                 C   s&   d| krd | }t|| ddS )N.zbKey does not contain dot separated section and key. Perhaps you wanted to use 'global.{}' instead?   )formatr   split)r   error_messager   r   r   _disassemble_key7   s    r"   userglobalsiteenvzenv-var)USERGLOBALSITEENVENV_VARzpip.inizpip.confc                  C   sp   dd t dD } tjtjt}tjtdt	r8dndt}tjt 
dt}tj| tj|gtj||giS )Nc                 S   s   g | ]}t j|tqS r   )ospathjoinCONFIG_BASENAME).0r-   r   r   r   
<listcomp>Q   s   z+get_configuration_files.<locals>.<listcomp>pip~z.pip)r   site_config_dirsr,   r-   r.   sysprefixr/   r   r   user_config_dirkindsr(   r)   r'   )global_config_filessite_config_filelegacy_config_filenew_config_filer   r   r   get_configuration_filesO   s(    
    r=   c                       s   e Zd ZdZd) f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e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'd( Z  ZS )*Configurationa  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.
    Nc              
      s   t t|   tjtjtjd g}||krJtdd	t
t|d d || _|| _tjtjtjtjtjg| _ddg| _dd | jD | _dd | jD | _g | _d S )	Nz5Got invalid value for load_only - should be one of {}z, versionhelpc                 S   s   i | ]
}|g qS r   r   r0   variantr   r   r   
<dictcomp>   s     z*Configuration.__init__.<locals>.<dictcomp>c                 S   s   i | ]
}|i qS r   r   rB   r   r   r   rD      s     )superr>   __init__r8   r'   r(   r)   r   r   r.   mapreprisolated	load_onlyr*   r+   _override_order_ignore_env_names_parsers_config_modified_parsers)selfrI   rJ   _valid_load_only	__class__r   r   rF   t   s0        
zConfiguration.__init__c                 C   s   |    | js|   dS )zELoads configuration from configuration files and environment
        N)_load_config_filesrI   _load_environment_varsrP   r   r   r   load   s    zConfiguration.loadc                 C   s<   | j dk	stdz|  d W S  tk
r6   Y dS X dS )z@Returns the file with highest priority in configuration
        Nz)Need to be specified a file to be editingr   )rJ   AssertionError_get_parser_to_modify
IndexErrorrV   r   r   r   get_file_to_edit   s    zConfiguration.get_file_to_editc                 C   s
   | j  S )z`Returns key-value pairs like dict.items() representing the loaded
        configuration
        )_dictionaryitemsrV   r   r   r   r]      s    zConfiguration.itemsc                 C   s4   z| j | W S  tk
r.   td|Y nX dS )z,Get a value from the configuration.
        No such key - {}N)r\   KeyErrorr   r   )rP   keyr   r   r   	get_value   s    zConfiguration.get_valuec                 C   sj   |    |  \}}|dk	rJt|\}}||s<|| |||| || j| j |< | || dS )z-Modify a value in the configuration.
        N)	_ensure_have_load_onlyrY   r"   has_sectionadd_sectionsetrN   rJ   _mark_as_modified)rP   r`   valuefnameparsersectionr   r   r   r   	set_value   s    

zConfiguration.set_valuec           	      C   s   |    || j| j kr&td||  \}}|dk	rt|\}}d}||r`|||}|rt	|
|}zt|}W n tk
r   d}Y nX |dkr|| | || ntd| j| j |= dS )z,Unset a value in the configuration.
        r^   NFz4Fatal Internal error [id=1]. Please report as a bug.)rb   rN   rJ   r   r   rY   r"   rc   remove_optioniterr]   nextStopIterationremove_sectionrf   )	rP   r`   rh   ri   rj   r   modified_somethingsection_itervalr   r   r   unset_value   s,    


zConfiguration.unset_valuec              
   C   sX   |    | jD ]D\}}td| ttj| t|d}|	| W 5 Q R X qdS )z*Save the current in-memory state.
        zWriting to %swN)
rb   rO   loggerinfor   r,   r-   dirnameopenwrite)rP   rh   ri   fr   r   r   save   s    zConfiguration.savec                 C   s$   | j d krtdtd| j  d S )Nz'Needed a specific file to be modifying.z$Will be working with %s variant only)rJ   r   rv   debugrV   r   r   r   rb     s    
z$Configuration._ensure_have_load_onlyc                 C   s$   i }| j D ]}|| j|  q
|S )z<A dictionary representing the loaded configuration.
        )rK   updaterN   )rP   retvalrC   r   r   r   r\   	  s    
zConfiguration._dictionaryc                 C   s   t |  }|tj dd tjgkr4td dS | D ]V\}}|D ]H}| j	dk	rp|| j	krptd|| qH| 
||}| j| ||f qHq<dS )z5Loads configuration from configuration files
        r   r   zZSkipping loading configuration files due to environment's PIP_CONFIG_FILE being os.devnullNz Skipping file '%s' (variant: %s))dict_iter_config_filesr8   r*   r,   devnullrv   r}   r]   rJ   
_load_filerM   append)rP   config_filesrC   filesrh   ri   r   r   r   rT     s"      z Configuration._load_config_filesc                 C   sL   t d|| | |}| D ]&}||}| j| | || q |S )Nz'For variant '%s', will try loading '%s')rv   r}   _construct_parsersectionsr]   rN   r~   _normalized_keys)rP   rC   rh   ri   rj   r]   r   r   r   r   2  s    

zConfiguration._load_filec              
   C   s   t  }tj|r|z|| W nX tk
rN   tdt	
d|dY n. t jk
rz } zt|dW 5 d }~X Y nX |S )Nzcontains invalid {} charactersF)reasonrh   )error)r   RawConfigParserr,   r-   existsreadUnicodeDecodeErrorr   r   localegetpreferredencodingError)rP   rh   ri   r   r   r   r   r   =  s    
zConfiguration._construct_parserc                 C   s"   | j tj | d|   dS )z7Loads configuration from environment variables
        z:env:N)rN   r8   r+   r~   r   _get_environ_varsrV   r   r   r   rU   T  s    z$Configuration._load_environment_varsc                 C   s.   i }|D ] \}}|d t | }|||< q|S )zNormalizes 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   )rP   rj   r]   
normalizedr   rs   r`   r   r   r   r   \  s
    
zConfiguration._normalized_keysc                 c   sR   t j D ]B\}}|do0|dd  | jk}|r
|dd  |fV  q
dS )z@Returns a generator with all environmental vars with prefix PIP_PIP_   N)r,   environr]   r   r   rL   )rP   r`   rs   should_be_yieldedr   r   r   r   i  s    
zConfiguration._get_environ_varsc                 c   s   t jdd}|dk	r&tj|gfV  ntjg fV  t }tj|tj fV  | j ob|o`t j	| }|rztj
|tj
 fV  tj|tj fV  dS )zYields variant and configuration files associated with it.

        This should be treated like items of a dictionary.
        PIP_CONFIG_FILEN)r,   r   getr8   r*   r=   r(   rI   r-   r   r'   r)   )rP   config_filer   should_load_user_configr   r   r   r   u  s    	z Configuration._iter_config_filesc                 C   s    | j | j }|std|d S )Nz4Fatal Internal error [id=2]. Please report as a bug.r?   )rM   rJ   r   )rP   parsersr   r   r   rY     s    z#Configuration._get_parser_to_modifyc                 C   s"   ||f}|| j kr| j | d S )N)rO   r   )rP   rh   ri   file_parser_tupler   r   r   rf     s    
zConfiguration._mark_as_modified)N)__name__
__module____qualname____doc__rF   rW   r[   r]   ra   rk   rt   r|   rb   propertyr\   rT   r   r   rU   r   r   r   rY   rf   __classcell__r   r   rR   r   r>   f   s*   	'
r>   ))r   r   loggingr,   r5   Zpip._vendor.six.movesr   pip._internal.exceptionsr   r   Zpip._internal.utilsr   pip._internal.utils.compatr   r   pip._internal.utils.miscr   r	   pip._internal.utils.typingr
   typingr   r   r   r   r   r   r   r   strr   	getLoggerr   rv   r   r"   r8   r/   r=   objectr>   r   r   r   r   <module>   s6   $


	