-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremove_room_from_cals.py
125 lines (99 loc) · 3.1 KB
/
remove_room_from_cals.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"""
Remove all events associated to a given room from the four calendar files
generated by pretalx:
schedule.ics
schedule.json
schedule.xcal
schedule.xml
This is mostly useful to remove posters from the calendar files. Just schedule
all posters in a single room and then run this script.
"""
import argparse
import json
import os
from bs4 import BeautifulSoup
import ics
def _process_json_cal(path, room):
modified = False
with open(path) as f:
data = json.load(f)
for day_dict in data['schedule']['conference']['days']:
if room in day_dict['rooms']:
del(day_dict['rooms'][room])
modified = True
if not modified:
return
with open(path, 'w') as f:
json.dump(data, f)
def _process_xml_cal(path, room):
modified = False
with open(path) as f:
soup = BeautifulSoup(f, 'xml')
for day in soup.schedule.find_all('day'):
valid_elements = []
for el in day.contents:
if getattr(el, 'name', None) == 'room' and el['name'] == room:
if valid_elements:
valid_elements.pop()
continue
valid_elements.append(el)
if len(valid_elements) != len(day.contents):
modified = True
day.contents = valid_elements
if not modified:
return
with open(path, 'w') as f:
f.write(soup.prettify())
def _process_xcal(path, room):
modified = False
with open(path) as f:
soup = BeautifulSoup(f, 'xml')
cal = soup.iCalendar.vcalendar
valid_elements = []
for el in cal.contents:
if getattr(el, 'name', None) == 'vevent' and \
el.location.string == room:
if valid_elements:
valid_elements.pop()
continue
valid_elements.append(el)
if len(valid_elements) != len(cal.contents):
modified = True
cal.contents = valid_elements
if not modified:
return
with open(path, 'w') as f:
f.write(soup.prettify())
def _process_ics(path, room):
with open(path) as f:
cal = ics.Calendar(f.read())
valid_events = set()
for e in list(cal.events):
if e.location == room:
continue
valid_events.add(e)
if len(valid_events) == len(cal.events):
return
cal.events = valid_events
with open(path, 'w') as f:
f.write(str(cal))
PROCESSORS = {
'.json': _process_json_cal,
'.xml': _process_xml_cal,
'.xcal': _process_xcal,
'.ics': _process_ics,
}
def process(cal_files, room):
for cal_file in cal_files:
_, ext = os.path.splitext(cal_file)
if ext not in PROCESSORS:
continue
PROCESSORS[ext](cal_file, room)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--room', '-r', type=str, required=True,
help='room name')
parser.add_argument('calendar_files', metavar='FILE', nargs='+',
help='calendar files to process')
args = parser.parse_args()
process(args.calendar_files, room=args.room)