Extract habit counter from event tags additionally

This commit is contained in:
Marty Oehme 2021-08-29 11:22:55 +02:00
parent 18cdd34300
commit e050acd288
Signed by: Marty
GPG key ID: B7538B8F50A1C800

View file

@ -9,14 +9,23 @@ def migrate(db, habitlist, events):
text = event["note"] text = event["note"]
tags = extract_tags(text) tags = extract_tags(text)
for habit in habitlist: for habit in habitlist:
habit_tag = f"#{habit['description']}" for tag in tags:
if habit_tag in tags: if habit["description"] in tag[0]:
print(f"register event: {habit['name']} in {text} at {event['end']}") print(
f"register event: {habit['name']} in {text} at {event['end']}"
)
add_to_database(c, habit["id"], event["end"]) add_to_database(c, habit["id"], event["end"])
def extract_tags(text, tagmarker="#"): def extract_tags(text, tagmarker="#"):
found_tags = re.findall(rf"{tagmarker}\w+(?:\(\d+\))?", text) """Returns 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', '')].
"""
found_tags = re.findall(rf"{tagmarker}(\w+)(?:\((\d+)\))?", text)
return found_tags return found_tags