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