Refactor repetitions, Add comments
This commit is contained in:
parent
e050acd288
commit
82054087f9
1 changed files with 37 additions and 8 deletions
|
@ -4,21 +4,35 @@ import re
|
||||||
|
|
||||||
def migrate(db, habitlist, events):
|
def migrate(db, habitlist, events):
|
||||||
c = db.cursor()
|
c = db.cursor()
|
||||||
habitlist = simple_habits_list(c, habitlist)
|
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 = []
|
||||||
for event in events:
|
for event in events:
|
||||||
text = event["note"]
|
text = event["note"]
|
||||||
tags = extract_tags(text)
|
tags = extract_tags(text)
|
||||||
for habit in habitlist:
|
for habit in habits:
|
||||||
for tag in tags:
|
for tag in tags:
|
||||||
if habit["description"] in tag[0]:
|
if habit["description"] in tag[0]:
|
||||||
print(
|
print(f"found event: {habit['name']} in {text} at {event['end']}")
|
||||||
f"register event: {habit['name']} in {text} at {event['end']}"
|
repetitions.append({"id": habit["id"], "timestamp": event["end"]})
|
||||||
)
|
return repetitions
|
||||||
add_to_database(c, habit["id"], event["end"])
|
|
||||||
|
|
||||||
|
|
||||||
def extract_tags(text, tagmarker="#"):
|
def extract_tags(text, tagmarker="#"):
|
||||||
"""Returns lists of tuples of all event tags found in text.
|
"""Return lists of tuples of all event tags found in text.
|
||||||
Parameters:
|
Parameters:
|
||||||
text (str): The text to search through.
|
text (str): The text to search through.
|
||||||
tagmarker (str): Optional character marking beginning of tag, defaults to '#'.
|
tagmarker (str): Optional character marking beginning of tag, defaults to '#'.
|
||||||
|
@ -30,6 +44,14 @@ def extract_tags(text, tagmarker="#"):
|
||||||
|
|
||||||
|
|
||||||
def simple_habits_list(c, habitlist):
|
def simple_habits_list(c, habitlist):
|
||||||
|
"""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.
|
||||||
|
"""
|
||||||
simple = []
|
simple = []
|
||||||
for h in habitlist:
|
for h in habitlist:
|
||||||
simple.append(
|
simple.append(
|
||||||
|
@ -43,6 +65,13 @@ def simple_habits_list(c, habitlist):
|
||||||
|
|
||||||
|
|
||||||
def fetch_habit_id(c, uuid):
|
def fetch_habit_id(c, uuid):
|
||||||
|
"""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.
|
||||||
|
"""
|
||||||
c.execute("select id from Habits where uuid = ?", ([uuid]))
|
c.execute("select id from Habits where uuid = ?", ([uuid]))
|
||||||
id = c.fetchone()
|
id = c.fetchone()
|
||||||
if id is not None:
|
if id is not None:
|
||||||
|
@ -67,5 +96,5 @@ def add_to_database(cursor, habit_id, timestamp, value=2):
|
||||||
)
|
)
|
||||||
except sqlite3.IntegrityError:
|
except sqlite3.IntegrityError:
|
||||||
# TODO better error handling
|
# TODO better error handling
|
||||||
print("fail to register: not unique")
|
print(f"fail to register {habit_id}: timestamp {timestamp} not unique")
|
||||||
pass
|
pass
|
||||||
|
|
Loading…
Reference in a new issue