From 5a44a6355948aad631776a125e76a9ae95c93a10 Mon Sep 17 00:00:00 2001 From: Stefan van der Walt Date: Fri, 11 Feb 2022 01:10:03 -0800 Subject: [PATCH] Add calendar name field Also move timezone to root of yaml file This will eventually be supported through https://github.com/ics-py/ics-py/pull/312, but this is a quick workaround in the interim. --- example/another_calendar.yaml | 1 + example/test_calendar.yaml | 4 ++-- tests/test_calendar.py | 39 ++++++++++++++++++++++++++--------- yaml2ics.py | 18 +++++++++++----- 4 files changed, 45 insertions(+), 17 deletions(-) diff --git a/example/another_calendar.yaml b/example/another_calendar.yaml index c2c8025..cf8c509 100644 --- a/example/another_calendar.yaml +++ b/example/another_calendar.yaml @@ -1,3 +1,4 @@ +name: Another Calendar events: - summary: Another Event begin: 2021-09-21 10:00:00 -07:00 diff --git a/example/test_calendar.yaml b/example/test_calendar.yaml index 8901756..b38b451 100644 --- a/example/test_calendar.yaml +++ b/example/test_calendar.yaml @@ -1,5 +1,5 @@ -meta: - tz: America/Los_Angeles +name: Test Calendar +timezone: America/Los_Angeles events: - summary: Event of the Century diff --git a/tests/test_calendar.py b/tests/test_calendar.py index 1fe9812..b25ad8a 100644 --- a/tests/test_calendar.py +++ b/tests/test_calendar.py @@ -5,6 +5,10 @@ from yaml2ics import events_to_calendar, files_to_calendar +def iowrap(s): + return io.StringIO(textwrap.dedent(s)) + + def test_calendar_structure(): cal = events_to_calendar([]) cal_str = cal.serialize() @@ -15,16 +19,14 @@ def test_calendar_structure(): def test_calendar_event(): cal = files_to_calendar( [ - io.StringIO( - textwrap.dedent( - """ + iowrap( + """ events: - summary: Earth Day begin: 2021-04-22 url: https://earthday.org location: Earth """ - ) ) ] ) @@ -37,11 +39,9 @@ def test_calendar_event(): def test_calendar_default_timezone(): cal = files_to_calendar( [ - io.StringIO( - textwrap.dedent( - """ - meta: - tz: Europe/Helsinki + iowrap( + """ + timezone: Europe/Helsinki events: - summary: New year's day @@ -55,7 +55,6 @@ def test_calendar_default_timezone(): - summary: Earth day (all day) begin: 2022-04-22 """ - ) ) ] ) @@ -76,3 +75,23 @@ def test_calendar_default_timezone(): # 1 Feb midnight assert "DTSTART:20211231T220000Z" # 1 jan assert "DTSTART:20220131T220000Z" # 1 feb + + +def test_calendar_name(): + cal = files_to_calendar( + [ + iowrap( + """ + name: My First Calendar + """ + ), + iowrap( + """ + name: My Second Calendar + """ + ), + ] + ) + cal_str = cal.serialize() + assert "NAME:My Second Calendar" in cal_str + assert cal_str.count("NAME:") == 1 diff --git a/yaml2ics.py b/yaml2ics.py index 5e511b6..906594a 100644 --- a/yaml2ics.py +++ b/yaml2ics.py @@ -97,18 +97,26 @@ def events_to_calendar(events: list) -> str: def files_to_calendar(files: list) -> ics.Calendar: """'main' function: list of files to our result""" all_events = [] + name = None + for f in files: if hasattr(f, "read"): calendar_yaml = yaml.load(f.read(), Loader=yaml.FullLoader) else: calendar_yaml = yaml.load(open(f, "r"), Loader=yaml.FullLoader) - tz = None - if "meta" in calendar_yaml: - if "tz" in calendar_yaml["meta"]: - tz = gettz(calendar_yaml["meta"]["tz"]) - for event in calendar_yaml["events"]: + tz = calendar_yaml.get("timezone", None) + if tz is not None: + tz = gettz(tz) + for event in calendar_yaml.get("events", []): all_events.append(event_from_yaml(event, tz=tz)) + + # We can only provide one calendar name, so we'll + # keep the last one we find + name = calendar_yaml.get("name", name) + calendar = events_to_calendar(all_events) + if name is not None: + calendar.extra.append(ics.ContentLine(name="NAME", value=name)) return calendar