
    G@d%                     <   d Z ddlZddlmZ ddlZddlZ	 ddlZn# e$ r Y nw xY w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 d	 Zd
 Zd Zd ZddZddZd Z G d de          Z G d de          Z G d de          Zd Zedk    r e             dS dS )z!
Environment variable utilities.
    N)Path)QMessageBox)_running_in_ci)CollectionsEditor)ima)run_shell_commandc                     t           j        j        }|                                 D ]@\  }}t	          |t
                    r&||v r"d |                    |          D             | |<   A| S )zDict --> Dict of listsc                 6    g | ]}|                                 S  )strip).0paths     4lib/python3.11/site-packages/spyder/utils/environ.py
<listcomp>z$envdict2listdict.<locals>.<listcomp>$   s     DDDTDJJLLDDD    )osr   pathsepitems
isinstancestrsplit)envdictsepkeyvals       r   envdict2listdictr      sj    
'/CMMOO E ESc3 	EC3JJDDSYYs^^DDDGCLNr   c                     |                                  D ]A\  }}t          |t                    r't          j        j                            |          | |<   B| S )zDict of lists --> Dict)r   r   listr   r   r   join)listdictr   r   s      r   listdict2envdictr"   (   sR    NN$$ 6 6Sc4   	6GO0055HSMOr   c                     	 t           j        dk    rbt          j        t          j        d          t          j                  d         } t          fdt          |           D                       }nkt           j        	                    dd          }| dt          j         d}t          |i d	
          }|                                \  }}t          |d          }n# t          $ r i cY S w xY w|S )z
    Get user environment variables from a subprocess.

    Returns
    -------
    env_var : dict
        Key-value pairs of environment variables.
    ntEnvironment   c                 J    g | ]}t          j        |          d d          S )N   )winreg	EnumValue)r   kr   s     r   r   z2get_user_environment_variables.<locals>.<listcomp>>   s/    III!!#q))"1"-IIIr   SHELL	/bin/bashz -l -c "z* -c 'import os; print(dict(os.environ))'" T)envtextN)r   namer)   OpenKeyHKEY_CURRENT_USERQueryInfoKeydictrangeenvirongetsys
executabler	   communicateeval	Exception)
num_valuesenv_varshellcmdprocstdoutstderrr   s          @r   get_user_environment_variablesrD   0   s   7d??.!9=IIC,S11!4JIIIIuZ7H7HIII GG JNN7K88E T T~T T T  %Sbt<<<D!--//NFF64((G   			 Ns   CC! !C0/C0c                  <    t                      } t          |           S )z<Return current user environment variables with parsed values)rD   r   )env_dicts    r   get_user_envrG   O   s    -//HH%%%r   c           	         t          |           }t          j        dk    r%t                      }t	          j        t          j        d          }|D ]=}	 t	          j        ||          \  }||<   # t          $ r t          j	        ||<   Y :w xY wt	          j        t          j        ddt          j
                  }|D ]&}t	          j        ||d||         ||                    '	 ddlm} ddlm}m}	m}
  |||	dd|
d           dS # t$          $ r4 t'          j        |t+          d          t+          d                     Y dS w xY wt          j        d	k    rPt-                      rBd
                    d |                                D                       }t3          |           dS t5          dt          j                  )z]
    Set user environment variables via HKCU (Windows) or shell startup file
    (Unix).
    r$   r%   r   )SendMessageTimeout)HWND_BROADCASTWM_SETTINGCHANGESMTO_ABORTIFHUNGi  WarningzModule <b>pywin32 was not found</b>.<br>Please restart this Windows <i>session</i> (not the computer) for changes to take effect.posix
c                 $    g | ]\  }}d | d| S )zexport =r   )r   r+   vs      r   r   z set_user_env.<locals>.<listcomp>v   s-    III1+A++++IIIr   zNot implemented for platform %sN)r"   r   r0   r4   r)   r1   r2   QueryValueExWindowsErrorREG_EXPAND_SZKEY_SET_VALUE
SetValueExwin32guirI   win32conrJ   rK   rL   r<   r   warningr   r   r    r   amend_user_shell_initNotImplementedError)r.   parentrF   typesr   r0   _xrI   rJ   rK   rL   r/   s               r   set_user_envr`   U   s-   
  $$H	w$nV5}EE 	3 	3D3"("5c4"@"@E$KK 3 3 3$2d3nV5}a#13 3 	I 	IDc4E$K$HHHH	3333334 4 4 4 4 4 4 4 4 4~/?,.>F F F F F 	 	 	) C D D     	 
G			yyII8H8HIIIJJd#####!"CRWMMMs$   A//B
B# D :EE Fc                    t           j        dk    rdS t          t           j        d                   }t           j                            dd          }d|v rg d}nd|v rd	d
g}nt          | d          |D ]}||z  }|                                r n|                                r|                                nd}d}d}	|r!|                                r||v r|	|v rd}
ndS | d| z   d|	 z   }
||v r,|	|v r(t          j	        | d|	 |
|t          j
                  }n|                                dz   |
z   }|                    |                                dz              dS )z;Set user environment variable for pytests on Unix platformsr$   NHOMEr,   r-   bash)z.bash_profilez.bash_loginz.profilezshz	.zprofilez.zshrcz not supported.ra   z# <<<< Spyder Environment <<<<z# >>>> Spyder Environment >>>>rO   z(.*))flagsz

)r   r0   r   r6   r7   r<   exists	read_textresubDOTALLrstrip
write_text)r/   restorerc   r,   
init_filesfile	init_filescriptm1m2new_text_scripts               r   r[   r[   |   s   	w$
6"##DJNN7K00EAAA

	%!8,

5111222  4K	 	E	 '0&6&6&8&8@Y  """bF	)B	)B 0 	2<<B&LLHHF999t#i2ii/	V||f&BB(F")LLL--//F*X5))D011111r   c                     |                                  }|                                 D ]\  }}t          |          ||<   |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.
    )copyr   r   )env_varsnew_env_varsr   vars       r   	clean_envr|      sG     ==??LNN$$ % %SHHSr   c                   <     e Zd ZdZd ed          df fd	Z xZS )RemoteEnvDialogz,Remote process environment variables dialog.NzEnvironment variablesTc                    t                                          |           	 |                     t          |          ||t	          j        d                     d S # t          $ rY}t          j        |t          d          t          d          
                    |          t          j                   Y d }~d S d }~ww xY w)Nr6   )titlereadonlyiconrM   zdAn error occurred while trying to show your environment variables. The error was<br><br><tt>{0}</tt>)super__init__setupr   r   r   r<   r   rZ   r   formatOk)selfr6   r]   r   r   e	__class__s         r   r   zRemoteEnvDialog.__init__   s       	JJ ))!Xi((	        	 	 	) ! " ""(&))        	s   8A 
C(AB<<C)__name__
__module____qualname____doc__r   r   __classcell__r   s   @r   r~   r~      sV        66'+q011D         r   r~   c                       e Zd ZdZddZdS )	EnvDialogzEnvironment variables DialogNc                 n    t                               | t          t          j                  |           d S )Nr]   )r~   r   r4   r   r6   )r   r]   s     r   r   zEnvDialog.__init__   s-      tBJ'7'7 GGGGGr   N)r   r   r   r   r   r   r   r   r   r      s4        &&H H H H H Hr   r   c                   .     e Zd ZdZd fd	Z fdZ xZS )UserEnvDialogz(User Environment Variables Viewer/EditorNc                 X   t          d          }d}t          j        dk    rt          d          }d}t                                          t                      |||           t          j        dk    r6|| }t          j        |t          d          t          d                     d S d S )NzUser environment variablesTr$   z.User environment variables in Windows registryFrM   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   r   r0   r   r   rG   r   rZ   )r   r]   r   r   r   s       r   r   zUserEnvDialog.__init__   s    .//7d??GHHEHAAA7d??~) 	> 	? 	?     ?r   c                     t           j        dk    r0t          t          |                                           |            t                                                       dS )zReimplement Qt methodr$   r   N)r   r0   r`   r"   	get_valuer   accept)r   r   s    r   r   zUserEnvDialog.accept   sN    7d??)$..*:*:;;DIIIIr   r   )r   r   r   r   r   r   r   r   s   @r   r   r      s\        22     4        r   r   c                      ddl } ddlm}  |            }t                      }|                                  | j        |                                           dS )z'Run Windows environment variable editorr   N)qapplication)r8   spyder.utils.qthelpersr   r   showexitexec)r8   r   r   dlgs       r   testr      s^    JJJ333333A
//CHHJJJCHSXXZZr   __main__r   )ra   F)r   r   pathlibr   ri   r8   r)   r<   qtpy.QtWidgetsr   spyder.config.baser   r    spyder.widgets.collectionseditorr   spyder.utils.icon_managerr   spyder.utils.programsr	   r   r"   rD   rG   r`   r[   r|   r~   r   r   r   r   r   r   r   <module>r      s   
 
			       				 



	MMMM 	 	 	D	 ' & & & & & 0 / / / / / / / > > > > > > ) ) ) ) ) ) 3 3 3 3 3 3      >& & &$N $N $N $NN#2 #2 #2 #2L      '   0H H H H H H H H! ! ! ! !O ! ! !H   zDFFFFF s    ##