jrnlwarrior/open-todo-txt.py

111 lines
2.9 KiB
Python
Raw Normal View History

2021-10-30 17:49:08 +00:00
#!/usr/bin/env python3
# From: https://gist.github.com/varunagrawal/2b93c5dc721520ff6876e940c420ab05
# This hooks script logs any finished tasks currently in the todo.txt file
# pointed to by its settings.
# The on-exit event is triggered once, after all processing is complete.
import re
import os
import sys
import subprocess
2021-10-31 15:20:41 +00:00
import options
2021-10-31 10:40:36 +00:00
2021-10-30 17:49:08 +00:00
2021-10-31 15:20:41 +00:00
def get_todo_block_date(line, todo_block_title):
2021-10-31 11:05:20 +00:00
result = re.search(
2021-10-31 15:20:41 +00:00
rf"^\[(\d{{4}}-\d{{2}}-\d{{2}}).*\] ({re.escape(todo_block_title)}$)?",
2021-10-31 11:05:20 +00:00
line,
)
# we are entering todotxt data block
if result and result[2]:
return result[1]
2021-10-31 15:20:41 +00:00
# we enter block but not one for todotxt
2021-10-31 11:05:20 +00:00
elif result and not result[2]:
return False
# nothing changed, we didn't cross a date line
return "same"
2021-10-31 15:20:41 +00:00
def parse_file(options):
fname = options.jrnl_fname
2021-10-30 17:49:08 +00:00
lines_to_delete = []
with open(fname) as file:
line_number = 0
for line in file:
line_number += 1
2021-10-31 15:20:41 +00:00
todo_block = get_todo_block_date(line, options.todo_block_title)
2021-10-31 11:05:20 +00:00
if todo_block == False:
2021-10-31 15:34:51 +00:00
curdate = ""
2021-10-31 11:05:20 +00:00
continue
if todo_block != "same":
curdate = todo_block
2021-10-30 17:49:08 +00:00
continue
2021-10-31 15:34:51 +00:00
if not curdate:
continue
2021-10-30 17:49:08 +00:00
completed_task = re.search(r"^x ((?:\([A-D]\))? ?.*)$", line)
2021-10-31 15:20:41 +00:00
if completed_task and options.taskwarrior_log_done:
log_done_to_taskwarrior(completed_task[1], curdate, options.dryrun)
2021-10-30 17:49:08 +00:00
lines_to_delete.append(line_number)
if lines_to_delete:
2021-10-31 15:20:41 +00:00
delete_logged_tasks_from_file(fname, lines_to_delete, options.dryrun)
2021-10-30 17:49:08 +00:00
def get_prio_string(task_string):
prio = re.search(r"^\(([ABC])\) (.*)$", task_string)
prio_string = ""
if prio:
task_string = prio[2]
if prio[1] == "A":
prio_string = "prio:H"
elif prio[1] == "B":
prio_string = "prio:M"
elif prio[1] == "C":
prio_string = "prio:L"
return prio_string
2021-10-31 15:20:41 +00:00
def log_done_to_taskwarrior(task_string, date, dryrun):
2021-10-31 14:44:41 +00:00
overrides = ["rc.context=none", "rc.verbose=nothing", "rc.hooks=0"]
2021-10-30 17:49:08 +00:00
cmd = [
"task",
2021-10-31 14:44:41 +00:00
*overrides,
2021-10-30 17:49:08 +00:00
"log",
f'"{task_string}"',
f"entry:{date}",
f"end:{date}",
get_prio_string(task_string),
]
2021-10-31 15:20:41 +00:00
if dryrun:
2021-10-30 17:49:08 +00:00
print(cmd)
return
subprocess.Popen(cmd)
2021-10-31 15:20:41 +00:00
def delete_logged_tasks_from_file(fname, lines, dryrun):
if dryrun:
2021-10-31 10:40:36 +00:00
print(f"deleting lines: {lines}")
2021-10-30 17:49:08 +00:00
return
cur_line = 0
repl_file = fname + ".bak"
with open(fname) as read_file, open(repl_file, "w") as write_file:
for line in read_file:
cur_line += 1
if lines and cur_line == lines[0]:
lines.pop(0)
continue
write_file.write(line)
os.rename(repl_file, fname)
2021-10-31 15:20:41 +00:00
if __name__ == "__main__":
parse_file(options.init())
sys.exit(0)