a
    a                     @   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
mZmZmZ ddlmZ ddlmZ d	d
 Zdd Zdd ZG dd deZG dd deZz2ddl	mZ dd ZdddZG dd deZW n ey   Y n0 dd Zedkre  dS )z!
Environment variable utilities.
    N)QDialogQMessageBox)_)CollectionsEditor)PY2	iteritemsto_text_stringto_binary_string)ima)to_unicode_from_fsc                 C   s>   t jj}| D ],}|| | v rdd | | |D | |< q| S )zDict --> Dict of listsc                 S   s   g | ]}|  qS  )strip).0pathr   r   3lib/python3.9/site-packages/spyder/utils/environ.py
<listcomp>       z$envdict2listdict.<locals>.<listcomp>)osr   pathsepsplit)Zenvdictsepkeyr   r   r   envdict2listdict   s
    r   c                 C   s2   | D ](}t | | trtjj| | | |< q| S )zDict of lists --> Dict)
isinstancelistr   r   r   join)Zlistdictr   r   r   r   listdict2envdict"   s    r   c                 C   s   |   }t| D ]n\}}trrzt|}W n6 ty^   zt|}W n tyX   d}Y n0 Y n0 t|dd||< qt|||< q|S )z
    Remove non-ascii entries from a dictionary of environments variables.

    The values will be converted to strings or bytes (on Python 2). If an
    exception is raised, an empty string will be used.
     zutf-8)encoding)copyr   r   r   UnicodeDecodeErrorr   	Exceptionr	   )Zenv_varsZnew_env_varsr   varZunicode_varr   r   r   	clean_env*   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| | z$| jt|tddtdd W nD tyx } z,t	
|tdtd|t	j W Y d }~n
d }~0 0 d S )NzEnvironment variablesTenviron)titlereadonlyiconWarningzdAn error occurred while trying to show your environment variables. The error was<br><br><tt>{0}</tt>)superr$   __init__setupr   r   r
   r(   r!   r   warningformatZOk)selfr%   parente	__class__r   r   r+   N   s"    
zRemoteEnvDialog.__init__)N)__name__
__module____qualname____doc__r+   __classcell__r   r   r2   r   r$   K   s   r$   c                   @   s   e Zd ZdZdddZdS )	EnvDialogzEnvironment variables DialogNc                 C   s   t j| ttj|d d S )Nr0   )r$   r+   dictr   r%   r/   r0   r   r   r   r+   d   s    zEnvDialog.__init__)N)r4   r5   r6   r7   r+   r   r   r   r   r9   b   s   r9   )winregc                  C   sj   t  } ttjd}tdt|d D ]8}z t||}|d | |d < W q(   Y  qbY q(0 q(t| S )z0Return HKCU (current user) environment variablesEnvironmentr      )r;   r=   OpenKeyHKEY_CURRENT_USERrangeZQueryInfoKeyZ	EnumValuer   )regr   indexvaluer   r   r   get_user_envl   s    rF   c           
   	   C   s   t | } t }ttjd}| D ]:}zt||\}||< W q  tyX   tj||< Y q 0 q ttjddtj}| D ]}t	||d|| | |  qtz6ddl
m} ddlm}m}m}	 |||dd|	d W n( ty   t|tdtd Y n0 dS )	z-Set HKCU (current user) environment variablesr>   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;   r=   r@   rA   ZQueryValueExZWindowsErrorZREG_EXPAND_SZZKEY_SET_VALUEZ
SetValueExZwin32guirG   Zwin32conrH   rI   rJ   r!   r   r-   r   )
rC   r0   typesr   nameZ_xrG   rH   rI   rJ   r   r   r   set_user_envx   s.    rM   c                       s*   e Zd ZdZd fdd	Zdd Z  ZS )WinUserEnvDialogz)Windows User Environment Variables EditorNc                    sF   t t| | | jt dd |d u r,| }t|tdtd d S )NzHKEY_CURRENT_USER\Environment)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*   rN   r+   r,   rF   r   r-   r   r<   r2   r   r   r+      s    zWinUserEnvDialog.__init__c                 C   s"   t t|  | d t|  dS )zReimplement Qt methodr:   N)rM   r   Z	get_valuer   accept)r/   r   r   r   rO      s    zWinUserEnvDialog.accept)N)r4   r5   r6   r7   r+   rO   r8   r   r   r2   r   rN      s   rN   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.qthelpersrP   r   rL   rN   r9   ZshowZexec_)rP   ZappZdialogr   r   r   main   s    
rR   __main__)N)r7   r   Zqtpy.QtWidgetsr   r   Zspyder.config.baser   Z spyder.widgets.collectionseditorr   Zspyder.py3compatr   r   r   r	   Zspyder.utils.icon_managerr
   Zspyder.utils.encodingr   r   r   r#   r$   r9   r=   rF   rM   rN   r!   rR   r4   r   r   r   r   <module>   s,   	!
