Skip to content

Commit

Permalink
chore: fix main user package home setting for export
Browse files Browse the repository at this point in the history
  • Loading branch information
MyPyDavid committed Feb 18, 2024
1 parent 3d4493b commit 5d669be
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 160 deletions.
154 changes: 32 additions & 122 deletions src/raman_fitting/config/filepath_helper.py
Original file line number Diff line number Diff line change
@@ -1,137 +1,47 @@
""" this module prepares the local file paths for data and results"""

from typing import Dict
import logging

from pathlib import Path

import raman_fitting
from raman_fitting.config.settings import Settings
from loguru import logger

logger = logging.getLogger(__name__)

def check_and_make_dirs(destdir: Path) -> None:
_destfile = None
if destdir.suffix:
_destfile = destdir
destdir = _destfile.parent

def get_directory_paths_for_run_mode(run_mode: str = "", **kwargs) -> Dict:
"""
Parameters
----------
run_mode : str, optional
this is name of the run mode. The default is ''.
**kwargs : TYPE
kwargs can contain keys such as DATASET_DIR to overwrite the standard config paths.
Returns
-------
dest_dirs : dict
dict containing 3 keys [RESULTS_DIR, DATASET_DIR, INDEX_FILE]
"""
run_mode_config = run_mode.lower()
config_aliases = {"make_index": "normal"}
if run_mode_config in config_aliases.keys():
run_mode_config = config_aliases[run_mode_config]

dest_dirs = Settings.get_run_mode_paths(run_mode_config)
if kwargs:
dest_dirs = override_from_kwargs(dest_dirs, **kwargs)

check_and_make_dirs(dest_dirs["DATASET_DIR"], dest_dirs["RESULTS_DIR"])

return dest_dirs


def check_and_make_dirs(dataset_dir: Path, results_dir: Path) -> None:
create_dataset_dir(dataset_dir)

if not results_dir.is_dir():
results_dir.mkdir(exist_ok=True, parents=True)
if not destdir.is_dir():
destdir.mkdir(exist_ok=True, parents=True)
logger.info(
f"check_and_make_dirs the results directory did not exist and was created at:\n{results_dir}\n"
f"check_and_make_dirs the results directory did not exist and was created at:\n{destdir}\n"
)

if _destfile:
_destfile.touch()

def override_from_kwargs(_dict, **kwargs):
_kwargs = kwargs
if _kwargs:
_keys = [i for i in _dict.keys() if i in _kwargs.keys()]
_new_dict = {
k: Path(val) if not _kwargs.get(k, None) else _kwargs[k]
for k, val in _dict.items()
}
if _new_dict != _dict:
logger.debug(f"override_from_kwargs keys {_keys} were overwritten")
return _new_dict
else:
return _dict


