2021-08-28 21:03:12 +00:00
|
|
|
import sqlite3
|
2021-08-29 09:11:58 +00:00
|
|
|
import re
|
2021-08-28 21:03:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
def migrate(db, habitlist, events):
|
|
|
|
c = db.cursor()
|
2021-08-29 09:11:58 +00:00
|
|
|
habitlist = simple_habits_list(c, habitlist)
|
2021-08-28 21:03:12 +00:00
|
|
|
for event in events:
|
|
|
|
text = event["note"]
|
2021-08-29 09:11:58 +00:00
|
|
|
tags = extract_tags(text)
|
2021-08-28 21:03:12 +00:00
|
|
|
for habit in habitlist:
|
2021-08-29 09:11:58 +00:00
|
|
|
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 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
|
2021-08-29 08:13:28 +00:00
|
|
|
|
|
|
|
|
2021-08-29 09:11:58 +00:00
|
|
|
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]
|
2021-08-28 21:03:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
# does not do:
|
|
|
|
# ranges in habits (should check for habit being range, then taking #habit(3) number into account)
|
|
|
|
# also, range trackers: multiply value by 1,000
|
|
|
|
# non-range habits but still #habit(3) number included, adding multiple?
|
|
|
|
|
|
|
|
|
2021-08-29 08:13:28 +00:00
|
|
|
def add_to_database(cursor, habit_id, timestamp, value=2):
|
|
|
|
try:
|
|
|
|
cursor.execute(
|
|
|
|
"""
|
|
|
|
INSERT INTO
|
|
|
|
Repetitions(id, habit, timestamp, value)
|
|
|
|
VALUES (NULL, ?, ?, ?)
|
|
|
|
""",
|
|
|
|
(habit_id, timestamp, value),
|
|
|
|
)
|
|
|
|
except sqlite3.IntegrityError:
|
|
|
|
# TODO better error handling
|
|
|
|
print("fail to register: not unique")
|
|
|
|
pass
|