helm/home/profiles/task/bin/task-note.py

62 lines
1.9 KiB
Python
Executable File

#! /usr/bin/env cached-nix-shell
#! nix-shell -i python3 -p python3
"""Manage Taskwarrior notes"""
import argparse
import logging
import os
import sys
import datetime
NOTES_DIR = "~/kitaab/vimwiki/task-notes"
EDITOR = os.environ["EDITOR"]
logging.basicConfig(level=logging.DEBUG)
def write_note(task_id: int):
"""Open `$EDITOR` to take notes about task with ID `task_id`."""
task_uuid = os.popen(f"task _get {task_id}.uuid").read().rstrip()
if not task_uuid:
logging.error(f"{task_id} has no UUID!")
sys.exit(1)
logging.debug(f"Task {task_id} has UUID {task_uuid}")
notes_dir = os.path.expanduser(NOTES_DIR)
os.makedirs(notes_dir, exist_ok=True)
notes_basename = f"{task_uuid}.wiki"
notes_file = os.path.join(notes_dir, notes_basename)
logging.debug(f"Notes file is {notes_file}")
if not os.path.exists(notes_file):
logging.info("Adding description to empty notes file")
task_description = os.popen(f"task _get {task_id}.description").read()
task_project = os.popen(f"task _get {task_id}.project").read().strip("\n").replace(".", ":")
task_tags = os.popen(f"task _get {task_id}.tags").read().split(",")
if len(task_tags):
task_tags = ":".join(task_tags).strip("\n") + ":"
time = datetime.datetime.now().strftime("%y-%m-%d %H:%M")
with open(notes_file, "w") as f:
f.write(f"%title {task_description}")
f.write(f":task:{task_project}:{task_tags if len(task_tags) else ''}\n")
f.write(f"%date {time}\n\n")
f.flush()
os.execlp(EDITOR, EDITOR, notes_file)
def update_task(task_id: int):
os.popen(f"task {task_id} modify +note")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Write Taskwarrior notes")
parser.add_argument('task_id', metavar='ID', type=int, help="ID of the task to note")
args = parser.parse_args()
write_note(args.task_id)