diff --git a/CHANGELOG.md b/CHANGELOG.md index af1cc16..fe51c3c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,9 +9,9 @@ and this project tries to adhere to [Semantic Versioning](https://semver.org/spe ### Added -* 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 + * Does not do multiple entries for simple trackers in nomie yet +* Add range habits, using nomie maximum value for range targets in Loop * Adding ranges defaults target value to half of maximum nomie value ### Fixed diff --git a/loop/repetitions.py b/loop/repetitions.py index c3e52ce..4567e7a 100644 --- a/loop/repetitions.py +++ b/loop/repetitions.py @@ -7,7 +7,12 @@ def migrate(db, habitlist, events): habits = simple_habits_list(c, habitlist) repetitions = extract_all_repetitions(habits, events) for rep in repetitions: - add_to_database(c, rep["id"], rep["timestamp"]) + add_to_database( + c, rep["id"], rep["timestamp"], 2 if "value" not in rep else rep["value"] + ) + + +LOOP_RANGE_VALUE_MULTIPLIER = 1000 def extract_all_repetitions(habits, events): @@ -26,8 +31,17 @@ def extract_all_repetitions(habits, events): for habit in habits: for tag in tags: if habit["description"] in tag[0]: - print(f"found event: {habit['name']} in {text} at {event['end']}") + print( + f"found repetition: {habit['name']} in {text} at {event['end']}" + ) repetitions.append({"id": habit["id"], "timestamp": event["end"]}) + if tag[1]: + print(f"repetition contains counter: {tag[0]} ({tag[1]})") + if "type" in habit and habit["type"] == 1: + repetitions[-1]["value"] = ( + tag[1] * LOOP_RANGE_VALUE_MULTIPLIER + ) + print(f"added counter to ranged habit.") return repetitions @@ -39,10 +53,14 @@ def extract_tags(text, tagmarker="#"): Returns: tags (list): List of tuples in the form [('tag', '3'), ('anothertag', '')]. """ - found_tags = re.findall(rf"{tagmarker}(\w+)(?:\((\d+)\))?", text) - return found_tags + 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 +# TODO possibly just get rid of this entirely def simple_habits_list(c, habitlist): """Return simplified collection of habits, only carrying name, tag, id. Parameters: @@ -61,6 +79,8 @@ def simple_habits_list(c, habitlist): "id": fetch_habit_id(c, h["uuid"]), } ) + if "type" in h: + simple[-1]["type"] = h["type"] return simple @@ -97,4 +117,3 @@ def add_to_database(cursor, habit_id, timestamp, value=2): except sqlite3.IntegrityError: # TODO better error handling print(f"fail to register {habit_id}: timestamp {timestamp} not unique") - pass