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:
parent
0d2e68a03d
commit
f8f0a2077d
1 changed files with 14 additions and 16 deletions
30
topen.py
30
topen.py
|
|
@ -122,13 +122,25 @@ class Opt:
|
||||||
help_text: str = ""
|
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] = {
|
OPTIONS: dict[str, Opt] = {
|
||||||
"task_id": Opt(None, None, None, default=None),
|
"task_id": Opt(None, None, None, default=None),
|
||||||
"task_rc": Opt(
|
"task_rc": Opt(
|
||||||
("--task-rc",),
|
("--task-rc",),
|
||||||
"TASKRC",
|
"TASKRC",
|
||||||
None, # taskrc has no key for this
|
None, # taskrc has no key for this
|
||||||
default=Path("~/.taskrc"),
|
default=_determine_default_task_rc(),
|
||||||
metavar="FILE",
|
metavar="FILE",
|
||||||
cast=Path,
|
cast=Path,
|
||||||
help_text="Location of taskwarrior config file",
|
help_text="Location of taskwarrior config file",
|
||||||
|
|
@ -214,8 +226,6 @@ class TConf:
|
||||||
"""If set topen will give no feedback on note editing."""
|
"""If set topen will give no feedback on note editing."""
|
||||||
|
|
||||||
def __post_init__(self):
|
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_rc = _real_path(self.task_rc)
|
||||||
self.task_data = _real_path(self.task_data)
|
self.task_data = _real_path(self.task_data)
|
||||||
if self.notes_dir == NON_EXISTENT_PATH:
|
if self.notes_dir == NON_EXISTENT_PATH:
|
||||||
|
|
@ -225,14 +235,6 @@ class TConf:
|
||||||
def __or__(self, other: Any, /) -> Self:
|
def __or__(self, other: Any, /) -> Self:
|
||||||
return self.__class__(**asdict(self) | asdict(other))
|
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:
|
def _default_notes_dir(self) -> Path:
|
||||||
return self.task_data.joinpath("notes")
|
return self.task_data.joinpath("notes")
|
||||||
|
|
||||||
|
|
@ -250,7 +252,7 @@ def build_config() -> TConf:
|
||||||
cli = parse_cli()
|
cli = parse_cli()
|
||||||
env = parse_env()
|
env = parse_env()
|
||||||
rc_path = Path(
|
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()
|
).expanduser()
|
||||||
rc = parse_rc(rc_path) if rc_path.exists() else {}
|
rc = parse_rc(rc_path) if rc_path.exists() else {}
|
||||||
|
|
||||||
|
|
@ -334,9 +336,5 @@ def whisper(text: str) -> None:
|
||||||
print(text)
|
print(text)
|
||||||
|
|
||||||
|
|
||||||
def _real_path(p: Path | str) -> Path:
|
|
||||||
return Path(os.path.expandvars(p)).expanduser()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue