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@616 | 25 | from imiptools.dates import format_datetime, get_datetime, get_datetime_tzid, \ |
paul@617 | 26 | get_duration, get_period, \ |
paul@616 | 27 | get_tzid, to_timezone, to_utc_datetime |
paul@543 | 28 | from imiptools.period import Period, RecurringPeriod, period_overlaps |
paul@213 | 29 | from vCalendar import iterwrite, parse, ParseError, to_dict, to_node |
paul@256 | 30 | from vRecurrence import get_parameters, get_rule |
paul@213 | 31 | import email.utils |
paul@213 | 32 | |
paul@213 | 33 | try: |
paul@213 | 34 | from cStringIO import StringIO |
paul@213 | 35 | except ImportError: |
paul@213 | 36 | from StringIO import StringIO |
paul@213 | 37 | |
paul@213 | 38 | class Object: |
paul@213 | 39 | |
paul@213 | 40 | "Access to calendar structures." |
paul@213 | 41 | |
paul@213 | 42 | def __init__(self, fragment): |
paul@213 | 43 | self.objtype, (self.details, self.attr) = fragment.items()[0] |
paul@213 | 44 | |
paul@535 | 45 | def get_uid(self): |
paul@535 | 46 | return self.get_value("UID") |
paul@535 | 47 | |
paul@535 | 48 | def get_recurrenceid(self): |
paul@563 | 49 | |
paul@563 | 50 | """ |
paul@563 | 51 | Return the recurrence identifier, normalised to a UTC datetime if |
paul@563 | 52 | specified as a datetime, converted to a date object otherwise. If no |
paul@563 | 53 | recurrence identifier is present, None is returned. |
paul@563 | 54 | """ |
paul@563 | 55 | |
paul@563 | 56 | recurrenceid = self.get_utc_datetime("RECURRENCE-ID") |
paul@563 | 57 | return recurrenceid and format_datetime(recurrenceid) |
paul@535 | 58 | |
paul@535 | 59 | # Structure access. |
paul@535 | 60 | |
paul@524 | 61 | def copy(self): |
paul@524 | 62 | return Object(to_dict(self.to_node())) |
paul@524 | 63 | |
paul@213 | 64 | def get_items(self, name, all=True): |
paul@213 | 65 | return get_items(self.details, name, all) |
paul@213 | 66 | |
paul@213 | 67 | def get_item(self, name): |
paul@213 | 68 | return get_item(self.details, name) |
paul@213 | 69 | |
paul@213 | 70 | def get_value_map(self, name): |
paul@213 | 71 | return get_value_map(self.details, name) |
paul@213 | 72 | |
paul@213 | 73 | def get_values(self, name, all=True): |
paul@213 | 74 | return get_values(self.details, name, all) |
paul@213 | 75 | |
paul@213 | 76 | def get_value(self, name): |
paul@213 | 77 | return get_value(self.details, name) |
paul@213 | 78 | |
paul@506 | 79 | def get_utc_datetime(self, name, date_tzid=None): |
paul@506 | 80 | return get_utc_datetime(self.details, name, date_tzid) |
paul@213 | 81 | |
paul@417 | 82 | def get_date_values(self, name, tzid=None): |
paul@417 | 83 | items = get_date_value_items(self.details, name, tzid) |
paul@389 | 84 | return items and [value for value, attr in items] |
paul@352 | 85 | |
paul@417 | 86 | def get_date_value_items(self, name, tzid=None): |
paul@417 | 87 | return get_date_value_items(self.details, name, tzid) |
paul@352 | 88 | |
paul@318 | 89 | def get_datetime(self, name): |
paul@567 | 90 | t = get_datetime_item(self.details, name) |
paul@567 | 91 | if not t: return None |
paul@567 | 92 | dt, attr = t |
paul@318 | 93 | return dt |
paul@318 | 94 | |
paul@620 | 95 | def set_datetime(self, name, dt, tzid): |
paul@620 | 96 | |
paul@620 | 97 | """ |
paul@620 | 98 | Set a datetime for property 'name' using 'dt' and 'tzid', returning |
paul@620 | 99 | whether an update has occurred. |
paul@620 | 100 | """ |
paul@620 | 101 | |
paul@620 | 102 | if dt: |
paul@620 | 103 | old_value = self.get_value(name) |
paul@620 | 104 | self[name] = [get_datetime_item(dt, tzid)] |
paul@620 | 105 | return format_datetime(dt) != old_value |
paul@620 | 106 | |
paul@620 | 107 | return False |
paul@620 | 108 | |
paul@289 | 109 | def get_datetime_item(self, name): |
paul@289 | 110 | return get_datetime_item(self.details, name) |
paul@289 | 111 | |
paul@392 | 112 | def get_duration(self, name): |
paul@392 | 113 | return get_duration(self.get_value(name)) |
paul@392 | 114 | |
paul@213 | 115 | def to_node(self): |
paul@213 | 116 | return to_node({self.objtype : [(self.details, self.attr)]}) |
paul@213 | 117 | |
paul@213 | 118 | def to_part(self, method): |
paul@213 | 119 | return to_part(method, [self.to_node()]) |
paul@213 | 120 | |
paul@213 | 121 | # Direct access to the structure. |
paul@213 | 122 | |
paul@392 | 123 | def has_key(self, name): |
paul@392 | 124 | return self.details.has_key(name) |
paul@392 | 125 | |
paul@524 | 126 | def get(self, name): |
paul@524 | 127 | return self.details.get(name) |
paul@524 | 128 | |
paul@213 | 129 | def __getitem__(self, name): |
paul@213 | 130 | return self.details[name] |
paul@213 | 131 | |
paul@213 | 132 | def __setitem__(self, name, value): |
paul@213 | 133 | self.details[name] = value |
paul@213 | 134 | |
paul@213 | 135 | def __delitem__(self, name): |
paul@213 | 136 | del self.details[name] |
paul@213 | 137 | |
paul@524 | 138 | def remove(self, name): |
paul@524 | 139 | try: |
paul@524 | 140 | del self[name] |
paul@524 | 141 | except KeyError: |
paul@524 | 142 | pass |
paul@524 | 143 | |
paul@524 | 144 | def remove_all(self, names): |
paul@524 | 145 | for name in names: |
paul@524 | 146 | self.remove(name) |
paul@524 | 147 | |
paul@256 | 148 | # Computed results. |
paul@256 | 149 | |
paul@458 | 150 | def get_periods(self, tzid, end): |
paul@620 | 151 | |
paul@620 | 152 | """ |
paul@620 | 153 | Return periods defined by this object, employing the given 'tzid' where |
paul@620 | 154 | no time zone information is defined, and limiting the collection to a |
paul@620 | 155 | window of time with the given 'end'. |
paul@620 | 156 | """ |
paul@620 | 157 | |
paul@458 | 158 | return get_periods(self, tzid, end) |
paul@360 | 159 | |
paul@620 | 160 | def set_period(self, period): |
paul@620 | 161 | |
paul@620 | 162 | "Set the given 'period' as the main start and end." |
paul@620 | 163 | |
paul@620 | 164 | result = self.set_datetime("DTSTART", period.get_start(), period.start_attr().get("TZID")) |
paul@620 | 165 | result = self.set_datetime("DTEND", period.get_end(), period.end_attr().get("TZID")) or result |
paul@620 | 166 | return result |
paul@620 | 167 | |
paul@422 | 168 | def get_tzid(self): |
paul@562 | 169 | |
paul@562 | 170 | """ |
paul@562 | 171 | Return a time zone identifier used by the start or end datetimes, |
paul@562 | 172 | potentially suitable for converting dates to datetimes. |
paul@562 | 173 | """ |
paul@562 | 174 | |
paul@560 | 175 | if not self.has_key("DTSTART"): |
paul@560 | 176 | return None |
paul@422 | 177 | dtstart, dtstart_attr = self.get_datetime_item("DTSTART") |
paul@422 | 178 | dtend, dtend_attr = self.get_datetime_item("DTEND") |
paul@422 | 179 | return get_tzid(dtstart_attr, dtend_attr) |
paul@422 | 180 | |
paul@619 | 181 | def is_shared(self): |
paul@619 | 182 | |
paul@619 | 183 | """ |
paul@619 | 184 | Return whether this object is shared based on the presence of a SEQUENCE |
paul@619 | 185 | property. |
paul@619 | 186 | """ |
paul@619 | 187 | |
paul@619 | 188 | return self.get_value("SEQUENCE") is not None |
paul@619 | 189 | |
paul@213 | 190 | # Construction and serialisation. |
paul@213 | 191 | |
paul@213 | 192 | def make_calendar(nodes, method=None): |
paul@213 | 193 | |
paul@213 | 194 | """ |
paul@213 | 195 | Return a complete calendar node wrapping the given 'nodes' and employing the |
paul@213 | 196 | given 'method', if indicated. |
paul@213 | 197 | """ |
paul@213 | 198 | |
paul@213 | 199 | return ("VCALENDAR", {}, |
paul@213 | 200 | (method and [("METHOD", {}, method)] or []) + |
paul@213 | 201 | [("VERSION", {}, "2.0")] + |
paul@213 | 202 | nodes |
paul@213 | 203 | ) |
paul@213 | 204 | |
paul@327 | 205 | def make_freebusy(freebusy, uid, organiser, organiser_attr=None, attendee=None, |
paul@562 | 206 | attendee_attr=None, period=None): |
paul@222 | 207 | |
paul@222 | 208 | """ |
paul@222 | 209 | Return a calendar node defining the free/busy details described in the given |
paul@292 | 210 | 'freebusy' list, employing the given 'uid', for the given 'organiser' and |
paul@292 | 211 | optional 'organiser_attr', with the optional 'attendee' providing recipient |
paul@292 | 212 | details together with the optional 'attendee_attr'. |
paul@327 | 213 | |
paul@562 | 214 | The result will be constrained to the 'period' if specified. |
paul@222 | 215 | """ |
paul@222 | 216 | |
paul@222 | 217 | record = [] |
paul@222 | 218 | rwrite = record.append |
paul@222 | 219 | |
paul@292 | 220 | rwrite(("ORGANIZER", organiser_attr or {}, organiser)) |
paul@222 | 221 | |
paul@222 | 222 | if attendee: |
paul@292 | 223 | rwrite(("ATTENDEE", attendee_attr or {}, attendee)) |
paul@222 | 224 | |
paul@222 | 225 | rwrite(("UID", {}, uid)) |
paul@222 | 226 | |
paul@222 | 227 | if freebusy: |
paul@327 | 228 | |
paul@327 | 229 | # Get a constrained view if start and end limits are specified. |
paul@327 | 230 | |
paul@563 | 231 | if period: |
paul@563 | 232 | periods = period_overlaps(freebusy, period, True) |
paul@563 | 233 | else: |
paul@563 | 234 | periods = freebusy |
paul@327 | 235 | |
paul@327 | 236 | # Write the limits of the resource. |
paul@327 | 237 | |
paul@563 | 238 | if periods: |
paul@563 | 239 | rwrite(("DTSTART", {"VALUE" : "DATE-TIME"}, format_datetime(periods[0].get_start_point()))) |
paul@563 | 240 | rwrite(("DTEND", {"VALUE" : "DATE-TIME"}, format_datetime(periods[-1].get_end_point()))) |
paul@563 | 241 | else: |
paul@563 | 242 | rwrite(("DTSTART", {"VALUE" : "DATE-TIME"}, format_datetime(period.get_start_point()))) |
paul@563 | 243 | rwrite(("DTEND", {"VALUE" : "DATE-TIME"}, format_datetime(period.get_end_point()))) |
paul@327 | 244 | |
paul@458 | 245 | for p in periods: |
paul@458 | 246 | if p.transp == "OPAQUE": |
paul@529 | 247 | rwrite(("FREEBUSY", {"FBTYPE" : "BUSY"}, "/".join( |
paul@562 | 248 | map(format_datetime, [p.get_start_point(), p.get_end_point()]) |
paul@529 | 249 | ))) |
paul@222 | 250 | |
paul@222 | 251 | return ("VFREEBUSY", {}, record) |
paul@222 | 252 | |
paul@213 | 253 | def parse_object(f, encoding, objtype=None): |
paul@213 | 254 | |
paul@213 | 255 | """ |
paul@213 | 256 | Parse the iTIP content from 'f' having the given 'encoding'. If 'objtype' is |
paul@213 | 257 | given, only objects of that type will be returned. Otherwise, the root of |
paul@213 | 258 | the content will be returned as a dictionary with a single key indicating |
paul@213 | 259 | the object type. |
paul@213 | 260 | |
paul@213 | 261 | Return None if the content was not readable or suitable. |
paul@213 | 262 | """ |
paul@213 | 263 | |
paul@213 | 264 | try: |
paul@213 | 265 | try: |
paul@213 | 266 | doctype, attrs, elements = obj = parse(f, encoding=encoding) |
paul@213 | 267 | if objtype and doctype == objtype: |
paul@213 | 268 | return to_dict(obj)[objtype][0] |
paul@213 | 269 | elif not objtype: |
paul@213 | 270 | return to_dict(obj) |
paul@213 | 271 | finally: |
paul@213 | 272 | f.close() |
paul@213 | 273 | |
paul@213 | 274 | # NOTE: Handle parse errors properly. |
paul@213 | 275 | |
paul@213 | 276 | except (ParseError, ValueError): |
paul@213 | 277 | pass |
paul@213 | 278 | |
paul@213 | 279 | return None |
paul@213 | 280 | |
paul@213 | 281 | def to_part(method, calendar): |
paul@213 | 282 | |
paul@213 | 283 | """ |
paul@213 | 284 | Write using the given 'method', the 'calendar' details to a MIME |
paul@213 | 285 | text/calendar part. |
paul@213 | 286 | """ |
paul@213 | 287 | |
paul@213 | 288 | encoding = "utf-8" |
paul@213 | 289 | out = StringIO() |
paul@213 | 290 | try: |
paul@213 | 291 | to_stream(out, make_calendar(calendar, method), encoding) |
paul@213 | 292 | part = MIMEText(out.getvalue(), "calendar", encoding) |
paul@213 | 293 | part.set_param("method", method) |
paul@213 | 294 | return part |
paul@213 | 295 | |
paul@213 | 296 | finally: |
paul@213 | 297 | out.close() |
paul@213 | 298 | |
paul@213 | 299 | def to_stream(out, fragment, encoding="utf-8"): |
paul@213 | 300 | iterwrite(out, encoding=encoding).append(fragment) |
paul@213 | 301 | |
paul@213 | 302 | # Structure access functions. |
paul@213 | 303 | |
paul@213 | 304 | def get_items(d, name, all=True): |
paul@213 | 305 | |
paul@213 | 306 | """ |
paul@213 | 307 | Get all items from 'd' for the given 'name', returning single items if |
paul@213 | 308 | 'all' is specified and set to a false value and if only one value is |
paul@213 | 309 | present for the name. Return None if no items are found for the name or if |
paul@213 | 310 | many items are found but 'all' is set to a false value. |
paul@213 | 311 | """ |
paul@213 | 312 | |
paul@213 | 313 | if d.has_key(name): |
paul@462 | 314 | items = d[name] |
paul@213 | 315 | if all: |
paul@462 | 316 | return items |
paul@462 | 317 | elif len(items) == 1: |
paul@462 | 318 | return items[0] |
paul@213 | 319 | else: |
paul@213 | 320 | return None |
paul@213 | 321 | else: |
paul@213 | 322 | return None |
paul@213 | 323 | |
paul@213 | 324 | def get_item(d, name): |
paul@213 | 325 | return get_items(d, name, False) |
paul@213 | 326 | |
paul@213 | 327 | def get_value_map(d, name): |
paul@213 | 328 | |
paul@213 | 329 | """ |
paul@213 | 330 | Return a dictionary for all items in 'd' having the given 'name'. The |
paul@213 | 331 | dictionary will map values for the name to any attributes or qualifiers |
paul@213 | 332 | that may have been present. |
paul@213 | 333 | """ |
paul@213 | 334 | |
paul@213 | 335 | items = get_items(d, name) |
paul@213 | 336 | if items: |
paul@213 | 337 | return dict(items) |
paul@213 | 338 | else: |
paul@213 | 339 | return {} |
paul@213 | 340 | |
paul@462 | 341 | def values_from_items(items): |
paul@462 | 342 | return map(lambda x: x[0], items) |
paul@462 | 343 | |
paul@213 | 344 | def get_values(d, name, all=True): |
paul@213 | 345 | if d.has_key(name): |
paul@462 | 346 | items = d[name] |
paul@462 | 347 | if not all and len(items) == 1: |
paul@462 | 348 | return items[0][0] |
paul@213 | 349 | else: |
paul@462 | 350 | return values_from_items(items) |
paul@213 | 351 | else: |
paul@213 | 352 | return None |
paul@213 | 353 | |
paul@213 | 354 | def get_value(d, name): |
paul@213 | 355 | return get_values(d, name, False) |
paul@213 | 356 | |
paul@417 | 357 | def get_date_value_items(d, name, tzid=None): |
paul@352 | 358 | |
paul@352 | 359 | """ |
paul@389 | 360 | Obtain items from 'd' having the given 'name', where a single item yields |
paul@389 | 361 | potentially many values. Return a list of tuples of the form (value, |
paul@389 | 362 | attributes) where the attributes have been given for the property in 'd'. |
paul@352 | 363 | """ |
paul@352 | 364 | |
paul@403 | 365 | items = get_items(d, name) |
paul@403 | 366 | if items: |
paul@403 | 367 | all_items = [] |
paul@403 | 368 | for item in items: |
paul@403 | 369 | values, attr = item |
paul@417 | 370 | if not attr.has_key("TZID") and tzid: |
paul@417 | 371 | attr["TZID"] = tzid |
paul@403 | 372 | if not isinstance(values, list): |
paul@403 | 373 | values = [values] |
paul@403 | 374 | for value in values: |
paul@403 | 375 | all_items.append((get_datetime(value, attr) or get_period(value, attr), attr)) |
paul@403 | 376 | return all_items |
paul@352 | 377 | else: |
paul@352 | 378 | return None |
paul@352 | 379 | |
paul@506 | 380 | def get_utc_datetime(d, name, date_tzid=None): |
paul@506 | 381 | |
paul@506 | 382 | """ |
paul@506 | 383 | Return the value provided by 'd' for 'name' as a datetime in the UTC zone |
paul@506 | 384 | or as a date, converting any date to a datetime if 'date_tzid' is specified. |
paul@506 | 385 | """ |
paul@506 | 386 | |
paul@348 | 387 | t = get_datetime_item(d, name) |
paul@348 | 388 | if not t: |
paul@348 | 389 | return None |
paul@348 | 390 | else: |
paul@348 | 391 | dt, attr = t |
paul@506 | 392 | return to_utc_datetime(dt, date_tzid) |
paul@289 | 393 | |
paul@289 | 394 | def get_datetime_item(d, name): |
paul@562 | 395 | |
paul@562 | 396 | """ |
paul@562 | 397 | Return the value provided by 'd' for 'name' as a datetime or as a date, |
paul@562 | 398 | together with the attributes describing it. Return None if no value exists |
paul@562 | 399 | for 'name' in 'd'. |
paul@562 | 400 | """ |
paul@562 | 401 | |
paul@348 | 402 | t = get_item(d, name) |
paul@348 | 403 | if not t: |
paul@348 | 404 | return None |
paul@348 | 405 | else: |
paul@348 | 406 | value, attr = t |
paul@613 | 407 | dt = get_datetime(value, attr) |
paul@616 | 408 | tzid = get_datetime_tzid(dt) |
paul@616 | 409 | if tzid: |
paul@616 | 410 | attr["TZID"] = tzid |
paul@613 | 411 | return dt, attr |
paul@213 | 412 | |
paul@528 | 413 | # Conversion functions. |
paul@528 | 414 | |
paul@213 | 415 | def get_addresses(values): |
paul@213 | 416 | return [address for name, address in email.utils.getaddresses(values)] |
paul@213 | 417 | |
paul@213 | 418 | def get_address(value): |
paul@333 | 419 | value = value.lower() |
paul@333 | 420 | return value.startswith("mailto:") and value[7:] or value |
paul@213 | 421 | |
paul@213 | 422 | def get_uri(value): |
paul@213 | 423 | return value.lower().startswith("mailto:") and value.lower() or ":" in value and value or "mailto:%s" % value.lower() |
paul@213 | 424 | |
paul@309 | 425 | uri_value = get_uri |
paul@309 | 426 | |
paul@309 | 427 | def uri_values(values): |
paul@309 | 428 | return map(get_uri, values) |
paul@309 | 429 | |
paul@213 | 430 | def uri_dict(d): |
paul@213 | 431 | return dict([(get_uri(key), value) for key, value in d.items()]) |
paul@213 | 432 | |
paul@213 | 433 | def uri_item(item): |
paul@213 | 434 | return get_uri(item[0]), item[1] |
paul@213 | 435 | |
paul@213 | 436 | def uri_items(items): |
paul@213 | 437 | return [(get_uri(value), attr) for value, attr in items] |
paul@213 | 438 | |
paul@220 | 439 | # Operations on structure data. |
paul@220 | 440 | |
paul@220 | 441 | def is_new_object(old_sequence, new_sequence, old_dtstamp, new_dtstamp, partstat_set): |
paul@220 | 442 | |
paul@220 | 443 | """ |
paul@220 | 444 | Return for the given 'old_sequence' and 'new_sequence', 'old_dtstamp' and |
paul@220 | 445 | 'new_dtstamp', and the 'partstat_set' indication, whether the object |
paul@220 | 446 | providing the new information is really newer than the object providing the |
paul@220 | 447 | old information. |
paul@220 | 448 | """ |
paul@220 | 449 | |
paul@220 | 450 | have_sequence = old_sequence is not None and new_sequence is not None |
paul@220 | 451 | is_same_sequence = have_sequence and int(new_sequence) == int(old_sequence) |
paul@220 | 452 | |
paul@220 | 453 | have_dtstamp = old_dtstamp and new_dtstamp |
paul@220 | 454 | is_old_dtstamp = have_dtstamp and new_dtstamp < old_dtstamp or old_dtstamp and not new_dtstamp |
paul@220 | 455 | |
paul@220 | 456 | is_old_sequence = have_sequence and ( |
paul@220 | 457 | int(new_sequence) < int(old_sequence) or |
paul@220 | 458 | is_same_sequence and is_old_dtstamp |
paul@220 | 459 | ) |
paul@220 | 460 | |
paul@220 | 461 | return is_same_sequence and partstat_set or not is_old_sequence |
paul@220 | 462 | |
paul@458 | 463 | def get_periods(obj, tzid, window_end, inclusive=False): |
paul@256 | 464 | |
paul@256 | 465 | """ |
paul@618 | 466 | Return periods for the given object 'obj', employing the given 'tzid' where |
paul@618 | 467 | no time zone information is available (for whole day events, for example), |
paul@618 | 468 | confining materialised periods to before the given 'window_end' datetime. |
paul@618 | 469 | |
paul@618 | 470 | If 'inclusive' is set to a true value, any period occurring at the |
paul@618 | 471 | 'window_end' will be included. |
paul@256 | 472 | """ |
paul@256 | 473 | |
paul@318 | 474 | rrule = obj.get_value("RRULE") |
paul@318 | 475 | |
paul@318 | 476 | # Use localised datetimes. |
paul@318 | 477 | |
paul@392 | 478 | dtstart, dtstart_attr = obj.get_datetime_item("DTSTART") |
paul@256 | 479 | |
paul@392 | 480 | if obj.has_key("DTEND"): |
paul@392 | 481 | dtend, dtend_attr = obj.get_datetime_item("DTEND") |
paul@392 | 482 | duration = dtend - dtstart |
paul@392 | 483 | elif obj.has_key("DURATION"): |
paul@392 | 484 | duration = obj.get_duration("DURATION") |
paul@392 | 485 | dtend = dtstart + duration |
paul@392 | 486 | dtend_attr = dtstart_attr |
paul@392 | 487 | else: |
paul@392 | 488 | dtend, dtend_attr = dtstart, dtstart_attr |
paul@256 | 489 | |
paul@618 | 490 | # Attempt to get time zone details from the object, using the supplied zone |
paul@618 | 491 | # only as a fallback. |
paul@618 | 492 | |
paul@562 | 493 | tzid = obj.get_tzid() or tzid |
paul@256 | 494 | |
paul@352 | 495 | if not rrule: |
paul@541 | 496 | periods = [RecurringPeriod(dtstart, dtend, tzid, "DTSTART", dtstart_attr, dtend_attr)] |
paul@352 | 497 | else: |
paul@352 | 498 | # Recurrence rules create multiple instances to be checked. |
paul@352 | 499 | # Conflicts may only be assessed within a period defined by policy |
paul@352 | 500 | # for the agent, with instances outside that period being considered |
paul@352 | 501 | # unchecked. |
paul@352 | 502 | |
paul@352 | 503 | selector = get_rule(dtstart, rrule) |
paul@352 | 504 | parameters = get_parameters(rrule) |
paul@352 | 505 | periods = [] |
paul@352 | 506 | |
paul@521 | 507 | until = parameters.get("UNTIL") |
paul@521 | 508 | if until: |
paul@521 | 509 | window_end = min(to_timezone(get_datetime(until, dtstart_attr), tzid), window_end) |
paul@521 | 510 | inclusive = True |
paul@521 | 511 | |
paul@360 | 512 | for start in selector.materialise(dtstart, window_end, parameters.get("COUNT"), parameters.get("BYSETPOS"), inclusive): |
paul@560 | 513 | create = len(start) == 3 and date or datetime |
paul@560 | 514 | start = to_timezone(create(*start), tzid) |
paul@352 | 515 | end = start + duration |
paul@541 | 516 | periods.append(RecurringPeriod(start, end, tzid, "RRULE")) |
paul@352 | 517 | |
paul@352 | 518 | # Add recurrence dates. |
paul@256 | 519 | |
paul@494 | 520 | rdates = obj.get_date_value_items("RDATE", tzid) |
paul@352 | 521 | |
paul@352 | 522 | if rdates: |
paul@494 | 523 | for rdate, rdate_attr in rdates: |
paul@389 | 524 | if isinstance(rdate, tuple): |
paul@541 | 525 | periods.append(RecurringPeriod(rdate[0], rdate[1], tzid, "RDATE", rdate_attr)) |
paul@389 | 526 | else: |
paul@541 | 527 | periods.append(RecurringPeriod(rdate, rdate + duration, tzid, "RDATE", rdate_attr)) |
paul@424 | 528 | |
paul@424 | 529 | # Return a sorted list of the periods. |
paul@424 | 530 | |
paul@542 | 531 | periods.sort() |
paul@352 | 532 | |
paul@352 | 533 | # Exclude exception dates. |
paul@352 | 534 | |
paul@417 | 535 | exdates = obj.get_date_values("EXDATE", tzid) |
paul@256 | 536 | |
paul@352 | 537 | if exdates: |
paul@352 | 538 | for exdate in exdates: |
paul@389 | 539 | if isinstance(exdate, tuple): |
paul@562 | 540 | period = Period(exdate[0], exdate[1], tzid) |
paul@389 | 541 | else: |
paul@562 | 542 | period = Period(exdate, exdate + duration, tzid) |
paul@424 | 543 | i = bisect_left(periods, period) |
paul@458 | 544 | while i < len(periods) and periods[i] == period: |
paul@424 | 545 | del periods[i] |
paul@256 | 546 | |
paul@256 | 547 | return periods |
paul@256 | 548 | |
paul@606 | 549 | def get_sender_identities(mapping): |
paul@606 | 550 | |
paul@606 | 551 | """ |
paul@606 | 552 | Return a mapping from actual senders to the identities for which they |
paul@606 | 553 | have provided data, extracting this information from the given |
paul@606 | 554 | 'mapping'. |
paul@606 | 555 | """ |
paul@606 | 556 | |
paul@606 | 557 | senders = {} |
paul@606 | 558 | |
paul@606 | 559 | for value, attr in mapping.items(): |
paul@606 | 560 | sent_by = attr.get("SENT-BY") |
paul@606 | 561 | if sent_by: |
paul@606 | 562 | sender = get_uri(sent_by) |
paul@606 | 563 | else: |
paul@606 | 564 | sender = value |
paul@606 | 565 | |
paul@606 | 566 | if not senders.has_key(sender): |
paul@606 | 567 | senders[sender] = [] |
paul@606 | 568 | |
paul@606 | 569 | senders[sender].append(value) |
paul@606 | 570 | |
paul@606 | 571 | return senders |
paul@606 | 572 | |
paul@618 | 573 | def get_window_end(tzid, days=100): |
paul@606 | 574 | |
paul@618 | 575 | """ |
paul@618 | 576 | Return a datetime in the time zone indicated by 'tzid' marking the end of a |
paul@618 | 577 | window of the given number of 'days'. |
paul@618 | 578 | """ |
paul@618 | 579 | |
paul@618 | 580 | return to_timezone(datetime.now(), tzid) + timedelta(days) |
paul@606 | 581 | |
paul@213 | 582 | # vim: tabstop=4 expandtab shiftwidth=4 |