forked from ucamhal/llic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench.py
183 lines (147 loc) · 5.14 KB
/
bench.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
"""
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN
BEGIN:VEVENT
DTSTAMP:19970714T170000Z
ORGANIZER;CN=John Doe:MAILTO:[email protected]
DTSTART:19970714T170000Z
DTEND:19970715T035959Z
SUMMARY:Bastille Day Party
END:VEVENT
END:VCALENDAR
"""
import datetime
import icalendar
import pytz
import llic
# We'll be generating this as a test
EXAMPLE_ICAL = """\
BEGIN:VCALENDAR\r\n\
VERSION:2.0\r\n\
PRODID:-//hacksw/handcal//NONSGML v1.0//EN\r\n\
BEGIN:VEVENT\r\n\
UID:[email protected]\r\n\
DTSTAMP:19970714T170000Z\r\n\
ORGANIZER;CN=John Doe:MAILTO:[email protected]\r\n\
DTSTART:19970714T170000Z\r\n\
DTEND:19970715T035959Z\r\n\
SUMMARY:Bastille Day Party\r\n\
END:VEVENT\r\n\
END:VCALENDAR\r\n\
"""
class ICalendarGenerator(object):
def generate_icalendar(self):
raise NotImplemented
def self_test(self):
"""
Verify that generate_icalendar() generates the expected iCal.
"""
cal = self.generate_icalendar(1)
actual = icalendar.Calendar.from_ical(cal).to_ical()
expected = icalendar.Calendar.from_ical(EXAMPLE_ICAL).to_ical()
if not actual == expected:
print cal
print actual
print expected
raise AssertionError("Generated calendar didn't match example.",
actual, expected)
class DodgyIO():
"""
A very simple file like object. Seems faster than [c]StringIO these
examples at least...
"""
def __init__(self):
self.bits = []
def write(self, string):
self.bits.append(string)
def getvalue(self):
return "".join(self.bits)
def close(self):
self.bits = None
class LlicICalendarGenerator(ICalendarGenerator):
def generate_icalendar(self, event_count=1):
out = DodgyIO()
cw = llic.CalendarWriter(out)
start = pytz.utc.localize(
datetime.datetime(1997, 7, 14, 17, 0, 0))
end = pytz.utc.localize(
datetime.datetime(1997, 7, 15, 3, 59, 59))
cw.begin("VCALENDAR")
cw.contentline("VERSION", "2.0")
cw.contentline("PRODID", "-//hacksw/handcal//NONSGML v1.0//EN")
for i in xrange(event_count):
cw.begin("VEVENT")
cw.contentline("UID", "uid{}@example.com".format(i + 1))
cw.contentline("DTSTAMP", cw.as_datetime(start))
cw.contentline("ORGANIZER;CN=John Doe",
"MAILTO:[email protected]")
cw.contentline("DTSTART", cw.as_datetime(start))
cw.contentline("DTEND", cw.as_datetime(end))
cw.contentline("SUMMARY", cw.as_text("Bastille Day Party"))
cw.end("VEVENT")
cw.end("VCALENDAR")
result = out.getvalue()
out.close()
return result
class ICalendarICalendarGenerator(ICalendarGenerator):
def generate_icalendar(self, event_count=1):
calendar = icalendar.Calendar()
calendar.add("VERSION", "2.0")
calendar.add("PRODID", "-//hacksw/handcal//NONSGML v1.0//EN")
start = pytz.utc.localize(
datetime.datetime(1997, 7, 14, 17, 0, 0))
end = pytz.utc.localize(
datetime.datetime(1997, 7, 15, 3, 59, 59))
for i in xrange(event_count):
event = icalendar.Event()
event.add("UID", "uid{}@example.com".format(i + 1))
event.add("DTSTAMP", start)
organiser = icalendar.vCalAddress("MAILTO:[email protected]")
organiser.params["CN"] = "John Doe"
event.add("ORGANIZER", organiser)
event.add("DTSTART", start)
event.add("DTEND", end)
event.add("SUMMARY", "Bastille Day Party")
# To generate exactly the iCal text we want we need to remove
# some auto-generated properties from the dates
event["DTSTAMP"].params.clear()
event["DTSTART"].params.clear()
event["DTEND"].params.clear()
calendar.add_component(event)
return calendar.to_ical()
class StupidICalendarGenerator(ICalendarGenerator):
def generate_icalendar(self, event_count=1):
bits = []
start = (
"BEGIN:VCALENDAR\r\n"
"VERSION:2.0\r\n"
"PRODID:-//hacksw/handcal//NONSGML v1.0//EN\r\n"
)
bits.append(start)
for i in xrange(event_count):
event = (
"BEGIN:VEVENT\r\n"
"UID:uid" + str(i + 1) + "@example.com\r\n"
"DTSTAMP:19970714T170000Z\r\n"
"ORGANIZER;CN=John Doe:MAILTO:[email protected]\r\n"
"DTSTART:19970714T170000Z\r\n"
"DTEND:19970715T035959Z\r\n"
"SUMMARY:Bastille Day Party\r\n"
"END:VEVENT\r\n"
)
bits.append(event)
end = (
"END:VCALENDAR\r\n"
)
bits.append(end)
return "".join(bits)
llic_gen = LlicICalendarGenerator()
ical_gen = ICalendarICalendarGenerator()
stupid_gen = StupidICalendarGenerator()
def self_test_all():
llic_gen.self_test()
ical_gen.self_test()
stupid_gen.self_test()
print("Self test: OK")