3
Ug"                 @   s   d dl Z d dlZd dlZd dlmZ d dlmZmZmZ d dl	m
Z
 d dlmZmZ d dlmZmZmZmZ d dlmZ d dlmZ d d	lmZmZ e jeZG d
d de
ZdS )    N)Values)AnyListOptional)Command)ERRORSUCCESS)ConfigurationKindget_configuration_fileskinds)PipError)
indent_log)get_progwrite_outputc               @   s*  e Zd ZdZdZdZddddZeee	 e
dd	d
Zeeee dddZeee	 ddddZeee	 ddddZeee	 ddddZeee	 ddddZeee	 ddddZeddddZddddZeee	 ddddZee	 e	e
edd d!Zddd"d#Zee	d$d%d&ZdS )'ConfigurationCommandaa  
    Manage local and global configuration.

    Subcommands:

    - list: List the active configuration (or from the file specified)
    - edit: Edit the configuration file in an editor
    - get: Get the value associated with name
    - set: Set the name=value
    - unset: Unset the value associated with name
    - debug: List the configuration files and values defined under them

    If none of --user, --global and --site are passed, a virtual
    environment configuration file is used if one is active and the file
    exists. Otherwise, all modifications happen to the user file by
    default.
    Ta  
        %prog [<file-option>] list
        %prog [<file-option>] [--editor <editor-path>] edit

        %prog [<file-option>] get name
        %prog [<file-option>] set name value
        %prog [<file-option>] unset name
        %prog [<file-option>] debug
    N)returnc             C   sl   | j jdddd dd | j jdddd	d
d | j jdddd	dd | j jdddd	dd | jjd| j  d S )Nz--editoreditorstorez\Editor to use to edit the file. Uses VISUAL or EDITOR environment variables if not provided.)destactiondefaulthelpz--globalglobal_file
store_trueFz+Use the system-wide configuration file onlyz--user	user_filez$Use the user configuration file onlyz--site	site_filez3Use the current environment configuration file onlyr   )cmd_opts
add_optionparserinsert_option_group)self r"   H/tmp/pip-unpacked-wheel-0ht26j5g/pip/_internal/commands/configuration.pyadd_options4   s2    z ConfigurationCommand.add_options)optionsargsr   c             C   s  | j | j| j| j| j| jd}| s0|d |krJtjddjt	| t
S |d }y| j||dkd	}W n0 tk
r } ztj|jd  t
S d }~X nX t|j|d
| _| jj  y|| ||dd   W n2 tk
r  } ztj|jd  t
S d }~X nX tS )N)listeditgetsetunsetdebugr   zNeed an action (%s) to perform.z, r)   r*   r+   r(   )
need_value)isolated	load_only   )r)   r*   r+   r(   )list_valuesopen_in_editorget_nameset_name_value
unset_namelist_config_valuesloggererrorjoinsortedr   _determine_filer   r&   r	   isolated_modeconfigurationloadr   )r!   r%   r&   handlersr   r/   er"   r"   r#   runZ   s6    

