U
��.e: � @ s� d dl Z d dlZd dlZd dlmZ d dlmZmZ d dlm Z m
Z
mZ d dlm
Z
d dlmZmZ e �e�ZG dd� de�ZdS ) � N)�Command)�ERROR�SUCCESS)�
Configuration�get_configuration_files�kinds)�PipError)�get_prog�write_outputc sx e Zd ZdZdZdZ� fdd�Zdd� Zdd � Zd
d� Z dd
� Z
dd� Zdd� Zdd� Z
dd� Zdd� Zdd� Z� ZS )�ConfigurationCommanda9 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
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 on the to the user file by
default.
Tz�
%prog [] list
%prog [] [--editor ] edit
%prog [] get name
%prog [] set name value
%prog [] unset name
c s� t t| �j||� d | _| jjdddd dd� | jjdddd d
d� | jjdddd d
d� | jjdddd dd� | j�d| j� d S )Nz--editor�editorZstorez\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 )�superr �__init__�
configurationZcmd_optsZ
add_option�parserZinsert_option_group)�self�args�kwargs�� __class__� �H/usr/lib/python3.8/site-packages/pip/_internal/commands/configuration.pyr 0 s> ����zConfigurationCommand.__init__c
C s | j | j| j| j| jd�}|r*|d |krHt�d�d�t |���� t
S |d }z| j||dkd�}W n: tk
r� } zt�|j
d � t
W Y �S d }~X Y nX t|j|d�| _| j�� z|| ||dd � � W n< tk
�r } zt�|j
d � t
W Y �S d }~X Y nX tS ) N)�list�edit�get�set�unsetr zNeed an action ({}) to perform.z, )r" r# r$ r! )�
need_value)�isolated� load_only� )�list_values�open_in_editor�get_name�set_name_value�
unset_name�logger�error�format�join�sortedr �_determine_filer r r Z
isolated_moder �loadr )r �optionsr Zhandlersr r' �er r r �runZ s> � � �
�
zConfigurationCommand.runc C s� dd� t j|jft j|jft j|jffD �}|s`|s8d S tdd� t� t j D ��rXt jS t jS nt |�dkrt|d S t
d��d S )Nc S s g | ]\}}|r|�qS r r )�.0�key�valuer r r �
� s �z8ConfigurationCommand._determine_file..c s s | ]}t j�|�V qd S )N)�os�path�exists)r8 Zsite_config_filer r r � � s �z7ConfigurationCommand._determine_file..r( r zLNeed exactly one file to operate upon (--user, --site, --global) to perform.)r ZUSERr ZGLOBALr ZSITEr �anyr �lenr )r r5 r% Zfile_optionsr r r r3 � s$
�
��z$ConfigurationCommand._determine_filec C s8 | j |ddd� t| j�� �D ]\}}td||� qd S )Nr r ��nz%s=%r)�_get_n_argsr2 r �itemsr
�r r5 r r9 r: r r r r) � s z ConfigurationCommand.list_valuesc C s* | j |ddd�}| j�|�}td|� d S )Nz
get [name]r( rB z%s)rD r Z get_valuer
rF r r r r+ � s zConfigurationCommand.get_namec C s. | j |ddd�\}}| j�||� | �� d S )Nzset [name] [value]� rB )rD r Z set_value�_save_configurationrF r r r r, � s z#ConfigurationCommand.set_name_valuec C s( | j |ddd�}| j�|� | �� d S )Nzunset [name]r( rB )rD r Zunset_valuerH )r r5 r r9 r r r r- � s zConfigurationCommand.unset_namec
C sp | � |�}| j�� }|d kr$td��zt�||g� W n4 tjk
rj } ztd�|j���W 5 d }~X Y nX d S )Nz%Could not determine appropriate file.z*Editor Subprocess exited with exit code {}) �_determine_editorr Zget_file_to_editr �
subprocessZ
check_callZCalledProcessErrorr0 �
returncode)r r5 r r Zfnamer6 r r r r* � s
��z#ConfigurationCommand.open_in_editorc C s<