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
1 changed files with 30 additions and 7 deletions

View File

@ -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: