diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index 7238e1c..251b4a4 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -22,7 +22,7 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip setuptools wheel - python -m pip install -U pymavlink mock coverage + python -m pip install -U platformdirs pymavlink mock coverage pip install build pip install -U . diff --git a/MethodicConfigurator/backend_filesystem.py b/MethodicConfigurator/backend_filesystem.py index 04b0d41..3f43bdc 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 @@ -518,6 +521,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, roaming=True, 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"(?