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@650 | 177 | def get_main_period_items(self, tzid): |
paul@650 | 178 | |
paul@650 | 179 | """ |
paul@650 | 180 | Return two (value, attributes) items corresponding to the main start-end |
paul@650 | 181 | period for the object. |
paul@650 | 182 | """ |
paul@650 | 183 | |
paul@650 | 184 | dtstart, dtstart_attr = self.get_datetime_item("DTSTART") |
paul@650 | 185 | |
paul@650 | 186 | if self.has_key("DTEND"): |
paul@650 | 187 | dtend, dtend_attr = self.get_datetime_item("DTEND") |
paul@650 | 188 | duration = dtend - dtstart |
paul@650 | 189 | elif self.has_key("DURATION"): |
paul@650 | 190 | duration = self.get_duration("DURATION") |
paul@650 | 191 | dtend = dtstart + duration |
paul@650 | 192 | dtend_attr = dtstart_attr |
paul@650 | 193 | else: |
paul@650 | 194 | dtend, dtend_attr = dtstart, dtstart_attr |
paul@650 | 195 | |
paul@650 | 196 | return (dtstart, dtstart_attr), (dtend, dtend_attr) |
paul@650 | 197 | |
paul@630 | 198 | def get_periods(self, tzid, end=None): |
paul@620 | 199 | |
paul@620 | 200 | """ |
paul@620 | 201 | Return periods defined by this object, employing the given 'tzid' where |
paul@620 | 202 | no time zone information is defined, and limiting the collection to a |
paul@620 | 203 | window of time with the given 'end'. |
paul@630 | 204 | |
paul@630 | 205 | If 'end' is omitted, only explicit recurrences and recurrences from |
paul@630 | 206 | explicitly-terminated rules will be returned. |
paul@620 | 207 | """ |
paul@620 | 208 | |
paul@458 | 209 | return get_periods(self, tzid, end) |
paul@360 | 210 | |
paul@630 | 211 | def get_active_periods(self, recurrenceids, tzid, end=None): |
paul@630 | 212 | |
paul@630 | 213 | """ |
paul@630 | 214 | Return all periods specified by this object that are not replaced by |
paul@630 | 215 | those defined by 'recurrenceids', using 'tzid' as a fallback time zone |
paul@630 | 216 | to convert floating dates and datetimes, and using 'end' to indicate the |
paul@630 | 217 | end of the time window within which periods are considered. |
paul@630 | 218 | """ |
paul@630 | 219 | |
paul@630 | 220 | # Specific recurrences yield all specified periods. |
paul@630 | 221 | |
paul@630 | 222 | periods = self.get_periods(tzid, end) |
paul@630 | 223 | |
paul@630 | 224 | if self.get_recurrenceid(): |
paul@630 | 225 | return periods |
paul@630 | 226 | |
paul@630 | 227 | # Parent objects need to have their periods tested against redefined |
paul@630 | 228 | # recurrences. |
paul@630 | 229 | |
paul@630 | 230 | active = [] |
paul@630 | 231 | |
paul@630 | 232 | for p in periods: |
paul@630 | 233 | |
paul@630 | 234 | # Subtract any recurrences from the free/busy details of a |
paul@630 | 235 | # parent object. |
paul@630 | 236 | |
paul@648 | 237 | if not p.is_replaced(recurrenceids): |
paul@630 | 238 | active.append(p) |
paul@630 | 239 | |
paul@630 | 240 | return active |
paul@630 | 241 | |
paul@648 | 242 | def get_freebusy_period(self, period, only_organiser=False): |
paul@648 | 243 | |
paul@648 | 244 | """ |
paul@648 | 245 | Return a free/busy period for the given 'period' provided by this |
paul@648 | 246 | object, using the 'only_organiser' status to produce a suitable |
paul@648 | 247 | transparency value. |
paul@648 | 248 | """ |
paul@648 | 249 | |
paul@648 | 250 | return FreeBusyPeriod( |
paul@648 | 251 | period.get_start_point(), |
paul@648 | 252 | period.get_end_point(), |
paul@648 | 253 | self.get_value("UID"), |
paul@648 | 254 | only_organiser and "ORG" or self.get_value("TRANSP") or "OPAQUE", |
paul@648 | 255 | self.get_recurrenceid(), |
paul@648 | 256 | self.get_value("SUMMARY"), |
paul@648 | 257 | self.get_value("ORGANIZER") |
paul@648 | 258 | ) |
paul@648 | 259 | |
paul@648 | 260 | def get_participation_status(self, participant): |
paul@648 | 261 | |
paul@648 | 262 | """ |
paul@648 | 263 | Return the participation status of the given 'participant', with the |
paul@648 | 264 | special value "ORG" indicating organiser-only participation. |
paul@648 | 265 | """ |
paul@648 | 266 | |
paul@648 | 267 | attendees = self.get_value_map("ATTENDEE") |
paul@648 | 268 | organiser = self.get_value("ORGANIZER") |
paul@648 | 269 | |
paul@648 | 270 | for attendee, attendee_attr in attendees.items(): |
paul@648 | 271 | if attendee == participant: |
paul@648 | 272 | return attendee_attr.get("PARTSTAT", "NEEDS-ACTION") |
paul@648 | 273 | |
paul@648 | 274 | else: |
paul@648 | 275 | if organiser == participant: |
paul@648 | 276 | return "ORG" |
paul@648 | 277 | |
paul@648 | 278 | return None |
paul@648 | 279 | |
paul@648 | 280 | def get_participation(self, partstat, include_needs_action=False): |
paul@648 | 281 | |
paul@648 | 282 | """ |
paul@648 | 283 | Return whether 'partstat' indicates some kind of participation in an |
paul@648 | 284 | event. If 'include_needs_action' is specified as a true value, events |
paul@648 | 285 | not yet responded to will be treated as events with tentative |
paul@648 | 286 | participation. |
paul@648 | 287 | """ |
paul@648 | 288 | |
paul@648 | 289 | return not partstat in ("DECLINED", "DELEGATED", "NEEDS-ACTION") or \ |
paul@648 | 290 | include_needs_action and partstat == "NEEDS-ACTION" or \ |
paul@648 | 291 | partstat == "ORG" |
paul@648 | 292 | |
paul@422 | 293 | def get_tzid(self): |
paul@562 | 294 | |
paul@562 | 295 | """ |
paul@562 | 296 | Return a time zone identifier used by the start or end datetimes, |
paul@562 | 297 | potentially suitable for converting dates to datetimes. |
paul@562 | 298 | """ |
paul@562 | 299 | |
paul@560 | 300 | if not self.has_key("DTSTART"): |
paul@560 | 301 | return None |
paul@422 | 302 | dtstart, dtstart_attr = self.get_datetime_item("DTSTART") |
paul@630 | 303 | if self.has_key("DTEND"): |
paul@630 | 304 | dtend, dtend_attr = self.get_datetime_item("DTEND") |
paul@630 | 305 | else: |
paul@630 | 306 | dtend_attr = None |
paul@422 | 307 | return get_tzid(dtstart_attr, dtend_attr) |
paul@422 | 308 | |
paul@619 | 309 | def is_shared(self): |
paul@619 | 310 | |
paul@619 | 311 | """ |
paul@619 | 312 | Return whether this object is shared based on the presence of a SEQUENCE |
paul@619 | 313 | property. |
paul@619 | 314 | """ |
paul@619 | 315 | |
paul@619 | 316 | return self.get_value("SEQUENCE") is not None |
paul@619 | 317 | |
paul@650 | 318 | def possibly_active_from(self, dt, tzid): |
paul@650 | 319 | |
paul@650 | 320 | """ |
paul@650 | 321 | Return whether the object is possibly active from or after the given |
paul@650 | 322 | datetime 'dt' using 'tzid' to convert any dates or floating datetimes. |
paul@650 | 323 | """ |
paul@650 | 324 | |
paul@650 | 325 | dt = to_datetime(dt, tzid) |
paul@650 | 326 | periods = self.get_periods(tzid) |
paul@650 | 327 | |
paul@650 | 328 | for p in periods: |
paul@650 | 329 | if p.get_end_point() > dt: |
paul@650 | 330 | return True |
paul@650 | 331 | |
paul@650 | 332 | rrule = self.get_value("RRULE") |
paul@650 | 333 | parameters = rrule and get_parameters(rrule) |
paul@650 | 334 | until = parameters and parameters.get("UNTIL") |
paul@651 | 335 | count = parameters and parameters.get("COUNT") |
paul@650 | 336 | |
paul@651 | 337 | # Non-recurring periods or constrained recurrences that are not found to |
paul@651 | 338 | # lie beyond the specified datetime. |
paul@651 | 339 | |
paul@651 | 340 | if not rrule or until or count: |
paul@650 | 341 | return False |
paul@651 | 342 | |
paul@651 | 343 | # Unconstrained recurring periods will always lie beyond the specified |
paul@651 | 344 | # datetime. |
paul@651 | 345 | |
paul@651 | 346 | else: |
paul@650 | 347 | return True |
paul@650 | 348 | |
paul@627 | 349 | # Modification methods. |
paul@627 | 350 | |
paul@627 | 351 | def set_datetime(self, name, dt, tzid=None): |
paul@627 | 352 | |
paul@627 | 353 | """ |
paul@627 | 354 | Set a datetime for property 'name' using 'dt' and the optional fallback |
paul@627 | 355 | 'tzid', returning whether an update has occurred. |
paul@627 | 356 | """ |
paul@627 | 357 | |
paul@627 | 358 | if dt: |
paul@627 | 359 | old_value = self.get_value(name) |
paul@627 | 360 | self[name] = [get_item_from_datetime(dt, tzid)] |
paul@627 | 361 | return format_datetime(dt) != old_value |
paul@627 | 362 | |
paul@627 | 363 | return False |
paul@627 | 364 | |
paul@627 | 365 | def set_period(self, period): |
paul@627 | 366 | |
paul@627 | 367 | "Set the given 'period' as the main start and end." |
paul@627 | 368 | |
paul@627 | 369 | result = self.set_datetime("DTSTART", period.get_start()) |
paul@627 | 370 | result = self.set_datetime("DTEND", period.get_end()) or result |
paul@627 | 371 | return result |
paul@627 | 372 | |
paul@627 | 373 | def set_periods(self, periods): |
paul@627 | 374 | |
paul@627 | 375 | """ |
paul@627 | 376 | Set the given 'periods' as recurrence date properties, replacing the |
paul@627 | 377 | previous RDATE properties and ignoring any RRULE properties. |
paul@627 | 378 | """ |
paul@627 | 379 | |
paul@627 | 380 | update = False |
paul@627 | 381 | |
paul@627 | 382 | old_values = self.get_values("RDATE") |
paul@627 | 383 | new_rdates = [] |
paul@627 | 384 | |
paul@627 | 385 | if self.has_key("RDATE"): |
paul@627 | 386 | del self["RDATE"] |
paul@627 | 387 | |
paul@627 | 388 | for p in periods: |
paul@627 | 389 | if p.origin != "RRULE": |
paul@627 | 390 | new_rdates.append(get_period_item(p.get_start(), p.get_end())) |
paul@627 | 391 | |
paul@627 | 392 | self["RDATE"] = new_rdates |
paul@627 | 393 | |
paul@627 | 394 | # NOTE: To do: calculate the update status. |
paul@627 | 395 | return update |
paul@627 | 396 | |
paul@213 | 397 | # Construction and serialisation. |
paul@213 | 398 | |
paul@213 | 399 | def make_calendar(nodes, method=None): |
paul@213 | 400 | |
paul@213 | 401 | """ |
paul@213 | 402 | Return a complete calendar node wrapping the given 'nodes' and employing the |
paul@213 | 403 | given 'method', if indicated. |
paul@213 | 404 | """ |
paul@213 | 405 | |
paul@213 | 406 | return ("VCALENDAR", {}, |
paul@213 | 407 | (method and [("METHOD", {}, method)] or []) + |
paul@213 | 408 | [("VERSION", {}, "2.0")] + |
paul@213 | 409 | nodes |
paul@213 | 410 | ) |
paul@213 | 411 | |
paul@327 | 412 | def make_freebusy(freebusy, uid, organiser, organiser_attr=None, attendee=None, |
paul@562 | 413 | attendee_attr=None, period=None): |
paul@222 | 414 | |
paul@222 | 415 | """ |
paul@222 | 416 | Return a calendar node defining the free/busy details described in the given |
paul@292 | 417 | 'freebusy' list, employing the given 'uid', for the given 'organiser' and |
paul@292 | 418 | optional 'organiser_attr', with the optional 'attendee' providing recipient |
paul@292 | 419 | details together with the optional 'attendee_attr'. |
paul@327 | 420 | |
paul@562 | 421 | The result will be constrained to the 'period' if specified. |
paul@222 | 422 | """ |
paul@222 | 423 | |
paul@222 | 424 | record = [] |
paul@222 | 425 | rwrite = record.append |
paul@222 | 426 | |
paul@292 | 427 | rwrite(("ORGANIZER", organiser_attr or {}, organiser)) |
paul@222 | 428 | |
paul@222 | 429 | if attendee: |
paul@292 | 430 | rwrite(("ATTENDEE", attendee_attr or {}, attendee)) |
paul@222 | 431 | |
paul@222 | 432 | rwrite(("UID", {}, uid)) |
paul@222 | 433 | |
paul@222 | 434 | if freebusy: |
paul@327 | 435 | |
paul@327 | 436 | # Get a constrained view if start and end limits are specified. |
paul@327 | 437 | |
paul@563 | 438 | if period: |
paul@563 | 439 | periods = period_overlaps(freebusy, period, True) |
paul@563 | 440 | else: |
paul@563 | 441 | periods = freebusy |
paul@327 | 442 | |
paul@327 | 443 | # Write the limits of the resource. |
paul@327 | 444 | |
paul@563 | 445 | if periods: |
paul@563 | 446 | rwrite(("DTSTART", {"VALUE" : "DATE-TIME"}, format_datetime(periods[0].get_start_point()))) |
paul@563 | 447 | rwrite(("DTEND", {"VALUE" : "DATE-TIME"}, format_datetime(periods[-1].get_end_point()))) |
paul@563 | 448 | else: |
paul@563 | 449 | rwrite(("DTSTART", {"VALUE" : "DATE-TIME"}, format_datetime(period.get_start_point()))) |
paul@563 | 450 | rwrite(("DTEND", {"VALUE" : "DATE-TIME"}, format_datetime(period.get_end_point()))) |
paul@327 | 451 | |
paul@458 | 452 | for p in periods: |
paul@458 | 453 | if p.transp == "OPAQUE": |
paul@529 | 454 | rwrite(("FREEBUSY", {"FBTYPE" : "BUSY"}, "/".join( |
paul@562 | 455 | map(format_datetime, [p.get_start_point(), p.get_end_point()]) |
paul@529 | 456 | ))) |
paul@222 | 457 | |
paul@222 | 458 | return ("VFREEBUSY", {}, record) |
paul@222 | 459 | |
paul@213 | 460 | def parse_object(f, encoding, objtype=None): |
paul@213 | 461 | |
paul@213 | 462 | """ |
paul@213 | 463 | Parse the iTIP content from 'f' having the given 'encoding'. If 'objtype' is |
paul@213 | 464 | given, only objects of that type will be returned. Otherwise, the root of |
paul@213 | 465 | the content will be returned as a dictionary with a single key indicating |
paul@213 | 466 | the object type. |
paul@213 | 467 | |
paul@213 | 468 | Return None if the content was not readable or suitable. |
paul@213 | 469 | """ |
paul@213 | 470 | |
paul@213 | 471 | try: |
paul@213 | 472 | try: |
paul@213 | 473 | doctype, attrs, elements = obj = parse(f, encoding=encoding) |
paul@213 | 474 | if objtype and doctype == objtype: |
paul@213 | 475 | return to_dict(obj)[objtype][0] |
paul@213 | 476 | elif not objtype: |
paul@213 | 477 | return to_dict(obj) |
paul@213 | 478 | finally: |
paul@213 | 479 | f.close() |
paul@213 | 480 | |
paul@213 | 481 | # NOTE: Handle parse errors properly. |
paul@213 | 482 | |
paul@213 | 483 | except (ParseError, ValueError): |
paul@213 | 484 | pass |
paul@213 | 485 | |
paul@213 | 486 | return None |
paul@213 | 487 | |
paul@213 | 488 | def to_part(method, calendar): |
paul@213 | 489 | |
paul@213 | 490 | """ |
paul@213 | 491 | Write using the given 'method', the 'calendar' details to a MIME |
paul@213 | 492 | text/calendar part. |
paul@213 | 493 | """ |
paul@213 | 494 | |
paul@213 | 495 | encoding = "utf-8" |
paul@213 | 496 | out = StringIO() |
paul@213 | 497 | try: |
paul@213 | 498 | to_stream(out, make_calendar(calendar, method), encoding) |
paul@213 | 499 | part = MIMEText(out.getvalue(), "calendar", encoding) |
paul@213 | 500 | part.set_param("method", method) |
paul@213 | 501 | return part |
paul@213 | 502 | |
paul@213 | 503 | finally: |
paul@213 | 504 | out.close() |
paul@213 | 505 | |
paul@213 | 506 | def to_stream(out, fragment, encoding="utf-8"): |
paul@213 | 507 | iterwrite(out, encoding=encoding).append(fragment) |
paul@213 | 508 | |
paul@213 | 509 | # Structure access functions. |
paul@213 | 510 | |
paul@213 | 511 | def get_items(d, name, all=True): |
paul@213 | 512 | |
paul@213 | 513 | """ |
paul@213 | 514 | Get all items from 'd' for the given 'name', returning single items if |
paul@213 | 515 | 'all' is specified and set to a false value and if only one value is |
paul@213 | 516 | present for the name. Return None if no items are found for the name or if |
paul@213 | 517 | many items are found but 'all' is set to a false value. |
paul@213 | 518 | """ |
paul@213 | 519 | |
paul@213 | 520 | if d.has_key(name): |
paul@462 | 521 | items = d[name] |
paul@213 | 522 | if all: |
paul@462 | 523 | return items |
paul@462 | 524 | elif len(items) == 1: |
paul@462 | 525 | return items[0] |
paul@213 | 526 | else: |
paul@213 | 527 | return None |
paul@213 | 528 | else: |
paul@213 | 529 | return None |
paul@213 | 530 | |
paul@213 | 531 | def get_item(d, name): |
paul@213 | 532 | return get_items(d, name, False) |
paul@213 | 533 | |
paul@213 | 534 | def get_value_map(d, name): |
paul@213 | 535 | |
paul@213 | 536 | """ |
paul@213 | 537 | Return a dictionary for all items in 'd' having the given 'name'. The |
paul@213 | 538 | dictionary will map values for the name to any attributes or qualifiers |
paul@213 | 539 | that may have been present. |
paul@213 | 540 | """ |
paul@213 | 541 | |
paul@213 | 542 | items = get_items(d, name) |
paul@213 | 543 | if items: |
paul@213 | 544 | return dict(items) |
paul@213 | 545 | else: |
paul@213 | 546 | return {} |
paul@213 | 547 | |
paul@462 | 548 | def values_from_items(items): |
paul@462 | 549 | return map(lambda x: x[0], items) |
paul@462 | 550 | |
paul@213 | 551 | def get_values(d, name, all=True): |
paul@213 | 552 | if d.has_key(name): |
paul@462 | 553 | items = d[name] |
paul@462 | 554 | if not all and len(items) == 1: |
paul@462 | 555 | return items[0][0] |
paul@213 | 556 | else: |
paul@462 | 557 | return values_from_items(items) |
paul@213 | 558 | else: |
paul@213 | 559 | return None |
paul@213 | 560 | |
paul@213 | 561 | def get_value(d, name): |
paul@213 | 562 | return get_values(d, name, False) |
paul@213 | 563 | |
paul@417 | 564 | def get_date_value_items(d, name, tzid=None): |
paul@352 | 565 | |
paul@352 | 566 | """ |
paul@389 | 567 | Obtain items from 'd' having the given 'name', where a single item yields |
paul@389 | 568 | potentially many values. Return a list of tuples of the form (value, |
paul@389 | 569 | attributes) where the attributes have been given for the property in 'd'. |
paul@352 | 570 | """ |
paul@352 | 571 | |
paul@403 | 572 | items = get_items(d, name) |
paul@403 | 573 | if items: |
paul@403 | 574 | all_items = [] |
paul@403 | 575 | for item in items: |
paul@403 | 576 | values, attr = item |
paul@417 | 577 | if not attr.has_key("TZID") and tzid: |
paul@417 | 578 | attr["TZID"] = tzid |
paul@403 | 579 | if not isinstance(values, list): |
paul@403 | 580 | values = [values] |
paul@403 | 581 | for value in values: |
paul@403 | 582 | all_items.append((get_datetime(value, attr) or get_period(value, attr), attr)) |
paul@403 | 583 | return all_items |
paul@352 | 584 | else: |
paul@352 | 585 | return None |
paul@352 | 586 | |
paul@646 | 587 | def get_period_values(d, name, tzid=None): |
paul@630 | 588 | |
paul@630 | 589 | """ |
paul@630 | 590 | Return period values from 'd' for the given property 'name', using 'tzid' |
paul@646 | 591 | where specified to indicate the time zone. |
paul@630 | 592 | """ |
paul@630 | 593 | |
paul@630 | 594 | values = [] |
paul@630 | 595 | for value, attr in get_items(d, name) or []: |
paul@630 | 596 | if not attr.has_key("TZID") and tzid: |
paul@630 | 597 | attr["TZID"] = tzid |
paul@630 | 598 | start, end = get_period(value, attr) |
paul@646 | 599 | values.append(Period(start, end, tzid=tzid)) |
paul@630 | 600 | return values |
paul@630 | 601 | |
paul@506 | 602 | def get_utc_datetime(d, name, date_tzid=None): |
paul@506 | 603 | |
paul@506 | 604 | """ |
paul@506 | 605 | Return the value provided by 'd' for 'name' as a datetime in the UTC zone |
paul@506 | 606 | or as a date, converting any date to a datetime if 'date_tzid' is specified. |
paul@506 | 607 | """ |
paul@506 | 608 | |
paul@348 | 609 | t = get_datetime_item(d, name) |
paul@348 | 610 | if not t: |
paul@348 | 611 | return None |
paul@348 | 612 | else: |
paul@348 | 613 | dt, attr = t |
paul@506 | 614 | return to_utc_datetime(dt, date_tzid) |
paul@289 | 615 | |
paul@289 | 616 | def get_datetime_item(d, name): |
paul@562 | 617 | |
paul@562 | 618 | """ |
paul@562 | 619 | Return the value provided by 'd' for 'name' as a datetime or as a date, |
paul@562 | 620 | together with the attributes describing it. Return None if no value exists |
paul@562 | 621 | for 'name' in 'd'. |
paul@562 | 622 | """ |
paul@562 | 623 | |
paul@348 | 624 | t = get_item(d, name) |
paul@348 | 625 | if not t: |
paul@348 | 626 | return None |
paul@348 | 627 | else: |
paul@348 | 628 | value, attr = t |
paul@613 | 629 | dt = get_datetime(value, attr) |
paul@616 | 630 | tzid = get_datetime_tzid(dt) |
paul@616 | 631 | if tzid: |
paul@616 | 632 | attr["TZID"] = tzid |
paul@613 | 633 | return dt, attr |
paul@213 | 634 | |
paul@528 | 635 | # Conversion functions. |
paul@528 | 636 | |
paul@213 | 637 | def get_addresses(values): |
paul@213 | 638 | return [address for name, address in email.utils.getaddresses(values)] |
paul@213 | 639 | |
paul@213 | 640 | def get_address(value): |
paul@333 | 641 | value = value.lower() |
paul@333 | 642 | return value.startswith("mailto:") and value[7:] or value |
paul@213 | 643 | |
paul@213 | 644 | def get_uri(value): |
paul@213 | 645 | return value.lower().startswith("mailto:") and value.lower() or ":" in value and value or "mailto:%s" % value.lower() |
paul@213 | 646 | |
paul@309 | 647 | uri_value = get_uri |
paul@309 | 648 | |
paul@309 | 649 | def uri_values(values): |
paul@309 | 650 | return map(get_uri, values) |
paul@309 | 651 | |
paul@213 | 652 | def uri_dict(d): |
paul@213 | 653 | return dict([(get_uri(key), value) for key, value in d.items()]) |
paul@213 | 654 | |
paul@213 | 655 | def uri_item(item): |
paul@213 | 656 | return get_uri(item[0]), item[1] |
paul@213 | 657 | |
paul@213 | 658 | def uri_items(items): |
paul@213 | 659 | return [(get_uri(value), attr) for value, attr in items] |
paul@213 | 660 | |
paul@220 | 661 | # Operations on structure data. |
paul@220 | 662 | |
paul@220 | 663 | def is_new_object(old_sequence, new_sequence, old_dtstamp, new_dtstamp, partstat_set): |
paul@220 | 664 | |
paul@220 | 665 | """ |
paul@220 | 666 | Return for the given 'old_sequence' and 'new_sequence', 'old_dtstamp' and |
paul@220 | 667 | 'new_dtstamp', and the 'partstat_set' indication, whether the object |
paul@220 | 668 | providing the new information is really newer than the object providing the |
paul@220 | 669 | old information. |
paul@220 | 670 | """ |
paul@220 | 671 | |
paul@220 | 672 | have_sequence = old_sequence is not None and new_sequence is not None |
paul@220 | 673 | is_same_sequence = have_sequence and int(new_sequence) == int(old_sequence) |
paul@220 | 674 | |
paul@220 | 675 | have_dtstamp = old_dtstamp and new_dtstamp |
paul@220 | 676 | is_old_dtstamp = have_dtstamp and new_dtstamp < old_dtstamp or old_dtstamp and not new_dtstamp |
paul@220 | 677 | |
paul@220 | 678 | is_old_sequence = have_sequence and ( |
paul@220 | 679 | int(new_sequence) < int(old_sequence) or |
paul@220 | 680 | is_same_sequence and is_old_dtstamp |
paul@220 | 681 | ) |
paul@220 | 682 | |
paul@220 | 683 | return is_same_sequence and partstat_set or not is_old_sequence |
paul@220 | 684 | |
paul@630 | 685 | def get_periods(obj, tzid, end=None, inclusive=False): |
paul@256 | 686 | |
paul@256 | 687 | """ |
paul@618 | 688 | Return periods for the given object 'obj', employing the given 'tzid' where |
paul@618 | 689 | no time zone information is available (for whole day events, for example), |
paul@630 | 690 | confining materialised periods to before the given 'end' datetime. |
paul@618 | 691 | |
paul@630 | 692 | If 'end' is omitted, only explicit recurrences and recurrences from |
paul@630 | 693 | explicitly-terminated rules will be returned. |
paul@630 | 694 | |
paul@630 | 695 | If 'inclusive' is set to a true value, any period occurring at the 'end' |
paul@630 | 696 | will be included. |
paul@256 | 697 | """ |
paul@256 | 698 | |
paul@318 | 699 | rrule = obj.get_value("RRULE") |
paul@636 | 700 | parameters = rrule and get_parameters(rrule) |
paul@318 | 701 | |
paul@318 | 702 | # Use localised datetimes. |
paul@318 | 703 | |
paul@650 | 704 | (dtstart, dtstart_attr), (dtend, dtend_attr) = obj.get_main_period_items(tzid) |
paul@650 | 705 | duration = dtend - dtstart |
paul@256 | 706 | |
paul@618 | 707 | # Attempt to get time zone details from the object, using the supplied zone |
paul@618 | 708 | # only as a fallback. |
paul@618 | 709 | |
paul@638 | 710 | obj_tzid = obj.get_tzid() |
paul@256 | 711 | |
paul@352 | 712 | if not rrule: |
paul@541 | 713 | periods = [RecurringPeriod(dtstart, dtend, tzid, "DTSTART", dtstart_attr, dtend_attr)] |
paul@630 | 714 | |
paul@636 | 715 | elif end or parameters and parameters.has_key("UNTIL") or parameters.has_key("COUNT"): |
paul@630 | 716 | |
paul@352 | 717 | # Recurrence rules create multiple instances to be checked. |
paul@352 | 718 | # Conflicts may only be assessed within a period defined by policy |
paul@352 | 719 | # for the agent, with instances outside that period being considered |
paul@352 | 720 | # unchecked. |
paul@352 | 721 | |
paul@352 | 722 | selector = get_rule(dtstart, rrule) |
paul@352 | 723 | periods = [] |
paul@352 | 724 | |
paul@521 | 725 | until = parameters.get("UNTIL") |
paul@521 | 726 | if until: |
paul@650 | 727 | until_dt = to_timezone(get_datetime(until, dtstart_attr), obj_tzid) |
paul@650 | 728 | end = end and min(until_dt, end) or until_dt |
paul@521 | 729 | inclusive = True |
paul@521 | 730 | |
paul@630 | 731 | for recurrence_start in selector.materialise(dtstart, end, parameters.get("COUNT"), parameters.get("BYSETPOS"), inclusive): |
paul@630 | 732 | create = len(recurrence_start) == 3 and date or datetime |
paul@638 | 733 | recurrence_start = to_timezone(create(*recurrence_start), obj_tzid) |
paul@630 | 734 | recurrence_end = recurrence_start + duration |
paul@638 | 735 | periods.append(RecurringPeriod(recurrence_start, recurrence_end, tzid, "RRULE", dtstart_attr)) |
paul@352 | 736 | |
paul@635 | 737 | else: |
paul@635 | 738 | periods = [] |
paul@635 | 739 | |
paul@352 | 740 | # Add recurrence dates. |
paul@256 | 741 | |
paul@494 | 742 | rdates = obj.get_date_value_items("RDATE", tzid) |
paul@352 | 743 | |
paul@352 | 744 | if rdates: |
paul@494 | 745 | for rdate, rdate_attr in rdates: |
paul@389 | 746 | if isinstance(rdate, tuple): |
paul@541 | 747 | periods.append(RecurringPeriod(rdate[0], rdate[1], tzid, "RDATE", rdate_attr)) |
paul@389 | 748 | else: |
paul@541 | 749 | periods.append(RecurringPeriod(rdate, rdate + duration, tzid, "RDATE", rdate_attr)) |
paul@424 | 750 | |
paul@424 | 751 | # Return a sorted list of the periods. |
paul@424 | 752 | |
paul@542 | 753 | periods.sort() |
paul@352 | 754 | |
paul@352 | 755 | # Exclude exception dates. |
paul@352 | 756 | |
paul@638 | 757 | exdates = obj.get_date_value_items("EXDATE", tzid) |
paul@256 | 758 | |
paul@352 | 759 | if exdates: |
paul@638 | 760 | for exdate, exdate_attr in exdates: |
paul@389 | 761 | if isinstance(exdate, tuple): |
paul@638 | 762 | period = RecurringPeriod(exdate[0], exdate[1], tzid, "EXDATE", exdate_attr) |
paul@389 | 763 | else: |
paul@638 | 764 | period = RecurringPeriod(exdate, exdate + duration, tzid, "EXDATE", exdate_attr) |
paul@424 | 765 | i = bisect_left(periods, period) |
paul@458 | 766 | while i < len(periods) and periods[i] == period: |
paul@424 | 767 | del periods[i] |
paul@256 | 768 | |
paul@256 | 769 | return periods |
paul@256 | 770 | |
paul@606 | 771 | def get_sender_identities(mapping): |
paul@606 | 772 | |
paul@606 | 773 | """ |
paul@606 | 774 | Return a mapping from actual senders to the identities for which they |
paul@606 | 775 | have provided data, extracting this information from the given |
paul@606 | 776 | 'mapping'. |
paul@606 | 777 | """ |
paul@606 | 778 | |
paul@606 | 779 | senders = {} |
paul@606 | 780 | |
paul@606 | 781 | for value, attr in mapping.items(): |
paul@606 | 782 | sent_by = attr.get("SENT-BY") |
paul@606 | 783 | if sent_by: |
paul@606 | 784 | sender = get_uri(sent_by) |
paul@606 | 785 | else: |
paul@606 | 786 | sender = value |
paul@606 | 787 | |
paul@606 | 788 | if not senders.has_key(sender): |
paul@606 | 789 | senders[sender] = [] |
paul@606 | 790 | |
paul@606 | 791 | senders[sender].append(value) |
paul@606 | 792 | |
paul@606 | 793 | return senders |
paul@606 | 794 | |
paul@618 | 795 | def get_window_end(tzid, days=100): |
paul@606 | 796 | |
paul@618 | 797 | """ |
paul@618 | 798 | Return a datetime in the time zone indicated by 'tzid' marking the end of a |
paul@618 | 799 | window of the given number of 'days'. |
paul@618 | 800 | """ |
paul@618 | 801 | |
paul@618 | 802 | return to_timezone(datetime.now(), tzid) + timedelta(days) |
paul@606 | 803 | |
paul@213 | 804 | # vim: tabstop=4 expandtab shiftwidth=4 |