habitmove/migrate.py

93 lines
2.9 KiB
Python
Raw Normal View History

2021-08-26 19:57:49 +00:00
#!/usr/bin/env python
import json
2021-08-26 21:13:56 +00:00
import pandas as pd
2021-08-26 19:57:49 +00:00
import sqlite3
def load_file(filename):
with open(filename) as f:
nomie_data = json.load(f)
return nomie_data
2021-08-26 21:13:56 +00:00
def get_dataframe(nomie_data):
return pd.DataFrame(nomie_data["events"])
# generate a yes/no cli question with a default answer
2021-08-26 19:57:49 +00:00
def confirmation_question(question, default_no=True):
choices = " [y/N]: " if default_no else " [Y/n]: "
default_answer = "n" if default_no else "y"
reply = str(input(question + choices)).lower().strip() or default_answer
if reply[0] == "y":
return True
if reply[0] == "n":
return False
else:
return False if default_no else True
2021-08-26 21:13:56 +00:00
# display stats and ask user to confirm if they seem okay
2021-08-26 19:57:49 +00:00
def verify_continue(data):
trackers = ""
for t in data["trackers"]:
trackers += t + ", "
print(f"Found trackers: {trackers}")
print(f"with {len(data['events'])} event entries.")
if not confirmation_question("Do you want to continue?", default_no=False):
print("Aborted.")
exit(0)
def create_database(name):
return sqlite3.connect(name)
2021-08-26 21:13:56 +00:00
def create_loop_tables(db):
c = db.cursor()
c.execute(
""" CREATE TABLE IF NOT EXISTS Habits (
id integer PRIMARY KEY,
archived integer,
color integer,
description text,
freq_den integer,
freq_num integer,
highlight integer,
name text NOT NULL,
position integer,
reminder_hour integer,
reminder_min integer,
reminder_days integer,
type integer,
target_type integer,
target_value real,
unit text,
question text,
uuid text NOT NULL
); """
)
c.execute(
""" CREATE TABLE IF NOT EXISTS Repetitions (
id integer PRIMARY KEY,
habit integer,
timestamp integer,
value integer
); """
)
2021-08-26 19:57:49 +00:00
if __name__ == "__main__":
# load nomie json
nomie_data = load_file("input.json")
2021-08-26 21:13:56 +00:00
# DISABLED FOR DEBUGGING
2021-08-26 19:57:49 +00:00
verify_continue(nomie_data)
2021-08-26 21:13:56 +00:00
# bring up dataframe
df = get_dataframe(nomie_data)
2021-08-26 19:57:49 +00:00
# prep sqlite database
db = create_database("output.db")
2021-08-26 21:13:56 +00:00
create_loop_tables(db)