ref: Ensure paths are Path objects

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

View file

@ -30,23 +30,27 @@ DEFAULTS_DICT = {
@dataclass() @dataclass()
class TConf: class TConf:
task_rc: str task_rc: Path
task_data: str task_data: Path
task_id: int task_id: int
notes_dir: str notes_dir: Path
notes_ext: str notes_ext: str
notes_annot: str notes_annot: str
notes_editor: str notes_editor: str
notes_quiet: bool notes_quiet: bool
def _real_path(p: Path | str) -> Path:
return Path(os.path.expandvars(p)).expanduser()
def conf_from_dict(d: dict) -> TConf: def conf_from_dict(d: dict) -> TConf:
return TConf( return TConf(
task_rc=d["task_rc"], task_rc=_real_path(d["task_rc"]),
task_data=d["task_data"], task_data=_real_path(d["task_data"]),
task_id=d["task_id"], task_id=d["task_id"],
notes_dir=d["notes_dir"], notes_dir=_real_path(d["notes_dir"]),
notes_ext=d["notes_ext"], notes_ext=d["notes_ext"],
notes_annot=d["notes_annot"], notes_annot=d["notes_annot"],
notes_editor=d["notes_editor"], notes_editor=d["notes_editor"],
@ -148,11 +152,10 @@ def whisper(text: str) -> None:
def main(): def main():
# TODO: Don't forget to expand user (path.expanduser) and expand vars (os.path.expandvars) opts_overwrite = {"task_rc": DEFAULTS_DICT["task_rc"]} | parse_env() | parse_cli()
# Should probably be done when 'parsing' option object initially conf_file = _real_path(opts_overwrite["task_rc"])
pre_cfg = parse_env() | parse_cli() opts: dict = parse_conf(conf_file) | opts_overwrite
conf_file = Path(pre_cfg.get("task_rc", DEFAULTS_DICT["task_rc"])) cfg = conf_from_dict(opts)
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")
@ -172,9 +175,8 @@ def main():
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 | int, data_location: str) -> Task: def get_task(id: str | int, data_location: Path) -> Task:
# FIXME: This expansion should not be done here tw = TaskWarrior(data_location)
tw = TaskWarrior(os.path.expandvars(data_location))
try: try:
t = tw.tasks.get(id=id) t = tw.tasks.get(id=id)
except Task.DoesNotExist: except Task.DoesNotExist:
@ -183,7 +185,7 @@ def get_task(id: str | int, data_location: str) -> Task:
return t return t
def get_notes_file(uuid: str, notes_dir: str, notes_ext: str) -> Path: def get_notes_file(uuid: str, notes_dir: Path, notes_ext: str) -> Path:
return Path(notes_dir).joinpath(f"{uuid}.{notes_ext}") return Path(notes_dir).joinpath(f"{uuid}.{notes_ext}")