zConfigurationCommand.run)r%   r-   r   c             C   s   dd t j|jft j|jft j|jffD }|s`|s8d S tdd t t j D rXt jS t jS nt	|dkrt|d S t
dd S )Nc             S   s   g | ]\}}|r|qS r"   r"   ).0keyvaluer"   r"   r#   
<listcomp>   s   z8ConfigurationCommand._determine_file.<locals>.<listcomp>c             s   s   | ]}t jj|V  qd S )N)ospathexists)rB   site_config_filer"   r"   r#   	<genexpr>   s   z7ConfigurationCommand._determine_file.<locals>.<genexpr>r0   r   zLNeed exactly one file to operate upon (--user, --site, --global) to perform.)r   USERr   GLOBALr   SITEr   anyr   lenr   )r!   r%   r-   Zfile_optionsr"   r"   r#   r;      s     

z$ConfigurationCommand._determine_filec             C   s<   | j |ddd x&t| jj D ]\}}td|| q W d S )Nr'   r   )nz%s=%r)_get_n_argsr:   r=   itemsr   )r!   r%   r&   rC   rD   r"   r"   r#   r1      s    z ConfigurationCommand.list_valuesc             C   s*   | j |ddd}| jj|}td| d S )Nz
get [name]r0   )rP   z%s)rQ   r=   	get_valuer   )r!   r%   r&   rC   rD   r"   r"   r#   r3      s    zConfigurationCommand.get_namec             C   s.   | j |ddd\}}| jj|| | j  d S )Nzset [name] [value]   )rP   )rQ   r=   	set_value_save_configuration)r!   r%   r&   rC   rD   r"   r"   r#   r4      s    z#ConfigurationCommand.set_name_valuec             C   s(   | j |ddd}| jj| | j  d S )Nzunset [name]r0   )rP   )rQ   r=   unset_valuerV   )r!   r%   r&   rC   r"   r"   r#   r5      s    zConfigurationCommand.unset_namec             C   s   | j |ddd | j  xjt| jj D ]X\}}td| xD|D ]<}t , tjj	|}td|| |rr| j
| W dQ R X q@W q(W dS )z9List config key-value pairs across different config filesr,   r   )rP   z%s:z%s, exists: %rN)rQ   print_env_var_valuesr:   r=   Ziter_config_filesr   r   rF   rG   rH   print_config_file_values)r!   r%   r&   variantfilesfnameZfile_existsr"   r"   r#   r6      s    

z'ConfigurationCommand.list_config_values)rZ   r   c             C   s@   x:| j j|j D ]&\}}t  td|| W dQ R X qW dS )z.Get key-value pairs from the file of a variantz%s: %sN)r=   Zget_values_in_configrR   r   r   )r!   rZ   namerD   r"   r"   r#   rY      s    z-ConfigurationCommand.print_config_file_valuesc             C   sV   t dd t < x4t| jj D ]"\}}d|j  }t d|| q"W W dQ R X dS )z5Get key-values pairs present as environment variablesz%s:env_varPIP_z%s=%rN)r   r   r:   r=   Zget_environ_varsupper)r!   rC   rD   r^   r"   r"   r#   rX      s
    
z)ConfigurationCommand.print_env_var_valuesc             C   sp   | j |}| jj }|d kr$tdytj||g W n4 tjk
rj } ztdj|jW Y d d }~X nX d S )Nz%Could not determine appropriate file.z*Editor Subprocess exited with exit code {})	_determine_editorr=   get_file_to_editr   
subprocess
check_callCalledProcessErrorformat
returncode)r!   r%   r&   r   r\   r@   r"   r"   r#   r2      s    

z#ConfigurationCommand.open_in_editor)r&   examplerP   r   c             C   s<   t ||kr$dj|t |}t||dkr4|d S |S dS )zAHelper to make sure the command got the right number of argumentszJGot unexpected number of arguments, expected {}. (example: "{} config {}")r0   r   N)rO   rf   r   r   )r!   r&   rh   rP   msgr"   r"   r#   rQ      s    z ConfigurationCommand._get_n_argsc             C   s:   y| j j  W n& tk
r4   tjd tdY nX d S )Nz:Unable to save configuration. Please report this as a bug.zInternal Error.)r=   save	Exceptionr7   	exceptionr   )r!   r"   r"   r#   rV      s    z(ConfigurationCommand._save_configuration)r%   r   c             C   sD   |j d k	r|j S dtjkr$tjd S dtjkr8tjd S tdd S )NZVISUALZEDITORz"Could not determine editor to use.)r   rF   environr   )r!   r%   r"   r"   r#   ra     s    




z&ConfigurationCommand._determine_editor)__name__
__module____qualname____doc__ignore_require_venvusager$   r   r   strintrA   boolr   r
   r;   r1   r3   r4   r5   r6   rY   rX   r2   r   rQ   rV   ra   r"   r"   r"   r#   r      s"   	&-r   )loggingrF   rc   optparser   typingr   r   r   pip._internal.cli.base_commandr   pip._internal.cli.status_codesr   r   pip._internal.configurationr	   r
   r   r   pip._internal.exceptionsr   pip._internal.utils.loggingr   pip._internal.utils.miscr   r   	getLoggerrn   r7   r   r"   r"   r"   r#   <module>   s   
