diff --git a/frogmouth/data/config.py b/frogmouth/data/config.py index e9b37f4..e3de113 100644 --- a/frogmouth/data/config.py +++ b/frogmouth/data/config.py @@ -1,7 +1,7 @@ """Provides code for loading/saving configuration.""" - from __future__ import annotations +import os from dataclasses import asdict, dataclass, field from functools import lru_cache from json import dumps, loads @@ -29,13 +29,21 @@ class Config: def config_file() -> Path: """Get the path to the configuration file. + The location of the configfile can be explicitly set by the + environment variable ``FROGMOUTH_CFG_FILE``. + When this env-var is not set, we default to XDG-directories. + Returns: The path to the configuration file. Note: - As a side-effect, the configuration directory will be created if it + As a side-effect, the default configuration xdg-directory will be created if it does not exist. """ + + if cfg_file := os.getenv("FROGMOUTH_CFG_FILE"): + return Path(cfg_file) + (config_dir := xdg_config_home() / ORGANISATION_NAME / PACKAGE_NAME).mkdir( parents=True, exist_ok=True ) diff --git a/frogmouth/data/data_directory.py b/frogmouth/data/data_directory.py index 976efd3..12bc590 100644 --- a/frogmouth/data/data_directory.py +++ b/frogmouth/data/data_directory.py @@ -1,5 +1,5 @@ """Provides a function for working out the data directory location.""" - +import os from pathlib import Path from xdg import xdg_data_home @@ -10,12 +10,20 @@ def data_directory() -> Path: """Get the location of the data directory. + The location of the configfile can be explicitly set by the + environment variable ``FROGMOUTH_DATA_DIR``. + When this env-var is not set, we default to XDG-directories. + Returns: The location of the data directory. Note: - As a side effect, if the directory doesn't exist it will be created. + As a side effect, if the xdg-directory doesn't exist it will be created. """ + + if target_directory := os.getenv("FROGMOUTH_DATA_DIR"): + return Path(target_directory) + (target_directory := xdg_data_home() / ORGANISATION_NAME / PACKAGE_NAME).mkdir( parents=True, exist_ok=True )