B
    =)H[¯  ã               @   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 d dlm	Z	 d dl
mZ d dlmZmZ d dlmZ e  e¡ZG dd	„ d	eƒZdS )
é    N)ÚCommand)ÚConfigurationÚkinds)ÚPipError)Úvenv_config_file)ÚERRORÚSUCCESS)Úget_progc                   s|   e Zd ZdZdZdZdZ‡ 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dd„ Zdd„ Z‡  ZS )Ú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 --venv 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.
    Úconfigzâ
        %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
    z&Manage local and global configuration.c                s„   t t| ƒj||Ž 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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--venvÚ	venv_filez*Use the virtualenv configuration file onlyr   )Úsuperr
   Ú__init__ÚconfigurationÚcmd_optsÚ
add_optionÚparserÚinsert_option_group)ÚselfÚargsÚkwargs)Ú	__class__© úeC:\Users\HIRONO~1\AppData\Local\Temp\pip-install-qz7kpjmn\pip\pip\_internal\commands\configuration.pyr   ,   s6    zConfigurationCommand.__init__c          
   C   s  | j | j| j| j| jdœ}|r*|d |krHt d d t	|ƒ¡¡¡ t
S |d }y| j||dkd�}W n2 tk
r˜ } zt |jd ¡ t
S d }~X Y nX t|j|d�| _| j ¡  y|| ||dd … ƒ W n4 tk
�r } zt |jd ¡ t
S d }~X Y nX tS )	N)ÚlistÚeditÚgetÚsetÚunsetr   zNeed 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   Úisolated_moder   Úloadr   )r   Úoptionsr   Úhandlersr   r*   Úer!   r!   r"   ÚrunV   s2    

zConfigurationCommand.runc                s€   t j|jt j|jt j|ji‰ tˆ  ¡ ƒdkrN|s4d S t	j
 t¡rFt jS t jS n&tˆ  ¡ ƒdkrt‡ fdd„ˆ D ƒd S tdƒ‚d S )Nr   r+   c                s   g | ]}ˆ | r|‘qS r!   r!   )Ú.0Úkey)Úfile_optionsr!   r"   ú
<listcomp>’   s    z8ConfigurationCommand._determine_file.<locals>.<listcomp>zLNeed exactly one file to operate upon (--user, --venv, --global) to perform.)r   ÚUSERr   ÚGLOBALr   ÚVENVr   ÚsumÚvaluesÚosÚpathÚexistsr   r   )r   r9   r(   r!   )r?   r"   r6   �   s    z$ConfigurationCommand._determine_filec             C   s>   | j |ddd� x(t| j ¡ ƒD ]\}}t d||¡ q W d S )Nr#   r   )Únz%s=%r)Ú_get_n_argsr5   r   Úitemsr1   Úinfo)r   r9   r   r>   Úvaluer!   r!   r"   r,   ™   s    z ConfigurationCommand.list_valuesc             C   s,   | j |ddd�}| j |¡}t d|¡ d S )Nz
get [name]r+   )rI   z%s)rJ   r   Ú	get_valuer1   rL   )r   r9   r   r>   rM   r!   r!   r"   r.   Ÿ   s    zConfigurationCommand.get_namec             C   s.   | j |ddd�\}}| j ||¡ |  ¡  d S )Nzset [name] [value]é   )rI   )rJ   r   Ú	set_valueÚ_save_configuration)r   r9   r   r>   rM   r!   r!   r"   r/   ¥   s    z#ConfigurationCommand.set_name_valuec             C   s(   | j |ddd�}| j |¡ |  ¡  d S )Nzunset [name]r+   )rI   )rJ   r   Úunset_valuerQ   )r   r9   r   r>   r!   r!   r"   r0   «   s    zConfigurationCommand.unset_namec          
   C   sp   |   |¡}| j ¡ }|d kr$tdƒ‚yt ||g¡ W n4 tjk
rj } ztd |j¡ƒ‚W d d }~X Y 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ÚCalledProcessErrorr3   Ú
returncode)r   r9   r   r   Úfnamer;   r!   r!   r"   r-   ±   s    

z#ConfigurationCommand.open_in_editorc             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)Úlenr3   r	   r   )r   r   ÚexamplerI   Úmsgr!   r!   r"   rJ   À   s    z ConfigurationCommand._get_n_argsc             C   s>   y| j  ¡  W n* tk
r8   tjddd� tdƒ‚Y nX d S )Nz:Unable to save configuration. Please report this as a bug.r+   )Úexc_infozInternal Error.)r   ÚsaveÚ	Exceptionr1   r2   r   )r   r!   r!   r"   rQ   Ï   s    z(ConfigurationCommand._save_configurationc             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 )NÚVISUALÚEDITORz"Could not determine editor to use.)r   rF   Úenvironr   )r   r9   r!   r!   r"   rS   Û   s    




z&ConfigurationCommand._determine_editor)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚnameÚusageÚsummaryr   r<   r6   r,   r.   r/   r0   r-   rJ   rQ   rS   Ú__classcell__r!   r!   )r    r"   r
      s   *+r
   )ÚloggingrF   rU   Úpip._internal.basecommandr   Úpip._internal.configurationr   r   Úpip._internal.exceptionsr   Úpip._internal.locationsr   Úpip._internal.status_codesr   r   Úpip._internal.utils.miscr	   Ú	getLoggerrc   r1   r
   r!   r!   r!   r"   Ú<module>   s   
