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 reset_namespace(self): """Resets the namespace by removing all names defined by the user""" reply = QMessageBox.question( self, _("Reset IPython namespace"), _("All user-defined variables will be removed." "<br>Are you sure you want to reset the namespace?"), QMessageBox.Yes | QMessageBox.No, ) if reply == QMessageBox.Yes: self.execute("%reset -f")
def restart_kernel(self, client): """ Create a new kernel and connect it to `client` if the user asks for it """ # Took this bit of code (until if result == ) from the IPython project # (qt/frontend_widget.py - restart_kernel). # Licensed under the BSD license message = _('Are you sure you want to restart the kernel?') buttons = QMessageBox.Yes | QMessageBox.No result = QMessageBox.question(self, _('Restart kernel?'), message, buttons) if result == QMessageBox.Yes: client.show_restart_animation() # Close old kernel tab idx = self.extconsole.get_shell_index_from_id(client.kernel_widget_id) self.extconsole.close_console(index=idx, from_ipyclient=True) # Create a new one and connect it to the client self.extconsole.start_ipykernel(client)
def restart_kernel(self, client): """ Create a new kernel and connect it to `client` if the user asks for it """ # Took this bit of code (until if result == ) from the IPython project # (qt/frontend_widget.py - restart_kernel). # Licensed under the BSD license message = _('Are you sure you want to restart the kernel?') buttons = QMessageBox.Yes | QMessageBox.No result = QMessageBox.question(self, _('Restart kernel?'), message, buttons) if result == QMessageBox.Yes: client.show_restart_animation() # Close old kernel tab idx = self.extconsole.get_shell_index_from_id( client.kernel_widget_id) self.extconsole.close_console(index=idx, from_ipyclient=True) # Create a new one and connect it to the client self.extconsole.start_ipykernel(client)
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 add_path(self): self.redirect_stdio.emit(False) directory = getexistingdirectory(self, _("Select directory"), self.last_path) self.redirect_stdio.emit(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 close_client(self, index=None, client=None, force=False): """Close client tab from index or widget (or close current tab)""" if not self.tabwidget.count(): return if client is not None: index = self.tabwidget.indexOf(client) if index is None and client is None: index = self.tabwidget.currentIndex() if index is not None: client = self.tabwidget.widget(index) # Check if related clients or kernels are opened # and eventually ask before closing them if not force and isinstance(client, IPythonClient): idx = self.extconsole.get_shell_index_from_id( client.kernel_widget_id) if idx is not None: close_all = True if self.get_option('ask_before_closing'): ans = QMessageBox.question( self, self.get_plugin_title(), _("%s will be closed.\n" "Do you want to kill the associated kernel " "and all of its clients?") % client.get_name(), QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel) if ans == QMessageBox.Cancel: return close_all = ans == QMessageBox.Yes if close_all: self.extconsole.close_console(index=idx, from_ipyclient=True) self.close_related_clients(client) client.close() # Note: client index may have changed after closing related widgets self.tabwidget.removeTab(self.tabwidget.indexOf(client)) self.clients.remove(client) self.emit(SIGNAL('update_plugin_title()'))
def close_console(self, index=None, client=None, force=False): """Close console tab from index or widget (or close current tab)""" if not self.tabwidget.count(): return if client is not None: index = self.tabwidget.indexOf(client) if index is None and client is None: index = self.tabwidget.currentIndex() if index is not None: client = self.tabwidget.widget(index) # Check if related clients or kernels are opened # and eventually ask before closing them if not force and isinstance(client, IPythonClient): idx = self.extconsole.get_shell_index_from_id( client.kernel_widget_id) if idx is not None: close_all = True if self.get_option('ask_before_closing'): ans = QMessageBox.question(self, self.get_plugin_title(), _("%s will be closed.\n" "Do you want to kill the associated kernel " "and all of its clients?") % client.get_name(), QMessageBox.Yes|QMessageBox.No|QMessageBox.Cancel) if ans == QMessageBox.Cancel: return close_all = ans == QMessageBox.Yes if close_all: self.extconsole.close_console(index=idx, from_ipyclient=True) self.close_related_ipyclients(client) client.close() # Note: client index may have changed after closing related widgets self.tabwidget.removeTab(self.tabwidget.indexOf(client)) self.clients.remove(client) self.emit(SIGNAL('update_plugin_title()'))
def import_data(self, filenames=None): """Import data from text file""" title = _("Import data") if filenames is None: if self.filename is None: basedir = getcwd() else: basedir = osp.dirname(self.filename) filenames, _selfilter = getopenfilenames(self, title, basedir, iofunctions.load_filters) if not filenames: return elif is_text_string(filenames): filenames = [filenames] for filename in filenames: self.filename = to_text_string(filename) ext = osp.splitext(self.filename)[1].lower() if ext not in iofunctions.load_funcs: buttons = QMessageBox.Yes | QMessageBox.Cancel answer = QMessageBox.question(self, title, _("<b>Unsupported file extension '%s'</b><br><br>" "Would you like to import it anyway " "(by selecting a known file format)?" ) % ext, buttons) if answer == QMessageBox.Cancel: return formats = list(iofunctions.load_extensions.keys()) item, ok = QInputDialog.getItem(self, title, _('Open file as:'), formats, 0, False) if ok: ext = iofunctions.load_extensions[to_text_string(item)] else: return load_func = iofunctions.load_funcs[ext] # 'import_wizard' (self.setup_io) if is_text_string(load_func): # Import data with import wizard error_message = None try: text, _encoding = encoding.read(self.filename) if self.is_internal_shell: self.editor.import_from_string(text) else: base_name = osp.basename(self.filename) editor = ImportWizard(self, text, title=base_name, varname=fix_reference_name(base_name)) if editor.exec_(): var_name, clip_data = editor.get_data() monitor_set_global(self._get_sock(), var_name, clip_data) except Exception as error: error_message = str(error) else: QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) QApplication.processEvents() if self.is_internal_shell: namespace, error_message = load_func(self.filename) interpreter = self.shellwidget.interpreter for key in list(namespace.keys()): new_key = fix_reference_name(key, blacklist=list(interpreter.namespace.keys())) if new_key != key: namespace[new_key] = namespace.pop(key) if error_message is None: interpreter.namespace.update(namespace) else: error_message = monitor_load_globals(self._get_sock(), self.filename, ext) QApplication.restoreOverrideCursor() QApplication.processEvents() if error_message is not None: QMessageBox.critical(self, title, _("<b>Unable to load '%s'</b>" "<br><br>Error message:<br>%s" ) % (self.filename, error_message)) self.refresh_table()