ó
'^c           @   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
 m Z d  d l m Z d  d l m Z m Z e  j e  Z d e f d     YZ d S(	   i˙˙˙˙N(   t   Command(   t   ERRORt   SUCCESS(   t   Configurationt   get_configuration_filest   kinds(   t   PipError(   t   get_progt   write_outputt   ConfigurationCommandc           B   s}   e  Z d  Z e Z d Z d   Z d   Z d   Z d   Z	 d   Z
 d   Z d   Z d	   Z d
   Z d   Z d   Z RS(   s9  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.
    sâ   
        %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
    c      
   O   sÜ   t  t |   j | |   d  |  _ |  j j d d d d d d d  d d |  j j d	 d d
 d d d t d d |  j j d d d d d d t d d |  j j d d d d d d t d d |  j j	 d |  j  d  S(   Ns   --editort   destt   editort   actiont   storet   defaultt   helps\   Editor to use to edit the file. Uses VISUAL or EDITOR environment variables if not provided.s   --globalt   global_filet
   store_trues+   Use the system-wide configuration file onlys   --usert	   user_files$   Use the user configuration file onlys   --sitet	   site_files3   Use the current environment configuration file onlyi    (
   t   superR	   t   __init__t   Nonet   configurationt   cmd_optst
   add_optiont   Falset   parsert   insert_option_group(   t   selft   argst   kwargs(    (    sA   /tmp/pip-build-eiI0Wo/pip/pip/_internal/commands/configuration.pyR   0   s6    					c         C   s<  i |  j  d 6|  j d 6|  j d 6|  j d 6|  j d 6} | sO | d | k rx t j d j d j t	 |     t
 S| d } y |  j | d	 | d k } Wn( t k
 rË } t j | j d  t
 SXt d
 | j d |  |  _ |  j j   y | | | | d  Wn( t k
 r7} t j | j d  t
 SXt S(   Nt   listt   editt   gett   sett   unseti    s   Need an action ({}) to perform.s   , t
   need_valuet   isolatedt	   load_onlyi   (   R"   R#   R$   R!   (   t   list_valuest   open_in_editort   get_namet   set_name_valuet
   unset_namet   loggert   errort   formatt   joint   sortedR   t   _determine_fileR   R   R   t   isolated_modeR   t   loadR   (   R   t   optionsR   t   handlersR   R'   t   e(    (    sA   /tmp/pip-build-eiI0Wo/pip/pip/_internal/commands/configuration.pyt   runZ   s4    




c         C   s˝   g  t  j | j f t  j | j f t  j | j f f D] \ } } | r4 | ^ q4 } | s | sb d  St d   t	   t  j D  r t  j St  j Sn t
 |  d k r­ | d St d   d  S(   Nc         s   s!   |  ] } t  j j |  Vq d  S(   N(   t   ost   patht   exists(   t   .0t   site_config_file(    (    sA   /tmp/pip-build-eiI0Wo/pip/pip/_internal/commands/configuration.pys	   <genexpr>   s   i   i    sL   Need exactly one file to operate upon (--user, --site, --global) to perform.(   R   t   USERR   t   GLOBALR   t   SITER   R   t   anyR   t   lenR   (   R   R5   R%   t   keyt   valuet   file_options(    (    sA   /tmp/pip-build-eiI0Wo/pip/pip/_internal/commands/configuration.pyR2      s"    
c         C   sP   |  j  | d d d x3 t |  j j    D] \ } } t d | |  q, Wd  S(   NR    t   ni    s   %s=%r(   t   _get_n_argsR1   R   t   itemsR   (   R   R5   R   RC   RD   (    (    sA   /tmp/pip-build-eiI0Wo/pip/pip/_internal/commands/configuration.pyR(      s    "c         C   s;   |  j  | d d d } |  j j |  } t d |  d  S(   Ns
   get [name]RF   i   s   %s(   RG   R   t	   get_valueR   (   R   R5   R   RC   RD   (    (    sA   /tmp/pip-build-eiI0Wo/pip/pip/_internal/commands/configuration.pyR*   Ľ   s    c         C   s?   |  j  | d d d \ } } |  j j | |  |  j   d  S(   Ns   set [name] [value]RF   i   (   RG   R   t	   set_valuet   _save_configuration(   R   R5   R   RC   RD   (    (    sA   /tmp/pip-build-eiI0Wo/pip/pip/_internal/commands/configuration.pyR+   Ť   s    c         C   s6   |  j  | d d d } |  j j |  |  j   d  S(   Ns   unset [name]RF   i   (   RG   R   t   unset_valueRK   (   R   R5   R   RC   (    (    sA   /tmp/pip-build-eiI0Wo/pip/pip/_internal/commands/configuration.pyR,   ą   s    c         C   s   |  j  |  } |  j j   } | d  k r9 t d   n  y t j | | g  Wn. t j k
 r } t d j | j	    n Xd  S(   Ns%   Could not determine appropriate file.s*   Editor Subprocess exited with exit code {}(
   t   _determine_editorR   t   get_file_to_editR   R   t
   subprocesst
   check_callt   CalledProcessErrorR/   t
   returncode(   R   R5   R   R   t   fnameR7   (    (    sA   /tmp/pip-build-eiI0Wo/pip/pip/_internal/commands/configuration.pyR)   ˇ   s    c         C   sU   t  |  | k r9 d j | t   |  } t |   n  | d k rM | d S| Sd S(   sJ   Helper to make sure the command got the right number of arguments
        sJ   Got unexpected number of arguments, expected {}. (example: "{} config {}")i   i    N(   RB   R/   R   R   (   R   R   t   exampleRF   t   msg(    (    sA   /tmp/pip-build-eiI0Wo/pip/pip/_internal/commands/configuration.pyRG   Ć   s    c         C   sH   y |  j  j   Wn0 t k
 rC t j d d d t d   n Xd  S(   Ns:   Unable to save configuration. Please report this as a bug.t   exc_infoi   s   Internal Error.(   R   t   savet	   ExceptionR-   R.   R   (   R   (    (    sA   /tmp/pip-build-eiI0Wo/pip/pip/_internal/commands/configuration.pyRK   Ő   s    c         C   sZ   | j  d  k	 r | j  Sd t j k r0 t j d Sd t j k rJ t j d St d   d  S(   Nt   VISUALt   EDITORs"   Could not determine editor to use.(   R   R   R9   t   environR   (   R   R5   (    (    sA   /tmp/pip-build-eiI0Wo/pip/pip/_internal/commands/configuration.pyRM   á   s    (   t   __name__t
   __module__t   __doc__t   Truet   ignore_require_venvt   usageR   R8   R2   R(   R*   R+   R,   R)   RG   RK   RM   (    (    (    sA   /tmp/pip-build-eiI0Wo/pip/pip/_internal/commands/configuration.pyR	      s   	*	+								(   t   loggingR9   RO   t   pip._internal.cli.base_commandR    t   pip._internal.cli.status_codesR   R   t   pip._internal.configurationR   R   R   t   pip._internal.exceptionsR   t   pip._internal.utils.miscR   R   t	   getLoggerR\   R-   R	   (    (    (    sA   /tmp/pip-build-eiI0Wo/pip/pip/_internal/commands/configuration.pyt   <module>   s   