/usr/share/pyshared/spyderlib/plugins/runconfig.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 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 | # -*- coding: utf-8 -*-
#
# Copyright © 2010 Pierre Raybaut
# Licensed under the terms of the MIT License
# (see spyderlib/__init__.py for details)
"""Run configurations related dialogs and widgets and data models"""
from spyderlib.qt.QtGui import (QVBoxLayout, QDialog, QWidget, QGroupBox,
QLabel, QPushButton, QCheckBox, QLineEdit,
QComboBox, QHBoxLayout, QDialogButtonBox,
QStackedWidget, QGridLayout, QSizePolicy,
QRadioButton, QMessageBox)
from spyderlib.qt.QtCore import SIGNAL, SLOT, Qt
from spyderlib.qt.compat import getexistingdirectory
import os
import os.path as osp
# Local imports
from spyderlib.baseconfig import _
from spyderlib.config import get_icon, CONF
from spyderlib.utils.qthelpers import get_std_icon
class RunConfiguration(object):
"""Run configuration"""
def __init__(self, fname=None):
self.args = ''
self.args_enabled = False
self.wdir = None
self.wdir_enabled = False
if fname is not None:
self.wdir = osp.dirname(fname)
self.wdir_enabled = True
self.current = False
self.systerm = False
self.interact = False
self.python_args = ''
self.python_args_enabled = False
def set(self, options):
self.args = options.get('args', '')
self.args_enabled = options.get('args/enabled', False)
self.wdir = options.get('workdir', os.getcwdu())
self.wdir_enabled = options.get('workdir/enabled', False)
self.current = options.get('current', False)
self.systerm = options.get('systerm', False)
self.interact = options.get('interact', False)
self.python_args = options.get('python_args', '')
self.python_args_enabled = options.get('python_args/enabled', False)
def get(self):
return {
'args/enabled': self.args_enabled,
'args': self.args,
'workdir/enabled': self.wdir_enabled,
'workdir': self.wdir,
'current': self.current,
'systerm': self.systerm,
'interact': self.interact,
'python_args/enabled': self.python_args_enabled,
'python_args': self.python_args,
}
def get_working_directory(self):
if self.wdir_enabled:
return self.wdir
else:
return ''
def get_arguments(self):
if self.args_enabled:
return self.args
else:
return ''
def get_python_arguments(self):
if self.python_args_enabled:
return self.python_args
else:
return ''
def _get_run_configurations():
history_count = CONF.get('run', 'history', 20)
try:
return [(filename, options)
for filename, options in CONF.get('run', 'configurations', [])
if osp.isfile(filename)][:history_count]
except ValueError:
CONF.set('run', 'configurations', [])
return []
def _set_run_configurations(configurations):
history_count = CONF.get('run', 'history', 20)
CONF.set('run', 'configurations', configurations[:history_count])
def get_run_configuration(fname):
"""Return script *fname* run configuration"""
configurations = _get_run_configurations()
for filename, options in configurations:
if fname == filename:
runconf = RunConfiguration()
runconf.set(options)
return runconf
class RunConfigOptions(QWidget):
"""Run configuration options"""
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.runconf = RunConfiguration()
common_group = QGroupBox(_("General settings"))
common_layout = QGridLayout()
common_group.setLayout(common_layout)
self.clo_cb = QCheckBox(_("Command line options:"))
common_layout.addWidget(self.clo_cb, 0, 0)
self.clo_edit = QLineEdit()
self.connect(self.clo_cb, SIGNAL("toggled(bool)"),
self.clo_edit.setEnabled)
self.clo_edit.setEnabled(False)
common_layout.addWidget(self.clo_edit, 0, 1)
self.wd_cb = QCheckBox(_("Working directory:"))
common_layout.addWidget(self.wd_cb, 1, 0)
wd_layout = QHBoxLayout()
self.wd_edit = QLineEdit()
self.connect(self.wd_cb, SIGNAL("toggled(bool)"),
self.wd_edit.setEnabled)
self.wd_edit.setEnabled(False)
wd_layout.addWidget(self.wd_edit)
browse_btn = QPushButton(get_std_icon('DirOpenIcon'), "", self)
browse_btn.setToolTip(_("Select directory"))
self.connect(browse_btn, SIGNAL("clicked()"), self.select_directory)
wd_layout.addWidget(browse_btn)
common_layout.addLayout(wd_layout, 1, 1)
radio_group = QGroupBox(_("Interpreter"))
radio_layout = QVBoxLayout()
radio_group.setLayout(radio_layout)
self.current_radio = QRadioButton(_("Execute in current Python "
"or IPython interpreter"))
radio_layout.addWidget(self.current_radio)
self.new_radio = QRadioButton(_("Execute in a new dedicated "
"Python interpreter"))
radio_layout.addWidget(self.new_radio)
self.systerm_radio = QRadioButton(_("Execute in an external "
"system terminal"))
radio_layout.addWidget(self.systerm_radio)
new_group = QGroupBox(_("Dedicated Python interpreter"))
self.connect(self.current_radio, SIGNAL("toggled(bool)"),
new_group.setDisabled)
new_layout = QGridLayout()
new_group.setLayout(new_layout)
self.interact_cb = QCheckBox(_("Interact with the Python "
"interpreter after execution"))
new_layout.addWidget(self.interact_cb, 1, 0, 1, -1)
self.pclo_cb = QCheckBox(_("Command line options:"))
new_layout.addWidget(self.pclo_cb, 2, 0)
self.pclo_edit = QLineEdit()
self.connect(self.pclo_cb, SIGNAL("toggled(bool)"),
self.pclo_edit.setEnabled)
self.pclo_edit.setEnabled(False)
new_layout.addWidget(self.pclo_edit, 2, 1)
pclo_label = QLabel(_("The <b>-u</b> option is "
"added to these commands"))
pclo_label.setWordWrap(True)
new_layout.addWidget(pclo_label, 3, 1)
#TODO: Add option for "Post-mortem debugging"
layout = QVBoxLayout()
layout.addWidget(common_group)
layout.addWidget(radio_group)
layout.addWidget(new_group)
self.setLayout(layout)
def select_directory(self):
"""Select directory"""
basedir = unicode(self.wd_edit.text())
if not osp.isdir(basedir):
basedir = os.getcwdu()
directory = getexistingdirectory(self, _("Select directory"), basedir)
if directory:
self.wd_edit.setText(directory)
self.wd_cb.setChecked(True)
def set(self, options):
self.runconf.set(options)
self.clo_cb.setChecked(self.runconf.args_enabled)
self.clo_edit.setText(self.runconf.args)
self.wd_cb.setChecked(self.runconf.wdir_enabled)
self.wd_edit.setText(self.runconf.wdir)
if self.runconf.current:
self.current_radio.setChecked(True)
elif self.runconf.systerm:
self.systerm_radio.setChecked(True)
else:
self.new_radio.setChecked(True)
self.interact_cb.setChecked(self.runconf.interact)
self.pclo_cb.setChecked(self.runconf.python_args_enabled)
self.pclo_edit.setText(self.runconf.python_args)
def get(self):
self.runconf.args_enabled = self.clo_cb.isChecked()
self.runconf.args = unicode(self.clo_edit.text())
self.runconf.wdir_enabled = self.wd_cb.isChecked()
self.runconf.wdir = unicode(self.wd_edit.text())
self.runconf.current = self.current_radio.isChecked()
self.runconf.systerm = self.systerm_radio.isChecked()
self.runconf.interact = self.interact_cb.isChecked()
self.runconf.python_args_enabled = self.pclo_cb.isChecked()
self.runconf.python_args = unicode(self.pclo_edit.text())
return self.runconf.get()
def is_valid(self):
wdir = unicode(self.wd_edit.text())
if not self.wd_cb.isChecked() or osp.isdir(wdir):
return True
else:
QMessageBox.critical(self, _("Run configuration"),
_("The following working directory is "
"not valid:<br><b>%s</b>") % wdir)
return False
class SizeMixin(object):
"""Mixin to keep widget size accessible
even when C++ object has been deleted"""
def __init__(self):
self.win_size = None
def resizeEvent(self, event):
"""Reimplement Qt method"""
QDialog.resizeEvent(self, event)
self.win_size = self.size()
class RunConfigOneDialog(QDialog, SizeMixin):
"""Run configuration dialog box: single file version"""
def __init__(self, parent=None):
QDialog.__init__(self, parent)
SizeMixin.__init__(self)
# 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)
self.filename = None
self.runconfigoptions = RunConfigOptions(self)
bbox = QDialogButtonBox(QDialogButtonBox.Cancel)
bbox.addButton(_("Run"), QDialogButtonBox.AcceptRole)
self.connect(bbox, SIGNAL("accepted()"), SLOT("accept()"))
self.connect(bbox, SIGNAL("rejected()"), SLOT("reject()"))
btnlayout = QHBoxLayout()
btnlayout.addStretch(1)
btnlayout.addWidget(bbox)
layout = QVBoxLayout()
layout.addWidget(self.runconfigoptions)
layout.addLayout(btnlayout)
self.setLayout(layout)
self.setWindowIcon(get_icon("run.png"))
def setup(self, fname):
self.filename = fname
self.runconfigoptions.set(RunConfiguration(fname).get())
self.setWindowTitle(_("Run %s") % osp.basename(fname))
def accept(self):
"""Reimplement Qt method"""
if not self.runconfigoptions.is_valid():
return
configurations = _get_run_configurations()
configurations.insert(0, (self.filename, self.runconfigoptions.get()))
_set_run_configurations(configurations)
QDialog.accept(self)
def get_configuration(self):
# It is import to avoid accessing Qt C++ object as it has probably
# already been destroyed, due to the Qt.WA_DeleteOnClose attribute
return self.runconfigoptions.runconf
class RunConfigDialog(QDialog, SizeMixin):
"""Run configuration dialog box: multiple file version"""
def __init__(self, parent=None):
QDialog.__init__(self, parent)
SizeMixin.__init__(self)
# 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)
self.file_to_run = None
combo_label = QLabel(_("Select a run configuration:"))
self.combo = QComboBox()
self.combo.setMaxVisibleItems(20)
self.combo.setSizeAdjustPolicy(QComboBox.AdjustToMinimumContentsLength)
self.combo.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
self.stack = QStackedWidget()
bbox = QDialogButtonBox(QDialogButtonBox.Ok|QDialogButtonBox.Cancel)
run_btn = bbox.addButton(_("Run"), QDialogButtonBox.AcceptRole)
self.connect(run_btn, SIGNAL('clicked()'), self.run_btn_clicked)
self.connect(bbox, SIGNAL("accepted()"), SLOT("accept()"))
self.connect(bbox, SIGNAL("rejected()"), SLOT("reject()"))
btnlayout = QHBoxLayout()
btnlayout.addStretch(1)
btnlayout.addWidget(bbox)
layout = QVBoxLayout()
layout.addWidget(combo_label)
layout.addWidget(self.combo)
layout.addSpacing(10)
layout.addWidget(self.stack)
layout.addLayout(btnlayout)
self.setLayout(layout)
self.setWindowTitle(_("Run configurations"))
self.setWindowIcon(get_icon("run.png"))
def setup(self, fname):
configurations = _get_run_configurations()
for index, (filename, options) in enumerate(configurations):
if fname == filename:
break
else:
# There is no run configuration for script *fname*:
# creating a temporary configuration that will be kept only if
# dialog changes are accepted by the user
configurations.insert(0, (fname, RunConfiguration(fname).get()))
index = 0
for filename, options in configurations:
widget = RunConfigOptions(self)
widget.set(options)
self.combo.addItem(filename)
self.stack.addWidget(widget)
self.connect(self.combo, SIGNAL("currentIndexChanged(int)"),
self.stack.setCurrentIndex)
self.combo.setCurrentIndex(index)
def accept(self):
"""Reimplement Qt method"""
configurations = []
for index in range(self.stack.count()):
filename = unicode(self.combo.itemText(index))
runconfigoptions = self.stack.widget(index)
if not runconfigoptions.is_valid():
return
options = runconfigoptions.get()
configurations.append( (filename, options) )
_set_run_configurations(configurations)
QDialog.accept(self)
def run_btn_clicked(self):
self.file_to_run = unicode(self.combo.currentText())
|