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