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