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