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