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-30 08:39:07 +00:00
|
|
|
habits = simple_habits_list(c, habitlist)
|
|
|
|
repetitions = extract_all_repetitions(habits, events)
|
|
|
|
for rep in repetitions:
|
|
|
|
add_to_database(c, rep["id"], rep["timestamp"])
|
|
|
|
|
|
|
|
|
|
|
|
def extract_all_repetitions(habits, events):
|
|
|
|
"""Return list of all repetitions found of habits in events passed in.
|
|
|
|
Parameters:
|
|
|
|
habits (list): Collection of habits, with minimum necessary fields description and id.
|
|
|
|
events (list): Collection of events, with minimum necessary field end.
|
|
|
|
Returns:
|
|
|
|
repetitions (list): Collection of events transformed into Loop repetitions.
|
|
|
|
Contains fields id, timestamp, value (for ranges).
|
|
|
|
"""
|
|
|
|
repetitions = []
|
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-30 08:39:07 +00:00
|
|
|
for habit in habits:
|
2021-08-29 09:22:55 +00:00
|
|
|
for tag in tags:
|
|
|
|
if habit["description"] in tag[0]:
|
2021-08-30 08:39:07 +00:00
|
|
|
print(f"found event: {habit['name']} in {text} at {event['end']}")
|
|
|
|
repetitions.append({"id": habit["id"], "timestamp": event["end"]})
|
|
|
|
return repetitions
|
2021-08-29 09:11:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
def extract_tags(text, tagmarker="#"):
|
2021-08-30 08:39:07 +00:00
|
|
|
"""Return lists of tuples of all event tags found in text.
|
2021-08-29 09:22:55 +00:00
|
|
|
Parameters:
|
|
|
|
text (str): The text to search through.
|
|
|
|
tagmarker (str): Optional character marking beginning of tag, defaults to '#'.
|
|
|
|
Returns:
|
|
|
|
tags (list): List of tuples in the form [('tag', '3'), ('anothertag', '')].
|
|
|
|
"""
|
|
|
|
found_tags = re.findall(rf"{tagmarker}(\w+)(?:\((\d+)\))?", text)
|
2021-08-29 09:11:58 +00:00
|
|
|
return found_tags
|
|
|
|
|
|
|
|
|
|
|
|
def simple_habits_list(c, habitlist):
|
2021-08-30 08:39:07 +00:00
|
|
|
"""Return simplified collection of habits, only carrying name, tag, id.
|
|
|
|
Parameters:
|
|
|
|
c (sqlite.db.cursor): SQL cursor of database to query.
|
|
|
|
habitlist (list): Full habit collection to return a simplified view of.
|
|
|
|
Returns:
|
|
|
|
habitlist (list): Simple habitlist, habit name, tag, and id.
|
|
|
|
Copy of original, changes do not affect original.
|
|
|
|
"""
|
2021-08-29 09:11:58 +00:00
|
|
|
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):
|
2021-08-30 08:39:07 +00:00
|
|
|
"""Return sqlite internal id for habit with uuid.
|
|
|
|
Parameters:
|
|
|
|
c (sqlite.db.cursor): SQL cursor of database to query.
|
|
|
|
uuid (str): Unique id of habit to query for.
|
|
|
|
Returns:
|
|
|
|
id (int): SQLite internal id for habit queried for.
|
|
|
|
"""
|
2021-08-29 09:11:58 +00:00
|
|
|
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
|
2021-08-30 08:39:07 +00:00
|
|
|
print(f"fail to register {habit_id}: timestamp {timestamp} not unique")
|
2021-08-29 08:13:28 +00:00
|
|
|
pass
|