def test_get_config_value(self): with self.assertRaises(ValueError) as e: get_config_value(None) self.assertEqual(str(e.exception), 'name must be given') with self.assertRaises(ValueError) as e: get_config_value('') self.assertEqual(str(e.exception), 'name must be given') value = get_config_value('_im_not_in_', 'Yes!') self.assertEqual(value, 'Yes!')
def open_file(cls, path): launch_editor_command_template = get_config_value('launch_editor_command', None) if launch_editor_command_template: if isinstance(launch_editor_command_template, str) and launch_editor_command_template.strip(): launch_editor_command_template = launch_editor_command_template.strip() else: launch_editor_command_template = None if not launch_editor_command_template: raise WorkspaceError('configuration parameter "launch_editor_command" must be a non-empty string') else: try: # Windows: # Start a file with its associated application. os.startfile(path) return except AttributeError: if shutil.which('xdg-open'): # Unix Desktops # xdg-open opens a file or URL in the user's preferred application. launch_editor_command_template = 'xdg-open "{file}"' elif shutil.which('open'): # Mac OS X # Open a file or folder. The open command opens a file (or a folder or URL), just as # if you had double-clicked the file's icon. launch_editor_command_template = 'open "{file}"' else: print('warning: don\'t know how to open %s' % path) return launch_editor_command = launch_editor_command_template.format(file=path) try: # print('launch_editor_command:', launch_editor_command) subprocess.call(launch_editor_command, shell=True) except (IOError, OSError) as error: raise WorkspaceError(str(error))
def launch_notebook(cls, title: str, notebook_dir: str, notebook_path: str = None): # we start a new terminal/command window here so that non-expert users can close the Notebook session easily # by closing the newly created window. terminal_title = 'DeDop - %s' % title notebook_command = 'jupyter notebook --notebook-dir "%s"' % notebook_dir if notebook_path: notebook_command += ' "%s"' % notebook_path launch_notebook_command_template = get_config_value( 'launch_notebook_command', None) launch_notebook_in_new_terminal = True if launch_notebook_command_template: if isinstance(launch_notebook_command_template, str) and launch_notebook_command_template.strip(): launch_notebook_command_template = launch_notebook_command_template.strip( ) else: launch_notebook_command_template = None if not launch_notebook_command_template: raise WorkspaceError( 'configuration parameter "launch_notebook_command" must be a non-empty string' ) launch_notebook_in_new_terminal = get_config_value( 'launch_notebook_in_new_terminal', False) else: if sys.platform.startswith('win'): # Windows launch_notebook_command_template = 'start "{title}" /Min {command}' elif sys.platform == 'darwin': # Mac OS X launch_notebook_command_template = 'open -a Terminal "{command_file}"' elif shutil.which("konsole"): # KDE launch_notebook_command_template = 'konsole -p tabtitle="{title}" -e \'{command}\'' elif shutil.which("gnome-terminal"): # GNOME / Ubuntu launch_notebook_command_template = 'gnome-terminal --title "{title}" -e "bash -c \'{command}\'"' elif shutil.which("xterm"): launch_notebook_command_template = 'xterm -T "{title}" -e \'{command}\'' else: launch_notebook_command_template = notebook_command launch_notebook_in_new_terminal = False command_file = '' if '{command_file}' in launch_notebook_command_template: try: if not os.path.exists(DEFAULT_TEMP_DIR): os.makedirs(DEFAULT_TEMP_DIR) command_basename = 'dedop-notebook-server' if sys.platform.startswith('win'): command_file = os.path.join(DEFAULT_TEMP_DIR, command_basename + '.bat') with open(command_file, 'w') as fp: fp.write( 'call "{prefix}/Scripts/activate.bat" "{prefix}"\n' 'call {command}\n'.format( prefix=sys.prefix, command=notebook_command)) else: import stat command_file = os.path.join(DEFAULT_TEMP_DIR, command_basename) with open(command_file, 'w') as fp: fp.write('#!/bin/bash\n' 'source "{prefix}/bin/activate" "{prefix}"\n' '{command}\n'.format( prefix=sys.prefix, command=notebook_command)) os.chmod(command_file, stat.S_IEXEC | stat.S_IREAD | stat.S_IWRITE) except (OSError, IOError) as error: raise WorkspaceError(str(error)) launch_notebook_command = launch_notebook_command_template.format( title=terminal_title, command=notebook_command, command_file=command_file, prefix=sys.prefix) try: # print('calling:', open_notebook_command) subprocess.check_call(launch_notebook_command, shell=True) if launch_notebook_in_new_terminal: print('A terminal window titled "%s" has been opened.' % cls._limit_title(terminal_title, 30, mode='l')) print( 'Close that window or press CTRL+C within it to terminate the Notebook session.' ) except (subprocess.CalledProcessError, IOError, OSError) as error: raise WorkspaceError('failed to launch Jupyter Notebook: %s' % str(error))
def launch_notebook(cls, title: str, notebook_dir: str, notebook_path: str = None): # we start a new terminal/command window here so that non-expert users can close the Notebook session easily # by closing the newly created window. terminal_title = 'DeDop - %s' % title notebook_command = 'jupyter notebook --notebook-dir "%s"' % notebook_dir if notebook_path: notebook_command += ' "%s"' % notebook_path launch_notebook_command_template = get_config_value('launch_notebook_command', None) launch_notebook_in_new_terminal = True if launch_notebook_command_template: if isinstance(launch_notebook_command_template, str) and launch_notebook_command_template.strip(): launch_notebook_command_template = launch_notebook_command_template.strip() else: launch_notebook_command_template = None if not launch_notebook_command_template: raise WorkspaceError('configuration parameter "launch_notebook_command" must be a non-empty string') launch_notebook_in_new_terminal = get_config_value('launch_notebook_in_new_terminal', False) else: if sys.platform.startswith('win'): # Windows launch_notebook_command_template = 'start "{title}" /Min {command}' elif sys.platform == 'darwin': # Mac OS X launch_notebook_command_template = 'open -a Terminal "{command_file}"' elif shutil.which("konsole"): # KDE launch_notebook_command_template = 'konsole -p tabtitle="{title}" -e \'{command}\'' elif shutil.which("gnome-terminal"): # GNOME / Ubuntu launch_notebook_command_template = 'gnome-terminal --title "{title}" -e "bash -c \'{command}\'"' elif shutil.which("xterm"): launch_notebook_command_template = 'xterm -T "{title}" -e \'{command}\'' else: launch_notebook_command_template = notebook_command launch_notebook_in_new_terminal = False command_file = '' if '{command_file}' in launch_notebook_command_template: try: if not os.path.exists(DEFAULT_TEMP_DIR): os.makedirs(DEFAULT_TEMP_DIR) command_basename = 'dedop-notebook-server' if sys.platform.startswith('win'): command_file = os.path.join(DEFAULT_TEMP_DIR, command_basename + '.bat') with open(command_file, 'w') as fp: fp.write('call "{prefix}/Scripts/activate.bat" "{prefix}"\n' 'call {command}\n'.format(prefix=sys.prefix, command=notebook_command)) else: import stat command_file = os.path.join(DEFAULT_TEMP_DIR, command_basename) with open(command_file, 'w') as fp: fp.write('#!/bin/bash\n' 'source "{prefix}/bin/activate" "{prefix}"\n' '{command}\n'.format(prefix=sys.prefix, command=notebook_command)) os.chmod(command_file, stat.S_IEXEC | stat.S_IREAD | stat.S_IWRITE) except (OSError, IOError) as error: raise WorkspaceError(str(error)) launch_notebook_command = launch_notebook_command_template.format(title=terminal_title, command=notebook_command, command_file=command_file, prefix=sys.prefix) try: # print('calling:', open_notebook_command) subprocess.check_call(launch_notebook_command, shell=True) if launch_notebook_in_new_terminal: print('A terminal window titled "%s" has been opened.' % cls._limit_title(terminal_title, 30, mode='l')) print('Close that window or press CTRL+C within it to terminate the Notebook session.') except (subprocess.CalledProcessError, IOError, OSError) as error: raise WorkspaceError('failed to launch Jupyter Notebook: %s' % str(error))