Extract code into individual functions

This commit is contained in:
Marty Oehme 2025-03-31 21:01:35 +02:00
parent 0769d65610
commit dca5ebb9fb
Signed by: Marty
GPG key ID: 4E535BC19C61886E

97
topen
View file

@ -5,6 +5,7 @@
# Edits an existing task note file,
# or creates a new one.
import argparse
import subprocess
import sys
from pathlib import Path
@ -13,38 +14,88 @@ from tasklib import Task, TaskWarrior
TASK_DATA_DIR = "~/.local/share/task"
TASKNOTES_DIR = "~/.local/share/task/notes"
TASKNOTES_EXT = "md"
TASKNOTES_ANNOT = "Note"
TOPEN_DIR = "~/.local/share/task/notes"
TOPEN_EXT = "md"
TOPEN_ANNOT = "Note"
tw = TaskWarrior(data_location=TASK_DATA_DIR)
args = sys.argv
def parse_cli() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Taskwarrior note editing made easy.")
_ = parser.add_argument(
"id", help="The id/uuid of the taskwarrior task for which we edit notes"
)
_ = parser.add_argument(
"-d",
"--notes-dir",
default=TOPEN_DIR,
help="Location of topen notes",
)
_ = parser.add_argument(
"-x", "--extension", default=TOPEN_EXT, help="Extension of note files"
)
_ = parser.add_argument(
"-t",
"--annotation",
default=TOPEN_ANNOT,
help="Annotation content to set within taskwarrior",
)
_ = parser.add_argument(
"--task-data", default=TASK_DATA_DIR, help="Location of taskwarrior data"
)
if not len(args) == 2:
_ = sys.stderr.write("Please provide task ID as argument.\n")
sys.exit(1)
return parser.parse_args()
given_id = args[1]
try:
t = tw.tasks.get(id=given_id)
except Task.DoesNotExist:
t = tw.tasks.get(uuid=given_id)
def main():
args = parse_cli()
_ = sys.stderr.write(f"Editing note for: {t['description']} ({t['uuid']})\n")
if not args.id:
_ = sys.stderr.write("Please provide task ID as argument.\n")
notes_file = Path(TASKNOTES_DIR).joinpath(f"{t['uuid']}.{TASKNOTES_EXT}")
task = get_task(id=args.id, data_location=args.task_data)
uuid = task["uuid"]
if not uuid:
_ = sys.stderr.write(f"Could not find task for id: {args.id}.")
sys.exit(1)
fname = get_notes_file(uuid, notes_dir=args.notes_dir, notes_ext=args.extension)
proc = subprocess.Popen(f"nvim {notes_file}", shell=True)
_ = proc.wait()
# TODO: Add editor choice
open_editor(fname)
def add_annotation_if_missing(task: Task) -> None:
for annot in t["annotations"]:
if annot["description"] == "Note":
add_annotation_if_missing(task, annotation_content=args.annotation)
def get_task(id: str, data_location: str = TASK_DATA_DIR) -> Task:
tw = TaskWarrior(data_location)
try:
t = tw.tasks.get(id=id)
except Task.DoesNotExist:
t = tw.tasks.get(uuid=id)
return t
def get_notes_file(
uuid: str, notes_dir: str = TOPEN_DIR, notes_ext: str = TOPEN_EXT
) -> Path:
return Path(notes_dir).joinpath(f"{uuid}.{notes_ext}")
def open_editor(file: Path, editor: str = "nvim") -> None:
_ = sys.stderr.write(f"Editing note {file}\n")
proc = subprocess.Popen(f"{editor} {file}", shell=True)
_ = proc.wait()
def add_annotation_if_missing(
task: Task, annotation_content: str = TOPEN_ANNOT
) -> None:
for annot in task["annotations"] or []:
if annot["description"] == annotation_content:
return
t.add_annotation(TASKNOTES_ANNOT)
_ = sys.stderr.write(f"Added annotation.\n")
task.add_annotation(annotation_content)
_ = sys.stderr.write("Added annotation.\n")
add_annotation_if_missing(t)
if __name__ == "__main__":
main()