paul@213 | 1 | #!/usr/bin/env python |
paul@213 | 2 | |
paul@213 | 3 | """ |
paul@213 | 4 | Interpretation of vCalendar content. |
paul@213 | 5 | |
paul@1230 | 6 | Copyright (C) 2014, 2015, 2016, 2017 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@939 | 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@1204 | 30 | get_time, get_timestamp, get_tzid, to_datetime, \ |
paul@1204 | 31 | to_timezone, to_utc_datetime |
paul@1230 | 32 | from imiptools.freebusy import FreeBusyPeriod |
paul@1230 | 33 | from imiptools.period import Period, RecurringPeriod |
paul@213 | 34 | from vCalendar import iterwrite, parse, ParseError, to_dict, to_node |
paul@256 | 35 | from vRecurrence import get_parameters, get_rule |
paul@213 | 36 | import email.utils |
paul@213 | 37 | |
paul@213 | 38 | try: |
paul@213 | 39 | from cStringIO import StringIO |
paul@213 | 40 | except ImportError: |
paul@213 | 41 | from StringIO import StringIO |
paul@213 | 42 | |
paul@213 | 43 | class Object: |
paul@213 | 44 | |
paul@213 | 45 | "Access to calendar structures." |
paul@213 | 46 | |
paul@1276 | 47 | def __init__(self, fragment, tzid=None): |
paul@1123 | 48 | |
paul@1123 | 49 | """ |
paul@1276 | 50 | Initialise the object with the given 'fragment'. The optional 'tzid' |
paul@1276 | 51 | sets the fallback time zone used to convert datetimes without time zone |
paul@1276 | 52 | information. |
paul@1276 | 53 | |
paul@1276 | 54 | The 'fragment' must be a dictionary mapping an object type (such as |
paul@1276 | 55 | "VEVENT") to a tuple containing the object details and attributes, |
paul@1276 | 56 | each being a dictionary itself. |
paul@1123 | 57 | |
paul@1123 | 58 | The result of parse_object can be processed to obtain a fragment by |
paul@1123 | 59 | obtaining a collection of records for an object type. For example: |
paul@1123 | 60 | |
paul@1123 | 61 | l = parse_object(f, encoding, "VCALENDAR") |
paul@1123 | 62 | events = l["VEVENT"] |
paul@1123 | 63 | event = events[0] |
paul@1123 | 64 | |
paul@1123 | 65 | Then, the specific object must be presented as follows: |
paul@1123 | 66 | |
paul@1123 | 67 | object = Object({"VEVENT" : event}) |
paul@1204 | 68 | |
paul@1275 | 69 | A separately-stored, individual object can be obtained as follows: |
paul@1275 | 70 | |
paul@1275 | 71 | object = Object(parse_object(f, encoding)) |
paul@1275 | 72 | |
paul@1204 | 73 | A convienience function is also provided to initialise objects: |
paul@1204 | 74 | |
paul@1204 | 75 | object = new_object("VEVENT") |
paul@1123 | 76 | """ |
paul@1123 | 77 | |
paul@213 | 78 | self.objtype, (self.details, self.attr) = fragment.items()[0] |
paul@1276 | 79 | self.set_tzid(tzid) |
paul@1276 | 80 | |
paul@1322 | 81 | # Modify the object with separate recurrences. |
paul@1322 | 82 | |
paul@1322 | 83 | self.modifying = [] |
paul@1322 | 84 | self.cancelling = [] |
paul@1322 | 85 | |
paul@1276 | 86 | def set_tzid(self, tzid): |
paul@1276 | 87 | |
paul@1276 | 88 | """ |
paul@1276 | 89 | Set the fallback 'tzid' for interpreting datetimes without time zone |
paul@1276 | 90 | information. |
paul@1276 | 91 | """ |
paul@1276 | 92 | |
paul@1276 | 93 | self.tzid = tzid |
paul@213 | 94 | |
paul@1322 | 95 | def set_modifying(self, modifying): |
paul@1322 | 96 | |
paul@1322 | 97 | """ |
paul@1322 | 98 | Set the 'modifying' objects affecting the periods provided by this |
paul@1322 | 99 | object. Such modifications can only be performed on a parent object, not |
paul@1322 | 100 | a specific recurrence object. |
paul@1322 | 101 | """ |
paul@1322 | 102 | |
paul@1322 | 103 | if not self.get_recurrenceid(): |
paul@1322 | 104 | self.modifying = modifying |
paul@1322 | 105 | |
paul@1322 | 106 | def set_cancelling(self, cancelling): |
paul@1322 | 107 | |
paul@1322 | 108 | """ |
paul@1322 | 109 | Set the 'cancelling' objects affecting the periods provided by this |
paul@1322 | 110 | object. Such cancellations can only be performed on a parent object, not |
paul@1322 | 111 | a specific recurrence object. |
paul@1322 | 112 | """ |
paul@1322 | 113 | |
paul@1322 | 114 | if not self.get_recurrenceid(): |
paul@1322 | 115 | self.cancelling = cancelling |
paul@1322 | 116 | |
paul@1322 | 117 | # Basic object identification. |
paul@213 | 118 | |
paul@535 | 119 | def get_uid(self): |
paul@1322 | 120 | |
paul@1322 | 121 | "Return the universal identifier." |
paul@1322 | 122 | |
paul@535 | 123 | return self.get_value("UID") |
paul@535 | 124 | |
paul@535 | 125 | def get_recurrenceid(self): |
paul@563 | 126 | |
paul@563 | 127 | """ |
paul@563 | 128 | Return the recurrence identifier, normalised to a UTC datetime if |
paul@627 | 129 | specified as a datetime or date with accompanying time zone information, |
paul@627 | 130 | maintained as a date or floating datetime otherwise. If no recurrence |
paul@627 | 131 | identifier is present, None is returned. |
paul@627 | 132 | |
paul@627 | 133 | Note that this normalised form of the identifier may well not be the |
paul@627 | 134 | same as the originally-specified identifier because that could have been |
paul@627 | 135 | specified using an accompanying TZID attribute, whereas the normalised |
paul@627 | 136 | form is effectively a converted datetime value. |
paul@563 | 137 | """ |
paul@563 | 138 | |
paul@627 | 139 | if not self.has_key("RECURRENCE-ID"): |
paul@627 | 140 | return None |
paul@1322 | 141 | |
paul@627 | 142 | dt, attr = self.get_datetime_item("RECURRENCE-ID") |
paul@628 | 143 | |
paul@628 | 144 | # Coerce any date to a UTC datetime if TZID was specified. |
paul@628 | 145 | |
paul@627 | 146 | tzid = attr.get("TZID") |
paul@627 | 147 | if tzid: |
paul@627 | 148 | dt = to_timezone(to_datetime(dt, tzid), "UTC") |
paul@1322 | 149 | |
paul@627 | 150 | return format_datetime(dt) |
paul@627 | 151 | |
paul@1276 | 152 | def get_recurrence_start_point(self, recurrenceid): |
paul@627 | 153 | |
paul@627 | 154 | """ |
paul@627 | 155 | Return the start point corresponding to the given 'recurrenceid', using |
paul@1276 | 156 | the fallback time zone to define the specific point in time referenced |
paul@1276 | 157 | by the recurrence identifier if the identifier has a date |
paul@1276 | 158 | representation. |
paul@627 | 159 | |
paul@627 | 160 | If 'recurrenceid' is given as None, this object's recurrence identifier |
paul@627 | 161 | is used to obtain a start point, but if this object does not provide a |
paul@627 | 162 | recurrence, None is returned. |
paul@627 | 163 | |
paul@627 | 164 | A start point is typically used to match free/busy periods which are |
paul@627 | 165 | themselves defined in terms of UTC datetimes. |
paul@627 | 166 | """ |
paul@627 | 167 | |
paul@627 | 168 | recurrenceid = recurrenceid or self.get_recurrenceid() |
paul@627 | 169 | if recurrenceid: |
paul@1276 | 170 | return get_recurrence_start_point(recurrenceid, self.tzid) |
paul@627 | 171 | else: |
paul@627 | 172 | return None |
paul@535 | 173 | |
paul@1276 | 174 | def get_recurrence_start_points(self, recurrenceids): |
paul@1275 | 175 | |
paul@1275 | 176 | """ |
paul@1276 | 177 | Return start points for 'recurrenceids' using the fallback time zone for |
paul@1275 | 178 | identifiers with date representations. |
paul@1275 | 179 | """ |
paul@1275 | 180 | |
paul@1276 | 181 | return map(self.get_recurrence_start_point, recurrenceids) |
paul@679 | 182 | |
paul@535 | 183 | # Structure access. |
paul@535 | 184 | |
paul@1204 | 185 | def add(self, obj): |
paul@1204 | 186 | |
paul@1204 | 187 | "Add 'obj' to the structure." |
paul@1204 | 188 | |
paul@1204 | 189 | name = obj.objtype |
paul@1204 | 190 | if not self.details.has_key(name): |
paul@1204 | 191 | l = self.details[name] = [] |
paul@1204 | 192 | else: |
paul@1204 | 193 | l = self.details[name] |
paul@1204 | 194 | l.append((obj.details, obj.attr)) |
paul@1204 | 195 | |
paul@524 | 196 | def copy(self): |
paul@1276 | 197 | return Object(self.to_dict(), self.tzid) |
paul@524 | 198 | |
paul@1336 | 199 | # Access to (value, attributes) items. |
paul@1336 | 200 | |
paul@213 | 201 | def get_items(self, name, all=True): |
paul@213 | 202 | return get_items(self.details, name, all) |
paul@213 | 203 | |
paul@213 | 204 | def get_item(self, name): |
paul@213 | 205 | return get_item(self.details, name) |
paul@213 | 206 | |
paul@1336 | 207 | # Access to mappings. |
paul@1336 | 208 | |
paul@213 | 209 | def get_value_map(self, name): |
paul@213 | 210 | return get_value_map(self.details, name) |
paul@213 | 211 | |
paul@1336 | 212 | # Access to mapped values. |
paul@1336 | 213 | |
paul@213 | 214 | def get_values(self, name, all=True): |
paul@213 | 215 | return get_values(self.details, name, all) |
paul@213 | 216 | |
paul@213 | 217 | def get_value(self, name): |
paul@213 | 218 | return get_value(self.details, name) |
paul@213 | 219 | |
paul@1298 | 220 | def set_value(self, name, value, attr=None): |
paul@1298 | 221 | self.details[name] = [(value, attr or {})] |
paul@213 | 222 | |
paul@1336 | 223 | # Convenience methods asserting URI values. |
paul@1336 | 224 | |
paul@1336 | 225 | def get_uri_items(self, name, all=True): |
paul@1336 | 226 | return uri_items(self.get_items(name, all)) |
paul@1336 | 227 | |
paul@1336 | 228 | def get_uri_item(self, name): |
paul@1336 | 229 | return uri_item(self.get_item(name)) |
paul@1336 | 230 | |
paul@1336 | 231 | def get_uri_map(self, name): |
paul@1336 | 232 | return uri_dict(self.get_value_map(name)) |
paul@1336 | 233 | |
paul@1336 | 234 | def get_uri_values(self, name): |
paul@1336 | 235 | return uri_values(self.get_values(name)) |
paul@1336 | 236 | |
paul@1336 | 237 | def get_uri_value(self, name): |
paul@1336 | 238 | return uri_value(self.get_value(name)) |
paul@1336 | 239 | |
paul@1336 | 240 | get_uri = get_uri_value |
paul@1336 | 241 | |
paul@1336 | 242 | # Access to details as temporal objects. |
paul@1336 | 243 | |
paul@1276 | 244 | def get_utc_datetime(self, name): |
paul@1276 | 245 | return get_utc_datetime(self.details, name, self.tzid) |
paul@213 | 246 | |
paul@1276 | 247 | def get_date_value_items(self, name): |
paul@1276 | 248 | return get_date_value_items(self.details, name, self.tzid) |
paul@352 | 249 | |
paul@1276 | 250 | def get_date_value_item_periods(self, name): |
paul@1276 | 251 | return get_date_value_item_periods(self.details, name, |
paul@1276 | 252 | self.get_main_period().get_duration(), self.tzid) |
paul@878 | 253 | |
paul@1276 | 254 | def get_period_values(self, name): |
paul@1276 | 255 | return get_period_values(self.details, name, self.tzid) |
paul@630 | 256 | |
paul@318 | 257 | def get_datetime(self, name): |
paul@567 | 258 | t = get_datetime_item(self.details, name) |
paul@567 | 259 | if not t: return None |
paul@567 | 260 | dt, attr = t |
paul@318 | 261 | return dt |
paul@318 | 262 | |
paul@289 | 263 | def get_datetime_item(self, name): |
paul@289 | 264 | return get_datetime_item(self.details, name) |
paul@289 | 265 | |
paul@392 | 266 | def get_duration(self, name): |
paul@392 | 267 | return get_duration(self.get_value(name)) |
paul@392 | 268 | |
paul@1174 | 269 | # Serialisation. |
paul@1174 | 270 | |
paul@1204 | 271 | def to_dict(self): |
paul@1204 | 272 | return to_dict(self.to_node()) |
paul@1204 | 273 | |
paul@213 | 274 | def to_node(self): |
paul@213 | 275 | return to_node({self.objtype : [(self.details, self.attr)]}) |
paul@213 | 276 | |
paul@1174 | 277 | def to_part(self, method, encoding="utf-8", line_length=None): |
paul@1174 | 278 | return to_part(method, [self.to_node()], encoding, line_length) |
paul@213 | 279 | |
paul@1174 | 280 | def to_string(self, encoding="utf-8", line_length=None): |
paul@1174 | 281 | return to_string(self.to_node(), encoding, line_length) |
paul@1081 | 282 | |
paul@213 | 283 | # Direct access to the structure. |
paul@213 | 284 | |
paul@392 | 285 | def has_key(self, name): |
paul@392 | 286 | return self.details.has_key(name) |
paul@392 | 287 | |
paul@524 | 288 | def get(self, name): |
paul@524 | 289 | return self.details.get(name) |
paul@524 | 290 | |
paul@734 | 291 | def keys(self): |
paul@734 | 292 | return self.details.keys() |
paul@734 | 293 | |
paul@213 | 294 | def __getitem__(self, name): |
paul@213 | 295 | return self.details[name] |
paul@213 | 296 | |
paul@213 | 297 | def __setitem__(self, name, value): |
paul@213 | 298 | self.details[name] = value |
paul@213 | 299 | |
paul@213 | 300 | def __delitem__(self, name): |
paul@213 | 301 | del self.details[name] |
paul@213 | 302 | |
paul@524 | 303 | def remove(self, name): |
paul@524 | 304 | try: |
paul@524 | 305 | del self[name] |
paul@524 | 306 | except KeyError: |
paul@524 | 307 | pass |
paul@524 | 308 | |
paul@524 | 309 | def remove_all(self, names): |
paul@524 | 310 | for name in names: |
paul@524 | 311 | self.remove(name) |
paul@524 | 312 | |
paul@734 | 313 | def preserve(self, names): |
paul@734 | 314 | for name in self.keys(): |
paul@734 | 315 | if not name in names: |
paul@734 | 316 | self.remove(name) |
paul@734 | 317 | |
paul@256 | 318 | # Computed results. |
paul@256 | 319 | |
paul@1276 | 320 | def get_main_period(self): |
paul@797 | 321 | |
paul@797 | 322 | """ |
paul@797 | 323 | Return a period object corresponding to the main start-end period for |
paul@797 | 324 | the object. |
paul@797 | 325 | """ |
paul@797 | 326 | |
paul@879 | 327 | (dtstart, dtstart_attr), (dtend, dtend_attr) = self.get_main_period_items() |
paul@1276 | 328 | tzid = get_tzid(dtstart_attr, dtend_attr) or self.tzid |
paul@797 | 329 | return RecurringPeriod(dtstart, dtend, tzid, "DTSTART", dtstart_attr, dtend_attr) |
paul@797 | 330 | |
paul@879 | 331 | def get_main_period_items(self): |
paul@650 | 332 | |
paul@650 | 333 | """ |
paul@650 | 334 | Return two (value, attributes) items corresponding to the main start-end |
paul@650 | 335 | period for the object. |
paul@650 | 336 | """ |
paul@650 | 337 | |
paul@650 | 338 | dtstart, dtstart_attr = self.get_datetime_item("DTSTART") |
paul@650 | 339 | |
paul@650 | 340 | if self.has_key("DTEND"): |
paul@650 | 341 | dtend, dtend_attr = self.get_datetime_item("DTEND") |
paul@650 | 342 | elif self.has_key("DURATION"): |
paul@650 | 343 | duration = self.get_duration("DURATION") |
paul@650 | 344 | dtend = dtstart + duration |
paul@650 | 345 | dtend_attr = dtstart_attr |
paul@650 | 346 | else: |
paul@650 | 347 | dtend, dtend_attr = dtstart, dtstart_attr |
paul@650 | 348 | |
paul@650 | 349 | return (dtstart, dtstart_attr), (dtend, dtend_attr) |
paul@650 | 350 | |
paul@1276 | 351 | def get_periods(self, start=None, end=None, inclusive=False): |
paul@620 | 352 | |
paul@620 | 353 | """ |
paul@1276 | 354 | Return periods defined by this object, employing the fallback time zone |
paul@1276 | 355 | where no time zone information is defined, and limiting the collection |
paul@1276 | 356 | to a window of time with the given 'start' and 'end'. |
paul@630 | 357 | |
paul@630 | 358 | If 'end' is omitted, only explicit recurrences and recurrences from |
paul@630 | 359 | explicitly-terminated rules will be returned. |
paul@1123 | 360 | |
paul@1123 | 361 | If 'inclusive' is set to a true value, any period occurring at the 'end' |
paul@1123 | 362 | will be included. |
paul@1123 | 363 | """ |
paul@1123 | 364 | |
paul@1276 | 365 | return get_periods(self, start, end, inclusive) |
paul@1123 | 366 | |
paul@1276 | 367 | def has_period(self, period): |
paul@1123 | 368 | |
paul@1123 | 369 | """ |
paul@1276 | 370 | Return whether this object, employing the fallback time zone where no |
paul@1276 | 371 | time zone information is defined, has the given 'period'. |
paul@620 | 372 | """ |
paul@620 | 373 | |
paul@1276 | 374 | return period in self.get_periods(end=period.get_start_point(), inclusive=True) |
paul@1123 | 375 | |
paul@1276 | 376 | def has_recurrence(self, recurrenceid): |
paul@1123 | 377 | |
paul@1123 | 378 | """ |
paul@1276 | 379 | Return whether this object, employing the fallback time zone where no |
paul@1276 | 380 | time zone information is defined, has the given 'recurrenceid'. |
paul@1123 | 381 | """ |
paul@1123 | 382 | |
paul@1276 | 383 | start_point = self.get_recurrence_start_point(recurrenceid) |
paul@1276 | 384 | |
paul@1276 | 385 | for p in self.get_periods(end=start_point, inclusive=True): |
paul@1123 | 386 | if p.get_start_point() == start_point: |
paul@1123 | 387 | return True |
paul@1276 | 388 | |
paul@1123 | 389 | return False |
paul@360 | 390 | |
paul@1322 | 391 | def get_updated_periods(self, start=None, end=None): |
paul@630 | 392 | |
paul@630 | 393 | """ |
paul@1322 | 394 | Return pairs of periods specified by this object and any modifying or |
paul@1322 | 395 | cancelling objects, providing correspondences between the original |
paul@1322 | 396 | period definitions and those applying after modifications and |
paul@1322 | 397 | cancellations have been considered. |
paul@1322 | 398 | |
paul@1322 | 399 | The fallback time zone is used to convert floating dates and datetimes, |
paul@1322 | 400 | and 'start' and 'end' respectively indicate the start and end of any |
paul@1322 | 401 | time window within which periods are considered. |
paul@630 | 402 | """ |
paul@630 | 403 | |
paul@630 | 404 | # Specific recurrences yield all specified periods. |
paul@630 | 405 | |
paul@1322 | 406 | original = self.get_periods(start, end) |
paul@630 | 407 | |
paul@630 | 408 | if self.get_recurrenceid(): |
paul@1329 | 409 | updated = [] |
paul@1329 | 410 | for p in original: |
paul@1329 | 411 | updated.append((p, p)) |
paul@1329 | 412 | return updated |
paul@630 | 413 | |
paul@630 | 414 | # Parent objects need to have their periods tested against redefined |
paul@630 | 415 | # recurrences. |
paul@630 | 416 | |
paul@1322 | 417 | modified = {} |
paul@1322 | 418 | |
paul@1322 | 419 | for obj in self.modifying: |
paul@1322 | 420 | periods = obj.get_periods(start, end) |
paul@1322 | 421 | if periods: |
paul@1322 | 422 | modified[obj.get_recurrenceid()] = periods[0] |
paul@1322 | 423 | |
paul@1322 | 424 | cancelled = set() |
paul@1322 | 425 | |
paul@1322 | 426 | for obj in self.cancelling: |
paul@1322 | 427 | cancelled.add(obj.get_recurrenceid()) |
paul@1322 | 428 | |
paul@1322 | 429 | updated = [] |
paul@1322 | 430 | |
paul@1322 | 431 | for p in original: |
paul@1322 | 432 | recurrenceid = p.is_replaced(modified.keys()) |
paul@1322 | 433 | |
paul@1322 | 434 | # Produce an original-to-modified correspondence, setting the origin |
paul@1322 | 435 | # to distinguish the period from the main period. |
paul@1322 | 436 | |
paul@1322 | 437 | if recurrenceid: |
paul@1322 | 438 | mp = modified.get(recurrenceid) |
paul@1322 | 439 | if mp.origin == "DTSTART" and p.origin != "DTSTART": |
paul@1322 | 440 | mp.origin = "DTSTART-RECUR" |
paul@1322 | 441 | updated.append((p, mp)) |
paul@1327 | 442 | continue |
paul@1322 | 443 | |
paul@1322 | 444 | # Produce an original-to-null correspondence where cancellation has |
paul@1322 | 445 | # occurred. |
paul@1322 | 446 | |
paul@1322 | 447 | recurrenceid = p.is_replaced(cancelled) |
paul@1322 | 448 | |
paul@1322 | 449 | if recurrenceid: |
paul@1322 | 450 | updated.append((p, None)) |
paul@1327 | 451 | continue |
paul@1322 | 452 | |
paul@1322 | 453 | # Produce an identity correspondence where no modification or |
paul@1322 | 454 | # cancellation has occurred. |
paul@1322 | 455 | |
paul@1322 | 456 | updated.append((p, p)) |
paul@1322 | 457 | |
paul@1322 | 458 | return updated |
paul@1322 | 459 | |
paul@1322 | 460 | def get_active_periods(self, start=None, end=None): |
paul@1322 | 461 | |
paul@1322 | 462 | """ |
paul@1322 | 463 | Return all periods specified by this object that are not replaced by |
paul@1322 | 464 | those defined by modifying or cancelling objects, using the fallback |
paul@1322 | 465 | time zone to convert floating dates and datetimes, and using 'start' and |
paul@1322 | 466 | 'end' to respectively indicate the start and end of the time window |
paul@1322 | 467 | within which periods are considered. |
paul@1322 | 468 | """ |
paul@1322 | 469 | |
paul@630 | 470 | active = [] |
paul@630 | 471 | |
paul@1322 | 472 | for old, new in self.get_updated_periods(start, end): |
paul@1322 | 473 | if new: |
paul@1322 | 474 | active.append(new) |
paul@630 | 475 | |
paul@630 | 476 | return active |
paul@630 | 477 | |
paul@648 | 478 | def get_freebusy_period(self, period, only_organiser=False): |
paul@648 | 479 | |
paul@648 | 480 | """ |
paul@648 | 481 | Return a free/busy period for the given 'period' provided by this |
paul@648 | 482 | object, using the 'only_organiser' status to produce a suitable |
paul@648 | 483 | transparency value. |
paul@648 | 484 | """ |
paul@648 | 485 | |
paul@648 | 486 | return FreeBusyPeriod( |
paul@648 | 487 | period.get_start_point(), |
paul@648 | 488 | period.get_end_point(), |
paul@648 | 489 | self.get_value("UID"), |
paul@648 | 490 | only_organiser and "ORG" or self.get_value("TRANSP") or "OPAQUE", |
paul@648 | 491 | self.get_recurrenceid(), |
paul@648 | 492 | self.get_value("SUMMARY"), |
paul@1336 | 493 | self.get_uri("ORGANIZER") |
paul@648 | 494 | ) |
paul@648 | 495 | |
paul@648 | 496 | def get_participation_status(self, participant): |
paul@648 | 497 | |
paul@648 | 498 | """ |
paul@648 | 499 | Return the participation status of the given 'participant', with the |
paul@648 | 500 | special value "ORG" indicating organiser-only participation. |
paul@648 | 501 | """ |
paul@648 | 502 | |
paul@1336 | 503 | attendees = self.get_uri_map("ATTENDEE") |
paul@1336 | 504 | organiser = self.get_uri("ORGANIZER") |
paul@648 | 505 | |
paul@692 | 506 | attendee_attr = attendees.get(participant) |
paul@692 | 507 | if attendee_attr: |
paul@692 | 508 | return attendee_attr.get("PARTSTAT", "NEEDS-ACTION") |
paul@692 | 509 | elif organiser == participant: |
paul@692 | 510 | return "ORG" |
paul@648 | 511 | |
paul@648 | 512 | return None |
paul@648 | 513 | |
paul@648 | 514 | def get_participation(self, partstat, include_needs_action=False): |
paul@648 | 515 | |
paul@648 | 516 | """ |
paul@648 | 517 | Return whether 'partstat' indicates some kind of participation in an |
paul@648 | 518 | event. If 'include_needs_action' is specified as a true value, events |
paul@648 | 519 | not yet responded to will be treated as events with tentative |
paul@648 | 520 | participation. |
paul@648 | 521 | """ |
paul@648 | 522 | |
paul@648 | 523 | return not partstat in ("DECLINED", "DELEGATED", "NEEDS-ACTION") or \ |
paul@648 | 524 | include_needs_action and partstat == "NEEDS-ACTION" or \ |
paul@648 | 525 | partstat == "ORG" |
paul@648 | 526 | |
paul@422 | 527 | def get_tzid(self): |
paul@562 | 528 | |
paul@562 | 529 | """ |
paul@562 | 530 | Return a time zone identifier used by the start or end datetimes, |
paul@1276 | 531 | potentially suitable for converting dates to datetimes. Where no |
paul@1276 | 532 | identifier is associated with the datetimes, provide any fallback time |
paul@1276 | 533 | zone identifier. |
paul@562 | 534 | """ |
paul@562 | 535 | |
paul@1276 | 536 | (dtstart, dtstart_attr), (dtend, dtend_attr) = self.get_main_period_items() |
paul@1276 | 537 | return get_tzid(dtstart_attr, dtend_attr) or self.tzid |
paul@422 | 538 | |
paul@619 | 539 | def is_shared(self): |
paul@619 | 540 | |
paul@619 | 541 | """ |
paul@619 | 542 | Return whether this object is shared based on the presence of a SEQUENCE |
paul@619 | 543 | property. |
paul@619 | 544 | """ |
paul@619 | 545 | |
paul@619 | 546 | return self.get_value("SEQUENCE") is not None |
paul@619 | 547 | |
paul@1276 | 548 | def possibly_active_from(self, dt): |
paul@650 | 549 | |
paul@650 | 550 | """ |
paul@650 | 551 | Return whether the object is possibly active from or after the given |
paul@1276 | 552 | datetime 'dt' using the fallback time zone to convert any dates or |
paul@1276 | 553 | floating datetimes. |
paul@650 | 554 | """ |
paul@650 | 555 | |
paul@1276 | 556 | dt = to_datetime(dt, self.tzid) |
paul@1276 | 557 | periods = self.get_periods() |
paul@650 | 558 | |
paul@650 | 559 | for p in periods: |
paul@650 | 560 | if p.get_end_point() > dt: |
paul@650 | 561 | return True |
paul@650 | 562 | |
paul@672 | 563 | return self.possibly_recurring_indefinitely() |
paul@672 | 564 | |
paul@672 | 565 | def possibly_recurring_indefinitely(self): |
paul@672 | 566 | |
paul@672 | 567 | "Return whether this object may recur indefinitely." |
paul@672 | 568 | |
paul@650 | 569 | rrule = self.get_value("RRULE") |
paul@650 | 570 | parameters = rrule and get_parameters(rrule) |
paul@650 | 571 | until = parameters and parameters.get("UNTIL") |
paul@651 | 572 | count = parameters and parameters.get("COUNT") |
paul@650 | 573 | |
paul@672 | 574 | # Non-recurring periods or constrained recurrences. |
paul@651 | 575 | |
paul@651 | 576 | if not rrule or until or count: |
paul@650 | 577 | return False |
paul@651 | 578 | |
paul@672 | 579 | # Unconstrained recurring periods will always lie beyond any specified |
paul@651 | 580 | # datetime. |
paul@651 | 581 | |
paul@651 | 582 | else: |
paul@650 | 583 | return True |
paul@650 | 584 | |
paul@627 | 585 | # Modification methods. |
paul@627 | 586 | |
paul@1276 | 587 | def set_datetime(self, name, dt): |
paul@627 | 588 | |
paul@627 | 589 | """ |
paul@1276 | 590 | Set a datetime for property 'name' using 'dt' and the fallback time zone |
paul@1276 | 591 | where necessary, returning whether an update has occurred. |
paul@627 | 592 | """ |
paul@627 | 593 | |
paul@627 | 594 | if dt: |
paul@627 | 595 | old_value = self.get_value(name) |
paul@1276 | 596 | self[name] = [get_item_from_datetime(dt, self.tzid)] |
paul@627 | 597 | return format_datetime(dt) != old_value |
paul@627 | 598 | |
paul@627 | 599 | return False |
paul@627 | 600 | |
paul@627 | 601 | def set_period(self, period): |
paul@627 | 602 | |
paul@627 | 603 | "Set the given 'period' as the main start and end." |
paul@627 | 604 | |
paul@627 | 605 | result = self.set_datetime("DTSTART", period.get_start()) |
paul@627 | 606 | result = self.set_datetime("DTEND", period.get_end()) or result |
paul@661 | 607 | if self.has_key("DURATION"): |
paul@661 | 608 | del self["DURATION"] |
paul@661 | 609 | |
paul@627 | 610 | return result |
paul@627 | 611 | |
paul@627 | 612 | def set_periods(self, periods): |
paul@627 | 613 | |
paul@627 | 614 | """ |
paul@627 | 615 | Set the given 'periods' as recurrence date properties, replacing the |
paul@627 | 616 | previous RDATE properties and ignoring any RRULE properties. |
paul@627 | 617 | """ |
paul@627 | 618 | |
paul@880 | 619 | old_values = set(self.get_date_value_item_periods("RDATE") or []) |
paul@627 | 620 | new_rdates = [] |
paul@627 | 621 | |
paul@627 | 622 | if self.has_key("RDATE"): |
paul@627 | 623 | del self["RDATE"] |
paul@627 | 624 | |
paul@812 | 625 | main_changed = False |
paul@812 | 626 | |
paul@627 | 627 | for p in periods: |
paul@1291 | 628 | if p.origin == "DTSTART": |
paul@1291 | 629 | main_changed = self.set_period(p) |
paul@1291 | 630 | elif p.origin != "RRULE" and p != self.get_main_period(): |
paul@627 | 631 | new_rdates.append(get_period_item(p.get_start(), p.get_end())) |
paul@627 | 632 | |
paul@661 | 633 | if new_rdates: |
paul@661 | 634 | self["RDATE"] = new_rdates |
paul@661 | 635 | |
paul@880 | 636 | return main_changed or old_values != set(self.get_date_value_item_periods("RDATE") or []) |
paul@661 | 637 | |
paul@872 | 638 | def set_rule(self, rule): |
paul@872 | 639 | |
paul@872 | 640 | """ |
paul@872 | 641 | Set the given 'rule' in this object, replacing the previous RRULE |
paul@872 | 642 | property, returning whether the object has changed. The provided 'rule' |
paul@872 | 643 | must be an item. |
paul@872 | 644 | """ |
paul@872 | 645 | |
paul@872 | 646 | if not rule: |
paul@872 | 647 | return False |
paul@872 | 648 | |
paul@872 | 649 | old_rrule = self.get_item("RRULE") |
paul@872 | 650 | self["RRULE"] = [rule] |
paul@872 | 651 | return old_rrule != rule |
paul@872 | 652 | |
paul@872 | 653 | def set_exceptions(self, exceptions): |
paul@872 | 654 | |
paul@872 | 655 | """ |
paul@872 | 656 | Set the given 'exceptions' in this object, replacing the previous EXDATE |
paul@872 | 657 | properties, returning whether the object has changed. The provided |
paul@872 | 658 | 'exceptions' must be a collection of items. |
paul@872 | 659 | """ |
paul@872 | 660 | |
paul@880 | 661 | old_exdates = set(self.get_date_value_item_periods("EXDATE") or []) |
paul@872 | 662 | if exceptions: |
paul@872 | 663 | self["EXDATE"] = exceptions |
paul@880 | 664 | return old_exdates != set(self.get_date_value_item_periods("EXDATE") or []) |
paul@872 | 665 | elif old_exdates: |
paul@872 | 666 | del self["EXDATE"] |
paul@872 | 667 | return True |
paul@872 | 668 | else: |
paul@872 | 669 | return False |
paul@872 | 670 | |
paul@809 | 671 | def update_dtstamp(self): |
paul@809 | 672 | |
paul@809 | 673 | "Update the DTSTAMP in the object." |
paul@809 | 674 | |
paul@809 | 675 | dtstamp = self.get_utc_datetime("DTSTAMP") |
paul@809 | 676 | utcnow = get_time() |
paul@809 | 677 | dtstamp = format_datetime(dtstamp and dtstamp > utcnow and dtstamp or utcnow) |
paul@809 | 678 | self["DTSTAMP"] = [(dtstamp, {})] |
paul@809 | 679 | return dtstamp |
paul@809 | 680 | |
paul@809 | 681 | def update_sequence(self, increment=False): |
paul@809 | 682 | |
paul@809 | 683 | "Set or update the SEQUENCE in the object." |
paul@809 | 684 | |
paul@809 | 685 | sequence = self.get_value("SEQUENCE") or "0" |
paul@809 | 686 | self["SEQUENCE"] = [(str(int(sequence) + (increment and 1 or 0)), {})] |
paul@809 | 687 | return sequence |
paul@809 | 688 | |
paul@878 | 689 | def update_exceptions(self, excluded, asserted): |
paul@784 | 690 | |
paul@784 | 691 | """ |
paul@784 | 692 | Update the exceptions to any rule by applying the list of 'excluded' |
paul@878 | 693 | periods. Where 'asserted' periods are provided, exceptions will be |
paul@878 | 694 | removed corresponding to those periods. |
paul@784 | 695 | """ |
paul@784 | 696 | |
paul@885 | 697 | old_exdates = self.get_date_value_item_periods("EXDATE") or [] |
paul@878 | 698 | new_exdates = set(old_exdates) |
paul@878 | 699 | new_exdates.update(excluded) |
paul@878 | 700 | new_exdates.difference_update(asserted) |
paul@784 | 701 | |
paul@885 | 702 | if not new_exdates and self.has_key("EXDATE"): |
paul@878 | 703 | del self["EXDATE"] |
paul@878 | 704 | else: |
paul@784 | 705 | self["EXDATE"] = [] |
paul@878 | 706 | for p in new_exdates: |
paul@878 | 707 | self["EXDATE"].append(get_period_item(p.get_start(), p.get_end())) |
paul@784 | 708 | |
paul@878 | 709 | return set(old_exdates) != new_exdates |
paul@784 | 710 | |
paul@1276 | 711 | def correct_object(self, permitted_values): |
paul@661 | 712 | |
paul@1276 | 713 | "Correct the object's period details using the 'permitted_values'." |
paul@661 | 714 | |
paul@661 | 715 | corrected = set() |
paul@661 | 716 | rdates = [] |
paul@661 | 717 | |
paul@1276 | 718 | for period in self.get_periods(): |
paul@939 | 719 | corrected_period = period.get_corrected(permitted_values) |
paul@627 | 720 | |
paul@939 | 721 | if corrected_period is period: |
paul@661 | 722 | if period.origin == "RDATE": |
paul@661 | 723 | rdates.append(period) |
paul@661 | 724 | continue |
paul@661 | 725 | |
paul@661 | 726 | if period.origin == "DTSTART": |
paul@939 | 727 | self.set_period(corrected_period) |
paul@661 | 728 | corrected.add("DTSTART") |
paul@661 | 729 | elif period.origin == "RDATE": |
paul@939 | 730 | rdates.append(corrected_period) |
paul@661 | 731 | corrected.add("RDATE") |
paul@661 | 732 | |
paul@661 | 733 | if "RDATE" in corrected: |
paul@661 | 734 | self.set_periods(rdates) |
paul@661 | 735 | |
paul@661 | 736 | return corrected |
paul@627 | 737 | |
paul@213 | 738 | # Construction and serialisation. |
paul@213 | 739 | |
paul@213 | 740 | def make_calendar(nodes, method=None): |
paul@213 | 741 | |
paul@213 | 742 | """ |
paul@213 | 743 | Return a complete calendar node wrapping the given 'nodes' and employing the |
paul@213 | 744 | given 'method', if indicated. |
paul@213 | 745 | """ |
paul@213 | 746 | |
paul@213 | 747 | return ("VCALENDAR", {}, |
paul@213 | 748 | (method and [("METHOD", {}, method)] or []) + |
paul@213 | 749 | [("VERSION", {}, "2.0")] + |
paul@213 | 750 | nodes |
paul@213 | 751 | ) |
paul@213 | 752 | |
paul@327 | 753 | def make_freebusy(freebusy, uid, organiser, organiser_attr=None, attendee=None, |
paul@562 | 754 | attendee_attr=None, period=None): |
paul@222 | 755 | |
paul@222 | 756 | """ |
paul@222 | 757 | Return a calendar node defining the free/busy details described in the given |
paul@292 | 758 | 'freebusy' list, employing the given 'uid', for the given 'organiser' and |
paul@292 | 759 | optional 'organiser_attr', with the optional 'attendee' providing recipient |
paul@292 | 760 | details together with the optional 'attendee_attr'. |
paul@327 | 761 | |
paul@562 | 762 | The result will be constrained to the 'period' if specified. |
paul@222 | 763 | """ |
paul@222 | 764 | |
paul@222 | 765 | record = [] |
paul@222 | 766 | rwrite = record.append |
paul@222 | 767 | |
paul@292 | 768 | rwrite(("ORGANIZER", organiser_attr or {}, organiser)) |
paul@222 | 769 | |
paul@222 | 770 | if attendee: |
paul@292 | 771 | rwrite(("ATTENDEE", attendee_attr or {}, attendee)) |
paul@222 | 772 | |
paul@222 | 773 | rwrite(("UID", {}, uid)) |
paul@222 | 774 | |
paul@222 | 775 | if freebusy: |
paul@327 | 776 | |
paul@327 | 777 | # Get a constrained view if start and end limits are specified. |
paul@327 | 778 | |
paul@563 | 779 | if period: |
paul@1189 | 780 | periods = freebusy.get_overlapping([period]) |
paul@563 | 781 | else: |
paul@563 | 782 | periods = freebusy |
paul@327 | 783 | |
paul@327 | 784 | # Write the limits of the resource. |
paul@327 | 785 | |
paul@563 | 786 | if periods: |
paul@563 | 787 | rwrite(("DTSTART", {"VALUE" : "DATE-TIME"}, format_datetime(periods[0].get_start_point()))) |
paul@563 | 788 | rwrite(("DTEND", {"VALUE" : "DATE-TIME"}, format_datetime(periods[-1].get_end_point()))) |
paul@563 | 789 | else: |
paul@563 | 790 | rwrite(("DTSTART", {"VALUE" : "DATE-TIME"}, format_datetime(period.get_start_point()))) |
paul@563 | 791 | rwrite(("DTEND", {"VALUE" : "DATE-TIME"}, format_datetime(period.get_end_point()))) |
paul@327 | 792 | |
paul@458 | 793 | for p in periods: |
paul@458 | 794 | if p.transp == "OPAQUE": |
paul@529 | 795 | rwrite(("FREEBUSY", {"FBTYPE" : "BUSY"}, "/".join( |
paul@562 | 796 | map(format_datetime, [p.get_start_point(), p.get_end_point()]) |
paul@529 | 797 | ))) |
paul@222 | 798 | |
paul@222 | 799 | return ("VFREEBUSY", {}, record) |
paul@222 | 800 | |
paul@1276 | 801 | def parse_calendar(f, encoding, tzid=None): |
paul@1269 | 802 | |
paul@1269 | 803 | """ |
paul@1269 | 804 | Parse the iTIP content from 'f' having the given 'encoding'. Return a |
paul@1276 | 805 | mapping from object types to collections of calendar objects. If 'tzid' is |
paul@1276 | 806 | specified, use it to set the fallback time zone on all returned objects. |
paul@1269 | 807 | """ |
paul@1269 | 808 | |
paul@1269 | 809 | cal = parse_object(f, encoding, "VCALENDAR") |
paul@1269 | 810 | d = {} |
paul@1269 | 811 | |
paul@1269 | 812 | for objtype, values in cal.items(): |
paul@1269 | 813 | d[objtype] = l = [] |
paul@1269 | 814 | for value in values: |
paul@1276 | 815 | l.append(Object({objtype : value}, tzid)) |
paul@1269 | 816 | |
paul@1269 | 817 | return d |
paul@1269 | 818 | |
paul@213 | 819 | def parse_object(f, encoding, objtype=None): |
paul@213 | 820 | |
paul@213 | 821 | """ |
paul@213 | 822 | Parse the iTIP content from 'f' having the given 'encoding'. If 'objtype' is |
paul@213 | 823 | given, only objects of that type will be returned. Otherwise, the root of |
paul@213 | 824 | the content will be returned as a dictionary with a single key indicating |
paul@213 | 825 | the object type. |
paul@213 | 826 | |
paul@213 | 827 | Return None if the content was not readable or suitable. |
paul@213 | 828 | """ |
paul@213 | 829 | |
paul@213 | 830 | try: |
paul@213 | 831 | try: |
paul@213 | 832 | doctype, attrs, elements = obj = parse(f, encoding=encoding) |
paul@213 | 833 | if objtype and doctype == objtype: |
paul@213 | 834 | return to_dict(obj)[objtype][0] |
paul@213 | 835 | elif not objtype: |
paul@213 | 836 | return to_dict(obj) |
paul@213 | 837 | finally: |
paul@213 | 838 | f.close() |
paul@213 | 839 | |
paul@213 | 840 | # NOTE: Handle parse errors properly. |
paul@213 | 841 | |
paul@213 | 842 | except (ParseError, ValueError): |
paul@213 | 843 | pass |
paul@213 | 844 | |
paul@213 | 845 | return None |
paul@213 | 846 | |
paul@1072 | 847 | def parse_string(s, encoding, objtype=None): |
paul@1072 | 848 | |
paul@1072 | 849 | """ |
paul@1072 | 850 | Parse the iTIP content from 's' having the given 'encoding'. If 'objtype' is |
paul@1072 | 851 | given, only objects of that type will be returned. Otherwise, the root of |
paul@1072 | 852 | the content will be returned as a dictionary with a single key indicating |
paul@1072 | 853 | the object type. |
paul@1072 | 854 | |
paul@1072 | 855 | Return None if the content was not readable or suitable. |
paul@1072 | 856 | """ |
paul@1072 | 857 | |
paul@1072 | 858 | return parse_object(StringIO(s), encoding, objtype) |
paul@1072 | 859 | |
paul@1174 | 860 | def to_part(method, fragments, encoding="utf-8", line_length=None): |
paul@213 | 861 | |
paul@213 | 862 | """ |
paul@1174 | 863 | Write using the given 'method', the given 'fragments' to a MIME |
paul@213 | 864 | text/calendar part. |
paul@213 | 865 | """ |
paul@213 | 866 | |
paul@213 | 867 | out = StringIO() |
paul@213 | 868 | try: |
paul@1174 | 869 | to_stream(out, make_calendar(fragments, method), encoding, line_length) |
paul@213 | 870 | part = MIMEText(out.getvalue(), "calendar", encoding) |
paul@213 | 871 | part.set_param("method", method) |
paul@213 | 872 | return part |
paul@213 | 873 | |
paul@213 | 874 | finally: |
paul@213 | 875 | out.close() |
paul@213 | 876 | |
paul@1174 | 877 | def to_stream(out, fragment, encoding="utf-8", line_length=None): |
paul@1084 | 878 | |
paul@1084 | 879 | "Write to the 'out' stream the given 'fragment'." |
paul@1084 | 880 | |
paul@1174 | 881 | iterwrite(out, encoding=encoding, line_length=line_length).append(fragment) |
paul@213 | 882 | |
paul@1174 | 883 | def to_string(fragment, encoding="utf-8", line_length=None): |
paul@1084 | 884 | |
paul@1084 | 885 | "Return a string encoding the given 'fragment'." |
paul@1084 | 886 | |
paul@1072 | 887 | out = StringIO() |
paul@1072 | 888 | try: |
paul@1174 | 889 | to_stream(out, fragment, encoding, line_length) |
paul@1072 | 890 | return out.getvalue() |
paul@1072 | 891 | |
paul@1072 | 892 | finally: |
paul@1072 | 893 | out.close() |
paul@1072 | 894 | |
paul@1321 | 895 | def new_object(object_type, organiser=None, organiser_attr=None, tzid=None): |
paul@1298 | 896 | |
paul@1298 | 897 | """ |
paul@1298 | 898 | Make a new object of the given 'object_type' and optional 'organiser', |
paul@1298 | 899 | with optional 'organiser_attr' describing any organiser identity in more |
paul@1321 | 900 | detail. An optional 'tzid' can also be provided. |
paul@1298 | 901 | """ |
paul@1204 | 902 | |
paul@1298 | 903 | details = {} |
paul@1204 | 904 | |
paul@1298 | 905 | if organiser: |
paul@1298 | 906 | details["UID"] = [(make_uid(organiser), {})] |
paul@1298 | 907 | details["ORGANIZER"] = [(organiser, organiser_attr or {})] |
paul@1298 | 908 | details["DTSTAMP"] = [(get_timestamp(), {})] |
paul@1298 | 909 | |
paul@1321 | 910 | return Object({object_type : (details, {})}, tzid) |
paul@1204 | 911 | |
paul@1204 | 912 | def make_uid(user): |
paul@1204 | 913 | |
paul@1204 | 914 | "Return a unique identifier for a new object by the given 'user'." |
paul@1204 | 915 | |
paul@1204 | 916 | utcnow = get_timestamp() |
paul@1204 | 917 | return "imip-agent-%s-%s" % (utcnow, get_address(user)) |
paul@1204 | 918 | |
paul@213 | 919 | # Structure access functions. |
paul@213 | 920 | |
paul@213 | 921 | def get_items(d, name, all=True): |
paul@213 | 922 | |
paul@213 | 923 | """ |
paul@213 | 924 | Get all items from 'd' for the given 'name', returning single items if |
paul@213 | 925 | 'all' is specified and set to a false value and if only one value is |
paul@213 | 926 | present for the name. Return None if no items are found for the name or if |
paul@213 | 927 | many items are found but 'all' is set to a false value. |
paul@213 | 928 | """ |
paul@213 | 929 | |
paul@213 | 930 | if d.has_key(name): |
paul@712 | 931 | items = [(value or None, attr) for value, attr in d[name]] |
paul@213 | 932 | if all: |
paul@462 | 933 | return items |
paul@462 | 934 | elif len(items) == 1: |
paul@462 | 935 | return items[0] |
paul@213 | 936 | else: |
paul@213 | 937 | return None |
paul@213 | 938 | else: |
paul@213 | 939 | return None |
paul@213 | 940 | |
paul@213 | 941 | def get_item(d, name): |
paul@213 | 942 | return get_items(d, name, False) |
paul@213 | 943 | |
paul@213 | 944 | def get_value_map(d, name): |
paul@213 | 945 | |
paul@213 | 946 | """ |
paul@213 | 947 | Return a dictionary for all items in 'd' having the given 'name'. The |
paul@213 | 948 | dictionary will map values for the name to any attributes or qualifiers |
paul@213 | 949 | that may have been present. |
paul@213 | 950 | """ |
paul@213 | 951 | |
paul@213 | 952 | items = get_items(d, name) |
paul@213 | 953 | if items: |
paul@213 | 954 | return dict(items) |
paul@213 | 955 | else: |
paul@213 | 956 | return {} |
paul@213 | 957 | |
paul@462 | 958 | def values_from_items(items): |
paul@462 | 959 | return map(lambda x: x[0], items) |
paul@462 | 960 | |
paul@213 | 961 | def get_values(d, name, all=True): |
paul@213 | 962 | if d.has_key(name): |
paul@462 | 963 | items = d[name] |
paul@462 | 964 | if not all and len(items) == 1: |
paul@462 | 965 | return items[0][0] |
paul@213 | 966 | else: |
paul@462 | 967 | return values_from_items(items) |
paul@213 | 968 | else: |
paul@213 | 969 | return None |
paul@213 | 970 | |
paul@213 | 971 | def get_value(d, name): |
paul@213 | 972 | return get_values(d, name, False) |
paul@213 | 973 | |
paul@417 | 974 | def get_date_value_items(d, name, tzid=None): |
paul@352 | 975 | |
paul@352 | 976 | """ |
paul@389 | 977 | Obtain items from 'd' having the given 'name', where a single item yields |
paul@389 | 978 | potentially many values. Return a list of tuples of the form (value, |
paul@389 | 979 | attributes) where the attributes have been given for the property in 'd'. |
paul@352 | 980 | """ |
paul@352 | 981 | |
paul@403 | 982 | items = get_items(d, name) |
paul@403 | 983 | if items: |
paul@403 | 984 | all_items = [] |
paul@403 | 985 | for item in items: |
paul@403 | 986 | values, attr = item |
paul@417 | 987 | if not attr.has_key("TZID") and tzid: |
paul@417 | 988 | attr["TZID"] = tzid |
paul@403 | 989 | if not isinstance(values, list): |
paul@403 | 990 | values = [values] |
paul@403 | 991 | for value in values: |
paul@403 | 992 | all_items.append((get_datetime(value, attr) or get_period(value, attr), attr)) |
paul@403 | 993 | return all_items |
paul@352 | 994 | else: |
paul@352 | 995 | return None |
paul@352 | 996 | |
paul@878 | 997 | def get_date_value_item_periods(d, name, duration, tzid=None): |
paul@878 | 998 | |
paul@878 | 999 | """ |
paul@878 | 1000 | Obtain items from 'd' having the given 'name', where a single item yields |
paul@878 | 1001 | potentially many values. The 'duration' must be provided to define the |
paul@878 | 1002 | length of periods having only a start datetime. Return a list of periods |
paul@878 | 1003 | corresponding to the property in 'd'. |
paul@878 | 1004 | """ |
paul@878 | 1005 | |
paul@878 | 1006 | items = get_date_value_items(d, name, tzid) |
paul@878 | 1007 | if not items: |
paul@878 | 1008 | return items |
paul@878 | 1009 | |
paul@878 | 1010 | periods = [] |
paul@878 | 1011 | |
paul@878 | 1012 | for value, attr in items: |
paul@878 | 1013 | if isinstance(value, tuple): |
paul@878 | 1014 | periods.append(RecurringPeriod(value[0], value[1], tzid, name, attr)) |
paul@878 | 1015 | else: |
paul@878 | 1016 | periods.append(RecurringPeriod(value, value + duration, tzid, name, attr)) |
paul@878 | 1017 | |
paul@878 | 1018 | return periods |
paul@878 | 1019 | |
paul@646 | 1020 | def get_period_values(d, name, tzid=None): |
paul@630 | 1021 | |
paul@630 | 1022 | """ |
paul@630 | 1023 | Return period values from 'd' for the given property 'name', using 'tzid' |
paul@646 | 1024 | where specified to indicate the time zone. |
paul@630 | 1025 | """ |
paul@630 | 1026 | |
paul@630 | 1027 | values = [] |
paul@630 | 1028 | for value, attr in get_items(d, name) or []: |
paul@630 | 1029 | if not attr.has_key("TZID") and tzid: |
paul@630 | 1030 | attr["TZID"] = tzid |
paul@630 | 1031 | start, end = get_period(value, attr) |
paul@646 | 1032 | values.append(Period(start, end, tzid=tzid)) |
paul@630 | 1033 | return values |
paul@630 | 1034 | |
paul@506 | 1035 | def get_utc_datetime(d, name, date_tzid=None): |
paul@506 | 1036 | |
paul@506 | 1037 | """ |
paul@506 | 1038 | Return the value provided by 'd' for 'name' as a datetime in the UTC zone |
paul@506 | 1039 | or as a date, converting any date to a datetime if 'date_tzid' is specified. |
paul@720 | 1040 | If no datetime or date is available, None is returned. |
paul@506 | 1041 | """ |
paul@506 | 1042 | |
paul@348 | 1043 | t = get_datetime_item(d, name) |
paul@348 | 1044 | if not t: |
paul@348 | 1045 | return None |
paul@348 | 1046 | else: |
paul@348 | 1047 | dt, attr = t |
paul@720 | 1048 | return dt is not None and to_utc_datetime(dt, date_tzid) or None |
paul@289 | 1049 | |
paul@289 | 1050 | def get_datetime_item(d, name): |
paul@562 | 1051 | |
paul@562 | 1052 | """ |
paul@562 | 1053 | Return the value provided by 'd' for 'name' as a datetime or as a date, |
paul@562 | 1054 | together with the attributes describing it. Return None if no value exists |
paul@562 | 1055 | for 'name' in 'd'. |
paul@562 | 1056 | """ |
paul@562 | 1057 | |
paul@348 | 1058 | t = get_item(d, name) |
paul@348 | 1059 | if not t: |
paul@348 | 1060 | return None |
paul@348 | 1061 | else: |
paul@348 | 1062 | value, attr = t |
paul@613 | 1063 | dt = get_datetime(value, attr) |
paul@616 | 1064 | tzid = get_datetime_tzid(dt) |
paul@616 | 1065 | if tzid: |
paul@616 | 1066 | attr["TZID"] = tzid |
paul@613 | 1067 | return dt, attr |
paul@213 | 1068 | |
paul@528 | 1069 | # Conversion functions. |
paul@528 | 1070 | |
paul@792 | 1071 | def get_address_parts(values): |
paul@792 | 1072 | |
paul@792 | 1073 | "Return name and address tuples for each of the given 'values'." |
paul@792 | 1074 | |
paul@792 | 1075 | l = [] |
paul@792 | 1076 | for name, address in values and email.utils.getaddresses(values) or []: |
paul@792 | 1077 | if is_mailto_uri(name): |
paul@792 | 1078 | name = name[7:] # strip "mailto:" |
paul@792 | 1079 | l.append((name, address)) |
paul@792 | 1080 | return l |
paul@792 | 1081 | |
paul@213 | 1082 | def get_addresses(values): |
paul@790 | 1083 | |
paul@790 | 1084 | """ |
paul@790 | 1085 | Return only addresses from the given 'values' which may be of the form |
paul@790 | 1086 | "Common Name <recipient@domain>", with the latter part being the address |
paul@790 | 1087 | itself. |
paul@790 | 1088 | """ |
paul@790 | 1089 | |
paul@792 | 1090 | return [address for name, address in get_address_parts(values)] |
paul@213 | 1091 | |
paul@213 | 1092 | def get_address(value): |
paul@790 | 1093 | |
paul@790 | 1094 | "Return an e-mail address from the given 'value'." |
paul@790 | 1095 | |
paul@712 | 1096 | if not value: return None |
paul@792 | 1097 | return get_addresses([value])[0] |
paul@792 | 1098 | |
paul@792 | 1099 | def get_verbose_address(value, attr=None): |
paul@792 | 1100 | |
paul@792 | 1101 | """ |
paul@792 | 1102 | Return a verbose e-mail address featuring any name from the given 'value' |
paul@792 | 1103 | and any accompanying 'attr' dictionary. |
paul@792 | 1104 | """ |
paul@792 | 1105 | |
paul@810 | 1106 | l = get_address_parts([value]) |
paul@810 | 1107 | if not l: |
paul@810 | 1108 | return value |
paul@810 | 1109 | name, address = l[0] |
paul@792 | 1110 | if not name: |
paul@792 | 1111 | name = attr and attr.get("CN") |
paul@792 | 1112 | if name and address: |
paul@792 | 1113 | return "%s <%s>" % (name, address) |
paul@792 | 1114 | else: |
paul@792 | 1115 | return address |
paul@792 | 1116 | |
paul@792 | 1117 | def is_mailto_uri(value): |
paul@1251 | 1118 | |
paul@1251 | 1119 | """ |
paul@1251 | 1120 | Return whether 'value' is a mailto: URI, with the protocol potentially being |
paul@1251 | 1121 | in upper case. |
paul@1251 | 1122 | """ |
paul@1251 | 1123 | |
paul@792 | 1124 | return value.lower().startswith("mailto:") |
paul@213 | 1125 | |
paul@213 | 1126 | def get_uri(value): |
paul@790 | 1127 | |
paul@790 | 1128 | "Return a URI for the given 'value'." |
paul@790 | 1129 | |
paul@712 | 1130 | if not value: return None |
paul@1251 | 1131 | |
paul@1251 | 1132 | # Normalise to "mailto:" or return other URI form. |
paul@1251 | 1133 | |
paul@792 | 1134 | return is_mailto_uri(value) and ("mailto:%s" % value[7:]) or \ |
paul@712 | 1135 | ":" in value and value or \ |
paul@790 | 1136 | "mailto:%s" % get_address(value) |
paul@213 | 1137 | |
paul@792 | 1138 | def uri_parts(values): |
paul@792 | 1139 | |
paul@792 | 1140 | "Return any common name plus the URI for each of the given 'values'." |
paul@792 | 1141 | |
paul@792 | 1142 | return [(name, get_uri(address)) for name, address in get_address_parts(values)] |
paul@792 | 1143 | |
paul@309 | 1144 | uri_value = get_uri |
paul@309 | 1145 | |
paul@309 | 1146 | def uri_values(values): |
paul@309 | 1147 | return map(get_uri, values) |
paul@309 | 1148 | |
paul@213 | 1149 | def uri_dict(d): |
paul@213 | 1150 | return dict([(get_uri(key), value) for key, value in d.items()]) |
paul@213 | 1151 | |
paul@213 | 1152 | def uri_item(item): |
paul@213 | 1153 | return get_uri(item[0]), item[1] |
paul@213 | 1154 | |
paul@213 | 1155 | def uri_items(items): |
paul@213 | 1156 | return [(get_uri(value), attr) for value, attr in items] |
paul@213 | 1157 | |
paul@220 | 1158 | # Operations on structure data. |
paul@220 | 1159 | |
paul@682 | 1160 | def is_new_object(old_sequence, new_sequence, old_dtstamp, new_dtstamp, ignore_dtstamp): |
paul@220 | 1161 | |
paul@220 | 1162 | """ |
paul@220 | 1163 | Return for the given 'old_sequence' and 'new_sequence', 'old_dtstamp' and |
paul@682 | 1164 | 'new_dtstamp', and the 'ignore_dtstamp' indication, whether the object |
paul@220 | 1165 | providing the new information is really newer than the object providing the |
paul@220 | 1166 | old information. |
paul@220 | 1167 | """ |
paul@220 | 1168 | |
paul@220 | 1169 | have_sequence = old_sequence is not None and new_sequence is not None |
paul@220 | 1170 | is_same_sequence = have_sequence and int(new_sequence) == int(old_sequence) |
paul@220 | 1171 | |
paul@220 | 1172 | have_dtstamp = old_dtstamp and new_dtstamp |
paul@220 | 1173 | is_old_dtstamp = have_dtstamp and new_dtstamp < old_dtstamp or old_dtstamp and not new_dtstamp |
paul@220 | 1174 | |
paul@220 | 1175 | is_old_sequence = have_sequence and ( |
paul@220 | 1176 | int(new_sequence) < int(old_sequence) or |
paul@220 | 1177 | is_same_sequence and is_old_dtstamp |
paul@220 | 1178 | ) |
paul@220 | 1179 | |
paul@682 | 1180 | return is_same_sequence and ignore_dtstamp or not is_old_sequence |
paul@220 | 1181 | |
paul@1176 | 1182 | def check_delegation(attendee_map, attendee, attendee_attr): |
paul@1176 | 1183 | |
paul@1176 | 1184 | """ |
paul@1176 | 1185 | Using the 'attendee_map', check the attributes for the given 'attendee' |
paul@1176 | 1186 | provided as 'attendee_attr', following the delegation chain back to the |
paul@1177 | 1187 | delegators and forward again to yield the delegate identities in each |
paul@1177 | 1188 | case. Pictorially... |
paul@1177 | 1189 | |
paul@1177 | 1190 | attendee -> DELEGATED-FROM -> delegator |
paul@1177 | 1191 | ? <- DELEGATED-TO <--- |
paul@1177 | 1192 | |
paul@1177 | 1193 | Return whether 'attendee' was identified as a delegate by providing the |
paul@1177 | 1194 | identity of any delegators referencing the attendee. |
paul@1176 | 1195 | """ |
paul@1176 | 1196 | |
paul@1177 | 1197 | delegators = [] |
paul@1177 | 1198 | |
paul@1176 | 1199 | # The recipient should have a reference to the delegator. |
paul@1176 | 1200 | |
paul@1176 | 1201 | delegated_from = attendee_attr and attendee_attr.get("DELEGATED-FROM") |
paul@1177 | 1202 | if delegated_from: |
paul@1177 | 1203 | |
paul@1177 | 1204 | # Examine all delegators. |
paul@1177 | 1205 | |
paul@1177 | 1206 | for delegator in delegated_from: |
paul@1177 | 1207 | delegator_attr = attendee_map.get(delegator) |
paul@1176 | 1208 | |
paul@1177 | 1209 | # The delegator should have a reference to the recipient. |
paul@1176 | 1210 | |
paul@1177 | 1211 | delegated_to = delegator_attr and delegator_attr.get("DELEGATED-TO") |
paul@1177 | 1212 | if delegated_to and attendee in delegated_to: |
paul@1177 | 1213 | delegators.append(delegator) |
paul@1177 | 1214 | |
paul@1177 | 1215 | return delegators |
paul@1176 | 1216 | |
paul@1276 | 1217 | def get_periods(obj, start=None, end=None, inclusive=False): |
paul@256 | 1218 | |
paul@256 | 1219 | """ |
paul@1276 | 1220 | Return periods for the given object 'obj', employing the object's fallback |
paul@1276 | 1221 | time zone where no time zone information is available (for whole day events, |
paul@1276 | 1222 | for example), confining materialised periods to after the given 'start' |
paul@1276 | 1223 | datetime and before the given 'end' datetime. |
paul@618 | 1224 | |
paul@630 | 1225 | If 'end' is omitted, only explicit recurrences and recurrences from |
paul@630 | 1226 | explicitly-terminated rules will be returned. |
paul@630 | 1227 | |
paul@630 | 1228 | If 'inclusive' is set to a true value, any period occurring at the 'end' |
paul@630 | 1229 | will be included. |
paul@256 | 1230 | """ |
paul@256 | 1231 | |
paul@1276 | 1232 | tzid = obj.get_tzid() |
paul@318 | 1233 | rrule = obj.get_value("RRULE") |
paul@636 | 1234 | parameters = rrule and get_parameters(rrule) |
paul@318 | 1235 | |
paul@318 | 1236 | # Use localised datetimes. |
paul@318 | 1237 | |
paul@1276 | 1238 | main_period = obj.get_main_period() |
paul@797 | 1239 | |
paul@797 | 1240 | dtstart = main_period.get_start() |
paul@797 | 1241 | dtstart_attr = main_period.get_start_attr() |
paul@256 | 1242 | |
paul@618 | 1243 | # Attempt to get time zone details from the object, using the supplied zone |
paul@618 | 1244 | # only as a fallback. |
paul@618 | 1245 | |
paul@638 | 1246 | obj_tzid = obj.get_tzid() |
paul@256 | 1247 | |
paul@352 | 1248 | if not rrule: |
paul@797 | 1249 | periods = [main_period] |
paul@630 | 1250 | |
paul@636 | 1251 | elif end or parameters and parameters.has_key("UNTIL") or parameters.has_key("COUNT"): |
paul@630 | 1252 | |
paul@352 | 1253 | # Recurrence rules create multiple instances to be checked. |
paul@352 | 1254 | # Conflicts may only be assessed within a period defined by policy |
paul@352 | 1255 | # for the agent, with instances outside that period being considered |
paul@352 | 1256 | # unchecked. |
paul@352 | 1257 | |
paul@352 | 1258 | selector = get_rule(dtstart, rrule) |
paul@352 | 1259 | periods = [] |
paul@352 | 1260 | |
paul@521 | 1261 | until = parameters.get("UNTIL") |
paul@521 | 1262 | if until: |
paul@650 | 1263 | until_dt = to_timezone(get_datetime(until, dtstart_attr), obj_tzid) |
paul@650 | 1264 | end = end and min(until_dt, end) or until_dt |
paul@521 | 1265 | inclusive = True |
paul@521 | 1266 | |
paul@1238 | 1267 | # Define a selection period with a start point. The end will be handled |
paul@1238 | 1268 | # in the materialisation process below. |
paul@1238 | 1269 | |
paul@1238 | 1270 | selection_period = Period(start, None) |
paul@1238 | 1271 | |
paul@1238 | 1272 | # Obtain period instances, starting from the main period. Since counting |
paul@1238 | 1273 | # must start from the first period, filtering from a start date must be |
paul@1238 | 1274 | # done after the instances have been obtained. |
paul@1238 | 1275 | |
paul@1317 | 1276 | for recurrence_start in selector.materialise(dtstart, end, |
paul@1317 | 1277 | parameters.get("COUNT"), parameters.get("BYSETPOS"), inclusive): |
paul@1238 | 1278 | |
paul@1238 | 1279 | # Determine the resolution of the period. |
paul@1238 | 1280 | |
paul@630 | 1281 | create = len(recurrence_start) == 3 and date or datetime |
paul@638 | 1282 | recurrence_start = to_timezone(create(*recurrence_start), obj_tzid) |
paul@878 | 1283 | recurrence_end = recurrence_start + main_period.get_duration() |
paul@1238 | 1284 | |
paul@1238 | 1285 | # Create the period with accompanying metadata based on the main |
paul@1238 | 1286 | # period and event details. |
paul@1238 | 1287 | |
paul@1238 | 1288 | period = RecurringPeriod(recurrence_start, recurrence_end, tzid, "RRULE", dtstart_attr) |
paul@1238 | 1289 | |
paul@1317 | 1290 | # Use the main period where it occurs. |
paul@1317 | 1291 | |
paul@1317 | 1292 | if period == main_period: |
paul@1317 | 1293 | period = main_period |
paul@1317 | 1294 | |
paul@1238 | 1295 | # Filter out periods before the start. |
paul@1238 | 1296 | |
paul@1238 | 1297 | if period.within(selection_period): |
paul@1238 | 1298 | periods.append(period) |
paul@352 | 1299 | |
paul@635 | 1300 | else: |
paul@635 | 1301 | periods = [] |
paul@635 | 1302 | |
paul@352 | 1303 | # Add recurrence dates. |
paul@256 | 1304 | |
paul@1276 | 1305 | rdates = obj.get_date_value_item_periods("RDATE") |
paul@352 | 1306 | if rdates: |
paul@878 | 1307 | periods += rdates |
paul@424 | 1308 | |
paul@424 | 1309 | # Return a sorted list of the periods. |
paul@424 | 1310 | |
paul@542 | 1311 | periods.sort() |
paul@352 | 1312 | |
paul@352 | 1313 | # Exclude exception dates. |
paul@352 | 1314 | |
paul@1276 | 1315 | exdates = obj.get_date_value_item_periods("EXDATE") |
paul@256 | 1316 | |
paul@352 | 1317 | if exdates: |
paul@878 | 1318 | for period in exdates: |
paul@424 | 1319 | i = bisect_left(periods, period) |
paul@458 | 1320 | while i < len(periods) and periods[i] == period: |
paul@424 | 1321 | del periods[i] |
paul@256 | 1322 | |
paul@256 | 1323 | return periods |
paul@256 | 1324 | |
paul@1274 | 1325 | def get_main_period(periods): |
paul@1274 | 1326 | |
paul@1274 | 1327 | "Return the main period from 'periods' using origin information." |
paul@1274 | 1328 | |
paul@1274 | 1329 | for p in periods: |
paul@1274 | 1330 | if p.origin == "DTSTART": |
paul@1274 | 1331 | return p |
paul@1274 | 1332 | return None |
paul@1274 | 1333 | |
paul@1274 | 1334 | def get_recurrence_periods(periods): |
paul@1274 | 1335 | |
paul@1274 | 1336 | "Return recurrence periods from 'periods' using origin information." |
paul@1274 | 1337 | |
paul@1274 | 1338 | l = [] |
paul@1274 | 1339 | for p in periods: |
paul@1274 | 1340 | if p.origin != "DTSTART": |
paul@1274 | 1341 | l.append(p) |
paul@1274 | 1342 | return l |
paul@1274 | 1343 | |
paul@606 | 1344 | def get_sender_identities(mapping): |
paul@606 | 1345 | |
paul@606 | 1346 | """ |
paul@606 | 1347 | Return a mapping from actual senders to the identities for which they |
paul@606 | 1348 | have provided data, extracting this information from the given |
paul@1325 | 1349 | 'mapping'. The SENT-BY attribute provides sender information in preference |
paul@1325 | 1350 | to the property values given as the mapping keys. |
paul@606 | 1351 | """ |
paul@606 | 1352 | |
paul@606 | 1353 | senders = {} |
paul@606 | 1354 | |
paul@606 | 1355 | for value, attr in mapping.items(): |
paul@606 | 1356 | sent_by = attr.get("SENT-BY") |
paul@606 | 1357 | if sent_by: |
paul@606 | 1358 | sender = get_uri(sent_by) |
paul@606 | 1359 | else: |
paul@606 | 1360 | sender = value |
paul@606 | 1361 | |
paul@606 | 1362 | if not senders.has_key(sender): |
paul@606 | 1363 | senders[sender] = [] |
paul@606 | 1364 | |
paul@606 | 1365 | senders[sender].append(value) |
paul@606 | 1366 | |
paul@606 | 1367 | return senders |
paul@606 | 1368 | |
paul@1238 | 1369 | def get_window_end(tzid, days=100, start=None): |
paul@606 | 1370 | |
paul@618 | 1371 | """ |
paul@618 | 1372 | Return a datetime in the time zone indicated by 'tzid' marking the end of a |
paul@1238 | 1373 | window of the given number of 'days'. If 'start' is not indicated, the start |
paul@1238 | 1374 | of the window will be the current moment. |
paul@618 | 1375 | """ |
paul@618 | 1376 | |
paul@1238 | 1377 | return to_timezone(start or datetime.now(), tzid) + timedelta(days) |
paul@606 | 1378 | |
paul@1326 | 1379 | def update_attendees_with_delegates(stored_attendees, attendees): |
paul@1326 | 1380 | |
paul@1326 | 1381 | """ |
paul@1326 | 1382 | Update the 'stored_attendees' mapping with delegate information from the |
paul@1326 | 1383 | given 'attendees' mapping. |
paul@1326 | 1384 | """ |
paul@1326 | 1385 | |
paul@1326 | 1386 | # Check for delegated attendees. |
paul@1326 | 1387 | |
paul@1326 | 1388 | for attendee, attendee_attr in attendees.items(): |
paul@1326 | 1389 | |
paul@1326 | 1390 | # Identify delegates and check the delegation using the updated |
paul@1326 | 1391 | # attendee information. |
paul@1326 | 1392 | |
paul@1326 | 1393 | if not stored_attendees.has_key(attendee) and \ |
paul@1326 | 1394 | attendee_attr.has_key("DELEGATED-FROM") and \ |
paul@1326 | 1395 | check_delegation(stored_attendees, attendee, attendee_attr): |
paul@1326 | 1396 | |
paul@1326 | 1397 | stored_attendees[attendee] = attendee_attr |
paul@1326 | 1398 | |
paul@213 | 1399 | # vim: tabstop=4 expandtab shiftwidth=4 |