a
    !6a#                     @   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 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 )'ConfigurationCommandah  
    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 on the 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d| j  d S )Nz--editoreditorZ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   )Zcmd_optsZ
add_optionparserZinsert_option_groupself r   Clib/python3.9/site-packages/pip/_internal/commands/configuration.pyadd_options4   s:    z ConfigurationCommand.add_options)optionsargsr   c              
   C   s  | j | j| j| j| j| jd}|r.|d |vrHtddt	| t
S |d }z| j||dv d}W n8 ty } z t|jd  t
W  Y d }~S d }~0 0 t|j|d| _| j  z|| ||dd   W n: ty } z t|jd  t
W  Y d }~S d }~0 0 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   )list_valuesopen_in_editorget_nameset_name_value
unset_namelist_config_valuesloggererrorjoinsortedr   _determine_filer   r#   r	   Zisolated_modeconfigurationloadr   )r   r"   r#   handlersr   r,   er   r   r    runZ   s>    


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|V  qd S )N)ospathexists)r>   Zsite_config_filer   r   r    	<genexpr>   s   z7ConfigurationCommand._determine_file.<locals>.<genexpr>r-   r   zLNeed exactly one file to operate upon (--user, --site, --global) to perform.)r   ZUSERr   ZGLOBALr   ZSITEr   anyr   lenr   )r   r"   r*   Zfile_optionsr   r   r    r8      s$    



z$ConfigurationCommand._determine_filec                 C   s8   | j |ddd t| j D ]\}}td|| qd S )Nr$   r   n%s=%r)_get_n_argsr7   r9   itemsr   r   r"   r#   r?   r@   r   r   r    r.      s    z ConfigurationCommand.list_valuesc                 C   s*   | j |ddd}| j|}td| d S )Nz
get [name]r-   rH   z%s)rK   r9   Z	get_valuer   rM   r   r   r    r0      s    zConfigurationCommand.get_namec                 C   s.   | j |ddd\}}| j|| |   d S )Nzset [name] [value]   rH   )rK   r9   Z	set_value_save_configurationrM   r   r   r    r1      s    z#ConfigurationCommand.set_name_valuec                 C   s(   | j |ddd}| j| |   d S )Nzunset [name]r-   rH   )rK   r9   Zunset_valuerO   )r   r"   r#   r?   r   r   r    r2      s    zConfigurationCommand.unset_namec              
   C   s   | j |ddd |   t| j D ]h\}}td| |D ]P}t 6 tj	|}td|| |rn| 
| W d   q<1 s0    Y  q<q&dS )z9List config key-value pairs across different config filesr)   r   rH   %s:z%s, exists: %rN)rK   print_env_var_valuesr7   r9   Ziter_config_filesr   r   rB   rC   rD   print_config_file_values)r   r"   r#   variantfilesfnameZfile_existsr   r   r    r3      s    
z'ConfigurationCommand.list_config_values)rS   r   c              	   C   sP   | j | D ]:\}}t  td|| W d   q1 s@0    Y  qdS )z.Get key-value pairs from the file of a variantz%s: %sN)r9   Zget_values_in_configrL   r   r   )r   rS   namer@   r   r   r    rR      s
    z-ConfigurationCommand.print_config_file_valuesc                 C   sf   t dd t B t| j D ]"\}}d|  }t d|| q W d   n1 sX0    Y  dS )z5Get key-values pairs present as environment variablesrP   env_varZPIP_rJ   N)r   r   r7   r9   Zget_environ_varsupper)r   r?   r@   rW   r   r   r    rQ      s
    
z)ConfigurationCommand.print_env_var_valuesc              
   C   sr   |  |}| j }|d u r$tdzt||g W n6 tjyl } ztd|jW Y d }~n
d }~0 0 d S )Nz%Could not determine appropriate file.z*Editor Subprocess exited with exit code {})	_determine_editorr9   Zget_file_to_editr   
subprocessZ
check_callZCalledProcessErrorformat
returncode)r   r"   r#   r   rU   r<   r   r   r    r/      s    

z#ConfigurationCommand.open_in_editor)r#   examplerI   r   c                 C   s<   t ||kr$d|t |}t||dkr4|d S |S dS )zJHelper to make sure the command got the right number of arguments
        zJGot unexpected number of arguments, expected {}. (example: "{} config {}")r-   r   N)rG   r[   r   r   )r   r#   r]   rI   msgr   r   r    rK      s    z ConfigurationCommand._get_n_argsc                 C   s8   z| j   W n$ ty2   td tdY n0 d S )Nz:Unable to save configuration. Please report this as a bug.zInternal Error.)r9   Zsave	Exceptionr4   Z	exceptionr   r   r   r   r    rO      s    z(ConfigurationCommand._save_configuration)r"   r   c                 C   sD   |j d ur|j S dtjv r$tjd S dtjv r8tjd S tdd S )NZVISUALZEDITORz"Could not determine editor to use.)r   rB   environr   )r   r"   r   r   r    rY     s    




z&ConfigurationCommand._determine_editor)__name__
__module____qualname____doc__Zignore_require_venvZusager!   r   r   strintr=   boolr   r
   r8   r.   r0   r1   r2   r3   rR   rQ   r/   r   rK   rO   rY   r   r   r   r    r      s"   
&-r   )ZloggingrB   rZ   Zoptparser   typingr   r   r   Zpip._internal.cli.base_commandr   Zpip._internal.cli.status_codesr   r   Zpip._internal.configurationr	   r
   r   r   Zpip._internal.exceptionsr   Zpip._internal.utils.loggingr   Zpip._internal.utils.miscr   r   Z	getLoggerra   r4   r   r   r   r   r    <module>   s   
