helm/users/profiles/desktop/bin/task-note.py

58 lines
1.8 KiB
Python
Raw Normal View History

2022-05-22 07:54:04 -07:00
#! /usr/bin/env cached-nix-shell
#! nix-shell -i python3 -p python3
2022-05-22 06:20:46 -07:00
"""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}.md"
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()
2022-05-22 07:54:04 -07:00
task_project = os.popen(f"task _get {task_id}.project").read().strip("\n")
task_tags = os.popen(f"task _get {task_id}.tags").read().split(",")
if len(task_tags):
task_tags = ":".join(task_tags).strip("\n") + ":"
2022-05-22 06:20:46 -07:00
time = datetime.datetime.now().strftime("%y-%m-%d %H:%M")
with open(notes_file, "w") as f:
2022-05-22 07:54:04 -07:00
f.write(f"%title: {task_description}")
f.write(f":task:{task_project}:{task_tags if len(task_tags) else ''}\n")
2022-05-22 06:20:46 -07:00
f.write(f"%date: {time}\n\n")
f.flush()
os.execlp(EDITOR, EDITOR, notes_file)
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)