Skip to content

Commit

Permalink
Add support for modifying execution settings for Julia and Python Too…
Browse files Browse the repository at this point in the history
…l Specifications

Re spine-tools/Spine-Toolbox#2446
  • Loading branch information
PekkaSavolainen committed Dec 13, 2023
1 parent 082ca23 commit 2bbd557
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 28 deletions.
2 changes: 1 addition & 1 deletion spine_items/tool/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ def _show_execution_settings(self):
"""Updates the label in Tool properties to show the selected execution settings for this Tool."""
tstype = self._specification.tooltype
if tstype == "python" or tstype == "julia":
self.specification().set_execution_settings()
self.specification().init_execution_settings()
k_spec_name = self.specification().execution_settings["kernel_spec_name"]
env = self.specification().execution_settings["env"]
use_console = self.specification().execution_settings["use_jupyter_console"]
Expand Down
4 changes: 2 additions & 2 deletions spine_items/tool/tool_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def make_sysimage_arg(self):

def prepare(self, args):
"""See base class."""
self.tool_specification.set_execution_settings() # Set default execution settings if they are missing
self.tool_specification.init_execution_settings() # Set default execution settings if they are missing
julia_args = get_julia_path_and_project(self.tool_specification.execution_settings)
if not julia_args:
k_name = self.tool_specification.execution_settings["kernel_spec_name"]
Expand Down Expand Up @@ -297,7 +297,7 @@ def make_python_basic_console_commands(self, cmdline_args):

def prepare(self, args):
"""See base class."""
self.tool_specification.set_execution_settings() # Set default execution settings
self.tool_specification.init_execution_settings() # Initialize execution settings
cmdline_args = self.tool_specification.cmdline_args + args
if self.tool_specification.execution_settings["use_jupyter_console"]:
server_ip = "127.0.0.1"
Expand Down
44 changes: 36 additions & 8 deletions spine_items/tool/tool_specifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def _definition_local_entries():
"""See base class"""
return [("execution_settings",)]

def set_execution_settings(self):
def init_execution_settings(self):
"""Updates Tool specifications by adding the default execution settings dict for this specification."""

def is_equivalent(self, other):
Expand Down Expand Up @@ -429,13 +429,25 @@ def to_dict(self):
d["execution_settings"] = self.execution_settings
return d

def set_execution_settings(self):
"""Sets the execution_setting instance attribute to defaults if this
Julia Tool spec has not been updated yet.
def set_execution_settings(self, use_julia_jupyter_console, julia_exe, julia_project, julia_kernel):
"""Sets execution settings.
Returns:
void
Args:
use_julia_jupyter_console (bool): True for Jupyter Console, False for Basic Console
julia_exe (str): Abs. path to Julia executable
julia_project (str): Julia project
julia_kernel (str): Julia kernel for Jupyter Console
"""
d = dict()
d["kernel_spec_name"] = julia_kernel
d["env"] = ""
d["use_jupyter_console"] = use_julia_jupyter_console
d["executable"] = julia_exe
d["project"] = julia_project
self.execution_settings = d

def init_execution_settings(self):
"""Initializes execution_setting instance attribute to default settings."""
if not self.execution_settings:
# Use global (default) execution settings from Settings->Tools
# This part is for providing support for Julia Tool specs that do not have
Expand Down Expand Up @@ -533,7 +545,23 @@ def to_dict(self):
d["execution_settings"] = self.execution_settings
return d

def set_execution_settings(self):
def set_execution_settings(self, use_python_jupyter_console, python_exe, python_kernel, env):
"""Sets execution settings.
Args:
use_python_jupyter_console (bool): True for Jupyter Console, False for Basic Console
python_exe (str): Abs. path to Python executable
python_kernel (str): Julia kernel for Jupyter Console
env (str): empty string for regular kernels, 'conda' for Conda kernels
"""
d = dict()
d["kernel_spec_name"] = python_kernel
d["env"] = env
d["use_jupyter_console"] = use_python_jupyter_console
d["executable"] = python_exe
self.execution_settings = d

def init_execution_settings(self):
"""Sets the execution_setting instance attribute to defaults if this
Python Tool spec has not been updated yet.
Expand Down Expand Up @@ -653,7 +681,7 @@ def _includes_main_path_relative(self):
return None
return super()._includes_main_path_relative()

