paul@213 | 1 | #!/usr/bin/env python |
paul@213 | 2 | |
paul@213 | 3 | """ |
paul@213 | 4 | Interpretation of vCalendar content. |
paul@213 | 5 | |
paul@1230 | 6 | Copyright (C) 2014, 2015, 2016, 2017 Paul Boddie <paul@boddie.org.uk> |
paul@213 | 7 | |
paul@213 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@213 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@213 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@213 | 11 | version. |
paul@213 | 12 | |
paul@213 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@213 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@213 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@213 | 16 | details. |
paul@213 | 17 | |
paul@213 | 18 | You should have received a copy of the GNU General Public License along with |
paul@213 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@213 | 20 | """ |
paul@213 | 21 | |
paul@424 | 22 | from bisect import bisect_left |
paul@560 | 23 | from datetime import date, datetime, timedelta |
paul@213 | 24 | from email.mime.text import MIMEText |
paul@939 | 25 | from imiptools.dates import format_datetime, get_datetime, \ |
paul@625 | 26 | get_datetime_item as get_item_from_datetime, \ |
paul@625 | 27 | get_datetime_tzid, \ |
paul@628 | 28 | get_duration, get_period, get_period_item, \ |
paul@627 | 29 | get_recurrence_start_point, \ |
paul@1204 | 30 | get_time, get_timestamp, get_tzid, to_datetime, \ |
paul@1204 | 31 | to_timezone, to_utc_datetime |
paul@1230 | 32 | from imiptools.freebusy import FreeBusyPeriod |
paul@1230 | 33 | from imiptools.period import Period, RecurringPeriod |
paul@213 | 34 | from vCalendar import iterwrite, parse, ParseError, to_dict, to_node |
paul@256 | 35 | from vRecurrence import get_parameters, get_rule |
paul@213 | 36 | import email.utils |
paul@213 | 37 | |
paul@213 | 38 | try: |
paul@213 | 39 | from cStringIO import StringIO |
paul@213 | 40 | except ImportError: |
paul@213 | 41 | from StringIO import StringIO |
paul@213 | 42 | |
paul@213 | 43 | class Object: |
paul@213 | 44 | |
paul@213 | 45 | "Access to calendar structures." |
paul@213 | 46 | |
paul@1276 | 47 | def __init__(self, fragment, tzid=None): |
paul@1123 | 48 | |
paul@1123 | 49 | """ |
paul@1276 | 50 | Initialise the object with the given 'fragment'. The optional 'tzid' |
paul@1276 | 51 | sets the fallback time zone used to convert datetimes without time zone |
paul@1276 | 52 | information. |
paul@1276 | 53 | |
paul@1276 | 54 | The 'fragment' must be a dictionary mapping an object type (such as |
paul@1276 | 55 | "VEVENT") to a tuple containing the object details and attributes, |
paul@1276 | 56 | each being a dictionary itself. |
paul@1123 | 57 | |
paul@1123 | 58 | The result of parse_object can be processed to obtain a fragment by |
paul@1123 | 59 | obtaining a collection of records for an object type. For example: |
paul@1123 | 60 | |
paul@1123 | 61 | l = parse_object(f, encoding, "VCALENDAR") |
paul@1123 | 62 | events = l["VEVENT"] |
paul@1123 | 63 | event = events[0] |
paul@1123 | 64 | |
paul@1123 | 65 | Then, the specific object must be presented as follows: |
paul@1123 | 66 | |
paul@1123 | 67 | object = Object({"VEVENT" : event}) |
paul@1204 | 68 | |
paul@1275 | 69 | A separately-stored, individual object can be obtained as follows: |
paul@1275 | 70 | |
paul@1275 | 71 | object = Object(parse_object(f, encoding)) |
paul@1275 | 72 | |
paul@1204 | 73 | A convienience function is also provided to initialise objects: |
paul@1204 | 74 | |
paul@1204 | 75 | object = new_object("VEVENT") |
paul@1123 | 76 | """ |
paul@1123 | 77 | |
paul@213 | 78 | self.objtype, (self.details, self.attr) = fragment.items()[0] |
paul@1276 | 79 | self.set_tzid(tzid) |
paul@1276 | 80 | |
paul@1276 | 81 | def set_tzid(self, tzid): |
paul@1276 | 82 | |
paul@1276 | 83 | """ |
paul@1276 | 84 | Set the fallback 'tzid' for interpreting datetimes without time zone |
paul@1276 | 85 | information. |
paul@1276 | 86 | """ |
paul@1276 | 87 | |
paul@1276 | 88 | self.tzid = tzid |
paul@213 | 89 | |
paul@535 | 90 | def get_uid(self): |
paul@535 | 91 | return self.get_value("UID") |
paul@535 | 92 | |
paul@535 | 93 | def get_recurrenceid(self): |
paul@563 | 94 | |
paul@563 | 95 | """ |
paul@563 | 96 | Return the recurrence identifier, normalised to a UTC datetime if |
paul@627 | 97 | specified as a datetime or date with accompanying time zone information, |
paul@627 | 98 | maintained as a date or floating datetime otherwise. If no recurrence |
paul@627 | 99 | identifier is present, None is returned. |
paul@627 | 100 | |
paul@627 | 101 | Note that this normalised form of the identifier may well not be the |
paul@627 | 102 | same as the originally-specified identifier because that could have been |
paul@627 | 103 | specified using an accompanying TZID attribute, whereas the normalised |
paul@627 | 104 | form is effectively a converted datetime value. |
paul@563 | 105 | """ |
paul@563 | 106 | |
paul@627 | 107 | if not self.has_key("RECURRENCE-ID"): |
paul@627 | 108 | return None |
paul@627 | 109 | dt, attr = self.get_datetime_item("RECURRENCE-ID") |
paul@628 | 110 | |
paul@628 | 111 | # Coerce any date to a UTC datetime if TZID was specified. |
paul@628 | 112 | |
paul@627 | 113 | tzid = attr.get("TZID") |
paul@627 | 114 | if tzid: |
paul@627 | 115 | dt = to_timezone(to_datetime(dt, tzid), "UTC") |
paul@627 | 116 | return format_datetime(dt) |
paul@627 | 117 | |
paul@1276 | 118 | def get_recurrence_start_point(self, recurrenceid): |
paul@627 | 119 | |
paul@627 | 120 | """ |
paul@627 | 121 | Return the start point corresponding to the given 'recurrenceid', using |
paul@1276 | 122 | the fallback time zone to define the specific point in time referenced |
paul@1276 | 123 | by the recurrence identifier if the identifier has a date |
paul@1276 | 124 | representation. |
paul@627 | 125 | |
paul@627 | 126 | If 'recurrenceid' is given as None, this object's recurrence identifier |
paul@627 | 127 | is used to obtain a start point, but if this object does not provide a |
paul@627 | 128 | recurrence, None is returned. |
paul@627 | 129 | |
paul@627 | 130 | A start point is typically used to match free/busy periods which are |
paul@627 | 131 | themselves defined in terms of UTC datetimes. |
paul@627 | 132 | """ |
paul@627 | 133 | |
paul@627 | 134 | recurrenceid = recurrenceid or self.get_recurrenceid() |
paul@627 | 135 | if recurrenceid: |
paul@1276 | 136 | return get_recurrence_start_point(recurrenceid, self.tzid) |
paul@627 | 137 | else: |
paul@627 | 138 | return None |
paul@535 | 139 | |
paul@1276 | 140 | def get_recurrence_start_points(self, recurrenceids): |
paul@1275 | 141 | |
paul@1275 | 142 | """ |
paul@1276 | 143 | Return start points for 'recurrenceids' using the fallback time zone for |
paul@1275 | 144 | identifiers with date representations. |
paul@1275 | 145 | """ |
paul@1275 | 146 | |
paul@1276 | 147 | return map(self.get_recurrence_start_point, recurrenceids) |
paul@679 | 148 | |
paul@535 | 149 | # Structure access. |
paul@535 | 150 | |
paul@1204 | 151 | def add(self, obj): |
paul@1204 | 152 | |
paul@1204 | 153 | "Add 'obj' to the structure." |
paul@1204 | 154 | |
paul@1204 | 155 | name = obj.objtype |
paul@1204 | 156 | if not self.details.has_key(name): |
paul@1204 | 157 | l = self.details[name] = [] |
paul@1204 | 158 | else: |
paul@1204 | 159 | l = self.details[name] |
paul@1204 | 160 | l.append((obj.details, obj.attr)) |
paul@1204 | 161 | |
paul@524 | 162 | def copy(self): |
paul@1276 | 163 | return Object(self.to_dict(), self.tzid) |
paul@524 | 164 | |
paul@213 | 165 | def get_items(self, name, all=True): |
paul@213 | 166 | return get_items(self.details, name, all) |
paul@213 | 167 | |
paul@213 | 168 | def get_item(self, name): |
paul@213 | 169 | return get_item(self.details, name) |
paul@213 | 170 | |
paul@213 | 171 | def get_value_map(self, name): |
paul@213 | 172 | return get_value_map(self.details, name) |
paul@213 | 173 | |
paul@213 | 174 | def get_values(self, name, all=True): |
paul@213 | 175 | return get_values(self.details, name, all) |
paul@213 | 176 | |
paul@213 | 177 | def get_value(self, name): |
paul@213 | 178 | return get_value(self.details, name) |
paul@213 | 179 | |
paul@1298 | 180 | def set_value(self, name, value, attr=None): |
paul@1298 | 181 | self.details[name] = [(value, attr or {})] |
paul@1298 | 182 | |
paul@1276 | 183 | def get_utc_datetime(self, name): |
paul@1276 | 184 | return get_utc_datetime(self.details, name, self.tzid) |
paul@213 | 185 | |
paul@1276 | 186 | def get_date_value_items(self, name): |
paul@1276 | 187 | return get_date_value_items(self.details, name, self.tzid) |
paul@352 | 188 | |
paul@1276 | 189 | def get_date_value_item_periods(self, name): |
paul@1276 | 190 | return get_date_value_item_periods(self.details, name, |
paul@1276 | 191 | self.get_main_period().get_duration(), self.tzid) |
paul@878 | 192 | |
paul@1276 | 193 | def get_period_values(self, name): |
paul@1276 | 194 | return get_period_values(self.details, name, self.tzid) |
paul@630 | 195 | |
paul@318 | 196 | def get_datetime(self, name): |
paul@567 | 197 | t = get_datetime_item(self.details, name) |
paul@567 | 198 | if not t: return None |
paul@567 | 199 | dt, attr = t |
paul@318 | 200 | return dt |
paul@318 | 201 | |
paul@289 | 202 | def get_datetime_item(self, name): |
paul@289 | 203 | return get_datetime_item(self.details, name) |
paul@289 | 204 | |
paul@392 | 205 | def get_duration(self, name): |
paul@392 | 206 | return get_duration(self.get_value(name)) |
paul@392 | 207 | |
paul@1174 | 208 | # Serialisation. |
paul@1174 | 209 | |
paul@1204 | 210 | def to_dict(self): |
paul@1204 | 211 | return to_dict(self.to_node()) |
paul@1204 | 212 | |
paul@213 | 213 | def to_node(self): |
paul@213 | 214 | return to_node({self.objtype : [(self.details, self.attr)]}) |
paul@213 | 215 | |
paul@1174 | 216 | def to_part(self, method, encoding="utf-8", line_length=None): |
paul@1174 | 217 | return to_part(method, [self.to_node()], encoding, line_length) |
paul@213 | 218 | |
paul@1174 | 219 | def to_string(self, encoding="utf-8", line_length=None): |
paul@1174 | 220 | return to_string(self.to_node(), encoding, line_length) |
paul@1081 | 221 | |
paul@213 | 222 | # Direct access to the structure. |
paul@213 | 223 | |
paul@392 | 224 | def has_key(self, name): |
paul@392 | 225 | return self.details.has_key(name) |
paul@392 | 226 | |
paul@524 | 227 | def get(self, name): |
paul@524 | 228 | return self.details.get(name) |
paul@524 | 229 | |
paul@734 | 230 | def keys(self): |
paul@734 | 231 | return self.details.keys() |
paul@734 | 232 | |
paul@213 | 233 | def __getitem__(self, name): |
paul@213 | 234 | return self.details[name] |
paul@213 | 235 | |
paul@213 | 236 | def __setitem__(self, name, value): |
paul@213 | 237 | self.details[name] = value |
paul@213 | 238 | |
paul@213 | 239 | def __delitem__(self, name): |
paul@213 | 240 | del self.details[name] |
paul@213 | 241 | |
paul@524 | 242 | def remove(self, name): |
paul@524 | 243 | try: |
paul@524 | 244 | del self[name] |
paul@524 | 245 | except KeyError: |
paul@524 | 246 | pass |
paul@524 | 247 | |
paul@524 | 248 | def remove_all(self, names): |
paul@524 | 249 | for name in names: |
paul@524 | 250 | self.remove(name) |
paul@524 | 251 | |
paul@734 | 252 | def preserve(self, names): |
paul@734 | 253 | for name in self.keys(): |
paul@734 | 254 | if not name in names: |
paul@734 | 255 | self.remove(name) |
paul@734 | 256 | |
paul@256 | 257 | # Computed results. |
paul@256 | 258 | |
paul@1276 | 259 | def get_main_period(self): |
paul@797 | 260 | |
paul@797 | 261 | """ |
paul@797 | 262 | Return a period object corresponding to the main start-end period for |
paul@797 | 263 | the object. |
paul@797 | 264 | """ |
paul@797 | 265 | |
paul@879 | 266 | (dtstart, dtstart_attr), (dtend, dtend_attr) = self.get_main_period_items() |
paul@1276 | 267 | tzid = get_tzid(dtstart_attr, dtend_attr) or self.tzid |
paul@797 | 268 | return RecurringPeriod(dtstart, dtend, tzid, "DTSTART", dtstart_attr, dtend_attr) |
paul@797 | 269 | |
paul@879 | 270 | def get_main_period_items(self): |
paul@650 | 271 | |
paul@650 | 272 | """ |
paul@650 | 273 | Return two (value, attributes) items corresponding to the main start-end |
paul@650 | 274 | period for the object. |
paul@650 | 275 | """ |
paul@650 | 276 | |
paul@650 | 277 | dtstart, dtstart_attr = self.get_datetime_item("DTSTART") |
paul@650 | 278 | |
paul@650 | 279 | if self.has_key("DTEND"): |
paul@650 | 280 | dtend, dtend_attr = self.get_datetime_item("DTEND") |
paul@650 | 281 | elif self.has_key("DURATION"): |
paul@650 | 282 | duration = self.get_duration("DURATION") |
paul@650 | 283 | dtend = dtstart + duration |
paul@650 | 284 | dtend_attr = dtstart_attr |
paul@650 | 285 | else: |
paul@650 | 286 | dtend, dtend_attr = dtstart, dtstart_attr |
paul@650 | 287 | |
paul@650 | 288 | return (dtstart, dtstart_attr), (dtend, dtend_attr) |
paul@650 | 289 | |
paul@1276 | 290 | def get_periods(self, start=None, end=None, inclusive=False): |
paul@620 | 291 | |
paul@620 | 292 | """ |
paul@1276 | 293 | Return periods defined by this object, employing the fallback time zone |
paul@1276 | 294 | where no time zone information is defined, and limiting the collection |
paul@1276 | 295 | to a window of time with the given 'start' and 'end'. |
paul@630 | 296 | |
paul@630 | 297 | If 'end' is omitted, only explicit recurrences and recurrences from |
paul@630 | 298 | explicitly-terminated rules will be returned. |
paul@1123 | 299 | |
paul@1123 | 300 | If 'inclusive' is set to a true value, any period occurring at the 'end' |
paul@1123 | 301 | will be included. |
paul@1123 | 302 | """ |
paul@1123 | 303 | |
paul@1276 | 304 | return get_periods(self, start, end, inclusive) |
paul@1123 | 305 | |
paul@1276 | 306 | def has_period(self, period): |
paul@1123 | 307 | |
paul@1123 | 308 | """ |
paul@1276 | 309 | Return whether this object, employing the fallback time zone where no |
paul@1276 | 310 | time zone information is defined, has the given 'period'. |
paul@620 | 311 | """ |
paul@620 | 312 | |
paul@1276 | 313 | return period in self.get_periods(end=period.get_start_point(), inclusive=True) |
paul@1123 | 314 | |
paul@1276 | 315 | def has_recurrence(self, recurrenceid): |
paul@1123 | 316 | |
paul@1123 | 317 | """ |
paul@1276 | 318 | Return whether this object, employing the fallback time zone where no |
paul@1276 | 319 | time zone information is defined, has the given 'recurrenceid'. |
paul@1123 | 320 | """ |
paul@1123 | 321 | |
paul@1276 | 322 | start_point = self.get_recurrence_start_point(recurrenceid) |
paul@1276 | 323 | |
paul@1276 | 324 | for p in self.get_periods(end=start_point, inclusive=True): |
paul@1123 | 325 | if p.get_start_point() == start_point: |
paul@1123 | 326 | return True |
paul@1276 | 327 | |
paul@1123 | 328 | return False |
paul@360 | 329 | |
paul@1276 | 330 | def get_active_periods(self, recurrenceids, start=None, end=None): |
paul@630 | 331 | |
paul@630 | 332 | """ |
paul@630 | 333 | Return all periods specified by this object that are not replaced by |
paul@1276 | 334 | those defined by 'recurrenceids', using the fallback time zone to |
paul@1276 | 335 | convert floating dates and datetimes, and using 'start' and 'end' to |
paul@1238 | 336 | respectively indicate the start and end of the time window within which |
paul@1238 | 337 | periods are considered. |
paul@630 | 338 | """ |
paul@630 | 339 | |
paul@630 | 340 | # Specific recurrences yield all specified periods. |
paul@630 | 341 | |
paul@1276 | 342 | periods = self.get_periods(start, end) |
paul@630 | 343 | |
paul@630 | 344 | if self.get_recurrenceid(): |
paul@630 | 345 | return periods |
paul@630 | 346 | |
paul@630 | 347 | # Parent objects need to have their periods tested against redefined |
paul@630 | 348 | # recurrences. |
paul@630 | 349 | |
paul@630 | 350 | active = [] |
paul@630 | 351 | |
paul@630 | 352 | for p in periods: |
paul@630 | 353 | |
paul@630 | 354 | # Subtract any recurrences from the free/busy details of a |
paul@630 | 355 | # parent object. |
paul@630 | 356 | |
paul@648 | 357 | if not p.is_replaced(recurrenceids): |
paul@630 | 358 | active.append(p) |
paul@630 | 359 | |
paul@630 | 360 | return active |
paul@630 | 361 | |
paul@648 | 362 | def get_freebusy_period(self, period, only_organiser=False): |
paul@648 | 363 | |
paul@648 | 364 | """ |
paul@648 | 365 | Return a free/busy period for the given 'period' provided by this |
paul@648 | 366 | object, using the 'only_organiser' status to produce a suitable |
paul@648 | 367 | transparency value. |
paul@648 | 368 | """ |
paul@648 | 369 | |
paul@648 | 370 | return FreeBusyPeriod( |
paul@648 | 371 | period.get_start_point(), |
paul@648 | 372 | period.get_end_point(), |
paul@648 | 373 | self.get_value("UID"), |
paul@648 | 374 | only_organiser and "ORG" or self.get_value("TRANSP") or "OPAQUE", |
paul@648 | 375 | self.get_recurrenceid(), |
paul@648 | 376 | self.get_value("SUMMARY"), |
paul@814 | 377 | get_uri(self.get_value("ORGANIZER")) |
paul@648 | 378 | ) |
paul@648 | 379 | |
paul@648 | 380 | def get_participation_status(self, participant): |
paul@648 | 381 | |
paul@648 | 382 | """ |
paul@648 | 383 | Return the participation status of the given 'participant', with the |
paul@648 | 384 | special value "ORG" indicating organiser-only participation. |
paul@648 | 385 | """ |
paul@648 | 386 | |
paul@814 | 387 | attendees = uri_dict(self.get_value_map("ATTENDEE")) |
paul@814 | 388 | organiser = get_uri(self.get_value("ORGANIZER")) |
paul@648 | 389 | |
paul@692 | 390 | attendee_attr = attendees.get(participant) |
paul@692 | 391 | if attendee_attr: |
paul@692 | 392 | return attendee_attr.get("PARTSTAT", "NEEDS-ACTION") |
paul@692 | 393 | elif organiser == participant: |
paul@692 | 394 | return "ORG" |
paul@648 | 395 | |
paul@648 | 396 | return None |
paul@648 | 397 | |
paul@648 | 398 | def get_participation(self, partstat, include_needs_action=False): |
paul@648 | 399 | |
paul@648 | 400 | """ |
paul@648 | 401 | Return whether 'partstat' indicates some kind of participation in an |
paul@648 | 402 | event. If 'include_needs_action' is specified as a true value, events |
paul@648 | 403 | not yet responded to will be treated as events with tentative |
paul@648 | 404 | participation. |
paul@648 | 405 | """ |
paul@648 | 406 | |
paul@648 | 407 | return not partstat in ("DECLINED", "DELEGATED", "NEEDS-ACTION") or \ |
paul@648 | 408 | include_needs_action and partstat == "NEEDS-ACTION" or \ |
paul@648 | 409 | partstat == "ORG" |
paul@648 | 410 | |
paul@422 | 411 | def get_tzid(self): |
paul@562 | 412 | |
paul@562 | 413 | """ |
paul@562 | 414 | Return a time zone identifier used by the start or end datetimes, |
paul@1276 | 415 | potentially suitable for converting dates to datetimes. Where no |
paul@1276 | 416 | identifier is associated with the datetimes, provide any fallback time |
paul@1276 | 417 | zone identifier. |
paul@562 | 418 | """ |
paul@562 | 419 | |
paul@1276 | 420 | (dtstart, dtstart_attr), (dtend, dtend_attr) = self.get_main_period_items() |
paul@1276 | 421 | return get_tzid(dtstart_attr, dtend_attr) or self.tzid |
paul@422 | 422 | |
paul@619 | 423 | def is_shared(self): |
paul@619 | 424 | |
paul@619 | 425 | """ |
paul@619 | 426 | Return whether this object is shared based on the presence of a SEQUENCE |
paul@619 | 427 | property. |
paul@619 | 428 | """ |
paul@619 | 429 | |
paul@619 | 430 | return self.get_value("SEQUENCE") is not None |
paul@619 | 431 | |
paul@1276 | 432 | def possibly_active_from(self, dt): |
paul@650 | 433 | |
paul@650 | 434 | """ |
paul@650 | 435 | Return whether the object is possibly active from or after the given |
paul@1276 | 436 | datetime 'dt' using the fallback time zone to convert any dates or |
paul@1276 | 437 | floating datetimes. |
paul@650 | 438 | """ |
paul@650 | 439 | |
paul@1276 | 440 | dt = to_datetime(dt, self.tzid) |
paul@1276 | 441 | periods = self.get_periods() |
paul@650 | 442 | |
paul@650 | 443 | for p in periods: |
paul@650 | 444 | if p.get_end_point() > dt: |
paul@650 | 445 | return True |
paul@650 | 446 | |
paul@672 | 447 | return self.possibly_recurring_indefinitely() |
paul@672 | 448 | |
paul@672 | 449 | def possibly_recurring_indefinitely(self): |
paul@672 | 450 | |
paul@672 | 451 | "Return whether this object may recur indefinitely." |
paul@672 | 452 | |
paul@650 | 453 | rrule = self.get_value("RRULE") |
paul@650 | 454 | parameters = rrule and get_parameters(rrule) |
paul@650 | 455 | until = parameters and parameters.get("UNTIL") |
paul@651 | 456 | count = parameters and parameters.get("COUNT") |
paul@650 | 457 | |
paul@672 | 458 | # Non-recurring periods or constrained recurrences. |
paul@651 | 459 | |
paul@651 | 460 | if not rrule or until or count: |
paul@650 | 461 | return False |
paul@651 | 462 | |
paul@672 | 463 | # Unconstrained recurring periods will always lie beyond any specified |
paul@651 | 464 | # datetime. |
paul@651 | 465 | |
paul@651 | 466 | else: |
paul@650 | 467 | return True |
paul@650 | 468 | |
paul@627 | 469 | # Modification methods. |
paul@627 | 470 | |
paul@1276 | 471 | def set_datetime(self, name, dt): |
paul@627 | 472 | |
paul@627 | 473 | """ |
paul@1276 | 474 | Set a datetime for property 'name' using 'dt' and the fallback time zone |
paul@1276 | 475 | where necessary, returning whether an update has occurred. |
paul@627 | 476 | """ |
paul@627 | 477 | |
paul@627 | 478 | if dt: |
paul@627 | 479 | old_value = self.get_value(name) |
paul@1276 | 480 | self[name] = [get_item_from_datetime(dt, self.tzid)] |
paul@627 | 481 | return format_datetime(dt) != old_value |
paul@627 | 482 | |
paul@627 | 483 | return False |
paul@627 | 484 | |
paul@627 | 485 | def set_period(self, period): |
paul@627 | 486 | |
paul@627 | 487 | "Set the given 'period' as the main start and end." |
paul@627 | 488 | |
paul@627 | 489 | result = self.set_datetime("DTSTART", period.get_start()) |
paul@627 | 490 | result = self.set_datetime("DTEND", period.get_end()) or result |
paul@661 | 491 | if self.has_key("DURATION"): |
paul@661 | 492 | del self["DURATION"] |
paul@661 | 493 | |
paul@627 | 494 | return result |
paul@627 | 495 | |
paul@627 | 496 | def set_periods(self, periods): |
paul@627 | 497 | |
paul@627 | 498 | """ |
paul@627 | 499 | Set the given 'periods' as recurrence date properties, replacing the |
paul@627 | 500 | previous RDATE properties and ignoring any RRULE properties. |
paul@627 | 501 | """ |
paul@627 | 502 | |
paul@880 | 503 | old_values = set(self.get_date_value_item_periods("RDATE") or []) |
paul@627 | 504 | new_rdates = [] |
paul@627 | 505 | |
paul@627 | 506 | if self.has_key("RDATE"): |
paul@627 | 507 | del self["RDATE"] |
paul@627 | 508 | |
paul@812 | 509 | main_changed = False |
paul@812 | 510 | |
paul@627 | 511 | for p in periods: |
paul@1291 | 512 | if p.origin == "DTSTART": |
paul@1291 | 513 | main_changed = self.set_period(p) |
paul@1291 | 514 | elif p.origin != "RRULE" and p != self.get_main_period(): |
paul@627 | 515 | new_rdates.append(get_period_item(p.get_start(), p.get_end())) |
paul@627 | 516 | |
paul@661 | 517 | if new_rdates: |
paul@661 | 518 | self["RDATE"] = new_rdates |
paul@661 | 519 | |
paul@880 | 520 | return main_changed or old_values != set(self.get_date_value_item_periods("RDATE") or []) |
paul@661 | 521 | |
paul@872 | 522 | def set_rule(self, rule): |
paul@872 | 523 | |
paul@872 | 524 | """ |
paul@872 | 525 | Set the given 'rule' in this object, replacing the previous RRULE |
paul@872 | 526 | property, returning whether the object has changed. The provided 'rule' |
paul@872 | 527 | must be an item. |
paul@872 | 528 | """ |
paul@872 | 529 | |
paul@872 | 530 | if not rule: |
paul@872 | 531 | return False |
paul@872 | 532 | |
paul@872 | 533 | old_rrule = self.get_item("RRULE") |
paul@872 | 534 | self["RRULE"] = [rule] |
paul@872 | 535 | return old_rrule != rule |
paul@872 | 536 | |
paul@872 | 537 | def set_exceptions(self, exceptions): |
paul@872 | 538 | |
paul@872 | 539 | """ |
paul@872 | 540 | Set the given 'exceptions' in this object, replacing the previous EXDATE |
paul@872 | 541 | properties, returning whether the object has changed. The provided |
paul@872 | 542 | 'exceptions' must be a collection of items. |
paul@872 | 543 | """ |
paul@872 | 544 | |
paul@880 | 545 | old_exdates = set(self.get_date_value_item_periods("EXDATE") or []) |
paul@872 | 546 | if exceptions: |
paul@872 | 547 | self["EXDATE"] = exceptions |
paul@880 | 548 | return old_exdates != set(self.get_date_value_item_periods("EXDATE") or []) |
paul@872 | 549 | elif old_exdates: |
paul@872 | 550 | del self["EXDATE"] |
paul@872 | 551 | return True |
paul@872 | 552 | else: |
paul@872 | 553 | return False |
paul@872 | 554 | |
paul@809 | 555 | def update_dtstamp(self): |
paul@809 | 556 | |
paul@809 | 557 | "Update the DTSTAMP in the object." |
paul@809 | 558 | |
paul@809 | 559 | dtstamp = self.get_utc_datetime("DTSTAMP") |
paul@809 | 560 | utcnow = get_time() |
paul@809 | 561 | dtstamp = format_datetime(dtstamp and dtstamp > utcnow and dtstamp or utcnow) |
paul@809 | 562 | self["DTSTAMP"] = [(dtstamp, {})] |
paul@809 | 563 | return dtstamp |
paul@809 | 564 | |
paul@809 | 565 | def update_sequence(self, increment=False): |
paul@809 | 566 | |
paul@809 | 567 | "Set or update the SEQUENCE in the object." |
paul@809 | 568 | |
paul@809 | 569 | sequence = self.get_value("SEQUENCE") or "0" |
paul@809 | 570 | self["SEQUENCE"] = [(str(int(sequence) + (increment and 1 or 0)), {})] |
paul@809 | 571 | return sequence |
paul@809 | 572 | |
paul@878 | 573 | def update_exceptions(self, excluded, asserted): |
paul@784 | 574 | |
paul@784 | 575 | """ |
paul@784 | 576 | Update the exceptions to any rule by applying the list of 'excluded' |
paul@878 | 577 | periods. Where 'asserted' periods are provided, exceptions will be |
paul@878 | 578 | removed corresponding to those periods. |
paul@784 | 579 | """ |
paul@784 | 580 | |
paul@885 | 581 | old_exdates = self.get_date_value_item_periods("EXDATE") or [] |
paul@878 | 582 | new_exdates = set(old_exdates) |
paul@878 | 583 | new_exdates.update(excluded) |
paul@878 | 584 | new_exdates.difference_update(asserted) |
paul@784 | 585 | |
paul@885 | 586 | if not new_exdates and self.has_key("EXDATE"): |
paul@878 | 587 | del self["EXDATE"] |
paul@878 | 588 | else: |
paul@784 | 589 | self["EXDATE"] = [] |
paul@878 | 590 | for p in new_exdates: |
paul@878 | 591 | self["EXDATE"].append(get_period_item(p.get_start(), p.get_end())) |
paul@784 | 592 | |
paul@878 | 593 | return set(old_exdates) != new_exdates |
paul@784 | 594 | |
paul@1276 | 595 | def correct_object(self, permitted_values): |
paul@661 | 596 | |
paul@1276 | 597 | "Correct the object's period details using the 'permitted_values'." |
paul@661 | 598 | |
paul@661 | 599 | corrected = set() |
paul@661 | 600 | rdates = [] |
paul@661 | 601 | |
paul@1276 | 602 | for period in self.get_periods(): |
paul@939 | 603 | corrected_period = period.get_corrected(permitted_values) |
paul@627 | 604 | |
paul@939 | 605 | if corrected_period is period: |
paul@661 | 606 | if period.origin == "RDATE": |
paul@661 | 607 | rdates.append(period) |
paul@661 | 608 | continue |
paul@661 | 609 | |
paul@661 | 610 | if period.origin == "DTSTART": |
paul@939 | 611 | self.set_period(corrected_period) |
paul@661 | 612 | corrected.add("DTSTART") |
paul@661 | 613 | elif period.origin == "RDATE": |
paul@939 | 614 | rdates.append(corrected_period) |
paul@661 | 615 | corrected.add("RDATE") |
paul@661 | 616 | |
paul@661 | 617 | if "RDATE" in corrected: |
paul@661 | 618 | self.set_periods(rdates) |
paul@661 | 619 | |
paul@661 | 620 | return corrected |
paul@627 | 621 | |
paul@213 | 622 | # Construction and serialisation. |
paul@213 | 623 | |
paul@213 | 624 | def make_calendar(nodes, method=None): |
paul@213 | 625 | |
paul@213 | 626 | """ |
paul@213 | 627 | Return a complete calendar node wrapping the given 'nodes' and employing the |
paul@213 | 628 | given 'method', if indicated. |
paul@213 | 629 | """ |
paul@213 | 630 | |
paul@213 | 631 | return ("VCALENDAR", {}, |
paul@213 | 632 | (method and [("METHOD", {}, method)] or []) + |
paul@213 | 633 | [("VERSION", {}, "2.0")] + |
paul@213 | 634 | nodes |
paul@213 | 635 | ) |
paul@213 | 636 | |
paul@327 | 637 | def make_freebusy(freebusy, uid, organiser, organiser_attr=None, attendee=None, |
paul@562 | 638 | attendee_attr=None, period=None): |
paul@222 | 639 | |
paul@222 | 640 | """ |
paul@222 | 641 | Return a calendar node defining the free/busy details described in the given |
paul@292 | 642 | 'freebusy' list, employing the given 'uid', for the given 'organiser' and |
paul@292 | 643 | optional 'organiser_attr', with the optional 'attendee' providing recipient |
paul@292 | 644 | details together with the optional 'attendee_attr'. |
paul@327 | 645 | |
paul@562 | 646 | The result will be constrained to the 'period' if specified. |
paul@222 | 647 | """ |
paul@222 | 648 | |
paul@222 | 649 | record = [] |
paul@222 | 650 | rwrite = record.append |
paul@222 | 651 | |
paul@292 | 652 | rwrite(("ORGANIZER", organiser_attr or {}, organiser)) |
paul@222 | 653 | |
paul@222 | 654 | if attendee: |
paul@292 | 655 | rwrite(("ATTENDEE", attendee_attr or {}, attendee)) |
paul@222 | 656 | |
paul@222 | 657 | rwrite(("UID", {}, uid)) |
paul@222 | 658 | |
paul@222 | 659 | if freebusy: |
paul@327 | 660 | |
paul@327 | 661 | # Get a constrained view if start and end limits are specified. |
paul@327 | 662 | |
paul@563 | 663 | if period: |
paul@1189 | 664 | periods = freebusy.get_overlapping([period]) |
paul@563 | 665 | else: |
paul@563 | 666 | periods = freebusy |
paul@327 | 667 | |
paul@327 | 668 | # Write the limits of the resource. |
paul@327 | 669 | |
paul@563 | 670 | if periods: |
paul@563 | 671 | rwrite(("DTSTART", {"VALUE" : "DATE-TIME"}, format_datetime(periods[0].get_start_point()))) |
paul@563 | 672 | rwrite(("DTEND", {"VALUE" : "DATE-TIME"}, format_datetime(periods[-1].get_end_point()))) |
paul@563 | 673 | else: |
paul@563 | 674 | rwrite(("DTSTART", {"VALUE" : "DATE-TIME"}, format_datetime(period.get_start_point()))) |
paul@563 | 675 | rwrite(("DTEND", {"VALUE" : "DATE-TIME"}, format_datetime(period.get_end_point()))) |
paul@327 | 676 | |
paul@458 | 677 | for p in periods: |
paul@458 | 678 | if p.transp == "OPAQUE": |
paul@529 | 679 | rwrite(("FREEBUSY", {"FBTYPE" : "BUSY"}, "/".join( |
paul@562 | 680 | map(format_datetime, [p.get_start_point(), p.get_end_point()]) |
paul@529 | 681 | ))) |
paul@222 | 682 | |
paul@222 | 683 | return ("VFREEBUSY", {}, record) |
paul@222 | 684 | |
paul@1276 | 685 | def parse_calendar(f, encoding, tzid=None): |
paul@1269 | 686 | |
paul@1269 | 687 | """ |
paul@1269 | 688 | Parse the iTIP content from 'f' having the given 'encoding'. Return a |
paul@1276 | 689 | mapping from object types to collections of calendar objects. If 'tzid' is |
paul@1276 | 690 | specified, use it to set the fallback time zone on all returned objects. |
paul@1269 | 691 | """ |
paul@1269 | 692 | |
paul@1269 | 693 | cal = parse_object(f, encoding, "VCALENDAR") |
paul@1269 | 694 | d = {} |
paul@1269 | 695 | |
paul@1269 | 696 | for objtype, values in cal.items(): |
paul@1269 | 697 | d[objtype] = l = [] |
paul@1269 | 698 | for value in values: |
paul@1276 | 699 | l.append(Object({objtype : value}, tzid)) |
paul@1269 | 700 | |
paul@1269 | 701 | return d |
paul@1269 | 702 | |
paul@213 | 703 | def parse_object(f, encoding, objtype=None): |
paul@213 | 704 | |
paul@213 | 705 | """ |
paul@213 | 706 | Parse the iTIP content from 'f' having the given 'encoding'. If 'objtype' is |
paul@213 | 707 | given, only objects of that type will be returned. Otherwise, the root of |
paul@213 | 708 | the content will be returned as a dictionary with a single key indicating |
paul@213 | 709 | the object type. |
paul@213 | 710 | |
paul@213 | 711 | Return None if the content was not readable or suitable. |
paul@213 | 712 | """ |
paul@213 | 713 | |
paul@213 | 714 | try: |
paul@213 | 715 | try: |
paul@213 | 716 | doctype, attrs, elements = obj = parse(f, encoding=encoding) |
paul@213 | 717 | if objtype and doctype == objtype: |
paul@213 | 718 | return to_dict(obj)[objtype][0] |
paul@213 | 719 | elif not objtype: |
paul@213 | 720 | return to_dict(obj) |
paul@213 | 721 | finally: |
paul@213 | 722 | f.close() |
paul@213 | 723 | |
paul@213 | 724 | # NOTE: Handle parse errors properly. |
paul@213 | 725 | |
paul@213 | 726 | except (ParseError, ValueError): |
paul@213 | 727 | pass |
paul@213 | 728 | |
paul@213 | 729 | return None |
paul@213 | 730 | |
paul@1072 | 731 | def parse_string(s, encoding, objtype=None): |
paul@1072 | 732 | |
paul@1072 | 733 | """ |
paul@1072 | 734 | Parse the iTIP content from 's' having the given 'encoding'. If 'objtype' is |
paul@1072 | 735 | given, only objects of that type will be returned. Otherwise, the root of |
paul@1072 | 736 | the content will be returned as a dictionary with a single key indicating |
paul@1072 | 737 | the object type. |
paul@1072 | 738 | |
paul@1072 | 739 | Return None if the content was not readable or suitable. |
paul@1072 | 740 | """ |
paul@1072 | 741 | |
paul@1072 | 742 | return parse_object(StringIO(s), encoding, objtype) |
paul@1072 | 743 | |
paul@1174 | 744 | def to_part(method, fragments, encoding="utf-8", line_length=None): |
paul@213 | 745 | |
paul@213 | 746 | """ |
paul@1174 | 747 | Write using the given 'method', the given 'fragments' to a MIME |
paul@213 | 748 | text/calendar part. |
paul@213 | 749 | """ |
paul@213 | 750 | |
paul@213 | 751 | out = StringIO() |
paul@213 | 752 | try: |
paul@1174 | 753 | to_stream(out, make_calendar(fragments, method), encoding, line_length) |
paul@213 | 754 | part = MIMEText(out.getvalue(), "calendar", encoding) |
paul@213 | 755 | part.set_param("method", method) |
paul@213 | 756 | return part |
paul@213 | 757 | |
paul@213 | 758 | finally: |
paul@213 | 759 | out.close() |
paul@213 | 760 | |
paul@1174 | 761 | def to_stream(out, fragment, encoding="utf-8", line_length=None): |
paul@1084 | 762 | |
paul@1084 | 763 | "Write to the 'out' stream the given 'fragment'." |
paul@1084 | 764 | |
paul@1174 | 765 | iterwrite(out, encoding=encoding, line_length=line_length).append(fragment) |
paul@213 | 766 | |
paul@1174 | 767 | def to_string(fragment, encoding="utf-8", line_length=None): |
paul@1084 | 768 | |
paul@1084 | 769 | "Return a string encoding the given 'fragment'." |
paul@1084 | 770 | |
paul@1072 | 771 | out = StringIO() |
paul@1072 | 772 | try: |
paul@1174 | 773 | to_stream(out, fragment, encoding, line_length) |
paul@1072 | 774 | return out.getvalue() |
paul@1072 | 775 | |
paul@1072 | 776 | finally: |
paul@1072 | 777 | out.close() |
paul@1072 | 778 | |
paul@1321 | 779 | def new_object(object_type, organiser=None, organiser_attr=None, tzid=None): |
paul@1298 | 780 | |
paul@1298 | 781 | """ |
paul@1298 | 782 | Make a new object of the given 'object_type' and optional 'organiser', |
paul@1298 | 783 | with optional 'organiser_attr' describing any organiser identity in more |
paul@1321 | 784 | detail. An optional 'tzid' can also be provided. |
paul@1298 | 785 | """ |
paul@1204 | 786 | |
paul@1298 | 787 | details = {} |
paul@1204 | 788 | |
paul@1298 | 789 | if organiser: |
paul@1298 | 790 | details["UID"] = [(make_uid(organiser), {})] |
paul@1298 | 791 | details["ORGANIZER"] = [(organiser, organiser_attr or {})] |
paul@1298 | 792 | details["DTSTAMP"] = [(get_timestamp(), {})] |
paul@1298 | 793 | |
paul@1321 | 794 | return Object({object_type : (details, {})}, tzid) |
paul@1204 | 795 | |
paul@1204 | 796 | def make_uid(user): |
paul@1204 | 797 | |
paul@1204 | 798 | "Return a unique identifier for a new object by the given 'user'." |
paul@1204 | 799 | |
paul@1204 | 800 | utcnow = get_timestamp() |
paul@1204 | 801 | return "imip-agent-%s-%s" % (utcnow, get_address(user)) |
paul@1204 | 802 | |
paul@213 | 803 | # Structure access functions. |
paul@213 | 804 | |
paul@213 | 805 | def get_items(d, name, all=True): |
paul@213 | 806 | |
paul@213 | 807 | """ |
paul@213 | 808 | Get all items from 'd' for the given 'name', returning single items if |
paul@213 | 809 | 'all' is specified and set to a false value and if only one value is |
paul@213 | 810 | present for the name. Return None if no items are found for the name or if |
paul@213 | 811 | many items are found but 'all' is set to a false value. |
paul@213 | 812 | """ |
paul@213 | 813 | |
paul@213 | 814 | if d.has_key(name): |
paul@712 | 815 | items = [(value or None, attr) for value, attr in d[name]] |
paul@213 | 816 | if all: |
paul@462 | 817 | return items |
paul@462 | 818 | elif len(items) == 1: |
paul@462 | 819 | return items[0] |
paul@213 | 820 | else: |
paul@213 | 821 | return None |
paul@213 | 822 | else: |
paul@213 | 823 | return None |
paul@213 | 824 | |
paul@213 | 825 | def get_item(d, name): |
paul@213 | 826 | return get_items(d, name, False) |
paul@213 | 827 | |
paul@213 | 828 | def get_value_map(d, name): |
paul@213 | 829 | |
paul@213 | 830 | """ |
paul@213 | 831 | Return a dictionary for all items in 'd' having the given 'name'. The |
paul@213 | 832 | dictionary will map values for the name to any attributes or qualifiers |
paul@213 | 833 | that may have been present. |
paul@213 | 834 | """ |
paul@213 | 835 | |
paul@213 | 836 | items = get_items(d, name) |
paul@213 | 837 | if items: |
paul@213 | 838 | return dict(items) |
paul@213 | 839 | else: |
paul@213 | 840 | return {} |
paul@213 | 841 | |
paul@462 | 842 | def values_from_items(items): |
paul@462 | 843 | return map(lambda x: x[0], items) |
paul@462 | 844 | |
paul@213 | 845 | def get_values(d, name, all=True): |
paul@213 | 846 | if d.has_key(name): |
paul@462 | 847 | items = d[name] |
paul@462 | 848 | if not all and len(items) == 1: |
paul@462 | 849 | return items[0][0] |
paul@213 | 850 | else: |
paul@462 | 851 | return values_from_items(items) |
paul@213 | 852 | else: |
paul@213 | 853 | return None |
paul@213 | 854 | |
paul@213 | 855 | def get_value(d, name): |
paul@213 | 856 | return get_values(d, name, False) |
paul@213 | 857 | |
paul@417 | 858 | def get_date_value_items(d, name, tzid=None): |
paul@352 | 859 | |
paul@352 | 860 | """ |
paul@389 | 861 | Obtain items from 'd' having the given 'name', where a single item yields |
paul@389 | 862 | potentially many values. Return a list of tuples of the form (value, |
paul@389 | 863 | attributes) where the attributes have been given for the property in 'd'. |
paul@352 | 864 | """ |
paul@352 | 865 | |
paul@403 | 866 | items = get_items(d, name) |
paul@403 | 867 | if items: |
paul@403 | 868 | all_items = [] |
paul@403 | 869 | for item in items: |
paul@403 | 870 | values, attr = item |
paul@417 | 871 | if not attr.has_key("TZID") and tzid: |
paul@417 | 872 | attr["TZID"] = tzid |
paul@403 | 873 | if not isinstance(values, list): |
paul@403 | 874 | values = [values] |
paul@403 | 875 | for value in values: |
paul@403 | 876 | all_items.append((get_datetime(value, attr) or get_period(value, attr), attr)) |
paul@403 | 877 | return all_items |
paul@352 | 878 | else: |
paul@352 | 879 | return None |
paul@352 | 880 | |
paul@878 | 881 | def get_date_value_item_periods(d, name, duration, tzid=None): |
paul@878 | 882 | |
paul@878 | 883 | """ |
paul@878 | 884 | Obtain items from 'd' having the given 'name', where a single item yields |
paul@878 | 885 | potentially many values. The 'duration' must be provided to define the |
paul@878 | 886 | length of periods having only a start datetime. Return a list of periods |
paul@878 | 887 | corresponding to the property in 'd'. |
paul@878 | 888 | """ |
paul@878 | 889 | |
paul@878 | 890 | items = get_date_value_items(d, name, tzid) |
paul@878 | 891 | if not items: |
paul@878 | 892 | return items |
paul@878 | 893 | |
paul@878 | 894 | periods = [] |
paul@878 | 895 | |
paul@878 | 896 | for value, attr in items: |
paul@878 | 897 | if isinstance(value, tuple): |
paul@878 | 898 | periods.append(RecurringPeriod(value[0], value[1], tzid, name, attr)) |
paul@878 | 899 | else: |
paul@878 | 900 | periods.append(RecurringPeriod(value, value + duration, tzid, name, attr)) |
paul@878 | 901 | |
paul@878 | 902 | return periods |
paul@878 | 903 | |
paul@646 | 904 | def get_period_values(d, name, tzid=None): |
paul@630 | 905 | |
paul@630 | 906 | """ |
paul@630 | 907 | Return period values from 'd' for the given property 'name', using 'tzid' |
paul@646 | 908 | where specified to indicate the time zone. |
paul@630 | 909 | """ |
paul@630 | 910 | |
paul@630 | 911 | values = [] |
paul@630 | 912 | for value, attr in get_items(d, name) or []: |
paul@630 | 913 | if not attr.has_key("TZID") and tzid: |
paul@630 | 914 | attr["TZID"] = tzid |
paul@630 | 915 | start, end = get_period(value, attr) |
paul@646 | 916 | values.append(Period(start, end, tzid=tzid)) |
paul@630 | 917 | return values |
paul@630 | 918 | |
paul@506 | 919 | def get_utc_datetime(d, name, date_tzid=None): |
paul@506 | 920 | |
paul@506 | 921 | """ |
paul@506 | 922 | Return the value provided by 'd' for 'name' as a datetime in the UTC zone |
paul@506 | 923 | or as a date, converting any date to a datetime if 'date_tzid' is specified. |
paul@720 | 924 | If no datetime or date is available, None is returned. |
paul@506 | 925 | """ |
paul@506 | 926 | |
paul@348 | 927 | t = get_datetime_item(d, name) |
paul@348 | 928 | if not t: |
paul@348 | 929 | return None |
paul@348 | 930 | else: |
paul@348 | 931 | dt, attr = t |
paul@720 | 932 | return dt is not None and to_utc_datetime(dt, date_tzid) or None |
paul@289 | 933 | |
paul@289 | 934 | def get_datetime_item(d, name): |
paul@562 | 935 | |
paul@562 | 936 | """ |
paul@562 | 937 | Return the value provided by 'd' for 'name' as a datetime or as a date, |
paul@562 | 938 | together with the attributes describing it. Return None if no value exists |
paul@562 | 939 | for 'name' in 'd'. |
paul@562 | 940 | """ |
paul@562 | 941 | |
paul@348 | 942 | t = get_item(d, name) |
paul@348 | 943 | if not t: |
paul@348 | 944 | return None |
paul@348 | 945 | else: |
paul@348 | 946 | value, attr = t |
paul@613 | 947 | dt = get_datetime(value, attr) |
paul@616 | 948 | tzid = get_datetime_tzid(dt) |
paul@616 | 949 | if tzid: |
paul@616 | 950 | attr["TZID"] = tzid |
paul@613 | 951 | return dt, attr |
paul@213 | 952 | |
paul@528 | 953 | # Conversion functions. |
paul@528 | 954 | |
paul@792 | 955 | def get_address_parts(values): |
paul@792 | 956 | |
paul@792 | 957 | "Return name and address tuples for each of the given 'values'." |
paul@792 | 958 | |
paul@792 | 959 | l = [] |
paul@792 | 960 | for name, address in values and email.utils.getaddresses(values) or []: |
paul@792 | 961 | if is_mailto_uri(name): |
paul@792 | 962 | name = name[7:] # strip "mailto:" |
paul@792 | 963 | l.append((name, address)) |
paul@792 | 964 | return l |
paul@792 | 965 | |
paul@213 | 966 | def get_addresses(values): |
paul@790 | 967 | |
paul@790 | 968 | """ |
paul@790 | 969 | Return only addresses from the given 'values' which may be of the form |
paul@790 | 970 | "Common Name <recipient@domain>", with the latter part being the address |
paul@790 | 971 | itself. |
paul@790 | 972 | """ |
paul@790 | 973 | |
paul@792 | 974 | return [address for name, address in get_address_parts(values)] |
paul@213 | 975 | |
paul@213 | 976 | def get_address(value): |
paul@790 | 977 | |
paul@790 | 978 | "Return an e-mail address from the given 'value'." |
paul@790 | 979 | |
paul@712 | 980 | if not value: return None |
paul@792 | 981 | return get_addresses([value])[0] |
paul@792 | 982 | |
paul@792 | 983 | def get_verbose_address(value, attr=None): |
paul@792 | 984 | |
paul@792 | 985 | """ |
paul@792 | 986 | Return a verbose e-mail address featuring any name from the given 'value' |
paul@792 | 987 | and any accompanying 'attr' dictionary. |
paul@792 | 988 | """ |
paul@792 | 989 | |
paul@810 | 990 | l = get_address_parts([value]) |
paul@810 | 991 | if not l: |
paul@810 | 992 | return value |
paul@810 | 993 | name, address = l[0] |
paul@792 | 994 | if not name: |
paul@792 | 995 | name = attr and attr.get("CN") |
paul@792 | 996 | if name and address: |
paul@792 | 997 | return "%s <%s>" % (name, address) |
paul@792 | 998 | else: |
paul@792 | 999 | return address |
paul@792 | 1000 | |
paul@792 | 1001 | def is_mailto_uri(value): |
paul@1251 | 1002 | |
paul@1251 | 1003 | """ |
paul@1251 | 1004 | Return whether 'value' is a mailto: URI, with the protocol potentially being |
paul@1251 | 1005 | in upper case. |
paul@1251 | 1006 | """ |
paul@1251 | 1007 | |
paul@792 | 1008 | return value.lower().startswith("mailto:") |
paul@213 | 1009 | |
paul@213 | 1010 | def get_uri(value): |
paul@790 | 1011 | |
paul@790 | 1012 | "Return a URI for the given 'value'." |
paul@790 | 1013 | |
paul@712 | 1014 | if not value: return None |
paul@1251 | 1015 | |
paul@1251 | 1016 | # Normalise to "mailto:" or return other URI form. |
paul@1251 | 1017 | |
paul@792 | 1018 | return is_mailto_uri(value) and ("mailto:%s" % value[7:]) or \ |
paul@712 | 1019 | ":" in value and value or \ |
paul@790 | 1020 | "mailto:%s" % get_address(value) |
paul@213 | 1021 | |
paul@792 | 1022 | def uri_parts(values): |
paul@792 | 1023 | |
paul@792 | 1024 | "Return any common name plus the URI for each of the given 'values'." |
paul@792 | 1025 | |
paul@792 | 1026 | return [(name, get_uri(address)) for name, address in get_address_parts(values)] |
paul@792 | 1027 | |
paul@309 | 1028 | uri_value = get_uri |
paul@309 | 1029 | |
paul@309 | 1030 | def uri_values(values): |
paul@309 | 1031 | return map(get_uri, values) |
paul@309 | 1032 | |
paul@213 | 1033 | def uri_dict(d): |
paul@213 | 1034 | return dict([(get_uri(key), value) for key, value in d.items()]) |
paul@213 | 1035 | |
paul@213 | 1036 | def uri_item(item): |
paul@213 | 1037 | return get_uri(item[0]), item[1] |
paul@213 | 1038 | |
paul@213 | 1039 | def uri_items(items): |
paul@213 | 1040 | return [(get_uri(value), attr) for value, attr in items] |
paul@213 | 1041 | |
paul@220 | 1042 | # Operations on structure data. |
paul@220 | 1043 | |
paul@682 | 1044 | def is_new_object(old_sequence, new_sequence, old_dtstamp, new_dtstamp, ignore_dtstamp): |
paul@220 | 1045 | |
paul@220 | 1046 | """ |
paul@220 | 1047 | Return for the given 'old_sequence' and 'new_sequence', 'old_dtstamp' and |
paul@682 | 1048 | 'new_dtstamp', and the 'ignore_dtstamp' indication, whether the object |
paul@220 | 1049 | providing the new information is really newer than the object providing the |
paul@220 | 1050 | old information. |
paul@220 | 1051 | """ |
paul@220 | 1052 | |
paul@220 | 1053 | have_sequence = old_sequence is not None and new_sequence is not None |
paul@220 | 1054 | is_same_sequence = have_sequence and int(new_sequence) == int(old_sequence) |
paul@220 | 1055 | |
paul@220 | 1056 | have_dtstamp = old_dtstamp and new_dtstamp |
paul@220 | 1057 | is_old_dtstamp = have_dtstamp and new_dtstamp < old_dtstamp or old_dtstamp and not new_dtstamp |
paul@220 | 1058 | |
paul@220 | 1059 | is_old_sequence = have_sequence and ( |
paul@220 | 1060 | int(new_sequence) < int(old_sequence) or |
paul@220 | 1061 | is_same_sequence and is_old_dtstamp |
paul@220 | 1062 | ) |
paul@220 | 1063 | |
paul@682 | 1064 | return is_same_sequence and ignore_dtstamp or not is_old_sequence |
paul@220 | 1065 | |
paul@1176 | 1066 | def check_delegation(attendee_map, attendee, attendee_attr): |
paul@1176 | 1067 | |
paul@1176 | 1068 | """ |
paul@1176 | 1069 | Using the 'attendee_map', check the attributes for the given 'attendee' |
paul@1176 | 1070 | provided as 'attendee_attr', following the delegation chain back to the |
paul@1177 | 1071 | delegators and forward again to yield the delegate identities in each |
paul@1177 | 1072 | case. Pictorially... |
paul@1177 | 1073 | |
paul@1177 | 1074 | attendee -> DELEGATED-FROM -> delegator |
paul@1177 | 1075 | ? <- DELEGATED-TO <--- |
paul@1177 | 1076 | |
paul@1177 | 1077 | Return whether 'attendee' was identified as a delegate by providing the |
paul@1177 | 1078 | identity of any delegators referencing the attendee. |
paul@1176 | 1079 | """ |
paul@1176 | 1080 | |
paul@1177 | 1081 | delegators = [] |
paul@1177 | 1082 | |
paul@1176 | 1083 | # The recipient should have a reference to the delegator. |
paul@1176 | 1084 | |
paul@1176 | 1085 | delegated_from = attendee_attr and attendee_attr.get("DELEGATED-FROM") |
paul@1177 | 1086 | if delegated_from: |
paul@1177 | 1087 | |
paul@1177 | 1088 | # Examine all delegators. |
paul@1177 | 1089 | |
paul@1177 | 1090 | for delegator in delegated_from: |
paul@1177 | 1091 | delegator_attr = attendee_map.get(delegator) |
paul@1176 | 1092 | |
paul@1177 | 1093 | # The delegator should have a reference to the recipient. |
paul@1176 | 1094 | |
paul@1177 | 1095 | delegated_to = delegator_attr and delegator_attr.get("DELEGATED-TO") |
paul@1177 | 1096 | if delegated_to and attendee in delegated_to: |
paul@1177 | 1097 | delegators.append(delegator) |
paul@1177 | 1098 | |
paul@1177 | 1099 | return delegators |
paul@1176 | 1100 | |
paul@1276 | 1101 | def get_periods(obj, start=None, end=None, inclusive=False): |
paul@256 | 1102 | |
paul@256 | 1103 | """ |
paul@1276 | 1104 | Return periods for the given object 'obj', employing the object's fallback |
paul@1276 | 1105 | time zone where no time zone information is available (for whole day events, |
paul@1276 | 1106 | for example), confining materialised periods to after the given 'start' |
paul@1276 | 1107 | datetime and before the given 'end' datetime. |
paul@618 | 1108 | |
paul@630 | 1109 | If 'end' is omitted, only explicit recurrences and recurrences from |
paul@630 | 1110 | explicitly-terminated rules will be returned. |
paul@630 | 1111 | |
paul@630 | 1112 | If 'inclusive' is set to a true value, any period occurring at the 'end' |
paul@630 | 1113 | will be included. |
paul@256 | 1114 | """ |
paul@256 | 1115 | |
paul@1276 | 1116 | tzid = obj.get_tzid() |
paul@318 | 1117 | rrule = obj.get_value("RRULE") |
paul@636 | 1118 | parameters = rrule and get_parameters(rrule) |
paul@318 | 1119 | |
paul@318 | 1120 | # Use localised datetimes. |
paul@318 | 1121 | |
paul@1276 | 1122 | main_period = obj.get_main_period() |
paul@797 | 1123 | |
paul@797 | 1124 | dtstart = main_period.get_start() |
paul@797 | 1125 | dtstart_attr = main_period.get_start_attr() |
paul@256 | 1126 | |
paul@618 | 1127 | # Attempt to get time zone details from the object, using the supplied zone |
paul@618 | 1128 | # only as a fallback. |
paul@618 | 1129 | |
paul@638 | 1130 | obj_tzid = obj.get_tzid() |
paul@256 | 1131 | |
paul@352 | 1132 | if not rrule: |
paul@797 | 1133 | periods = [main_period] |
paul@630 | 1134 | |
paul@636 | 1135 | elif end or parameters and parameters.has_key("UNTIL") or parameters.has_key("COUNT"): |
paul@630 | 1136 | |
paul@352 | 1137 | # Recurrence rules create multiple instances to be checked. |
paul@352 | 1138 | # Conflicts may only be assessed within a period defined by policy |
paul@352 | 1139 | # for the agent, with instances outside that period being considered |
paul@352 | 1140 | # unchecked. |
paul@352 | 1141 | |
paul@352 | 1142 | selector = get_rule(dtstart, rrule) |
paul@352 | 1143 | periods = [] |
paul@352 | 1144 | |
paul@521 | 1145 | until = parameters.get("UNTIL") |
paul@521 | 1146 | if until: |
paul@650 | 1147 | until_dt = to_timezone(get_datetime(until, dtstart_attr), obj_tzid) |
paul@650 | 1148 | end = end and min(until_dt, end) or until_dt |
paul@521 | 1149 | inclusive = True |
paul@521 | 1150 | |
paul@1238 | 1151 | # Define a selection period with a start point. The end will be handled |
paul@1238 | 1152 | # in the materialisation process below. |
paul@1238 | 1153 | |
paul@1238 | 1154 | selection_period = Period(start, None) |
paul@1238 | 1155 | |
paul@1238 | 1156 | # Obtain period instances, starting from the main period. Since counting |
paul@1238 | 1157 | # must start from the first period, filtering from a start date must be |
paul@1238 | 1158 | # done after the instances have been obtained. |
paul@1238 | 1159 | |
paul@1317 | 1160 | for recurrence_start in selector.materialise(dtstart, end, |
paul@1317 | 1161 | parameters.get("COUNT"), parameters.get("BYSETPOS"), inclusive): |
paul@1238 | 1162 | |
paul@1238 | 1163 | # Determine the resolution of the period. |
paul@1238 | 1164 | |
paul@630 | 1165 | create = len(recurrence_start) == 3 and date or datetime |
paul@638 | 1166 | recurrence_start = to_timezone(create(*recurrence_start), obj_tzid) |
paul@878 | 1167 | recurrence_end = recurrence_start + main_period.get_duration() |
paul@1238 | 1168 | |
paul@1238 | 1169 | # Create the period with accompanying metadata based on the main |
paul@1238 | 1170 | # period and event details. |
paul@1238 | 1171 | |
paul@1238 | 1172 | period = RecurringPeriod(recurrence_start, recurrence_end, tzid, "RRULE", dtstart_attr) |
paul@1238 | 1173 | |
paul@1317 | 1174 | # Use the main period where it occurs. |
paul@1317 | 1175 | |
paul@1317 | 1176 | if period == main_period: |
paul@1317 | 1177 | period = main_period |
paul@1317 | 1178 | |
paul@1238 | 1179 | # Filter out periods before the start. |
paul@1238 | 1180 | |
paul@1238 | 1181 | if period.within(selection_period): |
paul@1238 | 1182 | periods.append(period) |
paul@352 | 1183 | |
paul@635 | 1184 | else: |
paul@635 | 1185 | periods = [] |
paul@635 | 1186 | |
paul@352 | 1187 | # Add recurrence dates. |
paul@256 | 1188 | |
paul@1276 | 1189 | rdates = obj.get_date_value_item_periods("RDATE") |
paul@352 | 1190 | if rdates: |
paul@878 | 1191 | periods += rdates |
paul@424 | 1192 | |
paul@424 | 1193 | # Return a sorted list of the periods. |
paul@424 | 1194 | |
paul@542 | 1195 | periods.sort() |
paul@352 | 1196 | |
paul@352 | 1197 | # Exclude exception dates. |
paul@352 | 1198 | |
paul@1276 | 1199 | exdates = obj.get_date_value_item_periods("EXDATE") |
paul@256 | 1200 | |
paul@352 | 1201 | if exdates: |
paul@878 | 1202 | for period in exdates: |
paul@424 | 1203 | i = bisect_left(periods, period) |
paul@458 | 1204 | while i < len(periods) and periods[i] == period: |
paul@424 | 1205 | del periods[i] |
paul@256 | 1206 | |
paul@256 | 1207 | return periods |
paul@256 | 1208 | |
paul@1274 | 1209 | def get_main_period(periods): |
paul@1274 | 1210 | |
paul@1274 | 1211 | "Return the main period from 'periods' using origin information." |
paul@1274 | 1212 | |
paul@1274 | 1213 | for p in periods: |
paul@1274 | 1214 | if p.origin == "DTSTART": |
paul@1274 | 1215 | return p |
paul@1274 | 1216 | return None |
paul@1274 | 1217 | |
paul@1274 | 1218 | def get_recurrence_periods(periods): |
paul@1274 | 1219 | |
paul@1274 | 1220 | "Return recurrence periods from 'periods' using origin information." |
paul@1274 | 1221 | |
paul@1274 | 1222 | l = [] |
paul@1274 | 1223 | for p in periods: |
paul@1274 | 1224 | if p.origin != "DTSTART": |
paul@1274 | 1225 | l.append(p) |
paul@1274 | 1226 | return l |
paul@1274 | 1227 | |
paul@606 | 1228 | def get_sender_identities(mapping): |
paul@606 | 1229 | |
paul@606 | 1230 | """ |
paul@606 | 1231 | Return a mapping from actual senders to the identities for which they |
paul@606 | 1232 | have provided data, extracting this information from the given |
paul@606 | 1233 | 'mapping'. |
paul@606 | 1234 | """ |
paul@606 | 1235 | |
paul@606 | 1236 | senders = {} |
paul@606 | 1237 | |
paul@606 | 1238 | for value, attr in mapping.items(): |
paul@606 | 1239 | sent_by = attr.get("SENT-BY") |
paul@606 | 1240 | if sent_by: |
paul@606 | 1241 | sender = get_uri(sent_by) |
paul@606 | 1242 | else: |
paul@606 | 1243 | sender = value |
paul@606 | 1244 | |
paul@606 | 1245 | if not senders.has_key(sender): |
paul@606 | 1246 | senders[sender] = [] |
paul@606 | 1247 | |
paul@606 | 1248 | senders[sender].append(value) |
paul@606 | 1249 | |
paul@606 | 1250 | return senders |
paul@606 | 1251 | |
paul@1238 | 1252 | def get_window_end(tzid, days=100, start=None): |
paul@606 | 1253 | |
paul@618 | 1254 | """ |
paul@618 | 1255 | Return a datetime in the time zone indicated by 'tzid' marking the end of a |
paul@1238 | 1256 | window of the given number of 'days'. If 'start' is not indicated, the start |
paul@1238 | 1257 | of the window will be the current moment. |
paul@618 | 1258 | """ |
paul@618 | 1259 | |
paul@1238 | 1260 | return to_timezone(start or datetime.now(), tzid) + timedelta(days) |
paul@606 | 1261 | |
paul@213 | 1262 | # vim: tabstop=4 expandtab shiftwidth=4 |