diff --git a/open-todo-txt.py b/open-todo-txt.py index d8df90b..e7aa47a 100755 --- a/open-todo-txt.py +++ b/open-todo-txt.py @@ -8,8 +8,12 @@ import re import os import sys -import subprocess +from subprocess import Popen, PIPE, check_output import options as opts +import json +from datetime import datetime + +TODAY = datetime.today().strftime("%Y-%m-%d") def get_todo_block_date(line, todo_block_title): @@ -30,6 +34,7 @@ def get_todo_block_date(line, todo_block_title): def process_file(options): fname = options.jrnl_fname lines_to_delete = [] + today_exists = False with open(fname) as file: line_number = 0 for line in file: @@ -51,9 +56,15 @@ def process_file(options): if handle_open_tasks(line, curdate, options): lines_to_delete.append(line_number) + if is_today(curdate): + today_exists = True + if lines_to_delete: delete_lines_from_file(fname, lines_to_delete, options.dryrun) + if not today_exists: + add_today(fname, options) + def handle_completed_tasks(line, date, options): completed_task = re.search( @@ -80,7 +91,7 @@ def log_completed_to_taskwarrior(task_string, date, options): if options.dryrun: print(cmd) return - subprocess.Popen(cmd) + Popen(cmd) def handle_open_tasks(line, date, options): @@ -108,7 +119,7 @@ def add_incomplete_to_taskwarrior(task_string, date, options): if options.dryrun: print(cmd) return - subprocess.Popen(cmd) + Popen(cmd) def get_prio_string(task_string): @@ -142,6 +153,30 @@ def delete_lines_from_file(fname, lines, dryrun): os.rename(repl_file, fname) +def is_today(cur_date): + if cur_date == TODAY: + return True + return False + + +def add_today(fname, options): + cmd = ["task", "+TODAY or +OVERDUE", "export"] + tasks = f"[{TODAY} 21:00] {options.todo_block_title}\n" + for task in json.loads(check_output(cmd)): + tasks += f"{task['description']}\n" + + if options.dryrun: + print(f"\nWRITING TODAY:\n{tasks}") + return + + repl_file = fname + ".bak" + with open(fname) as read_file, open(repl_file, "w") as write_file: + write_file.write(tasks + "\n") + for line in read_file: + write_file.write(line) + os.rename(repl_file, fname) + + if __name__ == "__main__": process_file(opts.init()) sys.exit(0)