102 lines
3.0 KiB
Python
102 lines
3.0 KiB
Python
#!/usr/bin/env python
|
|
|
|
import json
|
|
import re
|
|
from habitmove.nomiedata import Tracker, Event, Activity, NomieImport
|
|
|
|
|
|
def load_file(filename):
|
|
with open(filename) as f:
|
|
nomie_data = json.load(f)
|
|
return nomie_data
|
|
|
|
|
|
# generate a yes/no cli question with a default answer
|
|
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
|
|
|
|
|
|
# display stats and ask user to confirm if they seem okay
|
|
def verify_continue(data: NomieImport):
|
|
trackers = ""
|
|
for t in data.trackers:
|
|
trackers += t.label + ", "
|
|
trackers = trackers[:-2]
|
|
|
|
print(f"Exporting from nomie {data.version}:")
|
|
print(f"Found trackers: {trackers}")
|
|
print(
|
|
f"Found events: {len(data.events)} entries, containing {len(data.activities)} individual activities."
|
|
)
|
|
if not confirmation_question("Do you want to continue?", default_no=False):
|
|
print("Aborted.")
|
|
exit(0)
|
|
|
|
|
|
def get_trackers(raw_trackers):
|
|
tracker_list = list[Tracker]()
|
|
for tracker_tuple in raw_trackers.items():
|
|
tracker_list.append(Tracker(**tracker_tuple[1]))
|
|
return tracker_list
|
|
|
|
|
|
def get_notes(raw_notes):
|
|
notes = list[Event]()
|
|
for note in raw_notes:
|
|
note["id"] = note["_id"]
|
|
note.pop("_id")
|
|
note["text"] = note["note"]
|
|
note.pop("note")
|
|
notes.append(Event(**note))
|
|
return notes
|
|
|
|
|
|
def extract_tags_from_text(text, tagmarker="#"):
|
|
"""Return lists of tuples of all event tags found in text.
|
|
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', '')].
|
|
"""
|
|
string_tags = re.findall(rf"{tagmarker}(\w+)(?:\((\d+)\))?", text)
|
|
tags_with_int_counters = []
|
|
for tag in string_tags:
|
|
tags_with_int_counters.append((tag[0], None if tag[1] == "" else int(tag[1])))
|
|
return tags_with_int_counters
|
|
|
|
|
|
def get_activities(tracker_list, note_list):
|
|
activities = []
|
|
for note in note_list:
|
|
tag_list = extract_tags_from_text(note.text)
|
|
for tracker in tracker_list:
|
|
for tag in tag_list:
|
|
if tracker.tag in tag[0]:
|
|
activities.append(Activity(tracker=tracker, value=tag[1]))
|
|
return activities
|
|
|
|
|
|
# return the data belonging to nomie
|
|
def get_data(file, interactive=True):
|
|
raw_data = load_file(file)
|
|
nomie_version = raw_data["nomie"]["number"]
|
|
|
|
tracker_list = get_trackers(raw_data["trackers"])
|
|
note_list = get_notes(raw_data["events"])
|
|
activity_list = get_activities(tracker_list, note_list)
|
|
|
|
data = NomieImport(nomie_version, tracker_list, note_list, activity_list)
|
|
if interactive:
|
|
verify_continue(data)
|
|
|
|
return data
|