/usr/share/pyshared/spyderlib/utils/environ.py is in python-spyderlib 2.1.9-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | # -*- coding: utf-8 -*-
#
# Copyright © 2009-2010 Pierre Raybaut
# Licensed under the terms of the MIT License
# (see spyderlib/__init__.py for details)
"""
Environment variable utilities
"""
from spyderlib.qt.QtGui import QDialog, QMessageBox
import os
# Local imports
from spyderlib.baseconfig import _
from spyderlib.widgets.dicteditor import DictEditor
def envdict2listdict(envdict):
"""Dict --> Dict of lists"""
sep = os.path.pathsep
for key in envdict:
if sep in envdict[key]:
envdict[key] = [path.strip() for path in envdict[key].split(sep)]
return envdict
def listdict2envdict(listdict):
"""Dict of lists --> Dict"""
for key in listdict:
if isinstance(listdict[key], list):
listdict[key] = os.path.pathsep.join(listdict[key])
return listdict
class RemoteEnvDialog(DictEditor):
"""Remote process environment variables Dialog"""
def __init__(self, get_environ_func, set_environ_func, parent=None):
super(RemoteEnvDialog, self).__init__(parent)
self.setup(envdict2listdict(get_environ_func()),
title="os.environ", width=600, icon='environ.png')
self.set_environ = set_environ_func
def accept(self):
"""Reimplement Qt method"""
self.set_environ(listdict2envdict(self.get_value()))
QDialog.accept(self)
class EnvDialog(RemoteEnvDialog):
"""Environment variables Dialog"""
def __init__(self):
def get_environ_func():
return dict(os.environ)
def set_environ_func(env):
os.environ = env
RemoteEnvDialog.__init__(self, get_environ_func, set_environ_func)
try:
#---- Windows platform
from _winreg import (OpenKey, EnumValue, QueryInfoKey,
SetValueEx, QueryValueEx)
from _winreg import HKEY_CURRENT_USER, KEY_SET_VALUE, REG_EXPAND_SZ
def get_user_env():
"""Return HKCU (current user) environment variables"""
reg = dict()
key = OpenKey(HKEY_CURRENT_USER, "Environment")
for index in range(0, QueryInfoKey(key)[1]):
try:
value = EnumValue(key, index)
reg[value[0]] = value[1]
except:
break
return envdict2listdict(reg)
def set_user_env(reg, parent=None):
"""Set HKCU (current user) environment variables"""
reg = listdict2envdict(reg)
types = dict()
key = OpenKey(HKEY_CURRENT_USER, "Environment")
for name in reg:
try:
_x, types[name] = QueryValueEx(key, name)
except WindowsError:
types[name] = REG_EXPAND_SZ
key = OpenKey(HKEY_CURRENT_USER, "Environment", 0, KEY_SET_VALUE)
for name in reg:
SetValueEx(key, name, 0, types[name], reg[name])
try:
from win32gui import SendMessageTimeout
from win32con import (HWND_BROADCAST, WM_SETTINGCHANGE,
SMTO_ABORTIFHUNG)
SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0,
"Environment", SMTO_ABORTIFHUNG, 5000)
except ImportError:
QMessageBox.warning(parent, _("Warning"),
_("Module <b>pywin32 was not found</b>.<br>"
"Please restart this Windows <i>session</i> "
"(not the computer) for changes to take effect."))
class WinUserEnvDialog(DictEditor):
"""Windows User Environment Variables Editor"""
def __init__(self, parent=None):
super(WinUserEnvDialog, self).__init__(parent)
self.setup(get_user_env(),
title="HKEY_CURRENT_USER\Environment", width=600)
if parent is None:
parent = self
QMessageBox.warning(parent, _("Warning"),
_("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 applica"
"tion (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)"))
def accept(self):
"""Reimplement Qt method"""
set_user_env( listdict2envdict(self.get_value()), parent=self )
QDialog.accept(self)
except ImportError:
#---- Other platforms
pass
def main():
"""Run Windows environment variable editor"""
from spyderlib.utils.qthelpers import qapplication
app = qapplication()
dialog = WinUserEnvDialog()
dialog.show()
app.exec_()
if __name__ == "__main__":
main()
|