def set_execution_settings(self):
def init_execution_settings(self):
"""Updates old Executable Tool specifications by adding the
default execution settings dict for this specification.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def __init__(self, toolbox, specification=None, item=None):
index = next(iter(k for k, t in enumerate(TOOL_TYPES) if t.lower() == tooltype), -1)
self._ui.comboBox_tooltype.setCurrentIndex(index)
self._ui.textEdit_program.set_lexer_name(tooltype.lower())
specification.set_execution_settings() # Set default execution settings
specification.init_execution_settings() # Initialize execution settings
# spec dict needs to be set after setting the execution settings but before the
# optional widget is shown, in case the tooltype is Executable.
self.spec_dict = deepcopy(specification.to_dict())
Expand Down
6 changes: 3 additions & 3 deletions tests/tool/test_tool_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ def _make_python_tool_instance(use_jupyter_console, tool_spec_args=None):
mock.MagicMock(),
cmdline_args=tool_spec_args,
)
specification.set_execution_settings()
specification.init_execution_settings()
if use_jupyter_console:
specification.execution_settings["use_jupyter_console"] = True
specification.execution_settings["kernel_spec_name"] = "some_kernel"
Expand All @@ -379,7 +379,7 @@ def _make_julia_tool_instance(use_jupyter_console, tool_spec_args=None):
mock.MagicMock(),
cmdline_args=tool_spec_args,
)
specification.set_execution_settings()
specification.init_execution_settings()
if use_jupyter_console:
specification.execution_settings["use_jupyter_console"] = True
specification.execution_settings["kernel_spec_name"] = "some_julia_kernel"
Expand Down Expand Up @@ -415,7 +415,7 @@ def _make_executable_tool_instance(shell=None, cmd=None, tool_spec_args=None):
mock.MagicMock(),
cmdline_args=tool_spec_args,
)
specification.set_execution_settings()
specification.init_execution_settings()
if shell == "cmd.exe":
specification.execution_settings["shell"] = "cmd.exe"
elif shell == "bash":
Expand Down
6 changes: 3 additions & 3 deletions tests/tool/test_tool_specifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ def test_make_specification(self):
self.test_dict["definition_file_path"] = "/path/to/specification/file.json"
spec = make_specification(self.test_dict, self.qsettings, self.logger) # Make PythonTool
self.assertIsInstance(spec, PythonTool)
spec.set_execution_settings()
spec.init_execution_settings()
self.assertIsNotNone(spec.execution_settings)
self.assertTrue(len(spec.execution_settings.keys()), 4)
spec.to_dict()
# Convert to Julia spec
self.test_dict["tooltype"] = "Julia"
spec = make_specification(self.test_dict, self.qsettings, self.logger) # Make JuliaTool
self.assertIsInstance(spec, JuliaTool)
spec.set_execution_settings()
spec.init_execution_settings()
self.assertIsNotNone(spec.execution_settings)
self.assertTrue(len(spec.execution_settings.keys()), 5)
spec.to_dict()
Expand All @@ -87,7 +87,7 @@ def test_make_specification(self):
self.test_dict["tooltype"] = "Executable"
spec = make_specification(self.test_dict, self.qsettings, self.logger) # Make ExecutableTool
self.assertIsInstance(spec, ExecutableTool)
spec.set_execution_settings()
spec.init_execution_settings()
self.assertIsNotNone(spec.execution_settings)
self.assertTrue(len(spec.execution_settings.keys()), 2)
spec.to_dict()
Expand Down
2 changes: 1 addition & 1 deletion tests/tool/widgets/test_custom_menus.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def make_julia_tool_spec(self):
MockQSettings(),
mock_logger,
)
julia_tool_spec.set_execution_settings() # Sets defaults
julia_tool_spec.init_execution_settings() # Sets defaults
return julia_tool_spec

def make_exec_tool_spec(self):
Expand Down
18 changes: 9 additions & 9 deletions tests/tool/widgets/test_toolSpecificationEditorWindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def test_open_tool_specification_editor_with_julia_spec(self):
outputfiles=["results.txt"],
cmdline_args=["-A", "-B"],
)
julia_tool_spec.set_execution_settings() # Sets defaults
julia_tool_spec.init_execution_settings() # Sets defaults
self.make_tool_spec_editor(julia_tool_spec)
opt_widget = self.tool_specification_widget.optional_widget
self.assertIsInstance(opt_widget, JuliaToolSpecOptionalWidget)
Expand Down Expand Up @@ -195,7 +195,7 @@ def test_open_tool_specification_editor_with_executable_spec(self):
exec_tool_spec = ExecutableTool(
"a", "executable", self._temp_dir.name, ["fake_main_program.bat"], MockQSettings(), mock_logger
)
exec_tool_spec.set_execution_settings() # Sets defaults
exec_tool_spec.init_execution_settings() # Sets defaults
self.make_tool_spec_editor(exec_tool_spec)
opt_widget = self.tool_specification_widget._get_optional_widget("executable")
self.assertIsInstance(opt_widget, ExecutableToolSpecOptionalWidget)
Expand All @@ -221,7 +221,7 @@ def test_edit_and_save_program_file(self):
python_tool_spec = PythonTool(
"a", "python", self._temp_dir.name, [script_file_name], MockQSettings(), mock_logger
)
python_tool_spec.set_execution_settings() # Sets defaults
python_tool_spec.init_execution_settings() # Sets defaults
self.make_tool_spec_editor(python_tool_spec)
parent = self.tool_specification_widget.programfiles_model.index(0, 0)
index = self.tool_specification_widget.programfiles_model.index(0, 0, parent) # Index of 'hello.py'
Expand All @@ -245,7 +245,7 @@ def test_change_python_spec_options(self):
python_tool_spec = PythonTool(
"a", "python", self._temp_dir.name, [script_file_name], MockQSettings(), mock_logger
)
python_tool_spec.set_execution_settings() # Sets defaults
python_tool_spec.init_execution_settings() # Sets defaults
python_tool_spec.execution_settings["use_jupyter_console"] = True
python_tool_spec.execution_settings["kernel_spec_name"] = "python310"
with mock.patch("spine_items.tool.widgets.tool_spec_optional_widgets.KernelFetcher", new=FakeKernelFetcher):
Expand Down Expand Up @@ -291,7 +291,7 @@ def test_change_executable_spec_options(self):
exec_tool_spec = ExecutableTool(
"a", "executable", self._temp_dir.name, [batch_file, "data.file"], MockQSettings(), mock_logger
)
exec_tool_spec.set_execution_settings() # Sets defaults
exec_tool_spec.init_execution_settings() # Sets defaults
self.make_tool_spec_editor(exec_tool_spec)
file_path = Path(self._temp_dir.name, another_batch_file)
parent_main = self.tool_specification_widget.programfiles_model.index(0, 0) # Main program file item
Expand Down Expand Up @@ -351,7 +351,7 @@ def test_change_executable_spec_options(self):
def test_change_julia_project(self):
mock_logger = mock.MagicMock()
julia_tool_spec = JuliaTool("a", "julia", self._temp_dir.name, ["hello.jl"], MockQSettings(), mock_logger)
julia_tool_spec.set_execution_settings() # Sets defaults
julia_tool_spec.init_execution_settings() # Sets defaults
julia_tool_spec.execution_settings["use_jupyter_console"] = True
with mock.patch("spine_items.tool.widgets.tool_spec_optional_widgets.KernelFetcher", new=FakeKernelFetcher):
self.make_tool_spec_editor(julia_tool_spec)
Expand All @@ -371,7 +371,7 @@ def test_restore_unknown_saved_kernel_into_optional_widget(self):
python_tool_spec = PythonTool(
"a", "python", self._temp_dir.name, [script_file_name], MockQSettings(), mock_logger
)
python_tool_spec.set_execution_settings() # Sets defaults
python_tool_spec.init_execution_settings() # Sets defaults
python_tool_spec.execution_settings["use_jupyter_console"] = True
python_tool_spec.execution_settings["kernel_spec_name"] = "unknown_kernel"
with mock.patch(
Expand Down Expand Up @@ -403,7 +403,7 @@ def test_program_file_dialogs(self):
julia_tool_spec = JuliaTool(
"a", "julia", self._temp_dir.name, [script_file_name, data_file_name], MockQSettings(), mock_logger
)
julia_tool_spec.set_execution_settings() # Sets defaults
julia_tool_spec.init_execution_settings() # Sets defaults
self.make_tool_spec_editor(julia_tool_spec)
self.assertEqual("hello.jl", os.path.split(self.tool_specification_widget._current_main_program_file())[1])
# Test browse_main_program_file()
Expand Down Expand Up @@ -476,7 +476,7 @@ def test_add_rename_select_remove_input_and_output_files(self):
with open(main_path, "w") as h:
h.writelines(["println('Hello world')"]) # Make hello.jl
julia_tool_spec = JuliaTool("a", "julia", self._temp_dir.name, [main_file], MockQSettings(), mock_logger)
julia_tool_spec.set_execution_settings() # Sets defaults
julia_tool_spec.init_execution_settings() # Sets defaults
self.make_tool_spec_editor(julia_tool_spec)
# INPUT FILES
# Test add_input_files()
Expand Down

0 comments on commit 2bbd557

Please sign in to comment.