From aafd37ab5ac915261c84a0bbc015d96d78685f6b Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Sat, 21 Oct 2023 09:28:58 +0100 Subject: [PATCH] Read birthdays from YAML Closes: #22 --- agenda/__init__.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/agenda/__init__.py b/agenda/__init__.py index fbeb73f..95dccd4 100644 --- a/agenda/__init__.py +++ b/agenda/__init__.py @@ -305,16 +305,20 @@ def next_birthday(from_date: date, birth_date: date) -> tuple[date, int]: def get_birthdays(from_date: date, config: configparser.ConfigParser) -> list[Event]: """Get birthdays from config.""" - if "birthdays" not in config: - return [] + filename = config["data"]["entities"] + events = [] - for name, date_str in config["birthdays"].items(): - bday = next_birthday(from_date, datetime.strptime(date_str, "%Y-%m-%d").date()) + with open(filename) as f: + entities = yaml.safe_load(f) + + for entity in entities: + birthday = date(**entity["birthday"]) + bday, age = next_birthday(from_date, birthday) events.append( Event( - date=bday[0], + date=bday, name="birthday", - title=f"{name.title()} (aged {bday[1]})", + title=f'{entity["label"]} (aged {age})', ) )