Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support setting configfile via envvar FROGMOUTH_CFG_FILE #84

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions frogmouth/data/config.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
)
Expand Down
12 changes: 10 additions & 2 deletions frogmouth/data/data_directory.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
)
Expand Down