Skip to content

Commit b7ac67f

Browse files
committed
Fixed logic in setup to save config as a YAML file with complex objects properly serialised
1 parent 3ac7467 commit b7ac67f

File tree

1 file changed

+29
-22
lines changed

1 file changed

+29
-22
lines changed

src/murfey/cli/generate_config.py

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -309,21 +309,22 @@ def get_file_substring() -> str:
309309
return sorted_dict
310310

311311
# Start of add_software_packages
312+
console.print("acquisition_software", style="bold cyan")
313+
console.print(
314+
"This is where aquisition software packages present on the instrument "
315+
"machine can be set.",
316+
style="cyan",
317+
)
318+
console.print(
319+
"Options: 'epu', 'tomo', 'serialem', 'autotem', 'leica'",
320+
style="cyan",
321+
)
322+
package_info: dict = {}
312323
category = "software package"
313324
add_input = ask_for_input(category, again=False)
314-
package_info: dict = {}
315325
while add_input:
316326
# Collect inputs
317327
console.print("acquisition_software", style="bold cyan")
318-
console.print(
319-
"This is where aquisition software packages present on the instrument "
320-
"machine can be set.",
321-
style="cyan",
322-
)
323-
console.print(
324-
"Options: 'epu', 'tomo', 'serialem', 'autotem', 'leica'",
325-
style="cyan",
326-
)
327328
name = get_software_name()
328329
if name in package_info.keys():
329330
if confirm_overwrite(name) is False:
@@ -516,17 +517,24 @@ def run():
516517

517518
new_config[key] = populate_field(key, field)
518519

519-
# Validate the entire config one last time
520+
# Validate the entire config again and convert into JSON/YAML-safe dict
520521
try:
521-
MachineConfig.validate(new_config)
522-
except ValidationError as exc:
523-
for ve in exc.errors():
524-
if ve["type"] != "value_error.missing":
525-
print("Validation failed")
522+
new_config_json = MachineConfig(**new_config).json()
523+
new_config_safe = json.loads(new_config_json)
524+
except ValidationError as exception:
525+
# Print out validation errors found
526+
console.print("Validation failed", style="red")
527+
for error in exception.errors():
528+
console.print(f"{error}", style="red")
529+
# Offer to redo the setup, otherwise quit setup
530+
if ask_for_input("machine configuration", True) is True:
531+
return run()
526532
exit()
527533

528534
# Save config under its instrument name
529-
master_config: dict[str, dict] = {new_config["instrument_name"]: new_config}
535+
master_config: dict[str, dict] = {
536+
new_config_safe["instrument_name"]: new_config_safe
537+
}
530538

531539
# Create save path for config
532540
config_name = prompt(
@@ -551,11 +559,10 @@ def run():
551559
exit()
552560
# Check if settings already exist for this machine
553561
for key in master_config.keys():
554-
if key in old_config.keys():
555-
if confirm_overwrite(key) is False:
556-
old_config[key].update(new_config[key])
557-
else:
558-
old_config[key] = new_config[key]
562+
# Check if overwriting of existing config is needed
563+
if key in old_config.keys() and confirm_overwrite(key) is False:
564+
old_config[key].update(new_config[key])
565+
# Add new machine config
559566
else:
560567
old_config[key] = new_config[key]
561568
# Overwrite

0 commit comments

Comments
 (0)