From f8f0a2077d93be2ec7e21f28a973c91464391cb0 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Thu, 27 Nov 2025 19:49:39 +0100 Subject: [PATCH] ref: Extract determining default taskrc from TConf class To start streamlining the TConf class a little, by removing some internal logic which may be better residing in the Options dict, we extract determining the correct taskrc file. --- topen.py | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/topen.py b/topen.py index 1da9452..a35f494 100755 --- a/topen.py +++ b/topen.py @@ -122,13 +122,25 @@ class Opt: help_text: str = "" +def _real_path(p: Path | str) -> Path: + return Path(os.path.expandvars(p)).expanduser() + + +def _determine_default_task_rc() -> Path: + if _real_path("~/.taskrc").exists(): + return _real_path("~/.taskrc") + if _real_path("$XDG_CONFIG_HOME/task/taskrc").exists(): + return _real_path("$XDG_CONFIG_HOME/task/taskrc") + return _real_path("~/.config/task/taskrc") + + OPTIONS: dict[str, Opt] = { "task_id": Opt(None, None, None, default=None), "task_rc": Opt( ("--task-rc",), "TASKRC", None, # taskrc has no key for this - default=Path("~/.taskrc"), + default=_determine_default_task_rc(), metavar="FILE", cast=Path, help_text="Location of taskwarrior config file", @@ -214,8 +226,6 @@ class TConf: """If set topen will give no feedback on note editing.""" def __post_init__(self): - if self.task_rc == NON_EXISTENT_PATH: - self.task_rc = self._default_task_rc() self.task_rc = _real_path(self.task_rc) self.task_data = _real_path(self.task_data) if self.notes_dir == NON_EXISTENT_PATH: @@ -225,14 +235,6 @@ class TConf: def __or__(self, other: Any, /) -> Self: return self.__class__(**asdict(self) | asdict(other)) - def _default_task_rc(self) -> Path: - if Path("~/.taskrc").exists(): - return Path("~/.taskrc") - elif Path("$XDG_CONFIG_HOME/task/taskrc").exists(): - return Path("$XDG_CONFIG_HOME/task/taskrc") - else: - return Path("~/.config/task/taskrc") - def _default_notes_dir(self) -> Path: return self.task_data.joinpath("notes") @@ -250,7 +252,7 @@ def build_config() -> TConf: cli = parse_cli() env = parse_env() rc_path = Path( - cli.get("task_rc") or env.get("task_rc") or TConf(0).task_rc + cli.get("task_rc") or env.get("task_rc") or OPTIONS["task_rc"].default ).expanduser() rc = parse_rc(rc_path) if rc_path.exists() else {} @@ -334,9 +336,5 @@ def whisper(text: str) -> None: print(text) -def _real_path(p: Path | str) -> Path: - return Path(os.path.expandvars(p)).expanduser() - - if __name__ == "__main__": main()