Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions example/another_calendar.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
name: Another Calendar
events:
- summary: Another Event
begin: 2021-09-21 10:00:00 -07:00
Expand Down
4 changes: 2 additions & 2 deletions example/test_calendar.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
meta:
tz: America/Los_Angeles
name: Test Calendar
timezone: America/Los_Angeles

events:
- summary: Event of the Century
Expand Down
39 changes: 29 additions & 10 deletions tests/test_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
"""
)
)
]
)
Expand All @@ -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
Expand All @@ -55,7 +55,6 @@ def test_calendar_default_timezone():
- summary: Earth day (all day)
begin: 2022-04-22
"""
)
)
]
)
Expand All @@ -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
18 changes: 13 additions & 5 deletions yaml2ics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down