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