forked from festivus-es/festivus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
festivus.py
78 lines (59 loc) · 1.98 KB
/
festivus.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import datetime
import pathlib
import ics
def find_calendars():
calendar_paths = dict()
for path in pathlib.Path("data").glob("**/*.cal"):
location = path.parts[1:-1]
calendar_paths[location] = calendar_paths.get(location, [])
calendar_paths[location].append(path)
calendars = []
for location, paths in calendar_paths.items():
calendars.append(get_calendar(location, paths))
return calendars
def get_calendar(location, paths):
calendar = Calendar(location)
for path in paths:
with open(path, encoding="utf-8") as f:
calendar.parse(f)
return calendar
def generate_calendars():
calendars = find_calendars()
for calendar in calendars:
path = pathlib.Path("generated") / pathlib.Path(*calendar.location)
path.mkdir(parents=True, exist_ok=True)
file = path / "festivus.ics"
with open(file, "w") as f:
f.write(str(calendar.as_ical()))
class Calendar:
def __init__(self, location):
self.location = location
self.days = []
def parse(self, f):
source = f.readline()
for line in f:
self.add(Day(line.strip(), source.strip()))
def add(self, day):
self.days.append(day)
def __repr__(self):
return f"{self.location} {repr(self.days)}"
def as_ical(self):
ical = ics.Calendar()
for day in self.days:
event = ics.Event(
name=day.description, description=day.source, begin=day.date
)
event.make_all_day()
ical.events.add(event)
return ical
class Day:
def __init__(self, line, source):
date_str, self.description = line.split(" ", 1)
self.date = datetime.date.fromisoformat(date_str)
self.source = source
def __str__(self):
return f"{self.date} {self.description} {self.source}"
def __repr__(self):
return repr(str(self))
if __name__ == "__main__":
generate_calendars()