From ffb728d506b7ca0675f04cfada060ec37fa15447 Mon Sep 17 00:00:00 2001 From: Elif Demir Date: Wed, 15 Apr 2026 17:45:00 +0300 Subject: [PATCH] Read defaults from ~/.config/hello-hut/config.toml Uses tomllib where available; on Python < 3.11 the file is ignored. --- src/hello_hut/cli.py | 6 ++++-- src/hello_hut/config.py | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 src/hello_hut/config.py diff --git a/src/hello_hut/cli.py b/src/hello_hut/cli.py index d0a71a5a947ab2b2f06553eac49d8a7172b23b4a..857d4982fed467760cc744901a5147e082c7b149 100644 --- a/src/hello_hut/cli.py +++ b/src/hello_hut/cli.py @@ -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) diff --git a/src/hello_hut/config.py b/src/hello_hut/config.py new file mode 100644 index 0000000000000000000000000000000000000000..210194c578ef1964f3bbed8076c8fbc5f0e7ae45 --- /dev/null +++ b/src/hello_hut/config.py @@ -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)