Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
zain-sohail committed May 22, 2024
1 parent ffba4d7 commit ac553c2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 19 deletions.
27 changes: 27 additions & 0 deletions sed/core/user_dirs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,37 @@
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

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
30 changes: 11 additions & 19 deletions sed/dataset/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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


Expand Down Expand Up @@ -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,
),
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ac553c2

Please sign in to comment.