From 1cff94b8648a99929dec5e9f4fc26739398bb847 Mon Sep 17 00:00:00 2001 From: Wouter Vanden Hove Date: Sat, 14 Oct 2023 11:33:24 +0200 Subject: [PATCH 1/3] support setting configfile via envvar FROGMOUTH_CFG_FILE --- frogmouth/data/config.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/frogmouth/data/config.py b/frogmouth/data/config.py index e9b37f4..286d6a7 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,22 @@ 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"): + configfile = Path(cfg_file) + return configfile + (config_dir := xdg_config_home() / ORGANISATION_NAME / PACKAGE_NAME).mkdir( parents=True, exist_ok=True ) From fe9b8961c4118f707b126be6c4e01314c76fc415 Mon Sep 17 00:00:00 2001 From: Wouter Vanden Hove Date: Sat, 14 Oct 2023 13:44:42 +0200 Subject: [PATCH 2/3] support setting data-dir via envvar FROGMOUTH_DATA_DIR --- frogmouth/data/data_directory.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) 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 ) From fd83f50952593465cbd1f5a8cda591258672407d Mon Sep 17 00:00:00 2001 From: Wouter Vanden Hove Date: Sat, 14 Oct 2023 14:11:50 +0200 Subject: [PATCH 3/3] simplify --- frogmouth/data/config.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/frogmouth/data/config.py b/frogmouth/data/config.py index 286d6a7..e3de113 100644 --- a/frogmouth/data/config.py +++ b/frogmouth/data/config.py @@ -42,8 +42,7 @@ def config_file() -> Path: """ if cfg_file := os.getenv("FROGMOUTH_CFG_FILE"): - configfile = Path(cfg_file) - return configfile + return Path(cfg_file) (config_dir := xdg_config_home() / ORGANISATION_NAME / PACKAGE_NAME).mkdir( parents=True, exist_ok=True