paul@213 | 1 | #!/usr/bin/env python |
paul@213 | 2 | |
paul@213 | 3 | """ |
paul@213 | 4 | Interpretation of vCalendar content. |
paul@213 | 5 | |
paul@213 | 6 | Copyright (C) 2014, 2015 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@625 | 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@627 | 30 | get_tzid, to_datetime, to_timezone, to_utc_datetime |
paul@648 | 31 | from imiptools.period import FreeBusyPeriod, Period, RecurringPeriod, period_overlaps |
paul@213 | 32 | from vCalendar import iterwrite, parse, ParseError, to_dict, to_node |
paul@256 | 33 | from vRecurrence import get_parameters, get_rule |
paul@213 | 34 | import email.utils |
paul@213 | 35 | |
paul@213 | 36 | try: |
paul@213 | 37 | from cStringIO import StringIO |
paul@213 | 38 | except ImportError: |
paul@213 | 39 | from StringIO import StringIO |
paul@213 | 40 | |
paul@213 | 41 | class Object: |
paul@213 | 42 | |
paul@213 | 43 | "Access to calendar structures." |
paul@213 | 44 | |
paul@213 | 45 | def __init__(self, fragment): |
paul@213 | 46 | self.objtype, (self.details, self.attr) = fragment.items()[0] |
paul@213 | 47 | |
paul@535 | 48 | def get_uid(self): |
paul@535 | 49 | return self.get_value("UID") |
paul@535 | 50 | |
paul@535 | 51 | def get_recurrenceid(self): |
paul@563 | 52 | |
paul@563 | 53 | """ |
paul@563 | 54 | Return the recurrence identifier, normalised to a UTC datetime if |
paul@627 | 55 | specified as a datetime or date with accompanying time zone information, |
paul@627 | 56 | maintained as a date or floating datetime otherwise. If no recurrence |
paul@627 | 57 | identifier is present, None is returned. |
paul@627 | 58 | |
paul@627 | 59 | Note that this normalised form of the identifier may well not be the |
paul@627 | 60 | same as the originally-specified identifier because that could have been |
paul@627 | 61 | specified using an accompanying TZID attribute, whereas the normalised |
paul@627 | 62 | form is effectively a converted datetime value. |
paul@563 | 63 | """ |
paul@563 | 64 | |
paul@627 | 65 | if not self.has_key("RECURRENCE-ID"): |
paul@627 | 66 | return None |
paul@627 | 67 | dt, attr = self.get_datetime_item("RECURRENCE-ID") |
paul@628 | 68 | |
paul@628 | 69 | # Coerce any date to a UTC datetime if TZID was specified. |
paul@628 | 70 | |
paul@627 | 71 | tzid = attr.get("TZID") |
paul@627 | 72 | if tzid: |
paul@627 | 73 | dt = to_timezone(to_datetime(dt, tzid), "UTC") |
paul@627 | 74 | return format_datetime(dt) |
paul@627 | 75 | |
paul@627 | 76 | def get_recurrence_start_point(self, recurrenceid, tzid): |
paul@627 | 77 | |
paul@627 | 78 | """ |
paul@627 | 79 | Return the start point corresponding to the given 'recurrenceid', using |
paul@627 | 80 | the fallback 'tzid' to define the specific point in time referenced by |
paul@627 | 81 | the recurrence identifier if the identifier has a date representation. |
paul@627 | 82 | |
paul@627 | 83 | If 'recurrenceid' is given as None, this object's recurrence identifier |
paul@627 | 84 | is used to obtain a start point, but if this object does not provide a |
paul@627 | 85 | recurrence, None is returned. |
paul@627 | 86 | |
paul@627 | 87 | A start point is typically used to match free/busy periods which are |
paul@627 | 88 | themselves defined in terms of UTC datetimes. |
paul@627 | 89 | """ |
paul@627 | 90 | |
paul@627 | 91 | recurrenceid = recurrenceid or self.get_recurrenceid() |
paul@627 | 92 | if recurrenceid: |
paul@627 | 93 | return get_recurrence_start_point(recurrenceid, tzid) |
paul@627 | 94 | else: |
paul@627 | 95 | return None |
paul@535 | 96 | |
paul@535 | 97 | # Structure access. |
paul@535 | 98 | |
paul@524 | 99 | def copy(self): |
paul@524 | 100 | return Object(to_dict(self.to_node())) |
paul@524 | 101 | |
paul@213 | 102 | def get_items(self, name, all=True): |
paul@213 | 103 | return get_items(self.details, name, all) |
paul@213 | 104 | |
paul@213 | 105 | def get_item(self, name): |
paul@213 | 106 | return get_item(self.details, name) |
paul@213 | 107 | |
paul@213 | 108 | def get_value_map(self, name): |
paul@213 | 109 | return get_value_map(self.details, name) |
paul@213 | 110 | |
paul@213 | 111 | def get_values(self, name, all=True): |
paul@213 | 112 | return get_values(self.details, name, all) |
paul@213 | 113 | |
paul@213 | 114 | def get_value(self, name): |
paul@213 | 115 | return get_value(self.details, name) |
paul@213 | 116 | |
paul@506 | 117 | def get_utc_datetime(self, name, date_tzid=None): |
paul@506 | 118 | return get_utc_datetime(self.details, name, date_tzid) |
paul@213 | 119 | |
paul@417 | 120 | def get_date_values(self, name, tzid=None): |
paul@417 | 121 | items = get_date_value_items(self.details, name, tzid) |
paul@389 | 122 | return items and [value for value, attr in items] |
paul@352 | 123 | |
paul@417 | 124 | def get_date_value_items(self, name, tzid=None): |
paul@417 | 125 | return get_date_value_items(self.details, name, tzid) |
paul@352 | 126 | |
paul@646 | 127 | def get_period_values(self, name, tzid=None): |
paul@646 | 128 | return get_period_values(self.details, name, tzid) |
paul@630 | 129 | |
paul@318 | 130 | def get_datetime(self, name): |
paul@567 | 131 | t = get_datetime_item(self.details, name) |
paul@567 | 132 | if not t: return None |
paul@567 | 133 | dt, attr = t |
paul@318 | 134 | return dt |
paul@318 | 135 | |
paul@289 | 136 | def get_datetime_item(self, name): |
paul@289 | 137 | return get_datetime_item(self.details, name) |
paul@289 | 138 | |
paul@392 | 139 | def get_duration(self, name): |
paul@392 | 140 | return get_duration(self.get_value(name)) |
paul@392 | 141 | |
paul@213 | 142 | def to_node(self): |
paul@213 | 143 | return to_node({self.objtype : [(self.details, self.attr)]}) |
paul@213 | 144 | |
paul@213 | 145 | def to_part(self, method): |
paul@213 | 146 | return to_part(method, [self.to_node()]) |
paul@213 | 147 | |
paul@213 | 148 | # Direct access to the structure. |
paul@213 | 149 | |
paul@392 | 150 | def has_key(self, name): |
paul@392 | 151 | return self.details.has_key(name) |
paul@392 | 152 | |
paul@524 | 153 | def get(self, name): |
paul@524 | 154 | return self.details.get(name) |
paul@524 | 155 | |
paul@213 | 156 | def __getitem__(self, name): |
paul@213 | 157 | return self.details[name] |
paul@213 | 158 | |
paul@213 | 159 | def __setitem__(self, name, value): |
paul@213 | 160 | self.details[name] = value |
paul@213 | 161 | |
paul@213 | 162 | def __delitem__(self, name): |
paul@213 | 163 | del self.details[name] |
paul@213 | 164 | |
paul@524 | 165 | def remove(self, name): |
paul@524 | 166 | try: |
paul@524 | 167 | del self[name] |
paul@524 | 168 | except KeyError: |
paul@524 | 169 | pass |
paul@524 | 170 | |
paul@524 | 171 | def remove_all(self, names): |
paul@524 | 172 | for name in names: |
paul@524 | 173 | self.remove(name) |
paul@524 | 174 | |
paul@256 | 175 | # Computed results. |
paul@256 | 176 | |
paul@630 | 177 | def get_periods(self, tzid, end=None): |
paul@620 | 178 | |
paul@620 | 179 | """ |
paul@620 | 180 | Return periods defined by this object, employing the given 'tzid' where |
paul@620 | 181 | no time zone information is defined, and limiting the collection to a |
paul@620 | 182 | window of time with the given 'end'. |
paul@630 | 183 | |
paul@630 | 184 | If 'end' is omitted, only explicit recurrences and recurrences from |
paul@630 | 185 | explicitly-terminated rules will be returned. |
paul@620 | 186 | """ |
paul@620 | 187 | |
paul@458 | 188 | return get_periods(self, tzid, end) |
paul@360 | 189 | |
paul@630 | 190 | def get_active_periods(self, recurrenceids, tzid, end=None): |
paul@630 | 191 | |
paul@630 | 192 | """ |
paul@630 | 193 | Return all periods specified by this object that are not replaced by |
paul@630 | 194 | those defined by 'recurrenceids', using 'tzid' as a fallback time zone |
paul@630 | 195 | to convert floating dates and datetimes, and using 'end' to indicate the |
paul@630 | 196 | end of the time window within which periods are considered. |
paul@630 | 197 | """ |
paul@630 | 198 | |
paul@630 | 199 | # Specific recurrences yield all specified periods. |
paul@630 | 200 | |
paul@630 | 201 | periods = self.get_periods(tzid, end) |
paul@630 | 202 | |
paul@630 | 203 | if self.get_recurrenceid(): |
paul@630 | 204 | return periods |
paul@630 | 205 | |
paul@630 | 206 | # Parent objects need to have their periods tested against redefined |
paul@630 | 207 | # recurrences. |
paul@630 | 208 | |
paul@630 | 209 | active = [] |
paul@630 | 210 | |
paul@630 | 211 | for p in periods: |
paul@630 | 212 | |
paul@630 | 213 | # Subtract any recurrences from the free/busy details of a |
paul@630 | 214 | # parent object. |
paul@630 | 215 | |
paul@648 | 216 | if not p.is_replaced(recurrenceids): |
paul@630 | 217 | active.append(p) |
paul@630 | 218 | |
paul@630 | 219 | return active |
paul@630 | 220 | |
paul@648 | 221 | def get_freebusy_period(self, period, only_organiser=False): |
paul@648 | 222 | |
paul@648 | 223 | """ |
paul@648 | 224 | Return a free/busy period for the given 'period' provided by this |
paul@648 | 225 | object, using the 'only_organiser' status to produce a suitable |
paul@648 | 226 | transparency value. |
paul@648 | 227 | """ |
paul@648 | 228 | |
paul@648 | 229 | return FreeBusyPeriod( |
paul@648 | 230 | period.get_start_point(), |
paul@648 | 231 | period.get_end_point(), |
paul@648 | 232 | self.get_value("UID"), |
paul@648 | 233 | only_organiser and "ORG" or self.get_value("TRANSP") or "OPAQUE", |
paul@648 | 234 | self.get_recurrenceid(), |
paul@648 | 235 | self.get_value("SUMMARY"), |
paul@648 | 236 | self.get_value("ORGANIZER") |
paul@648 | 237 | ) |
paul@648 | 238 | |
paul@648 | 239 | def get_participation_status(self, participant): |
paul@648 | 240 | |
paul@648 | 241 | """ |
paul@648 | 242 | Return the participation status of the given 'participant', with the |
paul@648 | 243 | special value "ORG" indicating organiser-only participation. |
paul@648 | 244 | """ |
paul@648 | 245 | |
paul@648 | 246 | attendees = self.get_value_map("ATTENDEE") |
paul@648 | 247 | organiser = self.get_value("ORGANIZER") |
paul@648 | 248 | |
paul@648 | 249 | for attendee, attendee_attr in attendees.items(): |
paul@648 | 250 | if attendee == participant: |
paul@648 | 251 | return attendee_attr.get("PARTSTAT", "NEEDS-ACTION") |
paul@648 | 252 | |
paul@648 | 253 | else: |
paul@648 | 254 | if organiser == participant: |
paul@648 | 255 | return "ORG" |
paul@648 | 256 | |
paul@648 | 257 | return None |
paul@648 | 258 | |
paul@648 | 259 | def get_participation(self, partstat, include_needs_action=False): |
paul@648 | 260 | |
paul@648 | 261 | """ |
paul@648 | 262 | Return whether 'partstat' indicates some kind of participation in an |
paul@648 | 263 | event. If 'include_needs_action' is specified as a true value, events |
paul@648 | 264 | not yet responded to will be treated as events with tentative |
paul@648 | 265 | participation. |
paul@648 | 266 | """ |
paul@648 | 267 | |
paul@648 | 268 | return not partstat in ("DECLINED", "DELEGATED", "NEEDS-ACTION") or \ |
paul@648 | 269 | include_needs_action and partstat == "NEEDS-ACTION" or \ |
paul@648 | 270 | partstat == "ORG" |
paul@648 | 271 | |
paul@422 | 272 | def get_tzid(self): |
paul@562 | 273 | |
paul@562 | 274 | """ |
paul@562 | 275 | Return a time zone identifier used by the start or end datetimes, |
paul@562 | 276 | potentially suitable for converting dates to datetimes. |
paul@562 | 277 | """ |
paul@562 | 278 | |
paul@560 | 279 | if not self.has_key("DTSTART"): |
paul@560 | 280 | return None |
paul@422 | 281 | dtstart, dtstart_attr = self.get_datetime_item("DTSTART") |
paul@630 | 282 | if self.has_key("DTEND"): |
paul@630 | 283 | dtend, dtend_attr = self.get_datetime_item("DTEND") |
paul@630 | 284 | else: |
paul@630 | 285 | dtend_attr = None |
paul@422 | 286 | return get_tzid(dtstart_attr, dtend_attr) |
paul@422 | 287 | |
paul@619 | 288 | def is_shared(self): |
paul@619 | 289 | |
paul@619 | 290 | """ |
paul@619 | 291 | Return whether this object is shared based on the presence of a SEQUENCE |
paul@619 | 292 | property. |
paul@619 | 293 | """ |
paul@619 | 294 | |
paul@619 | 295 | return self.get_value("SEQUENCE") is not None |
paul@619 | 296 | |
paul@627 | 297 | # Modification methods. |
paul@627 | 298 | |
paul@627 | 299 | def set_datetime(self, name, dt, tzid=None): |
paul@627 | 300 | |
paul@627 | 301 | """ |
paul@627 | 302 | Set a datetime for property 'name' using 'dt' and the optional fallback |
paul@627 | 303 | 'tzid', returning whether an update has occurred. |
paul@627 | 304 | """ |
paul@627 | 305 | |
paul@627 | 306 | if dt: |
paul@627 | 307 | old_value = self.get_value(name) |
paul@627 | 308 | self[name] = [get_item_from_datetime(dt, tzid)] |
paul@627 | 309 | return format_datetime(dt) != old_value |
paul@627 | 310 | |
paul@627 | 311 | return False |
paul@627 | 312 | |
paul@627 | 313 | def set_period(self, period): |
paul@627 | 314 | |
paul@627 | 315 | "Set the given 'period' as the main start and end." |
paul@627 | 316 | |
paul@627 | 317 | result = self.set_datetime("DTSTART", period.get_start()) |
paul@627 | 318 | result = self.set_datetime("DTEND", period.get_end()) or result |
paul@627 | 319 | return result |
paul@627 | 320 | |
paul@627 | 321 | def set_periods(self, periods): |
paul@627 | 322 | |
paul@627 | 323 | """ |
paul@627 | 324 | Set the given 'periods' as recurrence date properties, replacing the |
paul@627 | 325 | previous RDATE properties and ignoring any RRULE properties. |
paul@627 | 326 | """ |
paul@627 | 327 | |
paul@627 | 328 | update = False |
paul@627 | 329 | |
paul@627 | 330 | old_values = self.get_values("RDATE") |
paul@627 | 331 | new_rdates = [] |
paul@627 | 332 | |
paul@627 | 333 | if self.has_key("RDATE"): |
paul@627 | 334 | del self["RDATE"] |
paul@627 | 335 | |
paul@627 | 336 | for p in periods: |
paul@627 | 337 | if p.origin != "RRULE": |
paul@627 | 338 | new_rdates.append(get_period_item(p.get_start(), p.get_end())) |
paul@627 | 339 | |
paul@627 | 340 | self["RDATE"] = new_rdates |
paul@627 | 341 | |
paul@627 | 342 | # NOTE: To do: calculate the update status. |
paul@627 | 343 | return update |
paul@627 | 344 | |
paul@213 | 345 | # Construction and serialisation. |
paul@213 | 346 | |
paul@213 | 347 | def make_calendar(nodes, method=None): |
paul@213 | 348 | |
paul@213 | 349 | """ |
paul@213 | 350 | Return a complete calendar node wrapping the given 'nodes' and employing the |
paul@213 | 351 | given 'method', if indicated. |
paul@213 | 352 | """ |
paul@213 | 353 | |
paul@213 | 354 | return ("VCALENDAR", {}, |
paul@213 | 355 | (method and [("METHOD", {}, method)] or []) + |
paul@213 | 356 | [("VERSION", {}, "2.0")] + |
paul@213 | 357 | nodes |
paul@213 | 358 | ) |
paul@213 | 359 | |
paul@327 | 360 | def make_freebusy(freebusy, uid, organiser, organiser_attr=None, attendee=None, |
paul@562 | 361 | attendee_attr=None, period=None): |
paul@222 | 362 | |
paul@222 | 363 | """ |
paul@222 | 364 | Return a calendar node defining the free/busy details described in the given |
paul@292 | 365 | 'freebusy' list, employing the given 'uid', for the given 'organiser' and |
paul@292 | 366 | optional 'organiser_attr', with the optional 'attendee' providing recipient |
paul@292 | 367 | details together with the optional 'attendee_attr'. |
paul@327 | 368 | |
paul@562 | 369 | The result will be constrained to the 'period' if specified. |
paul@222 | 370 | """ |
paul@222 | 371 | |
paul@222 | 372 | record = [] |
paul@222 | 373 | rwrite = record.append |
paul@222 | 374 | |
paul@292 | 375 | rwrite(("ORGANIZER", organiser_attr or {}, organiser)) |
paul@222 | 376 | |
paul@222 | 377 | if attendee: |
paul@292 | 378 | rwrite(("ATTENDEE", attendee_attr or {}, attendee)) |
paul@222 | 379 | |
paul@222 | 380 | rwrite(("UID", {}, uid)) |
paul@222 | 381 | |
paul@222 | 382 | if freebusy: |
paul@327 | 383 | |
paul@327 | 384 | # Get a constrained view if start and end limits are specified. |
paul@327 | 385 | |
paul@563 | 386 | if period: |
paul@563 | 387 | periods = period_overlaps(freebusy, period, True) |
paul@563 | 388 | else: |
paul@563 | 389 | periods = freebusy |
paul@327 | 390 | |
paul@327 | 391 | # Write the limits of the resource. |
paul@327 | 392 | |
paul@563 | 393 | if periods: |
paul@563 | 394 | rwrite(("DTSTART", {"VALUE" : "DATE-TIME"}, format_datetime(periods[0].get_start_point()))) |
paul@563 | 395 | rwrite(("DTEND", {"VALUE" : "DATE-TIME"}, format_datetime(periods[-1].get_end_point()))) |
paul@563 | 396 | else: |
paul@563 | 397 | rwrite(("DTSTART", {"VALUE" : "DATE-TIME"}, format_datetime(period.get_start_point()))) |
paul@563 | 398 | rwrite(("DTEND", {"VALUE" : "DATE-TIME"}, format_datetime(period.get_end_point()))) |
paul@327 | 399 | |
paul@458 | 400 | for p in periods: |
paul@458 | 401 | if p.transp == "OPAQUE": |
paul@529 | 402 | rwrite(("FREEBUSY", {"FBTYPE" : "BUSY"}, "/".join( |
paul@562 | 403 | map(format_datetime, [p.get_start_point(), p.get_end_point()]) |
paul@529 | 404 | ))) |
paul@222 | 405 | |
paul@222 | 406 | return ("VFREEBUSY", {}, record) |
paul@222 | 407 | |
paul@213 | 408 | def parse_object(f, encoding, objtype=None): |
paul@213 | 409 | |
paul@213 | 410 | """ |
paul@213 | 411 | Parse the iTIP content from 'f' having the given 'encoding'. If 'objtype' is |
paul@213 | 412 | given, only objects of that type will be returned. Otherwise, the root of |
paul@213 | 413 | the content will be returned as a dictionary with a single key indicating |
paul@213 | 414 | the object type. |
paul@213 | 415 | |
paul@213 | 416 | Return None if the content was not readable or suitable. |
paul@213 | 417 | """ |
paul@213 | 418 | |
paul@213 | 419 | try: |
paul@213 | 420 | try: |
paul@213 | 421 | doctype, attrs, elements = obj = parse(f, encoding=encoding) |
paul@213 | 422 | if objtype and doctype == objtype: |
paul@213 | 423 | return to_dict(obj)[objtype][0] |
paul@213 | 424 | elif not objtype: |
paul@213 | 425 | return to_dict(obj) |
paul@213 | 426 | finally: |
paul@213 | 427 | f.close() |
paul@213 | 428 | |
paul@213 | 429 | # NOTE: Handle parse errors properly. |
paul@213 | 430 | |
paul@213 | 431 | except (ParseError, ValueError): |
paul@213 | 432 | pass |
paul@213 | 433 | |
paul@213 | 434 | return None |
paul@213 | 435 | |
paul@213 | 436 | def to_part(method, calendar): |
paul@213 | 437 | |
paul@213 | 438 | """ |
paul@213 | 439 | Write using the given 'method', the 'calendar' details to a MIME |
paul@213 | 440 | text/calendar part. |
paul@213 | 441 | """ |
paul@213 | 442 | |
paul@213 | 443 | encoding = "utf-8" |
paul@213 | 444 | out = StringIO() |
paul@213 | 445 | try: |
paul@213 | 446 | to_stream(out, make_calendar(calendar, method), encoding) |
paul@213 | 447 | part = MIMEText(out.getvalue(), "calendar", encoding) |
paul@213 | 448 | part.set_param("method", method) |
paul@213 | 449 | return part |
paul@213 | 450 | |
paul@213 | 451 | finally: |
paul@213 | 452 | out.close() |
paul@213 | 453 | |
paul@213 | 454 | def to_stream(out, fragment, encoding="utf-8"): |
paul@213 | 455 | iterwrite(out, encoding=encoding).append(fragment) |
paul@213 | 456 | |
paul@213 | 457 | # Structure access functions. |
paul@213 | 458 | |
paul@213 | 459 | def get_items(d, name, all=True): |
paul@213 | 460 | |
paul@213 | 461 | """ |
paul@213 | 462 | Get all items from 'd' for the given 'name', returning single items if |
paul@213 | 463 | 'all' is specified and set to a false value and if only one value is |
paul@213 | 464 | present for the name. Return None if no items are found for the name or if |
paul@213 | 465 | many items are found but 'all' is set to a false value. |
paul@213 | 466 | """ |
paul@213 | 467 | |
paul@213 | 468 | if d.has_key(name): |
paul@462 | 469 | items = d[name] |
paul@213 | 470 | if all: |
paul@462 | 471 | return items |
paul@462 | 472 | elif len(items) == 1: |
paul@462 | 473 | return items[0] |
paul@213 | 474 | else: |
paul@213 | 475 | return None |
paul@213 | 476 | else: |
paul@213 | 477 | return None |
paul@213 | 478 | |
paul@213 | 479 | def get_item(d, name): |
paul@213 | 480 | return get_items(d, name, False) |
paul@213 | 481 | |
paul@213 | 482 | def get_value_map(d, name): |
paul@213 | 483 | |
paul@213 | 484 | """ |
paul@213 | 485 | Return a dictionary for all items in 'd' having the given 'name'. The |
paul@213 | 486 | dictionary will map values for the name to any attributes or qualifiers |
paul@213 | 487 | that may have been present. |
paul@213 | 488 | """ |
paul@213 | 489 | |
paul@213 | 490 | items = get_items(d, name) |
paul@213 | 491 | if items: |
paul@213 | 492 | return dict(items) |
paul@213 | 493 | else: |
paul@213 | 494 | return {} |
paul@213 | 495 | |
paul@462 | 496 | def values_from_items(items): |
paul@462 | 497 | return map(lambda x: x[0], items) |
paul@462 | 498 | |
paul@213 | 499 | def get_values(d, name, all=True): |
paul@213 | 500 | if d.has_key(name): |
paul@462 | 501 | items = d[name] |
paul@462 | 502 | if not all and len(items) == 1: |
paul@462 | 503 | return items[0][0] |
paul@213 | 504 | else: |
paul@462 | 505 | return values_from_items(items) |
paul@213 | 506 | else: |
paul@213 | 507 | return None |
paul@213 | 508 | |
paul@213 | 509 | def get_value(d, name): |
paul@213 | 510 | return get_values(d, name, False) |
paul@213 | 511 | |
paul@417 | 512 | def get_date_value_items(d, name, tzid=None): |
paul@352 | 513 | |
paul@352 | 514 | """ |
paul@389 | 515 | Obtain items from 'd' having the given 'name', where a single item yields |
paul@389 | 516 | potentially many values. Return a list of tuples of the form (value, |
paul@389 | 517 | attributes) where the attributes have been given for the property in 'd'. |
paul@352 | 518 | """ |
paul@352 | 519 | |
paul@403 | 520 | items = get_items(d, name) |
paul@403 | 521 | if items: |
paul@403 | 522 | all_items = [] |
paul@403 | 523 | for item in items: |
paul@403 | 524 | values, attr = item |
paul@417 | 525 | if not attr.has_key("TZID") and tzid: |
paul@417 | 526 | attr["TZID"] = tzid |
paul@403 | 527 | if not isinstance(values, list): |
paul@403 | 528 | values = [values] |
paul@403 | 529 | for value in values: |
paul@403 | 530 | all_items.append((get_datetime(value, attr) or get_period(value, attr), attr)) |
paul@403 | 531 | return all_items |
paul@352 | 532 | else: |
paul@352 | 533 | return None |
paul@352 | 534 | |
paul@646 | 535 | def get_period_values(d, name, tzid=None): |
paul@630 | 536 | |
paul@630 | 537 | """ |
paul@630 | 538 | Return period values from 'd' for the given property 'name', using 'tzid' |
paul@646 | 539 | where specified to indicate the time zone. |
paul@630 | 540 | """ |
paul@630 | 541 | |
paul@630 | 542 | values = [] |
paul@630 | 543 | for value, attr in get_items(d, name) or []: |
paul@630 | 544 | if not attr.has_key("TZID") and tzid: |
paul@630 | 545 | attr["TZID"] = tzid |
paul@630 | 546 | start, end = get_period(value, attr) |
paul@646 | 547 | values.append(Period(start, end, tzid=tzid)) |
paul@630 | 548 | return values |
paul@630 | 549 | |
paul@506 | 550 | def get_utc_datetime(d, name, date_tzid=None): |
paul@506 | 551 | |
paul@506 | 552 | """ |
paul@506 | 553 | Return the value provided by 'd' for 'name' as a datetime in the UTC zone |
paul@506 | 554 | or as a date, converting any date to a datetime if 'date_tzid' is specified. |
paul@506 | 555 | """ |
paul@506 | 556 | |
paul@348 | 557 | t = get_datetime_item(d, name) |
paul@348 | 558 | if not t: |
paul@348 | 559 | return None |
paul@348 | 560 | else: |
paul@348 | 561 | dt, attr = t |
paul@506 | 562 | return to_utc_datetime(dt, date_tzid) |
paul@289 | 563 | |
paul@289 | 564 | def get_datetime_item(d, name): |
paul@562 | 565 | |
paul@562 | 566 | """ |
paul@562 | 567 | Return the value provided by 'd' for 'name' as a datetime or as a date, |
paul@562 | 568 | together with the attributes describing it. Return None if no value exists |
paul@562 | 569 | for 'name' in 'd'. |
paul@562 | 570 | """ |
paul@562 | 571 | |
paul@348 | 572 | t = get_item(d, name) |
paul@348 | 573 | if not t: |
paul@348 | 574 | return None |
paul@348 | 575 | else: |
paul@348 | 576 | value, attr = t |
paul@613 | 577 | dt = get_datetime(value, attr) |
paul@616 | 578 | tzid = get_datetime_tzid(dt) |
paul@616 | 579 | if tzid: |
paul@616 | 580 | attr["TZID"] = tzid |
paul@613 | 581 | return dt, attr |
paul@213 | 582 | |
paul@528 | 583 | # Conversion functions. |
paul@528 | 584 | |
paul@213 | 585 | def get_addresses(values): |
paul@213 | 586 | return [address for name, address in email.utils.getaddresses(values)] |
paul@213 | 587 | |
paul@213 | 588 | def get_address(value): |
paul@333 | 589 | value = value.lower() |
paul@333 | 590 | return value.startswith("mailto:") and value[7:] or value |
paul@213 | 591 | |
paul@213 | 592 | def get_uri(value): |
paul@213 | 593 | return value.lower().startswith("mailto:") and value.lower() or ":" in value and value or "mailto:%s" % value.lower() |
paul@213 | 594 | |
paul@309 | 595 | uri_value = get_uri |
paul@309 | 596 | |
paul@309 | 597 | def uri_values(values): |
paul@309 | 598 | return map(get_uri, values) |
paul@309 | 599 | |
paul@213 | 600 | def uri_dict(d): |
paul@213 | 601 | return dict([(get_uri(key), value) for key, value in d.items()]) |
paul@213 | 602 | |
paul@213 | 603 | def uri_item(item): |
paul@213 | 604 | return get_uri(item[0]), item[1] |
paul@213 | 605 | |
paul@213 | 606 | def uri_items(items): |
paul@213 | 607 | return [(get_uri(value), attr) for value, attr in items] |
paul@213 | 608 | |
paul@220 | 609 | # Operations on structure data. |
paul@220 | 610 | |
paul@220 | 611 | def is_new_object(old_sequence, new_sequence, old_dtstamp, new_dtstamp, partstat_set): |
paul@220 | 612 | |
paul@220 | 613 | """ |
paul@220 | 614 | Return for the given 'old_sequence' and 'new_sequence', 'old_dtstamp' and |
paul@220 | 615 | 'new_dtstamp', and the 'partstat_set' indication, whether the object |
paul@220 | 616 | providing the new information is really newer than the object providing the |
paul@220 | 617 | old information. |
paul@220 | 618 | """ |
paul@220 | 619 | |
paul@220 | 620 | have_sequence = old_sequence is not None and new_sequence is not None |
paul@220 | 621 | is_same_sequence = have_sequence and int(new_sequence) == int(old_sequence) |
paul@220 | 622 | |
paul@220 | 623 | have_dtstamp = old_dtstamp and new_dtstamp |
paul@220 | 624 | is_old_dtstamp = have_dtstamp and new_dtstamp < old_dtstamp or old_dtstamp and not new_dtstamp |
paul@220 | 625 | |
paul@220 | 626 | is_old_sequence = have_sequence and ( |
paul@220 | 627 | int(new_sequence) < int(old_sequence) or |
paul@220 | 628 | is_same_sequence and is_old_dtstamp |
paul@220 | 629 | ) |
paul@220 | 630 | |
paul@220 | 631 | return is_same_sequence and partstat_set or not is_old_sequence |
paul@220 | 632 | |
paul@630 | 633 | def get_periods(obj, tzid, end=None, inclusive=False): |
paul@256 | 634 | |
paul@256 | 635 | """ |
paul@618 | 636 | Return periods for the given object 'obj', employing the given 'tzid' where |
paul@618 | 637 | no time zone information is available (for whole day events, for example), |
paul@630 | 638 | confining materialised periods to before the given 'end' datetime. |
paul@618 | 639 | |
paul@630 | 640 | If 'end' is omitted, only explicit recurrences and recurrences from |
paul@630 | 641 | explicitly-terminated rules will be returned. |
paul@630 | 642 | |
paul@630 | 643 | If 'inclusive' is set to a true value, any period occurring at the 'end' |
paul@630 | 644 | will be included. |
paul@256 | 645 | """ |
paul@256 | 646 | |
paul@318 | 647 | rrule = obj.get_value("RRULE") |
paul@636 | 648 | parameters = rrule and get_parameters(rrule) |
paul@318 | 649 | |
paul@318 | 650 | # Use localised datetimes. |
paul@318 | 651 | |
paul@392 | 652 | dtstart, dtstart_attr = obj.get_datetime_item("DTSTART") |
paul@256 | 653 | |
paul@392 | 654 | if obj.has_key("DTEND"): |
paul@392 | 655 | dtend, dtend_attr = obj.get_datetime_item("DTEND") |
paul@392 | 656 | duration = dtend - dtstart |
paul@392 | 657 | elif obj.has_key("DURATION"): |
paul@392 | 658 | duration = obj.get_duration("DURATION") |
paul@392 | 659 | dtend = dtstart + duration |
paul@392 | 660 | dtend_attr = dtstart_attr |
paul@392 | 661 | else: |
paul@392 | 662 | dtend, dtend_attr = dtstart, dtstart_attr |
paul@256 | 663 | |
paul@618 | 664 | # Attempt to get time zone details from the object, using the supplied zone |
paul@618 | 665 | # only as a fallback. |
paul@618 | 666 | |
paul@638 | 667 | obj_tzid = obj.get_tzid() |
paul@256 | 668 | |
paul@352 | 669 | if not rrule: |
paul@541 | 670 | periods = [RecurringPeriod(dtstart, dtend, tzid, "DTSTART", dtstart_attr, dtend_attr)] |
paul@630 | 671 | |
paul@636 | 672 | elif end or parameters and parameters.has_key("UNTIL") or parameters.has_key("COUNT"): |
paul@630 | 673 | |
paul@352 | 674 | # Recurrence rules create multiple instances to be checked. |
paul@352 | 675 | # Conflicts may only be assessed within a period defined by policy |
paul@352 | 676 | # for the agent, with instances outside that period being considered |
paul@352 | 677 | # unchecked. |
paul@352 | 678 | |
paul@352 | 679 | selector = get_rule(dtstart, rrule) |
paul@352 | 680 | periods = [] |
paul@352 | 681 | |
paul@521 | 682 | until = parameters.get("UNTIL") |
paul@521 | 683 | if until: |
paul@638 | 684 | end = min(to_timezone(get_datetime(until, dtstart_attr), obj_tzid), end) |
paul@521 | 685 | inclusive = True |
paul@521 | 686 | |
paul@630 | 687 | for recurrence_start in selector.materialise(dtstart, end, parameters.get("COUNT"), parameters.get("BYSETPOS"), inclusive): |
paul@630 | 688 | create = len(recurrence_start) == 3 and date or datetime |
paul@638 | 689 | recurrence_start = to_timezone(create(*recurrence_start), obj_tzid) |
paul@630 | 690 | recurrence_end = recurrence_start + duration |
paul@638 | 691 | periods.append(RecurringPeriod(recurrence_start, recurrence_end, tzid, "RRULE", dtstart_attr)) |
paul@352 | 692 | |
paul@635 | 693 | else: |
paul@635 | 694 | periods = [] |
paul@635 | 695 | |
paul@352 | 696 | # Add recurrence dates. |
paul@256 | 697 | |
paul@494 | 698 | rdates = obj.get_date_value_items("RDATE", tzid) |
paul@352 | 699 | |
paul@352 | 700 | if rdates: |
paul@494 | 701 | for rdate, rdate_attr in rdates: |
paul@389 | 702 | if isinstance(rdate, tuple): |
paul@541 | 703 | periods.append(RecurringPeriod(rdate[0], rdate[1], tzid, "RDATE", rdate_attr)) |
paul@389 | 704 | else: |
paul@541 | 705 | periods.append(RecurringPeriod(rdate, rdate + duration, tzid, "RDATE", rdate_attr)) |
paul@424 | 706 | |
paul@424 | 707 | # Return a sorted list of the periods. |
paul@424 | 708 | |
paul@542 | 709 | periods.sort() |
paul@352 | 710 | |
paul@352 | 711 | # Exclude exception dates. |
paul@352 | 712 | |
paul@638 | 713 | exdates = obj.get_date_value_items("EXDATE", tzid) |
paul@256 | 714 | |
paul@352 | 715 | if exdates: |
paul@638 | 716 | for exdate, exdate_attr in exdates: |
paul@389 | 717 | if isinstance(exdate, tuple): |
paul@638 | 718 | period = RecurringPeriod(exdate[0], exdate[1], tzid, "EXDATE", exdate_attr) |
paul@389 | 719 | else: |
paul@638 | 720 | period = RecurringPeriod(exdate, exdate + duration, tzid, "EXDATE", exdate_attr) |
paul@424 | 721 | i = bisect_left(periods, period) |
paul@458 | 722 | while i < len(periods) and periods[i] == period: |
paul@424 | 723 | del periods[i] |
paul@256 | 724 | |
paul@256 | 725 | return periods |
paul@256 | 726 | |
paul@606 | 727 | def get_sender_identities(mapping): |
paul@606 | 728 | |
paul@606 | 729 | """ |
paul@606 | 730 | Return a mapping from actual senders to the identities for which they |
paul@606 | 731 | have provided data, extracting this information from the given |
paul@606 | 732 | 'mapping'. |
paul@606 | 733 | """ |
paul@606 | 734 | |
paul@606 | 735 | senders = {} |
paul@606 | 736 | |
paul@606 | 737 | for value, attr in mapping.items(): |
paul@606 | 738 | sent_by = attr.get("SENT-BY") |
paul@606 | 739 | if sent_by: |
paul@606 | 740 | sender = get_uri(sent_by) |
paul@606 | 741 | else: |
paul@606 | 742 | sender = value |
paul@606 | 743 | |
paul@606 | 744 | if not senders.has_key(sender): |
paul@606 | 745 | senders[sender] = [] |
paul@606 | 746 | |
paul@606 | 747 | senders[sender].append(value) |
paul@606 | 748 | |
paul@606 | 749 | return senders |
paul@606 | 750 | |
paul@618 | 751 | def get_window_end(tzid, days=100): |
paul@606 | 752 | |
paul@618 | 753 | """ |
paul@618 | 754 | Return a datetime in the time zone indicated by 'tzid' marking the end of a |
paul@618 | 755 | window of the given number of 'days'. |
paul@618 | 756 | """ |
paul@618 | 757 | |
paul@618 | 758 | return to_timezone(datetime.now(), tzid) + timedelta(days) |
paul@606 | 759 | |
paul@213 | 760 | # vim: tabstop=4 expandtab shiftwidth=4 |