-
Notifications
You must be signed in to change notification settings - Fork 18
/
schedule_divoc.py
executable file
·231 lines (173 loc) · 7.03 KB
/
schedule_divoc.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import os
import sys
import json
import optparse
import pytz
import git as gitlib
from voc.schedule import Schedule, ScheduleEncoder, Event
from voc.c3data import C3data
from voc.tools import write
from wikitable2schedule import fetch_schedule
tz = pytz.timezone('Europe/Amsterdam')
parser = optparse.OptionParser()
parser.add_option('--online', action="store_true", dest="online", default=False)
parser.add_option('--show-assembly-warnings', action="store_true", dest="show_assembly_warnings", default=False)
parser.add_option('--fail', action="store_true", dest="exit_when_exception_occours", default=False)
parser.add_option('--git', action="store_true", dest="git", default=False)
parser.add_option('--debug', action="store_true", dest="debug", default=False)
options, args = parser.parse_args()
local = False
xc3 = 'eh21'
# wrong_acronym = 'eh'
acronym = 'eh21'
wiki_url = 'https://eh21.easterhegg.eu/user:sos' + '?do=export_xhtml'
main_schedule_url = 'https://fahrplan.eh21.easterhegg.eu/eh/schedule/export/schedule.json'
def fix_slug(event: dict):
event['slug'] = event['slug'].replace('eh-', 'eh21-').strip('_')
additional_schedule_urls = [
]
# this list/map is required to sort the events in the schedule.xml in the correct way
# other rooms/assemblies are added at the end on demand.
rooms = {
'stages': [
],
'rooms': [
],
'music': [
]
}
output_dir = "/srv/www/" + xc3
secondary_output_dir = "./" + xc3
if len(sys.argv) == 2:
output_dir = sys.argv[1]
if not os.path.exists(output_dir):
try:
if not os.path.exists(secondary_output_dir):
os.mkdir(output_dir)
else:
output_dir = secondary_output_dir
local = True
except Exception:
print('Please create directory named {} if you want to run in local mode'.format(secondary_output_dir))
exit(-1)
os.chdir(output_dir)
# if not os.path.exists("events"):
# os.mkdir("events")
def generate_wiki_schedule(wiki_url: str, full_schedule: Schedule):
try:
wiki_schedule = fetch_schedule(wiki_url)
write('Exporting... ')
wiki_schedule.export('wiki')
print('Wiki: done \n')
return wiki_schedule
except Exception as e:
print(e)
pass
def main():
global local, options
full_schedule = Schedule.from_url(main_schedule_url)
full_schedule.conference()['acronym'] = acronym
full_schedule.reset_generator()
print(' version: ' + full_schedule.version())
print(' contains {events_count} events, with local ids from {min_id} to {max_id}'.format(**full_schedule.stats.__dict__))
full_schedule.foreach_event_raw(fix_slug)
# add additional rooms from this local config now, so they are in the correct order
for key in rooms:
full_schedule.add_rooms(rooms[key])
previous_max_id = 0
# add events from additional_schedule's to full_schedule
for entry in additional_schedule_urls:
try:
# other_schedule = get_schedule(entry['name'], entry['url'])
other_schedule = Schedule.from_url(entry['url'])
if 'version' in other_schedule.schedule():
full_schedule._schedule['schedule']['version'] += "; {}".format(entry['name'])
print(' version: ' + other_schedule.version())
else:
print(' WARNING: schedule "{}" does not have a version number'.format(entry['name']))
print(' contains {events_count} events, with local ids from {min_id} to {max_id}'.format(**other_schedule.stats.__dict__))
id_offset = entry.get('id_offset')
if not id_offset:
id_offset = 0
min_id = other_schedule.stats.min_id + id_offset
max_id = other_schedule.stats.max_id + id_offset
print(' after adding the offset, ids reach from {} to {}'.format(min_id, max_id))
if previous_max_id >= min_id:
print(' WARNING: schedule "{}" has ID overlap with previous schedule'.format(entry['name']))
previous_max_id = max_id
if full_schedule.add_events_from(other_schedule, id_offset=id_offset, options=entry.get('options')):
print(' success')
except KeyboardInterrupt:
exit()
except Exception as e:
print(' UNEXPECTED ERROR:' + str(sys.exc_info()[1]))
if options.exit_when_exception_occours:
raise e
print('\nBuilding wiki schedule...')
# wiki
wiki_schedule = generate_wiki_schedule(wiki_url, full_schedule)
if wiki_schedule:
full_schedule['version'] += "; wiki"
full_schedule.add_events_from(wiki_schedule)
# write all events to one big schedule.json/xml
write('\nExporting... ')
full_schedule.export('everything')
# write separate file for each event, to get better git diffs
# full_schedule.foreach_event(lambda event: event.export('events/'))
# to get proper a state, we first have to remove all event files from the previous run
if not local or options.git:
git('rm events/* 2>/dev/null')
def export_event(event: Event):
origin_system = None
if isinstance(event, Event):
origin_system = event.origin.origin_system
with open("events/{}.json".format(event['guid']), "w") as fp:
json.dump({
**event,
'origin': origin_system or None,
}, fp, indent=2, cls=ScheduleEncoder)
full_schedule.foreach_event(export_event)
print('\nDone')
print(' version: ' + full_schedule.version())
print('\n rooms: ')
for room in full_schedule.rooms():
print(' - ' + room)
if not local or options.git:
content_did_not_change = os.system('/usr/bin/env git diff -U0 --no-prefix | grep -e "^[+-] " | grep -v version > /dev/null')
if content_did_not_change:
print('nothing relevant changed, reverting to previous state')
git('reset --hard')
else:
git('add *.json *.xml')
git('commit -m "version {}"'.format(full_schedule.version()))
# git('push')
push_c3data(full_schedule)
def push_c3data(schedule):
print("\n== Updating c3data via API…")
repo = gitlib.Repo('.')
c3data = C3data(schedule)
c3data.process_changed_events(repo, options)
'''
changed_items = repo.index.diff('HEAD~1', 'events')
for i in changed_items:
write(i.change_type + ': ')
try:
if i.change_type == 'D':
event_guid = os.path.splitext(os.path.basename(i.a_path))[0]
c3data.depublish_event(event_guid)
else:
event = load_json(i.a_path)
c3data.upsert_event(event)
except Exception as e:
print(e)
if options.exit_when_exception_occours:
raise e
'''
print("\n\n")
exit(2)
def git(args):
os.system('/usr/bin/env git {}'.format(args))
if __name__ == '__main__':
main()