def test_get_temp_dir_ensure_dir_exists(): """Test that the call to get_temp_dir creates the dir when it doesn't exists """ temp_dir = get_temp_dir(suffix='test') assert os.path.exists(temp_dir) os.rmdir(temp_dir) another_call = get_temp_dir(suffix='test') assert os.path.exists(another_call) assert another_call == temp_dir
def close_client(self, index=None, client=None, save=False): """ Close client tab from index or widget (or close current tab). The notebook is saved if `save` is `False`. """ 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) is_welcome = client.get_filename() == WELCOME if not save and not is_welcome: self.save_notebook(client) if not is_welcome: client.shutdown_kernel() client.close() # Delete notebook file if it is in temporary directory filename = client.get_filename() if filename.startswith(get_temp_dir()): try: os.remove(filename) except EnvironmentError: pass # Note: notebook index may have changed after closing related widgets self.tabwidget.removeTab(self.tabwidget.indexOf(client)) self.clients.remove(client) self.create_welcome_client()
def test_remove_old_stderr_files(ipyconsole, qtbot): """Test that we are removing old stderr files.""" # Create empty stderr file in our temp dir to see # if it's removed correctly. tmpdir = get_temp_dir() open(osp.join(tmpdir, 'foo.stderr'), 'a').close() # Assert that only that file is removed ipyconsole._remove_old_stderr_files() assert not osp.isfile(osp.join(tmpdir, 'foo.stderr'))
def std_filename(connection_file, extension, std_dir=None): """Filename to save kernel output.""" json_file = osp.basename(connection_file) file = json_file.split('.json')[0] + extension if std_dir is not None: file = osp.join(std_dir, file) else: try: file = osp.join(get_temp_dir(), file) except (IOError, OSError): file = None return file
def stderr_file(self): """Filename to save kernel stderr output.""" stderr_file = None if self.connection_file is not None: stderr_file = self.kernel_id + '.stderr' if self.stderr_dir is not None: stderr_file = osp.join(self.stderr_dir, stderr_file) else: try: stderr_file = osp.join(get_temp_dir(), stderr_file) except (IOError, OSError): stderr_file = None return stderr_file
def std_filename(self, extension): """Filename to save kernel output.""" file = None if self.connection_file is not None: file = self.kernel_id + extension if self.std_dir is not None: file = osp.join(self.std_dir, file) else: try: file = osp.join(get_temp_dir(), file) except (IOError, OSError): file = None return file
def _remove_old_std_files(self): """ Remove std files left by previous Spyder instances. This is only required on Windows because we can't clean up std files while Spyder is running on it. """ if os.name == 'nt': tmpdir = get_temp_dir() for fname in os.listdir(tmpdir): if osp.splitext(fname)[1] in ('.stderr', '.stdout', '.fault'): try: os.remove(osp.join(tmpdir, fname)) except Exception: pass
def close_client(self, index=None, client=None, save=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) is_welcome = client.get_filename() == WELCOME if not save and not is_welcome: client.save() wait_save = QEventLoop() QTimer.singleShot(1000, wait_save.quit) wait_save.exec_() path = client.get_filename() fname = osp.basename(path) nb_contents = nbformat.read(path, as_version=4) if ('untitled' in fname and len(nb_contents['cells']) > 0 and len(nb_contents['cells'][0]['source']) > 0): buttons = QMessageBox.Yes | QMessageBox.No answer = QMessageBox.question( self, self.get_plugin_title(), _("<b>{0}</b> has been modified." "<br>Do you want to " "save changes?".format(fname)), buttons) if answer == QMessageBox.Yes: self.save_as(close=True) if not is_welcome: client.shutdown_kernel() client.close() # Delete notebook file if it is in temporary directory filename = client.get_filename() if filename.startswith(get_temp_dir()): try: os.remove(filename) except EnvironmentError: pass # Note: notebook index may have changed after closing related widgets self.tabwidget.removeTab(self.tabwidget.indexOf(client)) self.clients.remove(client) self.create_welcome_client()
def close_client(self, index=None, save_before_close=True): """ Close client tab with given index (or close current tab). First save the notebook (unless this is the welcome client or `save_before_close` is False). Then delete the notebook if it is in `get_temp_dir()`. Then shutdown the kernel of the notebook and close the tab. Finally, create a welcome tab if there are no tabs. Parameters ---------- index : int or None, optional Index of tab to be closed. The default is None, meaning that the current tab is closed. save_before_close : bool, optional Whether to save the notebook before closing the tab. The default is True. Returns ------- The file name of the notebook, or None if no tab was closed. """ if not self.count(): return None if index is None: index = self.currentIndex() client = self.widget(index) filename = client.filename if not self.is_welcome_client(client): if save_before_close: filename = self.save_notebook(client) client.shutdown_kernel() client.close() # Delete notebook file if it is in temporary directory if filename.startswith(get_temp_dir()): try: os.remove(filename) except EnvironmentError: pass # Note: notebook index may have changed after closing related widgets self.removeTab(self.indexOf(client)) self.maybe_create_welcome_client() return filename
def get_temp_dir(): return TEMPDIR try: # Spyder 4 from spyder.api.plugins import SpyderPluginWidget except ImportError: # Spyder 3 from spyder.plugins import SpyderPluginWidget # Local imports from .utils.nbopen import nbopen, NBServerError from .widgets.client import NotebookClient NOTEBOOK_TMPDIR = osp.join(get_temp_dir(), 'notebooks') FILTER_TITLE = _("Jupyter notebooks") FILES_FILTER = "{} (*.ipynb)".format(FILTER_TITLE) PACKAGE_PATH = osp.dirname(__file__) WELCOME = osp.join(PACKAGE_PATH, 'utils', 'templates', 'welcome.html') class NotebookPlugin(SpyderPluginWidget): """IPython Notebook plugin.""" CONF_SECTION = 'notebook' focus_changed = Signal() def __init__(self, parent, testing=False): """Constructor.""" SpyderPluginWidget.__init__(self, parent)