/usr/share/pyshared/spyderlib/widgets/pathmanager.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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | # -*- coding: utf-8 -*-
#
# Copyright © 2009-2010 Pierre Raybaut
# Licensed under the terms of the MIT License
# (see spyderlib/__init__.py for details)
"""Spyder path manager"""
from __future__ import with_statement
from spyderlib.qt.QtGui import (QDialog, QListWidget, QDialogButtonBox,
QVBoxLayout, QHBoxLayout, QMessageBox,
QListWidgetItem)
from spyderlib.qt.QtCore import Qt, SIGNAL, SLOT
from spyderlib.qt.compat import getexistingdirectory
import os
import sys
import os.path as osp
# Local imports
from spyderlib.utils.qthelpers import get_std_icon, create_toolbutton
from spyderlib.baseconfig import _
from spyderlib.config import get_icon
class PathManager(QDialog):
def __init__(self, parent=None, pathlist=None, ro_pathlist=None, sync=True):
QDialog.__init__(self, parent)
# Destroying the C++ object right after closing the dialog box,
# otherwise it may be garbage-collected in another QThread
# (e.g. the editor's analysis thread in Spyder), thus leading to
# a segmentation fault on UNIX or an application crash on Windows
self.setAttribute(Qt.WA_DeleteOnClose)
assert isinstance(pathlist, list)
self.pathlist = pathlist
if ro_pathlist is None:
ro_pathlist = []
self.ro_pathlist = ro_pathlist
self.last_path = os.getcwdu()
self.setWindowTitle(_("PYTHONPATH manager"))
self.setWindowIcon(get_icon('pythonpath.png'))
self.resize(500, 300)
self.selection_widgets = []
layout = QVBoxLayout()
self.setLayout(layout)
top_layout = QHBoxLayout()
layout.addLayout(top_layout)
self.toolbar_widgets1 = self.setup_top_toolbar(top_layout)
self.listwidget = QListWidget(self)
self.connect(self.listwidget, SIGNAL("currentRowChanged(int)"),
self.refresh)
layout.addWidget(self.listwidget)
bottom_layout = QHBoxLayout()
layout.addLayout(bottom_layout)
self.sync_button = None
self.toolbar_widgets2 = self.setup_bottom_toolbar(bottom_layout, sync)
# Buttons configuration
bbox = QDialogButtonBox(QDialogButtonBox.Close)
self.connect(bbox, SIGNAL("rejected()"), SLOT("reject()"))
bottom_layout.addWidget(bbox)
self.update_list()
self.refresh()
def _add_widgets_to_layout(self, layout, widgets):
layout.setAlignment(Qt.AlignLeft)
for widget in widgets:
layout.addWidget(widget)
def setup_top_toolbar(self, layout):
toolbar = []
movetop_button = create_toolbutton(self,
text=_("Move to top"),
icon=get_icon('2uparrow.png'),
triggered=lambda: self.move_to(absolute=0),
text_beside_icon=True)
toolbar.append(movetop_button)
moveup_button = create_toolbutton(self,
text=_("Move up"),
icon=get_icon('1uparrow.png'),
triggered=lambda: self.move_to(relative=-1),
text_beside_icon=True)
toolbar.append(moveup_button)
movedown_button = create_toolbutton(self,
text=_("Move down"),
icon=get_icon('1downarrow.png'),
triggered=lambda: self.move_to(relative=1),
text_beside_icon=True)
toolbar.append(movedown_button)
movebottom_button = create_toolbutton(self,
text=_("Move to bottom"),
icon=get_icon('2downarrow.png'),
triggered=lambda: self.move_to(absolute=1),
text_beside_icon=True)
toolbar.append(movebottom_button)
self.selection_widgets.extend(toolbar)
self._add_widgets_to_layout(layout, toolbar)
return toolbar
def setup_bottom_toolbar(self, layout, sync=True):
toolbar = []
add_button = create_toolbutton(self, text=_("Add path"),
icon=get_icon('edit_add.png'),
triggered=self.add_path,
text_beside_icon=True)
toolbar.append(add_button)
remove_button = create_toolbutton(self, text=_("Remove path"),
icon=get_icon('edit_remove.png'),
triggered=self.remove_path,
text_beside_icon=True)
toolbar.append(remove_button)
self.selection_widgets.append(remove_button)
self._add_widgets_to_layout(layout, toolbar)
layout.addStretch(1)
if os.name == 'nt' and sync:
self.sync_button = create_toolbutton(self,
text=_("Synchronize..."),
icon=get_icon('synchronize.png'), triggered=self.synchronize,
tip=_("Synchronize Spyder's path list with PYTHONPATH "
"environment variable"),
text_beside_icon=True)
layout.addWidget(self.sync_button)
return toolbar
def synchronize(self):
"""
Synchronize Spyder's path list with PYTHONPATH environment variable
Only apply to: current user, on Windows platforms
"""
answer = QMessageBox.question(self, _("Synchronize"),
_("This will synchronize Spyder's path list with "
"<b>PYTHONPATH</b> environment variable for current user, "
"allowing you to run your Python modules outside Spyder "
"without having to configure sys.path. "
"<br>Do you want to clear contents of PYTHONPATH before "
"adding Spyder's path list?"),
QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel)
if answer == QMessageBox.Cancel:
return
elif answer == QMessageBox.Yes:
remove = True
else:
remove = False
from spyderlib.utils.environ import (get_user_env, set_user_env,
listdict2envdict)
env = get_user_env()
if remove:
ppath = self.pathlist+self.ro_pathlist
else:
ppath = env.get('PYTHONPATH', [])
if not isinstance(ppath, list):
ppath = [ppath]
ppath = [path for path in ppath
if path not in (self.pathlist+self.ro_pathlist)]
ppath.extend(self.pathlist+self.ro_pathlist)
env['PYTHONPATH'] = ppath
set_user_env( listdict2envdict(env), parent=self )
def get_path_list(self):
"""Return path list (does not include the read-only path list)"""
return self.pathlist
def update_list(self):
"""Update path list"""
self.listwidget.clear()
for name in self.pathlist+self.ro_pathlist:
item = QListWidgetItem(name)
item.setIcon(get_std_icon('DirClosedIcon'))
if name in self.ro_pathlist:
item.setFlags(Qt.NoItemFlags)
self.listwidget.addItem(item)
self.refresh()
def refresh(self, row=None):
"""Refresh widget"""
for widget in self.selection_widgets:
widget.setEnabled(self.listwidget.currentItem() is not None)
not_empty = self.listwidget.count() > 0
if self.sync_button is not None:
self.sync_button.setEnabled(not_empty)
def move_to(self, absolute=None, relative=None):
index = self.listwidget.currentRow()
if absolute is not None:
if absolute:
new_index = len(self.pathlist)-1
else:
new_index = 0
else:
new_index = index + relative
new_index = max(0, min(len(self.pathlist)-1, new_index))
path = self.pathlist.pop(index)
self.pathlist.insert(new_index, path)
self.update_list()
self.listwidget.setCurrentRow(new_index)
def remove_path(self):
answer = QMessageBox.warning(self, _("Remove path"),
_("Do you really want to remove selected path?"),
QMessageBox.Yes | QMessageBox.No)
if answer == QMessageBox.Yes:
self.pathlist.pop(self.listwidget.currentRow())
self.update_list()
def add_path(self):
self.emit(SIGNAL('redirect_stdio(bool)'), False)
directory = getexistingdirectory(self, _("Select directory"),
self.last_path)
self.emit(SIGNAL('redirect_stdio(bool)'), True)
if directory:
directory = osp.abspath(directory)
self.last_path = directory
if directory in self.pathlist:
answer = QMessageBox.question(self, _("Add path"),
_("This directory is already included in Spyder path "
"list.<br>Do you want to move it to the top of "
"the list?"),
QMessageBox.Yes | QMessageBox.No)
if answer == QMessageBox.Yes:
self.pathlist.remove(directory)
else:
return
self.pathlist.insert(0, directory)
self.update_list()
def test():
"""Run path manager test"""
from spyderlib.utils.qthelpers import qapplication
_app = qapplication()
test = PathManager(None, sys.path[:-10], sys.path[-10:])
test.exec_()
print test.get_path_list()
if __name__ == "__main__":
test()
|