def create_dataset_dir(DATASET_DIR): # pragma: no cover
if not DATASET_DIR.is_dir():
logger.warning(
f"The datafiles directory does not exist yet, the program will now try to create this folder.\n{DATASET_DIR}"
# therefore {settings.__package_name__} can not find any files.
# The program will now try to create this folder.
)
try:
DATASET_DIR.mkdir()
logger.warning(
f"""The datafiles directory has now been created at:
{DATASET_DIR}
please place your raman datafiles in this folder and run {raman_fitting.__package_name__} again.
{raman_fitting.__package_name__} exits now.
"""
def create_dir_or_ask_user_input(destdir: Path, ask_user=True):
counter, max_attempts = 0, 10
while not destdir.exists() and counter < max_attempts:
answer = "y"
if ask_user:
answer = input(
f"Directory to store files raman_fitting:\n{destdir}\nCan this be folder be created? (y/n)"
)
except Exception as exc:
logger.warning(
f"""The datafiles directory could not be created at:
{DATASET_DIR}
An unexpected error ocurred:
{exc}
please redefine the path for dataset_dir in the config settings.
"""
)
else:
# Check if dir is not empty else raise a warning
_diter = DATASET_DIR.iterdir()
try:
next(_diter)
except StopIteration:
logger.warning(
f"""The datafiles directory is empty:
{DATASET_DIR}
please place your files in here or
change this path in the config settings.
"""
)


def create_package_home_dir(package_home: Path):
if package_home.is_dir():
logger.info(
f"Package home directory exists at:\n{package_home}\n--------------------"
)
return
package_home_choice = input(
f"""Package home directory did not exist, will now be created at:)
{package_home}
--------------------
Choose yes(y) to continue or no(n) to select your directory."""
)
if package_home_choice.startswith("n"):
from tkinter import Tk, filedialog

root = Tk()
root.withdraw()
root.attributes("-topmost", True)
package_home = Path(filedialog.askdirectory())

try:
logger.warning(
f"Package home directory did not exist, will now be created at:\n{package_home}\n--------------------"
)
package_home.mkdir()
except Exception as exc:
logger.warning(
f"Package home mkdir unexpected error\n{exc}.\nFolder{package_home} could not be created, exiting."
)
exit()
if "y" not in answer.lower():
new_path_user = input(
"Please provide the directory to store files raman_fitting:"
)
try:
new_path = Path(new_path_user).resolve()
except Exception as e:
print(f"Exception: {e}")
counter += 1
destdir = new_path

destdir.mkdir(exist_ok=True, parents=True)
logger.info(f"Directory created: {destdir}")
return destdir
94 changes: 58 additions & 36 deletions src/raman_fitting/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import tempfile
from enum import StrEnum, auto


from pydantic import (
BaseModel,
DirectoryPath,
FilePath,
NewPath,
ConfigDict,
Field,
model_validator,
Expand All @@ -19,15 +19,17 @@
from raman_fitting.models.deconvolution.base_model import (
get_models_and_peaks_from_definitions,
)
from .filepath_helper import check_and_make_dirs, create_dir_or_ask_user_input


PACKAGE_NAME = "raman_fitting"
CURRENT_FILE: Path = Path(__file__).resolve()
PACKAGE_ROOT: Path = CURRENT_FILE.parent.parent
REPO_ROOT: Path = PACKAGE_ROOT.parent
DEFAULT_MODELS_DIR: Path = CURRENT_FILE.parent / "default_models"
INTERNAL_DEFAULT_MODELS: Path = CURRENT_FILE.parent / "default_models"
# MODEL_DIR: Path = PACKAGE_ROOT / "deconvolution_models"
EXAMPLE_FIXTURES: Path = PACKAGE_ROOT / "example_fixtures"
PYTEST_FIXUTRES: Path = REPO_ROOT / "tests" / "test_fixtures"
INTERNAL_EXAMPLE_FIXTURES: Path = PACKAGE_ROOT / "example_fixtures"
INTERNAL_PYTEST_FIXTURES: Path = REPO_ROOT / "tests" / "test_fixtures"

# Home dir from pathlib.Path for storing the results
USER_HOME_PACKAGE: Path = Path.home() / PACKAGE_NAME
Expand All @@ -52,9 +54,9 @@
class InternalPathSettings(BaseModel):
settings_file: FilePath = Field(CURRENT_FILE)
package_root: DirectoryPath = Field(PACKAGE_ROOT)
default_models_dir: DirectoryPath = Field(DEFAULT_MODELS_DIR)
example_fixtures: DirectoryPath = Field(EXAMPLE_FIXTURES)
pytest_fixtures: DirectoryPath = Field(PYTEST_FIXUTRES)
default_models_dir: DirectoryPath = Field(INTERNAL_DEFAULT_MODELS)
example_fixtures: DirectoryPath = Field(INTERNAL_EXAMPLE_FIXTURES)
pytest_fixtures: DirectoryPath = Field(INTERNAL_PYTEST_FIXTURES)
temp_dir: DirectoryPath = Field(TEMP_RESULTS_DIR)


Expand All @@ -73,26 +75,35 @@ class RunModes(StrEnum):
MAKE_INDEX = auto()


RUN_MODE_PATHS = {
"pytest": {
"RESULTS_DIR": TEMP_RESULTS_DIR,
"DATASET_DIR": EXAMPLE_FIXTURES,
"USER_CONFIG_FILE": EXAMPLE_FIXTURES / f"{PACKAGE_NAME}.toml",
"INDEX_FILE": TEMP_RESULTS_DIR / f"{PACKAGE_NAME}_index.csv",
},
"make_examples": {
"RESULTS_DIR": USER_HOME_PACKAGE / "make_examples",
"DATASET_DIR": EXAMPLE_FIXTURES,
"USER_CONFIG_FILE": EXAMPLE_FIXTURES / f"{PACKAGE_NAME}.toml",
"INDEX_FILE": USER_HOME_PACKAGE / "make_examples" / f"{PACKAGE_NAME}_index.csv",
},
"normal": {
"RESULTS_DIR": USER_HOME_PACKAGE / "results",
"DATASET_DIR": USER_HOME_PACKAGE / "datafiles",
"USER_CONFIG_FILE": USER_HOME_PACKAGE / "raman_fitting.toml",
"INDEX_FILE": USER_HOME_PACKAGE / f"{PACKAGE_NAME}_index.csv",
},
}
def get_run_mode_paths(run_mode: RunModes, user_package_home: Path = None):
if user_package_home is None:
user_package_home = USER_HOME_PACKAGE

RUN_MODE_PATHS = {
"pytest": {
"RESULTS_DIR": TEMP_RESULTS_DIR,
"DATASET_DIR": INTERNAL_EXAMPLE_FIXTURES,
"USER_CONFIG_FILE": INTERNAL_EXAMPLE_FIXTURES / f"{PACKAGE_NAME}.toml",
"INDEX_FILE": TEMP_RESULTS_DIR / f"{PACKAGE_NAME}_index.csv",
},
"make_examples": {
"RESULTS_DIR": user_package_home / "make_examples",
"DATASET_DIR": INTERNAL_EXAMPLE_FIXTURES,
"USER_CONFIG_FILE": INTERNAL_EXAMPLE_FIXTURES / f"{PACKAGE_NAME}.toml",
"INDEX_FILE": user_package_home
/ "make_examples"
/ f"{PACKAGE_NAME}_index.csv",
},
"normal": {
"RESULTS_DIR": user_package_home / "results",
"DATASET_DIR": user_package_home / "datafiles",
"USER_CONFIG_FILE": user_package_home / "raman_fitting.toml",
"INDEX_FILE": user_package_home / f"{PACKAGE_NAME}_index.csv",
},
}
if run_mode not in RUN_MODE_PATHS:
raise ValueError(f"Choice of run_mode {run_mode} not supported.")
return RUN_MODE_PATHS[run_mode]


class ExportPathSettings(BaseModel):
Expand Down Expand Up @@ -129,17 +140,26 @@ class RunModePaths(BaseModel):
run_mode: RunModes
results_dir: DirectoryPath
dataset_dir: DirectoryPath
user_config_file: FilePath | NewPath
index_file: FilePath | NewPath
user_config_file: Path
index_file: Path


def get_run_mode_paths(run_mode: RunModes) -> RunModePaths:
if run_mode not in RUN_MODE_PATHS:
raise ValueError(f"Choice of run_mode {run_mode} not supported.")
dest_dirs = RUN_MODE_PATHS[run_mode]
dest_dirs["RUN_MODE"] = run_mode
def initialize_run_mode_paths(
run_mode: RunModes, user_package_home: Path = None
) -> RunModePaths:
run_mode_paths = get_run_mode_paths(run_mode, user_package_home=user_package_home)

# USER_HOME_PACKAGE = get_user_destination_dir(USER_HOME_PACKAGE)
for destname, destdir in run_mode_paths.items():
destdir = Path(destdir)
check_and_make_dirs(destdir)
# dest_dirs["RUN_MODE"] = run_mode
# breakpoint()
return RunModePaths(**dest_dirs)
return RunModePaths(RUN_MODE=run_mode, **run_mode_paths)


def create_default_package_dir_or_ask():
return create_dir_or_ask_user_input(USER_HOME_PACKAGE)


class Settings(BaseSettings):
Expand All @@ -150,7 +170,9 @@ class Settings(BaseSettings):
validate_default=False,
)

destination_dir: DirectoryPath = Field(USER_HOME_PACKAGE)
destination_dir: DirectoryPath = Field(
default_factory=create_default_package_dir_or_ask
)
# export_folder_names_mapping: ExportPathSettings = Field(
# default_factory=get_default_path_settings,
# init_var=False,
Expand Down
7 changes: 5 additions & 2 deletions src/raman_fitting/exports/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
from typing import Dict, Any
from raman_fitting.config.settings import (
RunModes,
get_run_mode_paths,
initialize_run_mode_paths,
ExportPathSettings,
)
from raman_fitting.config import settings


from raman_fitting.exports.plotting_fit_results import fit_spectrum_plot
Expand All @@ -26,7 +27,9 @@ class ExportManager:
results: Dict[str, Any] | None = None

def __post_init__(self):
self.paths = get_run_mode_paths(self.run_mode)
self.paths = initialize_run_mode_paths(
self.run_mode, user_package_home=settings.destination_dir
)

def export_files(self):
# breakpoint() self.results
Expand Down

0 comments on commit 5d669be

Please sign in to comment.