mirror of
https://github.com/bec-project/bec_widgets.git
synced 2025-07-14 03:31:50 +02:00
feat: yaml_dialog.py interactive QFileDialog window to load/save .yaml files to/from dict
This commit is contained in:
59
bec_widgets/qt_utils/yaml_dialog.py
Normal file
59
bec_widgets/qt_utils/yaml_dialog.py
Normal file
@ -0,0 +1,59 @@
|
||||
import yaml
|
||||
from PyQt5.QtWidgets import QFileDialog
|
||||
|
||||
# TODO add pydantic/json scheme validation for config files as an optional argument
|
||||
|
||||
|
||||
def load_yaml(instance) -> dict:
|
||||
"""
|
||||
Load a YAML file and update the settings
|
||||
Args:
|
||||
instance: parent instance (named instance to avoid conflict with keyword in QFileDialog)
|
||||
|
||||
Returns:
|
||||
config(dict): dictionary of settings
|
||||
"""
|
||||
options = QFileDialog.Options()
|
||||
|
||||
options |= QFileDialog.DontUseNativeDialog
|
||||
file_path, _ = QFileDialog.getOpenFileName(
|
||||
instance, "Load Settings", "", "YAML Files (*.yaml);;All Files (*)", options=options
|
||||
)
|
||||
|
||||
if file_path:
|
||||
try:
|
||||
with open(file_path, "r") as file:
|
||||
config = yaml.safe_load(file)
|
||||
return config
|
||||
|
||||
except FileNotFoundError:
|
||||
print(f"The file {file_path} was not found.")
|
||||
except Exception as e:
|
||||
print(f"An error occurred while loading the settings from {file_path}: {e}")
|
||||
return None # Return None on exception to indicate failure
|
||||
|
||||
|
||||
def save_yaml(instance, config: dict) -> None:
|
||||
"""
|
||||
Save the settings to a YAML file
|
||||
Args:
|
||||
instance: parent instance (named instance to avoid conflict with keyword in QFileDialog)
|
||||
config(dict): dictionary of settings to save to .yaml file
|
||||
"""
|
||||
|
||||
options = QFileDialog.Options()
|
||||
options |= QFileDialog.DontUseNativeDialog
|
||||
file_path, _ = QFileDialog.getSaveFileName(
|
||||
instance, "Save Settings", "", "YAML Files (*.yaml);;All Files (*)", options=options
|
||||
)
|
||||
|
||||
if file_path:
|
||||
try:
|
||||
if not file_path.endswith(".yaml"):
|
||||
file_path += ".yaml"
|
||||
|
||||
with open(file_path, "w") as file:
|
||||
yaml.dump(config, file)
|
||||
print(f"Settings saved to {file_path}")
|
||||
except Exception as e:
|
||||
print(f"An error occurred while saving the settings to {file_path}: {e}")
|
Reference in New Issue
Block a user