ref: Extract configuration assembly into separate function
This commit is contained in:
parent
c164be29d3
commit
0d2e68a03d
1 changed files with 20 additions and 5 deletions
25
topen.py
25
topen.py
|
|
@ -30,7 +30,7 @@ from tasklib import Task, TaskWarrior
|
||||||
NON_EXISTENT_PATH = Path("%%%%I_DONT_EXIST_%%%%")
|
NON_EXISTENT_PATH = Path("%%%%I_DONT_EXIST_%%%%")
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main(cfg: "TConf | None" = None):
|
||||||
"""Runs the cli interface.
|
"""Runs the cli interface.
|
||||||
|
|
||||||
First sets up the correct options, with overrides in the following order:
|
First sets up the correct options, with overrides in the following order:
|
||||||
|
|
@ -43,10 +43,8 @@ def main():
|
||||||
|
|
||||||
If the task does not yet have a note annotation it also adds it automatically.
|
If the task does not yet have a note annotation it also adds it automatically.
|
||||||
"""
|
"""
|
||||||
opts_override = {"task_rc": TConf(0).task_rc} | parse_env() | parse_cli()
|
if not cfg:
|
||||||
conf_file = _real_path(opts_override["task_rc"])
|
cfg = build_config()
|
||||||
opts: dict = parse_rc(conf_file) | opts_override
|
|
||||||
cfg = TConf.from_dict(opts)
|
|
||||||
|
|
||||||
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")
|
||||||
|
|
@ -247,6 +245,23 @@ class TConf:
|
||||||
return cls(**d)
|
return cls(**d)
|
||||||
|
|
||||||
|
|
||||||
|
def build_config() -> TConf:
|
||||||
|
"""Return final configuration object."""
|
||||||
|
cli = parse_cli()
|
||||||
|
env = parse_env()
|
||||||
|
rc_path = Path(
|
||||||
|
cli.get("task_rc") or env.get("task_rc") or TConf(0).task_rc
|
||||||
|
).expanduser()
|
||||||
|
rc = parse_rc(rc_path) if rc_path.exists() else {}
|
||||||
|
|
||||||
|
# we use the 'parsed' XDG-included taskrc locations for defaults
|
||||||
|
defaults = {k: opt.default for k, opt in OPTIONS.items()}
|
||||||
|
defaults["task_rc"] = rc_path
|
||||||
|
|
||||||
|
merged = defaults | rc | env | cli # later wins
|
||||||
|
return TConf.from_dict({k: v for k, v in merged.items() if v is not None})
|
||||||
|
|
||||||
|
|
||||||
def parse_cli() -> dict:
|
def parse_cli() -> dict:
|
||||||
"""Parse cli options and arguments.
|
"""Parse cli options and arguments.
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue