Refactor repetition habit list

This commit is contained in:
Marty Oehme 2021-08-29 11:11:58 +02:00
parent 269a255885
commit 18cdd34300
Signed by: Marty
GPG key ID: B7538B8F50A1C800

View file

@ -1,20 +1,43 @@
import sqlite3 import sqlite3
import re
def migrate(db, habitlist, events): def migrate(db, habitlist, events):
c = db.cursor() c = db.cursor()
habitlist = simple_habits_list(c, habitlist)
for event in events: for event in events:
text = event["note"] text = event["note"]
tags = extract_tags(text)
for habit in habitlist: for habit in habitlist:
tag = f"#{habit['description']}" habit_tag = f"#{habit['description']}"
if tag in text: if habit_tag in tags:
print(f"register: {habit['name']} in {text} at {event['end']}") print(f"register event: {habit['name']} in {text} at {event['end']}")
add_to_database(c, fetch_habit_id(c, habit), event["end"]) add_to_database(c, habit["id"], event["end"])
def fetch_habit_id(c, habit): def extract_tags(text, tagmarker="#"):
c.execute("select id from Habits where uuid = ?", [(habit["uuid"])]) found_tags = re.findall(rf"{tagmarker}\w+(?:\(\d+\))?", text)
return c.fetchone()[0] 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: # does not do: