Skip to content

Commit 6e2658f

Browse files
committed
Standardize return types of functions to objects
- Make all functions return objects, until the very end - Rename event_ics_from_yaml → event_from_yaml - Rename events_to_calendar_ics → events_to_calendar - Add a test of whole-calendar events - This required splitting out some of the previous __main__ script to a separate function. I am not entirely sure if I made the right choice, but it should be easy to adjust later.
1 parent 1df99af commit 6e2658f

3 files changed

Lines changed: 51 additions & 19 deletions

File tree

tests/test_calendar.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,30 @@
1-
from yaml2ics import events_to_calendar_ics
1+
import io
2+
import textwrap
3+
4+
from util import parse_yaml
5+
6+
from yaml2ics import events_to_calendar, files_to_calendar
27

38

49
def test_calendar_structure():
5-
cal = events_to_calendar_ics([])
6-
assert cal.startswith('BEGIN:VCALENDAR')
7-
assert cal.endswith('END:VCALENDAR')
10+
cal = events_to_calendar([])
11+
cal_str = cal.serialize()
12+
assert cal_str.startswith('BEGIN:VCALENDAR')
13+
assert cal_str.endswith('END:VCALENDAR')
14+
15+
def test_calendar_event():
16+
cal = files_to_calendar(
17+
[io.StringIO(textwrap.dedent(
18+
'''
19+
events:
20+
- summary: Earth Day
21+
begin: 2021-04-22
22+
url: https://earthday.org
23+
location: Earth
24+
'''
25+
))]
26+
)
27+
cal_str = cal.serialize()
28+
assert cal_str.startswith('BEGIN:VCALENDAR')
29+
assert 'SUMMARY:Earth Day' in cal_str
30+
assert cal_str.endswith('END:VCALENDAR')

tests/test_events.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from util import parse_yaml
22

3-
from yaml2ics import event_ics_from_yaml
3+
from yaml2ics import event_from_yaml
44

55

66
def test_basic_structure():
7-
event = event_ics_from_yaml(
7+
event = event_from_yaml(
88
parse_yaml(
99
'''
1010
summary: Earth Day
@@ -28,7 +28,7 @@ def test_basic_structure():
2828

2929

3030
def test_all_day_event():
31-
event = event_ics_from_yaml(
31+
event = event_from_yaml(
3232
parse_yaml(
3333
'''
3434
summary: Earth Day
@@ -46,7 +46,7 @@ def test_all_day_event():
4646

4747

4848
def test_rrule():
49-
event = event_ics_from_yaml(
49+
event = event_from_yaml(
5050
parse_yaml(
5151
'''
5252
summary: Earth Day
@@ -65,7 +65,7 @@ def test_rrule():
6565

6666

6767
def test_event_with_time_range():
68-
event = event_ics_from_yaml(
68+
event = event_from_yaml(
6969
parse_yaml(
7070
'''
7171
summary: Event of the Century
@@ -82,7 +82,7 @@ def test_event_with_time_range():
8282

8383

8484
def test_event_with_duration():
85-
event = event_ics_from_yaml(
85+
event = event_from_yaml(
8686
parse_yaml(
8787
'''
8888
summary: Event of the Century

yaml2ics.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
}
2020

2121

22-
def event_ics_from_yaml(event_yaml: dict) -> ics.Event:
22+
def event_from_yaml(event_yaml: dict) -> ics.Event:
2323
d = event_yaml
2424
repeat = d.pop('repeat', None)
2525

@@ -74,11 +74,24 @@ def event_ics_from_yaml(event_yaml: dict) -> ics.Event:
7474
return event
7575

7676

77-
def events_to_calendar_ics(events: dict) -> str:
77+
def events_to_calendar(events: list) -> str:
7878
cal = ics.Calendar()
7979
for event in events:
8080
cal.events.append(event)
81-
return cal.serialize()
81+
return cal
82+
83+
def files_to_calendar(files: list) -> ics.Calendar:
84+
"""'main' function: list of files to our result"""
85+
all_events = [ ]
86+
for f in files:
87+
if hasattr(f, 'read'):
88+
calendar_yaml = yaml.load(f.read(), Loader=yaml.FullLoader)
89+
else:
90+
calendar_yaml = yaml.load(open(f, 'r'), Loader=yaml.FullLoader)
91+
for event in calendar_yaml['events']:
92+
all_events.append(event_from_yaml(event))
93+
calendar = events_to_calendar(all_events)
94+
return calendar
8295

8396

8497
if __name__ == '__main__':
@@ -92,10 +105,6 @@ def events_to_calendar_ics(events: dict) -> str:
92105
print(f'Error: {f} is not a file')
93106
sys.exit(-1)
94107

95-
all_events = []
96-
for f in files:
97-
calendar_yaml = yaml.load(open(f, 'r'), Loader=yaml.FullLoader)
98-
for event in calendar_yaml['events']:
99-
all_events.append(event_ics_from_yaml(event))
108+
calendar = files_to_calendar(files)
100109

101-
print(events_to_calendar_ics(all_events))
110+
print(calendar.serialize())

0 commit comments

Comments
 (0)