From cb5a27830951eb660ab1c4236cadb63755dcf140 Mon Sep 17 00:00:00 2001 From: "Dr.-Ing. Amilcar do Carmo Lucas" Date: Mon, 29 Apr 2024 12:59:16 +0200 Subject: [PATCH] FEATURE: remember the last selected directories on windows --- MethodicConfigurator/backend_filesystem.py | 86 +++++++++++++++++++ .../frontend_tkinter_directory_selection.py | 9 +- credits/CREDITS.md | 1 + credits/platformdirs-LICENSE | 21 +++++ credits/update_credits_licenses.py | 1 + windows/ardupilot_methodic_configurator.iss | 44 ++++++++-- windows/settings_template.json | 8 ++ 7 files changed, 159 insertions(+), 11 deletions(-) create mode 100644 credits/platformdirs-LICENSE create mode 100644 windows/settings_template.json diff --git a/MethodicConfigurator/backend_filesystem.py b/MethodicConfigurator/backend_filesystem.py index f4f913a..ce3e786 100644 --- a/MethodicConfigurator/backend_filesystem.py +++ b/MethodicConfigurator/backend_filesystem.py @@ -20,6 +20,7 @@ from re import compile as re_compile from re import match as re_match from re import escape as re_escape +from re import sub as re_sub # from sys import exit as sys_exit # from logging import debug as logging_debug @@ -37,6 +38,8 @@ from zipfile import ZipFile +from platformdirs import user_config_dir + from annotate_params import BASE_URL, PARAM_DEFINITION_XML_FILE, Par from annotate_params import get_xml_data from annotate_params import create_doc_dict @@ -476,6 +479,89 @@ def copy_fc_values_to_file(self, selected_file: str, params: Dict[str, float]): logging_warning("Parameter %s not found in the current parameter file", param) return ret + @staticmethod + def __get_settings_directory(): + settings_directory = user_config_dir(".ardupilot_methodic_configurator", False, ensure_exists=True) + + if not os_path.exists(settings_directory): + raise FileNotFoundError(f"The settings directory '{settings_directory}' does not exist.") + if not os_path.isdir(settings_directory): + raise NotADirectoryError(f"The path '{settings_directory}' is not a directory.") + + return settings_directory + + @staticmethod + def __get_settings_as_dict(): + settings_path = os_path.join(LocalFilesystem.__get_settings_directory(), "settings.json") + + settings = {} + + try: + with open(settings_path, "r", encoding='utf-8') as settings_file: + settings = json_load(settings_file) + except FileNotFoundError: + # If the file does not exist, it will be created later + pass + + if "Format version" not in settings: + settings["Format version"] = 1 + + if "directory_selection" not in settings: + settings["directory_selection"] = {} + return settings + + @staticmethod + def __set_settings_from_dict(settings): + settings_path = os_path.join(LocalFilesystem.__get_settings_directory(), "settings.json") + + with open(settings_path, "w", encoding='utf-8') as settings_file: + json_dump(settings, settings_file, indent=4) + + @staticmethod + def store_recently_used_template_dirs(template_dir: str, new_base_dir: str): + settings = LocalFilesystem.__get_settings_as_dict() + + # Regular expression pattern to match single backslashes + pattern = r"(?