Refactor date parsing logic

This commit is contained in:
Marty Oehme 2021-10-31 12:05:20 +01:00
parent 73c8c76d3c
commit 63545da224
Signed by: Marty
GPG key ID: B7538B8F50A1C800

View file

@ -12,6 +12,7 @@ import subprocess
import argparse import argparse
TODOTXT_FILE = "/home/marty/documents/records/todo.md" TODOTXT_FILE = "/home/marty/documents/records/todo.md"
TODO_BLOCK_TITLE = "todotxt"
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument( parser.add_argument(
@ -38,26 +39,34 @@ def is_logging_done_to_tw():
return True 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" curdate = "1970-01-01"
lines_to_delete = [] lines_to_delete = []
in_todotxt_block = False
with open(fname) as file: with open(fname) as file:
line_number = 0 line_number = 0
for line in file: for line in file:
line_number += 1 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 continue
completed_task = re.search(r"^x ((?:\([A-D]\))? ?.*)$", line) 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) os.rename(repl_file, fname)
extract_tw_data(TODOTXT_FILE) parse_file(TODOTXT_FILE)
sys.exit(0) 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