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