B
    Uvg                 @   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	m
Z
 d dlmZ d dlmZ d dlmZ e eZG dd	 d	eZdS )
    N)Command)ERRORSUCCESS)Configurationkinds)PipError)venv_config_file)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__ E/tmp/pip-install-r_9ig3yj/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.cli.base_commandr   pip._internal.cli.status_codesr   r   pip._internal.configurationr   r   pip._internal.exceptionsr   pip._internal.locationsr   pip._internal.utils.miscr	   	getLoggerrc   r1   r
   r!   r!   r!   r"   <module>   s   
