0
0
mirror of https://github.com/bec-project/bec_widgets.git synced 2025-07-14 11:41:49 +02:00

fix: yaml_dialog.py changed to use native solution of OS -> should prevent crashing on py3.11

This commit is contained in:
wyzula-jan
2023-12-06 15:29:21 +01:00
parent 2359a08519
commit 5adde23a45

View File

@ -1,10 +1,20 @@
# pylint: disable=no-name-in-module
from typing import Union
import yaml import yaml
from qtpy.QtWidgets import QFileDialog from qtpy.QtWidgets import QFileDialog
def load_yaml(instance) -> dict: def load_yaml(instance) -> Union[dict, None]:
"""
Load YAML file from disk.
Args:
instance: Instance of the calling widget.
Returns:
dict: Configuration data loaded from the YAML file.
"""
options = QFileDialog.Options() options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
file_path, _ = QFileDialog.getOpenFileName( file_path, _ = QFileDialog.getOpenFileName(
instance, "Load Settings", "", "YAML Files (*.yaml *.yml);;All Files (*)", options=options instance, "Load Settings", "", "YAML Files (*.yaml *.yml);;All Files (*)", options=options
) )
@ -18,20 +28,28 @@ def load_yaml(instance) -> dict:
except FileNotFoundError: except FileNotFoundError:
print(f"The file {file_path} was not found.") print(f"The file {file_path} was not found.")
except PermissionError:
print(f"Permission denied for file {file_path}.")
except yaml.YAMLError as e:
print(f"Error parsing YAML file {file_path}: {e}")
except Exception as e: except Exception as e:
print(f"An error occurred while loading the settings from {file_path}: {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: def save_yaml(instance, config: dict) -> None:
"""
Save YAML file to disk.
Args:
instance: Instance of the calling widget.
config: Configuration data to be saved.
"""
options = QFileDialog.Options() options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
file_path, _ = QFileDialog.getSaveFileName( file_path, _ = QFileDialog.getSaveFileName(
instance, "Save Settings", "", "YAML Files (*.yaml *.yml);;All Files (*)", options=options instance, "Save Settings", "", "YAML Files (*.yaml *.yml);;All Files (*)", options=options
) )
if not file_path: if not file_path:
return None return
try: try:
if not (file_path.endswith(".yaml") or file_path.endswith(".yml")): if not (file_path.endswith(".yaml") or file_path.endswith(".yml")):
file_path += ".yaml" file_path += ".yaml"