Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def set(self, section, option, value, verbose=False, save=True):
"""
Set an `option` on a given `section`.
If section is None, the `option` is added to the default section.
"""
section = self._check_section_option(section, option)
default_value = self.get_default(section, option)
if default_value is NoDefault:
# This let us save correctly string value options with
# no config default that contain non-ascii chars in
# Python 2
if PY2 and is_text_string(value):
value = repr(value)
default_value = value
self.set_default(section, option, default_value)
if isinstance(default_value, bool):
value = bool(value)
elif isinstance(default_value, float):
value = float(value)
elif isinstance(default_value, int):
value = int(value)
elif not is_text_string(default_value):
value = repr(value)
self._set(section, option, value, verbose)
if save:
"""
Start new console
fname:
string: filename of script to run
None: open an interpreter
wdir: working directory
args: command line options of the Python script
interact: inspect script interactively after its execution
debug: run pdb
python: True: Python interpreter, False: terminal
python_args: additionnal Python interpreter command line options
(option "-u" is mandatory, see widgets.externalshell package)
"""
# Note: fname is None <=> Python interpreter
if fname is not None and not is_text_string(fname):
fname = to_text_string(fname)
if wdir is not None and not is_text_string(wdir):
wdir = to_text_string(wdir)
if fname is not None and fname in self.filenames:
index = self.filenames.index(fname)
if self.get_option('single_tab'):
old_shell = self.shellwidgets[index]
if old_shell.is_running():
runconfig = get_run_configuration(fname)
if runconfig is None or runconfig.show_kill_warning:
if PYQT5:
answer = QMessageBox.question(self, self.get_plugin_title(),
_("%s is already running in a separate process.\n"
"Do you want to kill the process before starting "
"a new one?") % osp.basename(fname),
def _eval(self, text):
"""
Evaluate text and return (obj, valid)
where *obj* is the object represented by *text*
and *valid* is True if object evaluation did not raise any exception
"""
assert is_text_string(text)
ns = self.get_current_namespace()
try:
return eval(text, ns), True
except:
return None, False
def create_action(parent, text, shortcut=None, icon=None, tip=None,
toggled=None, triggered=None, data=None, menurole=None,
context=Qt.WindowShortcut):
"""Create a QAction"""
action = SpyderAction(text, parent)
if triggered is not None:
action.triggered.connect(triggered)
if toggled is not None:
action.toggled.connect(toggled)
action.setCheckable(True)
if icon is not None:
if is_text_string(icon):
icon = get_icon(icon)
action.setIcon(icon)
if tip is not None:
action.setToolTip(tip)
action.setStatusTip(tip)
if data is not None:
action.setData(to_qvariant(data))
if menurole is not None:
action.setMenuRole(menurole)
# Workround for Mac because setting context=Qt.WidgetShortcut
# there doesn't have any effect
if sys.platform == 'darwin':
action._shown_shortcut = None
if context == Qt.WidgetShortcut:
if shortcut is not None:
def _check_defaults(self, defaults):
"""Check if defaults are valid and update defaults values."""
if defaults is None:
defaults = [(self.DEFAULT_SECTION_NAME, {})]
elif isinstance(defaults, dict):
defaults = [(self.DEFAULT_SECTION_NAME, defaults)]
elif isinstance(defaults, list):
# Check is a list of tuples with strings and dictionaries
for sec, options in defaults:
assert is_text_string(sec)
assert isinstance(options, dict)
for opt, _ in options.items():
assert is_text_string(opt)
else:
raise ValueError('`defaults` must be a dict or a list of tuples!')
# This attribute is overriding a method from cp.ConfigParser
self.defaults = defaults
if defaults is not None:
self.reset_to_defaults(save=False)
return defaults
def _set(self, section, option, value, verbose):
"""Set method."""
if not self.has_section(section):
self.add_section(section)
if not is_text_string(value):
value = repr(value)
if verbose:
text = '[{}][{}] = {}'.format(section, option, value)
print(text) # spyder: test-skip
super(DefaultsConfig, self).set(section, option, value)
default_value = self.get_default(section, option)
if default_value is NoDefault:
# This let us save correctly string value options with
# no config default that contain non-ascii chars in
# Python 2
if PY2 and is_text_string(value):
value = repr(value)
default_value = value
self.set_default(section, option, default_value)
if isinstance(default_value, bool):
value = bool(value)
elif isinstance(default_value, float):
value = float(value)
elif isinstance(default_value, int):
value = int(value)
elif not is_text_string(default_value):
value = repr(value)
self._set(section, option, value, verbose)
if save:
self._save()
def _eval(self, text):
"""
Evaluate text and return (obj, valid)
where *obj* is the object represented by *text*
and *valid* is True if object evaluation did not raise any exception
"""
if not IS_EXT_INTERPRETER:
from spyder.py3compat import is_text_string
else:
from py3compat import is_text_string
assert is_text_string(text)
ns = self._get_current_namespace(with_magics=True)
try:
return eval(text, ns), True
except:
return None, False
def getobjdir(obj):
"""
For standard objects, will simply return dir(obj)
In special cases (e.g. WrapITK package), will return only string elements
of result returned by dir(obj)
"""
return [item for item in dir(obj) if is_text_string(item)]
def text_to_qcolor(text):
"""
Create a QColor from specified string
Avoid warning from Qt when an invalid QColor is instantiated
"""
color = QColor()
if not is_string(text): # testing for QString (PyQt API#1)
text = str(text)
if not is_text_string(text):
return color
if text.startswith('#') and len(text)==7:
correct = '#0123456789abcdef'
for char in text:
if char.lower() not in correct:
return color
elif text not in list(QColor.colorNames()):
return color
color.setNamedColor(text)
return color