-
Notifications
You must be signed in to change notification settings - Fork 0
/
calendarParser.py
221 lines (171 loc) · 5.64 KB
/
calendarParser.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
import os
import sys
import time
from datetime import datetime, timedelta
import pytz
import icalendar
import requests
import shutil
import sched
import utils
import calendarReciever
from Monitor import Monitor
import signal
def signal_handler(signal, frame):
'''Force quit when detected Ctrl+C'''
print('Exiting...')
os._exit(0)
signal.signal(signal.SIGINT, signal_handler)
# signal.pause()
# Global var CAL to keep track of the current calendar
CAL = None
# COMM = "date > test.txt"
COMM = "~/paol-code/setupCaptureGUI/PAOL-LecCap-GUI"
def main(calPath):
'''take path of calendar file and schedule all capturing events'''
global CAL
# Testing
# initialTest()
# If
if (len(sys.argv) < 3 and calPath is None):
return
if calPath is None:
calPath = sys.argv[2]
gcal = utils.getCal(calPath)
CAL = gcal
utils.printComponentName(gcal)
print ''
utils.printEventDetail(gcal)
print ''
scheduleEvent(gcal, COMM)
print("scheduled all")
calendarReciever.start_server()
# Keep the instance running and listen to requests
while 1:
pass
# calendarReciever.start_server()
def reload_program_config():
'''reload program config'''
cancel_all()
main()
def program_cleanup():
global CAL
CAL = None
cancel_all()
utils.MONITORS = []
print("Exiting...")
os._exit(0)
def updateCal(local_path, url):
'''
Get the latest calendar from url
Please specify the port number in URL
'''
r = requests.get(url, stream=True)
if r.status_code == 200:
with open(local_path, 'wb') as f:
r.raw.decode_content = True
shutil.copyfileobj(r.raw, f)
def scheduleEvent(gcal, comm):
''' Get all details of all scheduled VEVENTs'''
# initialize scheduler for events
s = sched.scheduler(time.time, time.sleep)
timezone = pytz.timezone("US/Eastern")
for component in gcal.walk():
if component.name == "VEVENT":
summary = component.get('summary')
start_time = component.get('dtstart').dt
end_time = component.get('dtend').dt
time_delta = end_time - start_time
# Create Cron Job base on schedule
seconds = time_delta.total_seconds()
#seconds = seconds % 60
comm0 = COMM + " " + summary + " " + str(seconds)
# create new Monitor
if start_time < timezone.localize(datetime.now()):
continue
job = Monitor(s, comm0, start_time)
utils.MONITORS.append(job)
# createCronJob(comm0, start_time)
# scheduleJob(comm0, start_time, s)
# t = threading.Thread(target=s.run)
# t.start()
for mo in utils.MONITORS:
mo.start()
def cancel_all():
'''Cancel all scheduled jobs'''
for m in utils.MONITORS:
m.stop()
print("canceled all jobs")
def calChangedCB(gcal):
'''Callback for calendar change from reciever'''
print("Detected calendar changed.")
timezone= pytz.timezone("US/Eastern")
mo_temp = []
for component in gcal.walk():
if component.name == "VEVENT":
summary = component.get('summary')
start_time = component.get('dtstart').dt
end_time = component.get('dtend').dt
time_delta = end_time - start_time
# Create Cron Job base on schedule
seconds = time_delta.total_seconds
#seconds = seconds % 60
comm0 = COMM + " " + summary + " " + str(seconds)
# create new Monitor
job = Monitor(0, comm0, start_time)
mo_temp.append(job)
print(utils.MONITORS)
for mo in utils.MONITORS:
if mo.dt < timezone.localize(datetime.now()):
continue
mo.stop()
utils.MONITORS = []
print mo_temp
for mo in mo_temp:
if mo.dt < timezone.localize(datetime.now()):
continue
utils.MONITORS.append(mo)
for mo in utils.MONITORS:
mo.start()
def initialTest():
global CAL
'''Tests if everything works as usual'''
# Read info from Calendar
gcal = utils.getCal('ICS/CalendarTest.ics')
utils.printComponentName(gcal)
print ''
utils.printEventDetail(gcal)
cal = icalendar.Calendar()
cal.add('prodid', '-//My calendar//umass.edu//')
cal.add('version', '2.0')
# Original Calendar
now = datetime.now()
cal = utils.addEventToCal(gcal=cal, sa=now+timedelta(0, 5),
ea=now+timedelta(0, 8))
cal = utils.addEventToCal(gcal=cal, sa=now+timedelta(0, 10),
ea=now+timedelta(0, 13))
f = open('temp.ics', 'wb')
f.write(cal.to_ical())
f.close()
# Testing
gcal = utils.getCal('temp.ics')
utils.printComponentName(gcal)
print ''
utils.printEventDetail(gcal)
print "\n"
comm = "date > test.txt"
scheduleEvent(gcal, comm)
time.sleep(5)
n_cal = icalendar.Calendar()
n_cal.add('prodid', '-//My calendar//umass.edu//')
n_cal.add('version', '2.0')
n_cal = utils.addEventToCal(gcal=n_cal, sa=now+timedelta(0, 5),
ea=now+timedelta(0, 8))
n_cal = utils.addEventToCal(gcal=n_cal, sa=now+timedelta(0, 13),
ea=now+timedelta(0, 16))
calChangedCB(n_cal)
# Recover to initial state
CAL = None
utils.MONITORS = []
if __name__ == "__main__":
main('ICS/Calendar.ics')