From 63545da224f59d1cf25a39594935f744eca99577 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Sun, 31 Oct 2021 12:05:20 +0100 Subject: [PATCH] Refactor date parsing logic --- open-todo-txt.py | 49 +++++++++++++++++++++++------------------------- 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/open-todo-txt.py b/open-todo-txt.py index 9733c0c..e445d11 100755 --- a/open-todo-txt.py +++ b/open-todo-txt.py @@ -12,6 +12,7 @@ import subprocess import argparse TODOTXT_FILE = "/home/marty/documents/records/todo.md" +TODO_BLOCK_TITLE = "todotxt" parser = argparse.ArgumentParser() parser.add_argument( @@ -38,26 +39,34 @@ def is_logging_done_to_tw(): return True -def extract_tw_data(fname): +def get_todo_block_date(line): + result = re.search( + rf"^\[(\d{{4}}-\d{{2}}-\d{{2}}).*\] ({re.escape(TODO_BLOCK_TITLE)}$)?", + line, + ) + # we are entering todotxt data block + if result and result[2]: + return result[1] + # we are in block but not one for todotxt + elif result and not result[2]: + return False + # nothing changed, we didn't cross a date line + return "same" + + +def parse_file(fname): curdate = "1970-01-01" lines_to_delete = [] - in_todotxt_block = False with open(fname) as file: line_number = 0 for line in file: line_number += 1 - newdate = re.search(r"^\[(\d{4}-\d{2}-\d{2}).*\] (todotxt$)?", line) - # we are entering todotxt data block - if newdate and newdate[2]: - curdate = newdate[1] - in_todotxt_block = True - continue - # we are in block but not one for todotxt - elif newdate and not newdate[2]: - in_todotxt_block = False - continue - if not in_todotxt_block: + todo_block = get_todo_block_date(line) + if todo_block == False: + continue + if todo_block != "same": + curdate = todo_block continue completed_task = re.search(r"^x ((?:\([A-D]\))? ?.*)$", line) @@ -120,17 +129,5 @@ def delete_logged_tasks_from_file(fname, lines): os.rename(repl_file, fname) -extract_tw_data(TODOTXT_FILE) +parse_file(TODOTXT_FILE) sys.exit(0) - -# The on-launch event is triggered once, after initialization, before any -# processing occurs. This hooks script has no effect on processing. - -# Output: -# - Optional feedback/error. -# echo 'on-launch' - -# Status: -# - 0: JSON ignored, non-JSON is feedback. -# - non-0: JSON ignored, non-JSON is error. -# exit 0