/usr/share/pyshared/schooltool/calendar/simple.py is in python-schooltool 1:2.1.0-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | #
# SchoolTool - common information systems platform for school administration
# Copyright (c) 2005 Shuttleworth Foundation
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
"""
Simple calendar events and calendars.
$Id$
"""
import datetime
import random
import itertools
import email.Utils
from pytz import utc
from zope.interface import implements
from schooltool.calendar.interfaces import ICalendar, ICalendarEvent
from schooltool.calendar.mixins import CalendarEventMixin, CalendarMixin
class SimpleCalendarEvent(CalendarEventMixin):
"""A simple implementation of ICalendarEvent.
>>> from datetime import datetime, timedelta
>>> from zope.interface.verify import verifyObject
>>> e = SimpleCalendarEvent(datetime(2004, 12, 15, 18, 57),
... timedelta(minutes=15),
... 'Work on schooltool.calendar.simple')
>>> verifyObject(ICalendarEvent, e)
True
If you do not specify a unique ID, a random one is generated
>>> e.unique_id is not None
True
Additional iCal information is also supported
>>> e2 = SimpleCalendarEvent(datetime(2004, 12, 15, 18, 57),
... timedelta(minutes=15),
... 'Work on schooltool.calendar.simple',
... description="Python for fun and profit",
... location="Mt. Vernon Stable",
... recurrence='FakeRecurrance')
>>> e2.description
'Python for fun and profit'
>>> e2.location
'Mt. Vernon Stable'
>>> e2.recurrence
'FakeRecurrance'
We're going to store all datetime objects in UTC
>>> e2.dtstart.tzname()
'UTC'
"""
implements(ICalendarEvent)
# Events in older versions had no allday events so this attribute
# must be initialized in here
allday = False
def __init__(self, dtstart, duration, title, description=None,
location=None, unique_id=None, recurrence=None, allday=False):
assert title is not None, 'title is required'
if dtstart.tzname() not in (None, 'UTC'):
raise ValueError, 'Can not store non UTC time info'
self.dtstart = dtstart.replace(tzinfo=utc)
self.duration = duration
self.title = title
self.description = description
self.location = location
self.recurrence = recurrence
self.allday = allday
self.unique_id = unique_id
if not self.unique_id:
self.unique_id = new_unique_id()
class ImmutableCalendar(CalendarMixin):
"""A simple read-only calendar.
>>> from datetime import datetime, timedelta
>>> from zope.interface.verify import verifyObject
>>> e = SimpleCalendarEvent(datetime(2004, 12, 15, 18, 57),
... timedelta(minutes=15),
... 'Work on schooltool.calendar.simple')
>>> calendar = ImmutableCalendar([e])
>>> verifyObject(ICalendar, calendar)
True
>>> [e.title for e in calendar]
['Work on schooltool.calendar.simple']
>>> len(calendar)
1
"""
implements(ICalendar)
def __init__(self, events=()):
self._events = tuple(events)
def __iter__(self):
return iter(self._events)
def __len__(self):
return len(self._events)
def combine_calendars(*calendars):
r"""Combine events from several calendars into one read-only calendar.
Suppose you have several calendars with events
>>> from datetime import datetime, timedelta
>>> from schooltool.calendar.simple import SimpleCalendarEvent
>>> e1 = SimpleCalendarEvent(datetime(2004, 12, 15, 18, 57),
... timedelta(minutes=15),
... 'Work on schooltool.calendar.simple')
>>> calendar1 = ImmutableCalendar([e1])
>>> e2 = SimpleCalendarEvent(datetime(2005, 2, 2, 20, 28),
... timedelta(minutes=10),
... 'Write a test for combine_calendars')
>>> calendar2 = ImmutableCalendar([e2])
You can combine them
>>> calendar = combine_calendars(calendar1, calendar2)
>>> titles = [e.title for e in calendar]
>>> titles.sort()
>>> print '\n'.join(titles)
Work on schooltool.calendar.simple
Write a test for combine_calendars
This calendar is read-only
>>> from schooltool.calendar.interfaces import IEditCalendar
>>> IEditCalendar.providedBy(calendar)
False
>>> from schooltool.calendar.interfaces import ICalendar
>>> ICalendar.providedBy(calendar)
True
"""
return ImmutableCalendar(itertools.chain(*calendars))
def new_unique_id():
"""Generate a new unique ID for a calendar event.
UID is randomly generated and follows RFC 822 addr-spec:
>>> uid = new_unique_id()
>>> '@' in uid
True
Note that it does not have the angle brackets
>>> '<' not in uid
True
>>> '>' not in uid
True
"""
more_uniqueness = '%d.%d' % (datetime.datetime.utcnow().microsecond,
random.randrange(10 ** 6, 10 ** 7))
# generate an rfc-822 style id and strip angle brackets
unique_id = email.Utils.make_msgid(more_uniqueness)[1:-1]
return unique_id
|