B
    nb\t                 @   s   d Z ddlZddlmZmZ ddlmZ ddlmZ	 ddl
mZ dd Zd	d
 ZG dd deZG dd deZy2ddlmZ dd ZdddZG dd deZW n ek
r   Y nX dd Zedkre  dS )z 
Environment variable utilities
    N)QDialogQMessageBox)_)icon_manager)CollectionsEditorc             C   sB   t jj}x4| D ],}|| | krdd | | |D | |< qW | S )zDict --> Dict of listsc             S   s   g | ]}|  qS  )strip).0pathr   r   3lib/python3.7/site-packages/spyder/utils/environ.py
<listcomp>   s    z$envdict2listdict.<locals>.<listcomp>)osr
   pathsepsplit)Zenvdictsepkeyr   r   r   envdict2listdict   s
    
 r   c             C   s6   x0| D ](}t | | trtjj| | | |< qW | S )zDict of lists --> Dict)
isinstancelistr   r
   r   join)Zlistdictr   r   r   r   listdict2envdict    s    
r   c                   s"   e Zd ZdZd fdd	Z  ZS )RemoteEnvDialogz,Remote process environment variables dialog.Nc          
      s~   t t| | y&| jt|tdddtdd W nB tk
rx } z$t	
|tdtd|t	j W d d }~X Y nX d S )NzEnvironment variablesi  Tenviron)titlewidthreadonlyiconWarningzdAn error occurred while trying to show your environment variables. The error was<br><br><tt>{0}</tt>)superr   __init__setupr   r   imar   	Exceptionr   warningformatZOk)selfr   parente)	__class__r   r   r   +   s    zRemoteEnvDialog.__init__)N)__name__
__module____qualname____doc__r   __classcell__r   r   )r(   r   r   (   s   r   c               @   s   e Zd ZdZdd ZdS )	EnvDialogzEnvironment variables Dialogc             C   s   t | ttj d S )N)r   r   dictr   r   )r%   r   r   r   r   B   s    zEnvDialog.__init__N)r)   r*   r+   r,   r   r   r   r   r   r.   @   s   r.   )winregc              C   sj   t  } ttjd}xLtdt|d D ]4}y t||}|d | |d < W q*   P Y q*X q*W t| S )z0Return HKCU (current user) environment variablesEnvironmentr      )r/   r0   OpenKeyHKEY_CURRENT_USERrangeZQueryInfoKeyZ	EnumValuer   )regr   indexvaluer   r   r   get_user_envJ   s    r9   c       
   	   C   s  t | } t }ttjd}xD| D ]<}yt||\}||< W q" tk
r\   tj||< Y q"X q"W ttjddtj}x&| D ]}t	||d|| | |  q|W y6ddl
m} ddlm}m}m}	 |||dd|	d W n* tk
r   t|tdtd Y nX dS )	z-Set HKCU (current user) environment variablesr1   r   )SendMessageTimeout)HWND_BROADCASTWM_SETTINGCHANGESMTO_ABORTIFHUNGi  r   zModule <b>pywin32 was not found</b>.<br>Please restart this Windows <i>session</i> (not the computer) for changes to take effect.N)r   r/   r0   r3   r4   ZQueryValueExZWindowsErrorZREG_EXPAND_SZZKEY_SET_VALUEZ
SetValueExZwin32guir:   Zwin32conr;   r<   r=   r"   r   r#   r   )
r6   r&   typesr   nameZ_xr:   r;   r<   r=   r   r   r   set_user_envV   s(    

r@   c                   s*   e Zd ZdZd fdd	Zdd Z  ZS )WinUserEnvDialogz)Windows User Environment Variables EditorNc                sH   t t| | | jt ddd |d kr.| }t|tdtd d S )NzHKEY_CURRENT_USER\EnvironmentiX  )r   r   r   a  If you accept changes, this will modify the current user environment variables directly <b>in Windows registry</b>. Use it with precautions, at your own risks.<br><br>Note that for changes to take effect, you will need to restart the parent process of this application (simply restart Spyder if you have executed it from a Windows shortcut, otherwise restart any application from which you may have executed it, like <i>Python(x,y) Home</i> for example))r   rA   r   r    r9   r   r#   r   )r%   r&   )r(   r   r   r   r   s    
zWinUserEnvDialog.__init__c             C   s"   t t|  | d t|  dS )zReimplement Qt method)r&   N)r@   r   Z	get_valuer   accept)r%   r   r   r   rB      s    zWinUserEnvDialog.accept)N)r)   r*   r+   r,   r   rB   r-   r   r   )r(   r   rA   p   s   rA   c              C   s>   ddl m}  |  }tjdkr$t }nt }|  |  dS )z'Run Windows environment variable editorr   )qapplicationntN)Zspyder.utils.qthelpersrC   r   r?   rA   r.   ZshowZexec_)rC   ZappZdialogr   r   r   main   s    
rE   __main__)N)r,   r   Zqtpy.QtWidgetsr   r   Zspyder.config.baser   Zspyder.utilsr   r!   Z1spyder.widgets.variableexplorer.collectionseditorr   r   r   r   r.   Zspyder.py3compatr0   r9   r@   rA   r"   rE   r)   r   r   r   r   <module>	   s&   	
