From 18cdd34300f3a3bad5d5f612e8f8233ba3a7c1c2 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Sun, 29 Aug 2021 11:11:58 +0200 Subject: [PATCH] Refactor repetition habit list --- loop/repetitions.py | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/loop/repetitions.py b/loop/repetitions.py index d70281f..d265e80 100644 --- a/loop/repetitions.py +++ b/loop/repetitions.py @@ -1,20 +1,43 @@ import sqlite3 +import re def migrate(db, habitlist, events): c = db.cursor() + habitlist = simple_habits_list(c, habitlist) for event in events: text = event["note"] + tags = extract_tags(text) for habit in habitlist: - tag = f"#{habit['description']}" - if tag in text: - print(f"register: {habit['name']} in {text} at {event['end']}") - add_to_database(c, fetch_habit_id(c, habit), event["end"]) + habit_tag = f"#{habit['description']}" + if habit_tag in tags: + print(f"register event: {habit['name']} in {text} at {event['end']}") + add_to_database(c, habit["id"], event["end"]) -def fetch_habit_id(c, habit): - c.execute("select id from Habits where uuid = ?", [(habit["uuid"])]) - return c.fetchone()[0] +def extract_tags(text, tagmarker="#"): + found_tags = re.findall(rf"{tagmarker}\w+(?:\(\d+\))?", text) + return found_tags + + +def simple_habits_list(c, habitlist): + simple = [] + for h in habitlist: + simple.append( + { + "name": h["name"], + "description": h["description"], + "id": fetch_habit_id(c, h["uuid"]), + } + ) + return simple + + +def fetch_habit_id(c, uuid): + c.execute("select id from Habits where uuid = ?", ([uuid])) + id = c.fetchone() + if id is not None: + return id[0] # does not do: