~reviewer/hello-hut

ffb728d506b7ca0675f04cfada060ec37fa15447 — Elif Demir 6 months ago 256ae90
Read defaults from ~/.config/hello-hut/config.toml

Uses tomllib where available; on Python < 3.11 the file is ignored.
2 files changed, 26 insertions(+), 2 deletions(-)

M src/hello_hut/cli.py
A src/hello_hut/config.py
M src/hello_hut/cli.py => src/hello_hut/cli.py +4 -2
@@ 4,6 4,7 @@ import argparse
import sys

from . import __version__
from .config import load_config
from .greetings import DEFAULT_NAME, greet, time_of_day_greeting




@@ 24,8 25,9 @@ def build_parser():

def main(argv=None):
    args = build_parser().parse_args(argv)
    name = args.name or DEFAULT_NAME
    shout = args.shout
    config = load_config()
    name = args.name or config.get("name", DEFAULT_NAME)
    shout = args.shout or bool(config.get("shout", False))

    salutation = time_of_day_greeting() if args.time_of_day else None
    message = greet(name, shout=shout, salutation=salutation)

A src/hello_hut/config.py => src/hello_hut/config.py +22 -0
@@ 0,0 1,22 @@
"""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") or os.path.join(os.environ["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)