habitmove/src/habitmove/habits.py

63 lines
1.9 KiB
Python

from habitmove.loopdata import Habit
def migrate(db, trackers):
c = db.cursor()
habits = trackers_to_habits(trackers)
for habit in habits:
existing_habit = check_habit_duplicate(c, habit)
if not existing_habit:
add_to_database(c, habit)
else:
print(f"Found duplicate Habit: {existing_habit} - skipping.")
return habits
# By default set goal to half of max value
NOMIE_MAX_TO_TARGET_VALUE_RATIO = 2
def trackers_to_habits(trackers):
habits = []
for t in trackers:
habits.append(
Habit(
archived=t.hidden,
color=11 if t.score != "-1" else 0,
description=t.tag,
name=f"{t.emoji} {t.label}",
unit="" if t.uom == "num" else t.uom,
uuid=t.id,
)
)
if t.type == "range" and len(habits) > 0:
habits[-1].type = 1
# nomie only has concept of max value,
# use a percentage of it for Loop range target
habits[-1].target_value = int(t.max) // NOMIE_MAX_TO_TARGET_VALUE_RATIO
return habits
def check_habit_duplicate(cursor, habit):
cursor.execute("select name from Habits where uuid = ?", [habit.uuid])
name = cursor.fetchone()
if name:
return name[0]
return False
def add_to_database(cursor, habit):
"""Takes a habit in the form of a dictionary and inserts it into the Habits table.
Parameters:
cursor (db.cursor): SQL executing cursor
habit (Habit): A Loop habit to be added to the database.
"""
habit_data = habit.__dict__
placeholder = ", ".join("?" * len(habit_data))
columns = ", ".join(habit_data.keys())
sql = "insert into `{table}` ({columns}) values ({values});".format(
table="Habits", columns=columns, values=placeholder
)
values = list(habit_data.values())
cursor.execute(sql, values)