~reviewer/hello-hut

ref: 9625423548ea198632914da4967098aa06d119af hello-hut/src/hello_hut/config.py -rw-r--r-- 725 bytes
96254235 — Felix Brandt Add --count to repeat the greeting 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
"""Optional user configuration in ~/.config/hello-hut/config.toml."""

import os

try:
    import tomllib
except ModuleNotFoundError:  # Python < 3.11
    tomllib = None


def config_path():
    base = os.environ.get("XDG_CONFIG_HOME")
    if not base:
        home = os.environ.get("HOME")
        if not home:
            return None
        base = os.path.join(home, ".config")
    return os.path.join(base, "hello-hut", "config.toml")


def load_config(path=None):
    """Return the parsed configuration, or an empty dict if there is none."""
    path = path or config_path()
    if not path or tomllib is None or not os.path.exists(path):
        return {}
    with open(path, "rb") as f:
        return tomllib.load(f)