nix config
at deck 61 lines 2.0 kB view raw
1#! /usr/bin/env cached-nix-shell 2#! nix-shell -i python3 -p python3 3 4"""Manage Taskwarrior notes""" 5 6import argparse 7import logging 8import os 9import sys 10import datetime 11 12NOTES_DIR = "~/kitaab/vimwiki/task-notes" 13EDITOR = os.environ["EDITOR"] 14 15logging.basicConfig(level=logging.DEBUG) 16 17 18def write_note(task_id: int): 19 """Open `$EDITOR` to take notes about task with ID `task_id`.""" 20 task_uuid = os.popen(f"task _get {task_id}.uuid").read().rstrip() 21 22 if not task_uuid: 23 logging.error(f"{task_id} has no UUID!") 24 sys.exit(1) 25 26 logging.debug(f"Task {task_id} has UUID {task_uuid}") 27 28 notes_dir = os.path.expanduser(NOTES_DIR) 29 os.makedirs(notes_dir, exist_ok=True) 30 notes_basename = f"{task_uuid}.wiki" 31 notes_file = os.path.join(notes_dir, notes_basename) 32 logging.debug(f"Notes file is {notes_file}") 33 34 if not os.path.exists(notes_file): 35 logging.info("Adding description to empty notes file") 36 task_description = os.popen(f"task _get {task_id}.description").read() 37 task_project = os.popen(f"task _get {task_id}.project").read().strip("\n").replace(".", ":") 38 task_tags = os.popen(f"task _get {task_id}.tags").read().split(",") 39 if len(task_tags): 40 task_tags = ":".join(task_tags).strip("\n") + ":" 41 time = datetime.datetime.now().strftime("%y-%m-%d %H:%M") 42 43 with open(notes_file, "w") as f: 44 f.write(f"%title {task_description}") 45 f.write(f":task:{task_project}:{task_tags if len(task_tags) else ''}\n") 46 f.write(f"%date {time}\n\n") 47 f.flush() 48 49 os.execlp(EDITOR, EDITOR, notes_file) 50 51 52def update_task(task_id: int): 53 os.popen(f"task {task_id} modify +note") 54 55 56if __name__ == "__main__": 57 parser = argparse.ArgumentParser(description="Write Taskwarrior notes") 58 parser.add_argument('task_id', metavar='ID', type=int, help="ID of the task to note") 59 args = parser.parse_args() 60 61 write_note(args.task_id)