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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
|
"""
Mixins for implementing calendars.
"""
__metaclass__ = type
class CalendarMixin:
"""Mixin for implementing ICalendar methods.
You do not have to use this mixin, however it might make implementation
easier, albeit potentially slower.
A class that uses this mixin must already implement ICalendar.__iter__.
>>> from schoolbell.interfaces import ICalendar
>>> from zope.interface import implements
>>> class MyCalendar(CalendarMixin):
... implements(ICalendar)
... def __iter__(self):
... return iter([])
>>> from zope.interface.verify import verifyObject
>>> verifyObject(ICalendar, MyCalendar())
True
"""
def find(self, unique_id):
"""Find a calendar event with a given UID.
This particular implementation simply performs a linear search by
iterating over all events and looking at their UIDs.
>>> from schoolbell.interfaces import ICalendar
>>> from zope.interface import implements
>>> class Event:
... def __init__(self, uid):
... self.unique_id = uid
>>> class MyCalendar(CalendarMixin):
... implements(ICalendar)
... def __iter__(self):
... return iter([Event(uid) for uid in 'a', 'b'])
>>> cal = MyCalendar()
>>> cal.find('a').unique_id
'a'
>>> cal.find('b').unique_id
'b'
>>> cal.find('c')
Traceback (most recent call last):
...
KeyError: 'c'
"""
for event in self:
if event.unique_id == unique_id:
return event
raise KeyError(unique_id)
def expand(self, first, last):
"""Return an iterator over all expanded events in a given time period.
See ICalendar for more details.
>>> from datetime import datetime, timedelta
>>> from schoolbell.interfaces import ICalendar
>>> from zope.interface import implements
>>> class Event:
... def __init__(self, dtstart, duration, title):
... self.dtstart = dtstart
... self.duration = duration
... self.title = title
>>> class MyCalendar(CalendarMixin):
... implements(ICalendar)
... def __iter__(self):
... return iter([Event(datetime(2004, 12, 14, 12, 30),
... timedelta(hours=1), 'a'),
... Event(datetime(2004, 12, 15, 16, 30),
... timedelta(hours=1), 'b'),
... Event(datetime(2004, 12, 15, 14, 30),
... timedelta(hours=1), 'c'),
... Event(datetime(2004, 12, 16, 17, 30),
... timedelta(hours=1), 'd'),
... ])
>>> cal = MyCalendar()
We will define a convenience function for showing all events returned
by expand:
>>> def show(first, last):
... events = cal.expand(first, last)
... print '[%s]' % ', '.join([e.title for e in events])
Events that fall inside the interval
>>> show(datetime(2004, 12, 1), datetime(2004, 12, 31))
[a, b, c, d]
>>> show(datetime(2004, 12, 15), datetime(2004, 12, 16))
[b, c]
Events that fall partially in the interval
>>> show(datetime(2004, 12, 15, 17, 0),
... datetime(2004, 12, 16, 18, 0))
[b, d]
Corner cases: if event.dtstart + event.duration == last, or
event.dtstart == first, the event is not included.
>>> show(datetime(2004, 12, 15, 15, 30),
... datetime(2004, 12, 15, 16, 30))
[]
TODO: recurring events
"""
for event in self:
# TODO: recurring events
dtstart = event.dtstart
dtend = dtstart + event.duration
if dtend > first and dtstart < last:
yield event
class EditableCalendarMixin:
"""Mixin for implementing some IEditCalendar methods.
This mixin implements `clear` and `update` by using `addEvent` and
`removeEvent`.
>>> class Event:
... def __init__(self, uid):
... self.unique_id = uid
>>> class SampleCalendar(EditableCalendarMixin):
... def __init__(self):
... self._events = {}
... def __iter__(self):
... return self._events.itervalues()
... def addEvent(self, event):
... self._events[event.unique_id] = event
... def removeEvent(self, event):
... del self._events[event.unique_id]
>>> cal = SampleCalendar()
>>> cal.addEvent(Event('a'))
>>> cal.addEvent(Event('b'))
>>> cal.addEvent(Event('c'))
>>> len(list(cal))
3
>>> cal2 = SampleCalendar()
>>> cal2.update(cal)
>>> len(list(cal2))
3
>>> cal.clear()
>>> list(cal)
[]
"""
def update(self, calendar):
"""Add all events from another calendar to this calendar."""
for event in calendar:
self.addEvent(event)
def clear(self):
"""Remove all events from the calendar."""
for event in list(self):
self.removeEvent(event)
class CalendarEventMixin:
"""Mixin for implementing ICalendarEvent comparison methods.
Calendar events are equal iff all their attributes are equal. We can get a
list of those attributes easily because ICalendarEvent is a schema.
>>> from schoolbell.interfaces import ICalendarEvent
>>> from zope.schema import getFieldNames
>>> all_attrs = getFieldNames(ICalendarEvent)
>>> 'unique_id' in all_attrs
True
>>> '__eq__' not in all_attrs
True
We will create a bunch of Event objects that differ in exactly one
attribute and compare them.
>>> class Event(CalendarEventMixin):
... def __init__(self, **kw):
... for attr in all_attrs:
... setattr(self, attr, '%s_default_value' % attr)
... for attr, value in kw.items():
... setattr(self, attr, value)
>>> e1 = Event()
>>> for attr in all_attrs:
... e2 = Event()
... setattr(e2, attr, 'some other value')
... assert e1 != e2, 'change in %s was not noticed' % attr
If you have two events with the same values in all ICalendarEvent
attributes, they are equal
>>> e1 = Event()
>>> e2 = Event()
>>> e1 == e2
True
even if they have extra attributes
>>> e1 = Event()
>>> e1.annotation = 'a'
>>> e2 = Event()
>>> e2.annotation = 'b'
>>> e1 == e2
True
Events are ordered by their date and time, title and, finally, UID (to
break any ties and provide a stable consistent ordering).
>>> from datetime import datetime
>>> e1 = Event(dtstart=datetime(2004, 12, 15))
>>> e2 = Event(dtstart=datetime(2004, 12, 16))
>>> e1 < e2
True
>>> e1 = Event(dtstart=datetime(2004, 12, 15), title="A")
>>> e2 = Event(dtstart=datetime(2004, 12, 15), title="B")
>>> e1 < e2
True
>>> e1 = Event(dtstart=datetime(2004, 12, 1), title="A", unique_id="X")
>>> e2 = Event(dtstart=datetime(2004, 12, 1), title="A", unique_id="Y")
>>> e1 < e2
True
"""
def __eq__(self, other):
"""Check whether two calendar events are equal."""
return (self.unique_id, self.dtstart, self.duration, self.title,
self.location, self.recurrence) \
== (other.unique_id, other.dtstart, other.duration, other.title,
other.location, other.recurrence)
def __ne__(self, other):
return not self.__eq__(other)
def __lt__(self, other):
return (self.dtstart, self.title, self.unique_id) \
< (other.dtstart, other.title, other.unique_id)
def __gt__(self, other):
return (self.dtstart, self.title, self.unique_id) \
> (other.dtstart, other.title, other.unique_id)
def __le__(self, other):
return (self.dtstart, self.title, self.unique_id) \
<= (other.dtstart, other.title, other.unique_id)
def __ge__(self, other):
return (self.dtstart, self.title, self.unique_id) \
>= (other.dtstart, other.title, other.unique_id)
def hasOccurrences(self):
raise NotImplementedError # TODO
def replace(self, **kw):
r"""Return a copy of this event with some attributes replaced.
>>> from schoolbell.interfaces import ICalendarEvent
>>> from zope.schema import getFieldNames
>>> all_attrs = getFieldNames(ICalendarEvent)
>>> class Event(CalendarEventMixin):
... def __init__(self, **kw):
... for attr in all_attrs:
... setattr(self, attr, '%s_default_value' % attr)
... for attr, value in kw.items():
... setattr(self, attr, value)
>>> from datetime import datetime, timedelta
>>> e1 = Event(dtstart=datetime(2004, 12, 15, 18, 57),
... duration=timedelta(minutes=15),
... title='Work on schoolbell.simple',
... location=None)
>>> e2 = e1.replace(location=u'Matar\u00f3')
>>> e2 == e1
False
>>> e2.title == e1.title
True
>>> e2.location
u'Matar\xf3'
>>> e3 = e2.replace(location=None)
>>> e3 == e1
True
"""
# The import is here to avoid cyclic dependencies
from schoolbell.simple import SimpleCalendarEvent
for attr in ['dtstart', 'duration', 'title', 'description',
'location', 'unique_id', 'recurrence']:
kw.setdefault(attr, getattr(self, attr))
return SimpleCalendarEvent(**kw)
|