o
    {ŸŽi$  ã                   @   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                   @   sH  e Zd ZdZdZdZd(dd„Zded	ee	 de
fd
d„Zdededee fdd„Zded	ee	 ddfdd„Zded	ee	 ddfdd„Zded	ee	 ddfdd„Zded	ee	 ddfdd„Zded	ee	 ddfdd„Zdeddfdd„Zd(dd„Zded	ee	 ddfdd„Zd	ee	 d e	d!e
defd"d#„Zd(d$d%„Zdede	f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
    ÚreturnNc                 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Ú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#   únC:\Users\psant\Downloads\Anu Raj Backend\school\venv\Lib\site-packages\pip/_internal/commands/configuration.pyÚadd_options4   s:   úûûûz ConfigurationCommand.add_optionsÚoptionsÚargsc              
   C   s  | j | j| j| j| j| jdœ}|r|d |vr$t dd t	|ƒ¡¡ t
S |d }z| j||dv d�}W n tyO } zt |jd ¡ t
W  Y d }~S d }~ww t|j|d�| _| j ¡  z|| ||dd … ƒ W tS  ty‡ } zt |jd ¡ t
W  Y d }~S d }~ww )	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	   Úisolated_modeÚconfigurationÚloadr   )r"   r&   r'   Úhandlersr   r0   Úer#   r#   r$   ÚrunZ   sF   ú
þ
ÿ€þÿ
ü€þzConfigurationCommand.runr.   c                 C   sz   dd„ t j|jft j|jft j|jffD ƒ}|s/|sd S tdd„ tƒ t j D ƒƒr,t jS t jS t	|ƒdkr9|d S t
dƒ‚)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)rC   Úsite_config_filer#   r#   r$   Ú	<genexpr>’   s
   € 
ÿ
ÿz7ConfigurationCommand._determine_file.<locals>.<genexpr>r1   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   s8   | j |ddd� t| j ¡ ƒD ]
\}}td||ƒ qd S )Nr(   r   ©Únú%s=%r)Ú_get_n_argsr;   r>   Úitemsr   ©r"   r&   r'   rD   rE   r#   r#   r$   r2   ¡   s   ÿz ConfigurationCommand.list_valuesc                 C   s*   | j |ddd�}| j |¡}td|ƒ d S )Nz
get [name]r1   rQ   z%s)rT   r>   Ú	get_valuer   rV   r#   r#   r$   r4   §   s   zConfigurationCommand.get_namec                 C   s.   | j |ddd�\}}| j ||¡ |  ¡  d S )Nzset [name] [value]é   rQ   )rT   r>   Ú	set_valueÚ_save_configurationrV   r#   r#   r$   r5   ­   s   z#ConfigurationCommand.set_name_valuec                 C   s(   | j |ddd�}| j |¡ |  ¡  d S )Nzunset [name]r1   rQ   )rT   r>   Úunset_valuerZ   )r"   r&   r'   rD   r#   r#   r$   r6   ³   s   zConfigurationCommand.unset_namec              
   C   s”   | j |ddd� |  ¡  t| j ¡ ƒD ]4\}}td|ƒ |D ](}tƒ � tj 	|¡}td||ƒ |r7|  
|¡ W d  ƒ q1 sAw   Y  qqdS )z9List config key-value pairs across different config filesr-   r   rQ   ú%s:z%s, exists: %rN)rT   Úprint_env_var_valuesr;   r>   Úiter_config_filesr   r   rG   rH   rI   Úprint_config_file_values)r"   r&   r'   ÚvariantÚfilesÚfnameZfile_existsr#   r#   r$   r7   ¹   s    
ÿ
€ûÿþz'ConfigurationCommand.list_config_valuesr`   c              	   C   sP   | j  |¡ ¡ D ]\}}tƒ � td||ƒ W d  ƒ q1 s w   Y  qdS )z.Get key-value pairs from the file of a variantz%s: %sN)r>   Úget_values_in_configrU   r   r   )r"   r`   ÚnamerE   r#   r#   r$   r_   Ê   s   

ÿÿþz-ConfigurationCommand.print_config_file_valuesc                 C   sh   t ddƒ tƒ �" t| j ¡ ƒD ]\}}d| ¡ › �}t d||ƒ qW d  ƒ dS 1 s-w   Y  dS )z5Get key-values pairs present as environment variablesr\   Úenv_varÚPIP_rS   N)r   r   r;   r>   Úget_environ_varsÚupper)r"   rD   rE   re   r#   r#   r$   r]   Ñ   s   
þ"ÿz)ConfigurationCommand.print_env_var_valuesc              
   C   sd   |   |¡}| j ¡ }|d u rtdƒ‚z
t ||g¡ W d S  tjy1 } ztd |j¡ƒ‚d }~ww )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   rb   rA   r#   r#   r$   r3   Ù   s   

þ€ÿz#ConfigurationCommand.open_in_editorÚexamplerR   c                 C   s8   t |ƒ|krd |tƒ |¡}t|ƒ‚|dkr|d S |S )zJHelper to make sure the command got the right number of arguments
        zJGot unexpected number of arguments, expected {}. (example: "{} config {}")r1   r   N)rP   rn   r   r   )r"   r'   rp   rR   Úmsgr#   r#   r$   rT   è   s   ýz ConfigurationCommand._get_n_argsc                 C   s2   z| j  ¡  W d S  ty   t d¡ tdƒ‚w )Nz:Unable to save configuration. Please report this as a bug.zInternal Error.)r>   ÚsaveÚ	Exceptionr8   Ú	exceptionr   r!   r#   r#   r$   rZ   ÷   s   ÿüz(ConfigurationCommand._save_configurationc                 C   s@   |j d ur|j S dtjv rtjd S dtjv rtjd S tdƒ‚)NZVISUALZEDITORz"Could not determine editor to use.)r   rG   Úenvironr   )r"   r&   r#   r#   r$   ri     s   




z&ConfigurationCommand._determine_editor)r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úignore_require_venvÚusager%   r   r   ÚstrÚintrB   Úboolr   r
   r<   r2   r4   r5   r6   r7   r_   r]   r3   r   rT   rZ   ri   r#   r#   r#   r$   r      s$    

&-

r   )ÚloggingrG   rk   Ú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   Ú	getLoggerrv   r8   r   r#   r#   r#   r$   Ú<module>   s    
