diff --git a/HISTORY.rst b/HISTORY.rst index 3d5d2f9..95acb99 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -2,6 +2,11 @@ History ======= +2023.10.21: Improved handling of choices + * When editing the choices for a parameter, the entryfield now accepts the choices + separated by spaces. If a choice has spaces or other special characters they can be + protected with quotes or backslash in the normal fashion for shell arguments. + 2023.7.10: Bugfix handling parameters with 0+ values * The default was not correctly handled for control parameters with 0+ arguments, diff --git a/control_parameters_step/control_parameters.py b/control_parameters_step/control_parameters.py index 90bb93b..92efdbb 100644 --- a/control_parameters_step/control_parameters.py +++ b/control_parameters_step/control_parameters.py @@ -432,12 +432,17 @@ def create_parser(self): else: name = dest nargs = nargs_values[data["nargs"]] + + # Compatibility for old flowcharts + if isinstance(data["choices"], str): + data["choices"] = json.loads(data["choices"]) + choices = data["choices"] if choices == "": choices = None else: # choices is a string representation of a list - choices = json.loads(choices.replace("'", '"')) + # choices = json.loads(choices.replace("'", '"')) if len(choices) == 0: choices = None diff --git a/control_parameters_step/tk_control_parameters.py b/control_parameters_step/tk_control_parameters.py index cfa1ab4..69a1f3a 100644 --- a/control_parameters_step/tk_control_parameters.py +++ b/control_parameters_step/tk_control_parameters.py @@ -1,6 +1,8 @@ # -*- coding: utf-8 -*- """The graphical part of a Control Parameters step""" +import json +import shlex import seamm from seamm_util import ureg, Q_, units_class # noqa: F401 @@ -129,6 +131,7 @@ def create_dialog(self): self["variables"] = sw.ScrolledColumns( frame, columns=[ + "", "", "Name", "Type", @@ -241,6 +244,15 @@ def edit(self): P = self.node.parameters self._variables = P["variables"].value + + # Turn choices into simple strings + for data in self._variables.values(): + # Compatibility for old flowcharts + if isinstance(data["choices"], str): + data["choices"] = json.loads(data["choices"]) + + data["choices"] = shlex.join(data["choices"]) + self.reset_dialog() self.reset_table() @@ -281,8 +293,11 @@ def handle_dialog(self, result): # Shortcut for parameters P = self.node.parameters - # Get the values for all the widgets. + # Get the values for all the widgets, fixing up choices + for data in self._variables.values(): + data["choices"] = shlex.split(data["choices"]) P["variables"].value = self._variables + self._variables = None for key in P: @@ -314,7 +329,7 @@ def add_variable(self): self._new["nargs"].set("a single value") self._new["overwrite"].set("No") self._new["default"].set("") - self._new["choices"].set("[]") + self._new["choices"].set("") self._new["help"].set("") self._new_variable_dialog.activate(geometry="centerscreenfirst")