diff --git a/sed/core/user_dirs.py b/sed/core/user_dirs.py index 5e8026f6..813fe9c5 100644 --- a/sed/core/user_dirs.py +++ b/sed/core/user_dirs.py @@ -7,6 +7,8 @@ USER_LOG_PATH (pathlib.Path): The path to the user-specific log directory. USER_DATA_PATH (pathlib.Path): The path to the user-specific data directory. """ +from pathlib import Path + from platformdirs import user_config_path from platformdirs import user_data_path from platformdirs import user_log_path @@ -14,3 +16,28 @@ USER_CONFIG_PATH = user_config_path(appname="sed", appauthor="OpenCOMPES", ensure_exists=True) USER_LOG_PATH = user_log_path(appname="sed", appauthor="OpenCOMPES", ensure_exists=True) USER_DATA_PATH = user_data_path(appname="sed", appauthor="OpenCOMPES", ensure_exists=True) + + +def construct_module_dirs(module: str, create_in: list = ["config", "data"]) -> dict[str, Path]: + """ + Constructs module-specific subdirectories within specified user-specific directories. + + Args: + module: Module subdirectory to append to the base paths. + create_in (list): Specifies which base paths to use ('config', 'data', etc.). + Defaults to ['config', 'data']. + + Returns: + dict: A dictionary with keys as the path types ('config', 'data', etc.) and + values as the constructed paths. + """ + path_types = {"config": USER_CONFIG_PATH, "data": USER_DATA_PATH} + constructed_paths = {} + + for path_type, base_path in path_types.items(): + if path_type in create_in: + path = base_path.joinpath(module) + path.mkdir(parents=True, exist_ok=True) + constructed_paths[path_type] = path + + return constructed_paths diff --git a/sed/dataset/dataset.py b/sed/dataset/dataset.py index 6e68080c..5b56a0d5 100644 --- a/sed/dataset/dataset.py +++ b/sed/dataset/dataset.py @@ -4,29 +4,22 @@ import os import shutil import zipfile +from pathlib import Path import requests from sed.core.config import load_config from sed.core.config import save_config from sed.core.logging import setup_logging -from sed.core.user_dirs import USER_CONFIG_PATH -from sed.core.user_dirs import USER_DATA_PATH +from sed.core.user_dirs import construct_module_dirs # Configure logging logger = setup_logging(__name__) -DATASETS_FILENAME = "datasets.json" -# Paths for user configuration and data directories -USER_CONFIG_DATASETS_DIR = USER_CONFIG_PATH / "datasets" -USER_CONFIG_DATASETS_DIR.mkdir(parents=True, exist_ok=True) - -USER_DATASETS_DIR = USER_DATA_PATH / "datasets" -USER_DATASETS_DIR.mkdir(parents=True, exist_ok=True) - -# Paths for the datasets JSON file -USER_JSON_PATH = USER_DATASETS_DIR.joinpath(DATASETS_FILENAME) -MODULE_JSON_PATH = os.path.join(os.path.dirname(__file__), DATASETS_FILENAME) +NAME = "datasets" +user_paths = construct_module_dirs(NAME) +json_path_user = user_paths["config"].joinpath(NAME).with_suffix(".json") +json_path_module = Path(os.path.dirname(__file__)).joinpath(NAME).with_suffix(".json") def load_datasets_dict() -> dict: @@ -40,10 +33,9 @@ def load_datasets_dict() -> dict: dict: The datasets dict loaded from user's datasets JSON file. """ # check if datasets.json exists in user_config_dir - if not os.path.exists(USER_JSON_PATH): - module_json = os.path.join(os.path.dirname(__file__), DATASETS_FILENAME) - shutil.copy(module_json, USER_JSON_PATH) - datasets = load_config(str(USER_JSON_PATH)) + if not os.path.exists(json_path_user): + shutil.copy(json_path_module, json_path_user) + datasets = load_config(str(json_path_user)) return datasets @@ -104,7 +96,7 @@ def set_data_path(data_name: str, data_path: str, existing_data_path: str) -> st # Set data path if not provided if data_path is None: data_path = existing_data_path or str( - USER_DATA_PATH.joinpath( + user_paths["data"].joinpath( "datasets", data_name, ), @@ -288,7 +280,7 @@ def load_dataset(data_name: str, data_path: str = None) -> str | tuple[str, list save_config( {data_name: dataset}, - str(USER_JSON_PATH), + str(json_path_user), ) # Save the updated dataset information # Return subdirectory paths if present