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