ref: Turn dict into conf obj

This commit is contained in:
Marty Oehme 2025-04-05 12:01:44 +02:00
parent c6f05d0b64
commit 0e167cf08a
Signed by: Marty
GPG key ID: 4E535BC19C61886E

View file

@ -12,6 +12,7 @@ import configparser
import os import os
import subprocess import subprocess
import sys import sys
from dataclasses import dataclass
from pathlib import Path from pathlib import Path
from tasklib import Task, TaskWarrior from tasklib import Task, TaskWarrior
@ -27,6 +28,32 @@ DEFAULTS_DICT = {
} }
@dataclass()
class TConf:
task_rc: str
task_data: str
task_id: int
notes_dir: str
notes_ext: str
notes_annot: str
notes_editor: str
notes_quiet: bool
def conf_from_dict(d: dict) -> TConf:
return TConf(
task_rc=d["task_rc"],
task_data=d["task_data"],
task_id=d["task_id"],
notes_dir=d["notes_dir"],
notes_ext=d["notes_ext"],
notes_annot=d["notes_annot"],
notes_editor=d["notes_editor"],
notes_quiet=d["notes_quiet"],
)
def parse_conf(conf_file: Path) -> dict: def parse_conf(conf_file: Path) -> dict:
c = configparser.ConfigParser( c = configparser.ConfigParser(
defaults=DEFAULTS_DICT, allow_unnamed_section=True, allow_no_value=True defaults=DEFAULTS_DICT, allow_unnamed_section=True, allow_no_value=True
@ -125,27 +152,27 @@ def main():
# Should probably be done when 'parsing' option object initially # Should probably be done when 'parsing' option object initially
pre_cfg = parse_env() | parse_cli() pre_cfg = parse_env() | parse_cli()
conf_file = Path(pre_cfg.get("task_rc", DEFAULTS_DICT["task_rc"])) conf_file = Path(pre_cfg.get("task_rc", DEFAULTS_DICT["task_rc"]))
cfg = parse_conf(conf_file) | pre_cfg cfg = conf_from_dict(parse_conf(conf_file) | pre_cfg)
if not cfg["task_id"]: if not cfg.task_id:
_ = sys.stderr.write("Please provide task ID as argument.\n") _ = sys.stderr.write("Please provide task ID as argument.\n")
if cfg["notes_quiet"]: if cfg.notes_quiet:
global IS_QUIET global IS_QUIET
IS_QUIET = True IS_QUIET = True
task = get_task(id=cfg["task_id"], data_location=cfg["task_data"]) task = get_task(id=cfg.task_id, data_location=cfg.task_data)
uuid = task["uuid"] uuid = task["uuid"]
if not uuid: if not uuid:
_ = sys.stderr.write(f"Could not find task for ID: {cfg['task_id']}.") _ = sys.stderr.write(f"Could not find task for ID: {cfg.task_id}.")
sys.exit(1) sys.exit(1)
fname = get_notes_file(uuid, notes_dir=cfg["notes_dir"], notes_ext=cfg["notes_ext"]) fname = get_notes_file(uuid, notes_dir=cfg.notes_dir, notes_ext=cfg.notes_ext)
open_editor(fname, editor=cfg["notes_editor"]) open_editor(fname, editor=cfg.notes_editor)
add_annotation_if_missing(task, annotation_content=cfg["notes_annot"]) add_annotation_if_missing(task, annotation_content=cfg.notes_annot)
def get_task(id: str, data_location: str) -> Task: def get_task(id: str | int, data_location: str) -> Task:
# FIXME: This expansion should not be done here # FIXME: This expansion should not be done here
tw = TaskWarrior(os.path.expandvars(data_location)) tw = TaskWarrior(os.path.expandvars(data_location))
try: try: