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.
This commit is contained in:
Marty Oehme 2025-11-27 19:49:39 +01:00
parent 0d2e68a03d
commit f8f0a2077d
Signed by: Marty
GPG key ID: 4E535BC19C61886E

View file

@ -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()