diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ad0ec2..87d1b3c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ and this project tries to adhere to [Semantic Versioning](https://semver.org/spe ## [Unreleased] * Add range habits, translating nomie max value to range target in Loop +* Add repetition import, translated from events + * Does not do ranged habits or multiple entries for simple trackers in nomie yet ## [0.2.1] - 2021-08-28 diff --git a/loop/habits.py b/loop/habits.py index fca3af2..a7298db 100644 --- a/loop/habits.py +++ b/loop/habits.py @@ -7,6 +7,7 @@ def migrate(db, trackers): add_to_database(c, habit) else: print(f"Found duplicate Habit: {existing_habit} - skipping.") + return habits def trackers_to_habits(trackers): @@ -17,7 +18,7 @@ def trackers_to_habits(trackers): { "archived": 0 if t["hidden"] == False else 1, "color": 11 if t.get("score", 0) != "-1" else 0, - "description": t["label"], + "description": t["tag"], "freq_den": 1, "freq_num": 1, "highlight": 0, @@ -50,47 +51,3 @@ def add_to_database(cursor, habit_dict): ) values = list(habit_dict.values()) cursor.execute(sql, values) - - -# grab next tracker info - -# look for unique id of next habit -# if exists skip and warn user - -# insert tracker into habit table - -# FOR REPETITION MIGRATION -# c.execute( -# """ -# INSERT INTO -# Repetitions(id, habit, timestamp, value) -# VALUES (NULL, 5, 1629676800000, 2) -# """ -# ) -# c.execute( -# """ -# INSERT INTO -# Repetitions(id, habit, timestamp, value) -# VALUES (NULL, 6, 1629676800000, 2) -# """ -# ) -# """ -# ) -# """ -# ) -# """ -# ) -# """ -# ) -# """ -# ) -# """ -# ) -# """ -# ) -# """ -# ) -# """ -# ) -# """ -# ) diff --git a/loop/migration.py b/loop/migration.py index a3a2c23..3cc9501 100644 --- a/loop/migration.py +++ b/loop/migration.py @@ -1,10 +1,16 @@ import loop.tables as tables import loop.habits as habits +import loop.repetitions as rep def migrate(trackers, events): db = tables.migrate("output.db") if trackers is not None: - habits.migrate(db, trackers) + + habitlist = habits.migrate(db, trackers) + + if events is not None: + rep.migrate(db, habitlist, events) + db.commit() db.close() diff --git a/loop/repetitions.py b/loop/repetitions.py new file mode 100644 index 0000000..f7d8113 --- /dev/null +++ b/loop/repetitions.py @@ -0,0 +1,33 @@ +import sqlite3 + + +def migrate(db, habitlist, events): + c = db.cursor() + for event in events: + text = event["note"] + for habit in habitlist: + if f"#{habit['description']}" in text: + c.execute("select id from Habits where uuid = ?", [(habit["uuid"])]) + id = c.fetchone()[0] + print(f"found: {habit['name']} in {text}") + try: + add_repetition(c, id, event["end"]) + except sqlite3.IntegrityError: + pass + + +# 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? + + +def add_repetition(cursor, habit_id, timestamp, value=2): + cursor.execute( + """ + INSERT INTO + Repetitions(id, habit, timestamp, value) + VALUES (NULL, ?, ?, ?) + """, + (habit_id, timestamp, value), + )