def test_save_as(self): """Test saving data to a new json file""" parameters = Parameters(source=self.parameters_location) name = 'parameters_copy.json' parameters.save(directory=self.temp_dir, name=name) source = str(Path(self.temp_dir, name)) params2 = Parameters(source=source, cast_values=False) self.assertEqual(parameters, params2)
def test_save(self): """Test saving to overwrite source file.""" # Save a copy to the temp directory parameters = Parameters(source=self.parameters_location, cast_values=True) name = 'parameters_copy.json' parameters.save(directory=self.temp_dir, name=name) source = str(Path(self.temp_dir, name)) params2 = Parameters(source=source, cast_values=True) self.assertEqual(parameters, params2) # Change a parameter and overwrite self.assertTrue(parameters['parameter_location'] != source) params2['parameter_location'] = source params2.save() # Load from the save file and confirm that saved values persisted params3 = Parameters(source=source, cast_values=True) self.assertEqual(params3['parameter_location'], source) self.assertEqual(params3, params2) self.assertNotEqual(params3, parameters)
def test_save_new(self): """Test saving a new params file.""" parameters = Parameters(source=None, cast_values=False) parameters['mystr'] = { "value": "hello", "section": "", "readableName": "", "helpTip": "", "recommended_values": "", "type": "str" } with self.assertRaises(Exception): # Missing directory and file name parameters.save() with self.assertRaises(Exception): parameters.save(directory=self.temp_dir) with self.assertRaises(Exception): parameters.save(name='my_params.json') parameters.save(directory=self.temp_dir, name='my_params.json')
class ParamsForm(QWidget): """The ParamsForm class is a QWidget that creates controls/inputs for each parameter in the provided json file. Parameters: ----------- json_file - path of parameters file to be edited. load_file - optional path of parameters file to load; parameters from this file will be copied over to the json_file. width - optional; used to set the width of the form controls. """ def __init__(self, json_file: str, load_file: str = None, width: int = 400): super().__init__() self.json_file = json_file self.load_file = json_file if not load_file else load_file self.width = width self.help_size = 12 self.help_color = 'darkgray' self.params = Parameters(source=self.load_file, cast_values=False) self.controls = {} self.create_controls() self.do_layout() def create_controls(self) -> None: """Create controls (inputs, labels, etc) for each item in the parameters file. """ for key, param in self.params.entries(): self.controls[key] = self.parameter_input(param) def parameter_input(self, param: Dict[str, str]) -> FormInput: """Construct a FormInput for the given parameter based on its python type and other attributes.""" type_inputs = { 'int': IntegerInput, 'float': FloatInput, 'bool': BoolInput, 'filepath': FileInput, 'directorypath': DirectoryInput } has_options = isinstance(param['recommended_values'], list) form_input = type_inputs.get(param['type'], SelectionInput if has_options else TextInput) return form_input(label=param['readableName'], value=param['value'], help_tip=param['helpTip'], options=param['recommended_values'], help_size=self.help_size, help_color=self.help_color) def do_layout(self) -> None: """Layout the form controls.""" vbox = QVBoxLayout() # Add the controls to the grid: for _param_name, form_input in self.controls.items(): vbox.addWidget(form_input) self.setLayout(vbox) self.setFixedWidth(self.width) self.show() def search(self, text: str) -> None: """Search for an input. Hides inputs which do not match. Searches labels, hints, and values. Parameter: ---------- text - text used to search; if empty all inputs are displayed. """ for form_input in self.controls.values(): if text == '' or form_input.matches(text): form_input.show() else: form_input.hide() def save(self) -> bool: """Save changes""" self.update_parameters() path = Path(self.json_file) self.params.save(path.parent, path.name) return True def update_parameters(self): """Update the parameters from the input values.""" for param_name, form_input in self.controls.items(): param = self.params[param_name] value = form_input.value() if value != param['value']: self.params[param_name]['value'] = value