paul@213 | 1 | #!/usr/bin/env python |
paul@213 | 2 | |
paul@213 | 3 | """ |
paul@213 | 4 | Interpretation of vCalendar content. |
paul@213 | 5 | |
paul@213 | 6 | Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk> |
paul@213 | 7 | |
paul@213 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@213 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@213 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@213 | 11 | version. |
paul@213 | 12 | |
paul@213 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@213 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@213 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@213 | 16 | details. |
paul@213 | 17 | |
paul@213 | 18 | You should have received a copy of the GNU General Public License along with |
paul@213 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@213 | 20 | """ |
paul@213 | 21 | |
paul@424 | 22 | from bisect import bisect_left |
paul@560 | 23 | from datetime import date, datetime, timedelta |
paul@213 | 24 | from email.mime.text import MIMEText |
paul@669 | 25 | from imiptools.dates import check_permitted_values, correct_datetime, \ |
paul@661 | 26 | format_datetime, get_datetime, \ |
paul@625 | 27 | get_datetime_item as get_item_from_datetime, \ |
paul@625 | 28 | get_datetime_tzid, \ |
paul@628 | 29 | get_duration, get_period, get_period_item, \ |
paul@627 | 30 | get_recurrence_start_point, \ |
paul@809 | 31 | get_time, get_tzid, to_datetime, to_timezone, \ |
paul@809 | 32 | to_utc_datetime |
paul@648 | 33 | from imiptools.period import FreeBusyPeriod, Period, RecurringPeriod, period_overlaps |
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@213 | 47 | def __init__(self, fragment): |
paul@213 | 48 | self.objtype, (self.details, self.attr) = fragment.items()[0] |
paul@213 | 49 | |
paul@535 | 50 | def get_uid(self): |
paul@535 | 51 | return self.get_value("UID") |
paul@535 | 52 | |
paul@535 | 53 | def get_recurrenceid(self): |
paul@563 | 54 | |
paul@563 | 55 | """ |
paul@563 | 56 | Return the recurrence identifier, normalised to a UTC datetime if |
paul@627 | 57 | specified as a datetime or date with accompanying time zone information, |
paul@627 | 58 | maintained as a date or floating datetime otherwise. If no recurrence |
paul@627 | 59 | identifier is present, None is returned. |
paul@627 | 60 | |
paul@627 | 61 | Note that this normalised form of the identifier may well not be the |
paul@627 | 62 | same as the originally-specified identifier because that could have been |
paul@627 | 63 | specified using an accompanying TZID attribute, whereas the normalised |
paul@627 | 64 | form is effectively a converted datetime value. |
paul@563 | 65 | """ |
paul@563 | 66 | |
paul@627 | 67 | if not self.has_key("RECURRENCE-ID"): |
paul@627 | 68 | return None |
paul@627 | 69 | dt, attr = self.get_datetime_item("RECURRENCE-ID") |
paul@628 | 70 | |
paul@628 | 71 | # Coerce any date to a UTC datetime if TZID was specified. |
paul@628 | 72 | |
paul@627 | 73 | tzid = attr.get("TZID") |
paul@627 | 74 | if tzid: |
paul@627 | 75 | dt = to_timezone(to_datetime(dt, tzid), "UTC") |
paul@627 | 76 | return format_datetime(dt) |
paul@627 | 77 | |
paul@627 | 78 | def get_recurrence_start_point(self, recurrenceid, tzid): |
paul@627 | 79 | |
paul@627 | 80 | """ |
paul@627 | 81 | Return the start point corresponding to the given 'recurrenceid', using |
paul@627 | 82 | the fallback 'tzid' to define the specific point in time referenced by |
paul@627 | 83 | the recurrence identifier if the identifier has a date representation. |
paul@627 | 84 | |
paul@627 | 85 | If 'recurrenceid' is given as None, this object's recurrence identifier |
paul@627 | 86 | is used to obtain a start point, but if this object does not provide a |
paul@627 | 87 | recurrence, None is returned. |
paul@627 | 88 | |
paul@627 | 89 | A start point is typically used to match free/busy periods which are |
paul@627 | 90 | themselves defined in terms of UTC datetimes. |
paul@627 | 91 | """ |
paul@627 | 92 | |
paul@627 | 93 | recurrenceid = recurrenceid or self.get_recurrenceid() |
paul@627 | 94 | if recurrenceid: |
paul@627 | 95 | return get_recurrence_start_point(recurrenceid, tzid) |
paul@627 | 96 | else: |
paul@627 | 97 | return None |
paul@535 | 98 | |
paul@679 | 99 | def get_recurrence_start_points(self, recurrenceids, tzid): |
paul@679 | 100 | return [self.get_recurrence_start_point(recurrenceid, tzid) for recurrenceid in recurrenceids] |
paul@679 | 101 | |
paul@535 | 102 | # Structure access. |
paul@535 | 103 | |
paul@524 | 104 | def copy(self): |
paul@524 | 105 | return Object(to_dict(self.to_node())) |
paul@524 | 106 | |
paul@213 | 107 | def get_items(self, name, all=True): |
paul@213 | 108 | return get_items(self.details, name, all) |
paul@213 | 109 | |
paul@213 | 110 | def get_item(self, name): |
paul@213 | 111 | return get_item(self.details, name) |
paul@213 | 112 | |
paul@213 | 113 | def get_value_map(self, name): |
paul@213 | 114 | return get_value_map(self.details, name) |
paul@213 | 115 | |
paul@213 | 116 | def get_values(self, name, all=True): |
paul@213 | 117 | return get_values(self.details, name, all) |
paul@213 | 118 | |
paul@213 | 119 | def get_value(self, name): |
paul@213 | 120 | return get_value(self.details, name) |
paul@213 | 121 | |
paul@506 | 122 | def get_utc_datetime(self, name, date_tzid=None): |
paul@506 | 123 | return get_utc_datetime(self.details, name, date_tzid) |
paul@213 | 124 | |
paul@417 | 125 | def get_date_values(self, name, tzid=None): |
paul@417 | 126 | items = get_date_value_items(self.details, name, tzid) |
paul@389 | 127 | return items and [value for value, attr in items] |
paul@352 | 128 | |
paul@417 | 129 | def get_date_value_items(self, name, tzid=None): |
paul@417 | 130 | return get_date_value_items(self.details, name, tzid) |
paul@352 | 131 | |
paul@878 | 132 | def get_date_value_item_periods(self, name, tzid=None): |
paul@878 | 133 | return get_date_value_item_periods(self.details, name, self.get_main_period(tzid).get_duration(), tzid) |
paul@878 | 134 | |
paul@646 | 135 | def get_period_values(self, name, tzid=None): |
paul@646 | 136 | return get_period_values(self.details, name, tzid) |
paul@630 | 137 | |
paul@318 | 138 | def get_datetime(self, name): |
paul@567 | 139 | t = get_datetime_item(self.details, name) |
paul@567 | 140 | if not t: return None |
paul@567 | 141 | dt, attr = t |
paul@318 | 142 | return dt |
paul@318 | 143 | |
paul@289 | 144 | def get_datetime_item(self, name): |
paul@289 | 145 | return get_datetime_item(self.details, name) |
paul@289 | 146 | |
paul@392 | 147 | def get_duration(self, name): |
paul@392 | 148 | return get_duration(self.get_value(name)) |
paul@392 | 149 | |
paul@213 | 150 | def to_node(self): |
paul@213 | 151 | return to_node({self.objtype : [(self.details, self.attr)]}) |
paul@213 | 152 | |
paul@213 | 153 | def to_part(self, method): |
paul@213 | 154 | return to_part(method, [self.to_node()]) |
paul@213 | 155 | |
paul@213 | 156 | # Direct access to the structure. |
paul@213 | 157 | |
paul@392 | 158 | def has_key(self, name): |
paul@392 | 159 | return self.details.has_key(name) |
paul@392 | 160 | |
paul@524 | 161 | def get(self, name): |
paul@524 | 162 | return self.details.get(name) |
paul@524 | 163 | |
paul@734 | 164 | def keys(self): |
paul@734 | 165 | return self.details.keys() |
paul@734 | 166 | |
paul@213 | 167 | def __getitem__(self, name): |
paul@213 | 168 | return self.details[name] |
paul@213 | 169 | |
paul@213 | 170 | def __setitem__(self, name, value): |
paul@213 | 171 | self.details[name] = value |
paul@213 | 172 | |
paul@213 | 173 | def __delitem__(self, name): |
paul@213 | 174 | del self.details[name] |
paul@213 | 175 | |
paul@524 | 176 | def remove(self, name): |
paul@524 | 177 | try: |
paul@524 | 178 | del self[name] |
paul@524 | 179 | except KeyError: |
paul@524 | 180 | pass |
paul@524 | 181 | |
paul@524 | 182 | def remove_all(self, names): |
paul@524 | 183 | for name in names: |
paul@524 | 184 | self.remove(name) |
paul@524 | 185 | |
paul@734 | 186 | def preserve(self, names): |
paul@734 | 187 | for name in self.keys(): |
paul@734 | 188 | if not name in names: |
paul@734 | 189 | self.remove(name) |
paul@734 | 190 | |
paul@256 | 191 | # Computed results. |
paul@256 | 192 | |
paul@797 | 193 | def get_main_period(self, tzid): |
paul@797 | 194 | |
paul@797 | 195 | """ |
paul@797 | 196 | Return a period object corresponding to the main start-end period for |
paul@797 | 197 | the object. |
paul@797 | 198 | """ |
paul@797 | 199 | |
paul@797 | 200 | (dtstart, dtstart_attr), (dtend, dtend_attr) = self.get_main_period_items(tzid) |
paul@797 | 201 | return RecurringPeriod(dtstart, dtend, tzid, "DTSTART", dtstart_attr, dtend_attr) |
paul@797 | 202 | |
paul@650 | 203 | def get_main_period_items(self, tzid): |
paul@650 | 204 | |
paul@650 | 205 | """ |
paul@650 | 206 | Return two (value, attributes) items corresponding to the main start-end |
paul@650 | 207 | period for the object. |
paul@650 | 208 | """ |
paul@650 | 209 | |
paul@650 | 210 | dtstart, dtstart_attr = self.get_datetime_item("DTSTART") |
paul@650 | 211 | |
paul@650 | 212 | if self.has_key("DTEND"): |
paul@650 | 213 | dtend, dtend_attr = self.get_datetime_item("DTEND") |
paul@650 | 214 | elif self.has_key("DURATION"): |
paul@650 | 215 | duration = self.get_duration("DURATION") |
paul@650 | 216 | dtend = dtstart + duration |
paul@650 | 217 | dtend_attr = dtstart_attr |
paul@650 | 218 | else: |
paul@650 | 219 | dtend, dtend_attr = dtstart, dtstart_attr |
paul@650 | 220 | |
paul@650 | 221 | return (dtstart, dtstart_attr), (dtend, dtend_attr) |
paul@650 | 222 | |
paul@630 | 223 | def get_periods(self, tzid, end=None): |
paul@620 | 224 | |
paul@620 | 225 | """ |
paul@620 | 226 | Return periods defined by this object, employing the given 'tzid' where |
paul@620 | 227 | no time zone information is defined, and limiting the collection to a |
paul@620 | 228 | window of time with the given 'end'. |
paul@630 | 229 | |
paul@630 | 230 | If 'end' is omitted, only explicit recurrences and recurrences from |
paul@630 | 231 | explicitly-terminated rules will be returned. |
paul@620 | 232 | """ |
paul@620 | 233 | |
paul@458 | 234 | return get_periods(self, tzid, end) |
paul@360 | 235 | |
paul@630 | 236 | def get_active_periods(self, recurrenceids, tzid, end=None): |
paul@630 | 237 | |
paul@630 | 238 | """ |
paul@630 | 239 | Return all periods specified by this object that are not replaced by |
paul@630 | 240 | those defined by 'recurrenceids', using 'tzid' as a fallback time zone |
paul@630 | 241 | to convert floating dates and datetimes, and using 'end' to indicate the |
paul@630 | 242 | end of the time window within which periods are considered. |
paul@630 | 243 | """ |
paul@630 | 244 | |
paul@630 | 245 | # Specific recurrences yield all specified periods. |
paul@630 | 246 | |
paul@630 | 247 | periods = self.get_periods(tzid, end) |
paul@630 | 248 | |
paul@630 | 249 | if self.get_recurrenceid(): |
paul@630 | 250 | return periods |
paul@630 | 251 | |
paul@630 | 252 | # Parent objects need to have their periods tested against redefined |
paul@630 | 253 | # recurrences. |
paul@630 | 254 | |
paul@630 | 255 | active = [] |
paul@630 | 256 | |
paul@630 | 257 | for p in periods: |
paul@630 | 258 | |
paul@630 | 259 | # Subtract any recurrences from the free/busy details of a |
paul@630 | 260 | # parent object. |
paul@630 | 261 | |
paul@648 | 262 | if not p.is_replaced(recurrenceids): |
paul@630 | 263 | active.append(p) |
paul@630 | 264 | |
paul@630 | 265 | return active |
paul@630 | 266 | |
paul@648 | 267 | def get_freebusy_period(self, period, only_organiser=False): |
paul@648 | 268 | |
paul@648 | 269 | """ |
paul@648 | 270 | Return a free/busy period for the given 'period' provided by this |
paul@648 | 271 | object, using the 'only_organiser' status to produce a suitable |
paul@648 | 272 | transparency value. |
paul@648 | 273 | """ |
paul@648 | 274 | |
paul@648 | 275 | return FreeBusyPeriod( |
paul@648 | 276 | period.get_start_point(), |
paul@648 | 277 | period.get_end_point(), |
paul@648 | 278 | self.get_value("UID"), |
paul@648 | 279 | only_organiser and "ORG" or self.get_value("TRANSP") or "OPAQUE", |
paul@648 | 280 | self.get_recurrenceid(), |
paul@648 | 281 | self.get_value("SUMMARY"), |
paul@814 | 282 | get_uri(self.get_value("ORGANIZER")) |
paul@648 | 283 | ) |
paul@648 | 284 | |
paul@648 | 285 | def get_participation_status(self, participant): |
paul@648 | 286 | |
paul@648 | 287 | """ |
paul@648 | 288 | Return the participation status of the given 'participant', with the |
paul@648 | 289 | special value "ORG" indicating organiser-only participation. |
paul@648 | 290 | """ |
paul@648 | 291 | |
paul@814 | 292 | attendees = uri_dict(self.get_value_map("ATTENDEE")) |
paul@814 | 293 | organiser = get_uri(self.get_value("ORGANIZER")) |
paul@648 | 294 | |
paul@692 | 295 | attendee_attr = attendees.get(participant) |
paul@692 | 296 | if attendee_attr: |
paul@692 | 297 | return attendee_attr.get("PARTSTAT", "NEEDS-ACTION") |
paul@692 | 298 | elif organiser == participant: |
paul@692 | 299 | return "ORG" |
paul@648 | 300 | |
paul@648 | 301 | return None |
paul@648 | 302 | |
paul@648 | 303 | def get_participation(self, partstat, include_needs_action=False): |
paul@648 | 304 | |
paul@648 | 305 | """ |
paul@648 | 306 | Return whether 'partstat' indicates some kind of participation in an |
paul@648 | 307 | event. If 'include_needs_action' is specified as a true value, events |
paul@648 | 308 | not yet responded to will be treated as events with tentative |
paul@648 | 309 | participation. |
paul@648 | 310 | """ |
paul@648 | 311 | |
paul@648 | 312 | return not partstat in ("DECLINED", "DELEGATED", "NEEDS-ACTION") or \ |
paul@648 | 313 | include_needs_action and partstat == "NEEDS-ACTION" or \ |
paul@648 | 314 | partstat == "ORG" |
paul@648 | 315 | |
paul@422 | 316 | def get_tzid(self): |
paul@562 | 317 | |
paul@562 | 318 | """ |
paul@562 | 319 | Return a time zone identifier used by the start or end datetimes, |
paul@562 | 320 | potentially suitable for converting dates to datetimes. |
paul@562 | 321 | """ |
paul@562 | 322 | |
paul@560 | 323 | if not self.has_key("DTSTART"): |
paul@560 | 324 | return None |
paul@422 | 325 | dtstart, dtstart_attr = self.get_datetime_item("DTSTART") |
paul@630 | 326 | if self.has_key("DTEND"): |
paul@630 | 327 | dtend, dtend_attr = self.get_datetime_item("DTEND") |
paul@630 | 328 | else: |
paul@630 | 329 | dtend_attr = None |
paul@422 | 330 | return get_tzid(dtstart_attr, dtend_attr) |
paul@422 | 331 | |
paul@619 | 332 | def is_shared(self): |
paul@619 | 333 | |
paul@619 | 334 | """ |
paul@619 | 335 | Return whether this object is shared based on the presence of a SEQUENCE |
paul@619 | 336 | property. |
paul@619 | 337 | """ |
paul@619 | 338 | |
paul@619 | 339 | return self.get_value("SEQUENCE") is not None |
paul@619 | 340 | |
paul@650 | 341 | def possibly_active_from(self, dt, tzid): |
paul@650 | 342 | |
paul@650 | 343 | """ |
paul@650 | 344 | Return whether the object is possibly active from or after the given |
paul@650 | 345 | datetime 'dt' using 'tzid' to convert any dates or floating datetimes. |
paul@650 | 346 | """ |
paul@650 | 347 | |
paul@650 | 348 | dt = to_datetime(dt, tzid) |
paul@650 | 349 | periods = self.get_periods(tzid) |
paul@650 | 350 | |
paul@650 | 351 | for p in periods: |
paul@650 | 352 | if p.get_end_point() > dt: |
paul@650 | 353 | return True |
paul@650 | 354 | |
paul@672 | 355 | return self.possibly_recurring_indefinitely() |
paul@672 | 356 | |
paul@672 | 357 | def possibly_recurring_indefinitely(self): |
paul@672 | 358 | |
paul@672 | 359 | "Return whether this object may recur indefinitely." |
paul@672 | 360 | |
paul@650 | 361 | rrule = self.get_value("RRULE") |
paul@650 | 362 | parameters = rrule and get_parameters(rrule) |
paul@650 | 363 | until = parameters and parameters.get("UNTIL") |
paul@651 | 364 | count = parameters and parameters.get("COUNT") |
paul@650 | 365 | |
paul@672 | 366 | # Non-recurring periods or constrained recurrences. |
paul@651 | 367 | |
paul@651 | 368 | if not rrule or until or count: |
paul@650 | 369 | return False |
paul@651 | 370 | |
paul@672 | 371 | # Unconstrained recurring periods will always lie beyond any specified |
paul@651 | 372 | # datetime. |
paul@651 | 373 | |
paul@651 | 374 | else: |
paul@650 | 375 | return True |
paul@650 | 376 | |
paul@627 | 377 | # Modification methods. |
paul@627 | 378 | |
paul@627 | 379 | def set_datetime(self, name, dt, tzid=None): |
paul@627 | 380 | |
paul@627 | 381 | """ |
paul@627 | 382 | Set a datetime for property 'name' using 'dt' and the optional fallback |
paul@627 | 383 | 'tzid', returning whether an update has occurred. |
paul@627 | 384 | """ |
paul@627 | 385 | |
paul@627 | 386 | if dt: |
paul@627 | 387 | old_value = self.get_value(name) |
paul@627 | 388 | self[name] = [get_item_from_datetime(dt, tzid)] |
paul@627 | 389 | return format_datetime(dt) != old_value |
paul@627 | 390 | |
paul@627 | 391 | return False |
paul@627 | 392 | |
paul@627 | 393 | def set_period(self, period): |
paul@627 | 394 | |
paul@627 | 395 | "Set the given 'period' as the main start and end." |
paul@627 | 396 | |
paul@627 | 397 | result = self.set_datetime("DTSTART", period.get_start()) |
paul@627 | 398 | result = self.set_datetime("DTEND", period.get_end()) or result |
paul@661 | 399 | if self.has_key("DURATION"): |
paul@661 | 400 | del self["DURATION"] |
paul@661 | 401 | |
paul@627 | 402 | return result |
paul@627 | 403 | |
paul@627 | 404 | def set_periods(self, periods): |
paul@627 | 405 | |
paul@627 | 406 | """ |
paul@627 | 407 | Set the given 'periods' as recurrence date properties, replacing the |
paul@627 | 408 | previous RDATE properties and ignoring any RRULE properties. |
paul@627 | 409 | """ |
paul@627 | 410 | |
paul@753 | 411 | old_values = set(self.get_date_values("RDATE") or []) |
paul@627 | 412 | new_rdates = [] |
paul@627 | 413 | |
paul@627 | 414 | if self.has_key("RDATE"): |
paul@627 | 415 | del self["RDATE"] |
paul@627 | 416 | |
paul@812 | 417 | main_changed = False |
paul@812 | 418 | |
paul@627 | 419 | for p in periods: |
paul@812 | 420 | if p.origin == "RDATE": |
paul@627 | 421 | new_rdates.append(get_period_item(p.get_start(), p.get_end())) |
paul@812 | 422 | elif p.origin == "DTSTART": |
paul@812 | 423 | main_changed = self.set_period(p) |
paul@627 | 424 | |
paul@661 | 425 | if new_rdates: |
paul@661 | 426 | self["RDATE"] = new_rdates |
paul@661 | 427 | |
paul@812 | 428 | return main_changed or old_values != set(self.get_date_values("RDATE") or []) |
paul@661 | 429 | |
paul@872 | 430 | def set_rule(self, rule): |
paul@872 | 431 | |
paul@872 | 432 | """ |
paul@872 | 433 | Set the given 'rule' in this object, replacing the previous RRULE |
paul@872 | 434 | property, returning whether the object has changed. The provided 'rule' |
paul@872 | 435 | must be an item. |
paul@872 | 436 | """ |
paul@872 | 437 | |
paul@872 | 438 | if not rule: |
paul@872 | 439 | return False |
paul@872 | 440 | |
paul@872 | 441 | old_rrule = self.get_item("RRULE") |
paul@872 | 442 | self["RRULE"] = [rule] |
paul@872 | 443 | return old_rrule != rule |
paul@872 | 444 | |
paul@872 | 445 | def set_exceptions(self, exceptions): |
paul@872 | 446 | |
paul@872 | 447 | """ |
paul@872 | 448 | Set the given 'exceptions' in this object, replacing the previous EXDATE |
paul@872 | 449 | properties, returning whether the object has changed. The provided |
paul@872 | 450 | 'exceptions' must be a collection of items. |
paul@872 | 451 | """ |
paul@872 | 452 | |
paul@872 | 453 | old_exdates = set(self.get_date_values("EXDATE") or []) |
paul@872 | 454 | if exceptions: |
paul@872 | 455 | self["EXDATE"] = exceptions |
paul@872 | 456 | return old_exdates != set(self.get_date_values("EXDATE") or []) |
paul@872 | 457 | elif old_exdates: |
paul@872 | 458 | del self["EXDATE"] |
paul@872 | 459 | return True |
paul@872 | 460 | else: |
paul@872 | 461 | return False |
paul@872 | 462 | |
paul@809 | 463 | def update_dtstamp(self): |
paul@809 | 464 | |
paul@809 | 465 | "Update the DTSTAMP in the object." |
paul@809 | 466 | |
paul@809 | 467 | dtstamp = self.get_utc_datetime("DTSTAMP") |
paul@809 | 468 | utcnow = get_time() |
paul@809 | 469 | dtstamp = format_datetime(dtstamp and dtstamp > utcnow and dtstamp or utcnow) |
paul@809 | 470 | self["DTSTAMP"] = [(dtstamp, {})] |
paul@809 | 471 | return dtstamp |
paul@809 | 472 | |
paul@809 | 473 | def update_sequence(self, increment=False): |
paul@809 | 474 | |
paul@809 | 475 | "Set or update the SEQUENCE in the object." |
paul@809 | 476 | |
paul@809 | 477 | sequence = self.get_value("SEQUENCE") or "0" |
paul@809 | 478 | self["SEQUENCE"] = [(str(int(sequence) + (increment and 1 or 0)), {})] |
paul@809 | 479 | return sequence |
paul@809 | 480 | |
paul@878 | 481 | def update_exceptions(self, excluded, asserted): |
paul@784 | 482 | |
paul@784 | 483 | """ |
paul@784 | 484 | Update the exceptions to any rule by applying the list of 'excluded' |
paul@878 | 485 | periods. Where 'asserted' periods are provided, exceptions will be |
paul@878 | 486 | removed corresponding to those periods. |
paul@784 | 487 | """ |
paul@784 | 488 | |
paul@878 | 489 | old_exdates = self.get_date_value_item_periods("EXDATE") |
paul@878 | 490 | new_exdates = set(old_exdates) |
paul@878 | 491 | new_exdates.update(excluded) |
paul@878 | 492 | new_exdates.difference_update(asserted) |
paul@784 | 493 | |
paul@878 | 494 | if not new_exdates: |
paul@878 | 495 | del self["EXDATE"] |
paul@878 | 496 | else: |
paul@784 | 497 | self["EXDATE"] = [] |
paul@878 | 498 | for p in new_exdates: |
paul@878 | 499 | self["EXDATE"].append(get_period_item(p.get_start(), p.get_end())) |
paul@784 | 500 | |
paul@878 | 501 | return set(old_exdates) != new_exdates |
paul@784 | 502 | |
paul@669 | 503 | def correct_object(self, tzid, permitted_values): |
paul@661 | 504 | |
paul@661 | 505 | "Correct the object's period details." |
paul@661 | 506 | |
paul@661 | 507 | corrected = set() |
paul@661 | 508 | rdates = [] |
paul@661 | 509 | |
paul@661 | 510 | for period in self.get_periods(tzid): |
paul@661 | 511 | start = period.get_start() |
paul@661 | 512 | end = period.get_end() |
paul@669 | 513 | start_errors = check_permitted_values(start, permitted_values) |
paul@669 | 514 | end_errors = check_permitted_values(end, permitted_values) |
paul@627 | 515 | |
paul@661 | 516 | if not (start_errors or end_errors): |
paul@661 | 517 | if period.origin == "RDATE": |
paul@661 | 518 | rdates.append(period) |
paul@661 | 519 | continue |
paul@661 | 520 | |
paul@661 | 521 | if start_errors: |
paul@669 | 522 | start = correct_datetime(start, permitted_values) |
paul@661 | 523 | if end_errors: |
paul@669 | 524 | end = correct_datetime(end, permitted_values) |
paul@661 | 525 | period = RecurringPeriod(start, end, period.tzid, period.origin, period.get_start_attr(), period.get_end_attr()) |
paul@661 | 526 | |
paul@661 | 527 | if period.origin == "DTSTART": |
paul@661 | 528 | self.set_period(period) |
paul@661 | 529 | corrected.add("DTSTART") |
paul@661 | 530 | elif period.origin == "RDATE": |
paul@661 | 531 | rdates.append(period) |
paul@661 | 532 | corrected.add("RDATE") |
paul@661 | 533 | |
paul@661 | 534 | if "RDATE" in corrected: |
paul@661 | 535 | self.set_periods(rdates) |
paul@661 | 536 | |
paul@661 | 537 | return corrected |
paul@627 | 538 | |
paul@213 | 539 | # Construction and serialisation. |
paul@213 | 540 | |
paul@213 | 541 | def make_calendar(nodes, method=None): |
paul@213 | 542 | |
paul@213 | 543 | """ |
paul@213 | 544 | Return a complete calendar node wrapping the given 'nodes' and employing the |
paul@213 | 545 | given 'method', if indicated. |
paul@213 | 546 | """ |
paul@213 | 547 | |
paul@213 | 548 | return ("VCALENDAR", {}, |
paul@213 | 549 | (method and [("METHOD", {}, method)] or []) + |
paul@213 | 550 | [("VERSION", {}, "2.0")] + |
paul@213 | 551 | nodes |
paul@213 | 552 | ) |
paul@213 | 553 | |
paul@327 | 554 | def make_freebusy(freebusy, uid, organiser, organiser_attr=None, attendee=None, |
paul@562 | 555 | attendee_attr=None, period=None): |
paul@222 | 556 | |
paul@222 | 557 | """ |
paul@222 | 558 | Return a calendar node defining the free/busy details described in the given |
paul@292 | 559 | 'freebusy' list, employing the given 'uid', for the given 'organiser' and |
paul@292 | 560 | optional 'organiser_attr', with the optional 'attendee' providing recipient |
paul@292 | 561 | details together with the optional 'attendee_attr'. |
paul@327 | 562 | |
paul@562 | 563 | The result will be constrained to the 'period' if specified. |
paul@222 | 564 | """ |
paul@222 | 565 | |
paul@222 | 566 | record = [] |
paul@222 | 567 | rwrite = record.append |
paul@222 | 568 | |
paul@292 | 569 | rwrite(("ORGANIZER", organiser_attr or {}, organiser)) |
paul@222 | 570 | |
paul@222 | 571 | if attendee: |
paul@292 | 572 | rwrite(("ATTENDEE", attendee_attr or {}, attendee)) |
paul@222 | 573 | |
paul@222 | 574 | rwrite(("UID", {}, uid)) |
paul@222 | 575 | |
paul@222 | 576 | if freebusy: |
paul@327 | 577 | |
paul@327 | 578 | # Get a constrained view if start and end limits are specified. |
paul@327 | 579 | |
paul@563 | 580 | if period: |
paul@563 | 581 | periods = period_overlaps(freebusy, period, True) |
paul@563 | 582 | else: |
paul@563 | 583 | periods = freebusy |
paul@327 | 584 | |
paul@327 | 585 | # Write the limits of the resource. |
paul@327 | 586 | |
paul@563 | 587 | if periods: |
paul@563 | 588 | rwrite(("DTSTART", {"VALUE" : "DATE-TIME"}, format_datetime(periods[0].get_start_point()))) |
paul@563 | 589 | rwrite(("DTEND", {"VALUE" : "DATE-TIME"}, format_datetime(periods[-1].get_end_point()))) |
paul@563 | 590 | else: |
paul@563 | 591 | rwrite(("DTSTART", {"VALUE" : "DATE-TIME"}, format_datetime(period.get_start_point()))) |
paul@563 | 592 | rwrite(("DTEND", {"VALUE" : "DATE-TIME"}, format_datetime(period.get_end_point()))) |
paul@327 | 593 | |
paul@458 | 594 | for p in periods: |
paul@458 | 595 | if p.transp == "OPAQUE": |
paul@529 | 596 | rwrite(("FREEBUSY", {"FBTYPE" : "BUSY"}, "/".join( |
paul@562 | 597 | map(format_datetime, [p.get_start_point(), p.get_end_point()]) |
paul@529 | 598 | ))) |
paul@222 | 599 | |
paul@222 | 600 | return ("VFREEBUSY", {}, record) |
paul@222 | 601 | |
paul@213 | 602 | def parse_object(f, encoding, objtype=None): |
paul@213 | 603 | |
paul@213 | 604 | """ |
paul@213 | 605 | Parse the iTIP content from 'f' having the given 'encoding'. If 'objtype' is |
paul@213 | 606 | given, only objects of that type will be returned. Otherwise, the root of |
paul@213 | 607 | the content will be returned as a dictionary with a single key indicating |
paul@213 | 608 | the object type. |
paul@213 | 609 | |
paul@213 | 610 | Return None if the content was not readable or suitable. |
paul@213 | 611 | """ |
paul@213 | 612 | |
paul@213 | 613 | try: |
paul@213 | 614 | try: |
paul@213 | 615 | doctype, attrs, elements = obj = parse(f, encoding=encoding) |
paul@213 | 616 | if objtype and doctype == objtype: |
paul@213 | 617 | return to_dict(obj)[objtype][0] |
paul@213 | 618 | elif not objtype: |
paul@213 | 619 | return to_dict(obj) |
paul@213 | 620 | finally: |
paul@213 | 621 | f.close() |
paul@213 | 622 | |
paul@213 | 623 | # NOTE: Handle parse errors properly. |
paul@213 | 624 | |
paul@213 | 625 | except (ParseError, ValueError): |
paul@213 | 626 | pass |
paul@213 | 627 | |
paul@213 | 628 | return None |
paul@213 | 629 | |
paul@213 | 630 | def to_part(method, calendar): |
paul@213 | 631 | |
paul@213 | 632 | """ |
paul@213 | 633 | Write using the given 'method', the 'calendar' details to a MIME |
paul@213 | 634 | text/calendar part. |
paul@213 | 635 | """ |
paul@213 | 636 | |
paul@213 | 637 | encoding = "utf-8" |
paul@213 | 638 | out = StringIO() |
paul@213 | 639 | try: |
paul@213 | 640 | to_stream(out, make_calendar(calendar, method), encoding) |
paul@213 | 641 | part = MIMEText(out.getvalue(), "calendar", encoding) |
paul@213 | 642 | part.set_param("method", method) |
paul@213 | 643 | return part |
paul@213 | 644 | |
paul@213 | 645 | finally: |
paul@213 | 646 | out.close() |
paul@213 | 647 | |
paul@213 | 648 | def to_stream(out, fragment, encoding="utf-8"): |
paul@213 | 649 | iterwrite(out, encoding=encoding).append(fragment) |
paul@213 | 650 | |
paul@213 | 651 | # Structure access functions. |
paul@213 | 652 | |
paul@213 | 653 | def get_items(d, name, all=True): |
paul@213 | 654 | |
paul@213 | 655 | """ |
paul@213 | 656 | Get all items from 'd' for the given 'name', returning single items if |
paul@213 | 657 | 'all' is specified and set to a false value and if only one value is |
paul@213 | 658 | present for the name. Return None if no items are found for the name or if |
paul@213 | 659 | many items are found but 'all' is set to a false value. |
paul@213 | 660 | """ |
paul@213 | 661 | |
paul@213 | 662 | if d.has_key(name): |
paul@712 | 663 | items = [(value or None, attr) for value, attr in d[name]] |
paul@213 | 664 | if all: |
paul@462 | 665 | return items |
paul@462 | 666 | elif len(items) == 1: |
paul@462 | 667 | return items[0] |
paul@213 | 668 | else: |
paul@213 | 669 | return None |
paul@213 | 670 | else: |
paul@213 | 671 | return None |
paul@213 | 672 | |
paul@213 | 673 | def get_item(d, name): |
paul@213 | 674 | return get_items(d, name, False) |
paul@213 | 675 | |
paul@213 | 676 | def get_value_map(d, name): |
paul@213 | 677 | |
paul@213 | 678 | """ |
paul@213 | 679 | Return a dictionary for all items in 'd' having the given 'name'. The |
paul@213 | 680 | dictionary will map values for the name to any attributes or qualifiers |
paul@213 | 681 | that may have been present. |
paul@213 | 682 | """ |
paul@213 | 683 | |
paul@213 | 684 | items = get_items(d, name) |
paul@213 | 685 | if items: |
paul@213 | 686 | return dict(items) |
paul@213 | 687 | else: |
paul@213 | 688 | return {} |
paul@213 | 689 | |
paul@462 | 690 | def values_from_items(items): |
paul@462 | 691 | return map(lambda x: x[0], items) |
paul@462 | 692 | |
paul@213 | 693 | def get_values(d, name, all=True): |
paul@213 | 694 | if d.has_key(name): |
paul@462 | 695 | items = d[name] |
paul@462 | 696 | if not all and len(items) == 1: |
paul@462 | 697 | return items[0][0] |
paul@213 | 698 | else: |
paul@462 | 699 | return values_from_items(items) |
paul@213 | 700 | else: |
paul@213 | 701 | return None |
paul@213 | 702 | |
paul@213 | 703 | def get_value(d, name): |
paul@213 | 704 | return get_values(d, name, False) |
paul@213 | 705 | |
paul@417 | 706 | def get_date_value_items(d, name, tzid=None): |
paul@352 | 707 | |
paul@352 | 708 | """ |
paul@389 | 709 | Obtain items from 'd' having the given 'name', where a single item yields |
paul@389 | 710 | potentially many values. Return a list of tuples of the form (value, |
paul@389 | 711 | attributes) where the attributes have been given for the property in 'd'. |
paul@352 | 712 | """ |
paul@352 | 713 | |
paul@403 | 714 | items = get_items(d, name) |
paul@403 | 715 | if items: |
paul@403 | 716 | all_items = [] |
paul@403 | 717 | for item in items: |
paul@403 | 718 | values, attr = item |
paul@417 | 719 | if not attr.has_key("TZID") and tzid: |
paul@417 | 720 | attr["TZID"] = tzid |
paul@403 | 721 | if not isinstance(values, list): |
paul@403 | 722 | values = [values] |
paul@403 | 723 | for value in values: |
paul@403 | 724 | all_items.append((get_datetime(value, attr) or get_period(value, attr), attr)) |
paul@403 | 725 | return all_items |
paul@352 | 726 | else: |
paul@352 | 727 | return None |
paul@352 | 728 | |
paul@878 | 729 | def get_date_value_item_periods(d, name, duration, tzid=None): |
paul@878 | 730 | |
paul@878 | 731 | """ |
paul@878 | 732 | Obtain items from 'd' having the given 'name', where a single item yields |
paul@878 | 733 | potentially many values. The 'duration' must be provided to define the |
paul@878 | 734 | length of periods having only a start datetime. Return a list of periods |
paul@878 | 735 | corresponding to the property in 'd'. |
paul@878 | 736 | """ |
paul@878 | 737 | |
paul@878 | 738 | items = get_date_value_items(d, name, tzid) |
paul@878 | 739 | if not items: |
paul@878 | 740 | return items |
paul@878 | 741 | |
paul@878 | 742 | periods = [] |
paul@878 | 743 | |
paul@878 | 744 | for value, attr in items: |
paul@878 | 745 | if isinstance(value, tuple): |
paul@878 | 746 | periods.append(RecurringPeriod(value[0], value[1], tzid, name, attr)) |
paul@878 | 747 | else: |
paul@878 | 748 | periods.append(RecurringPeriod(value, value + duration, tzid, name, attr)) |
paul@878 | 749 | |
paul@878 | 750 | return periods |
paul@878 | 751 | |
paul@646 | 752 | def get_period_values(d, name, tzid=None): |
paul@630 | 753 | |
paul@630 | 754 | """ |
paul@630 | 755 | Return period values from 'd' for the given property 'name', using 'tzid' |
paul@646 | 756 | where specified to indicate the time zone. |
paul@630 | 757 | """ |
paul@630 | 758 | |
paul@630 | 759 | values = [] |
paul@630 | 760 | for value, attr in get_items(d, name) or []: |
paul@630 | 761 | if not attr.has_key("TZID") and tzid: |
paul@630 | 762 | attr["TZID"] = tzid |
paul@630 | 763 | start, end = get_period(value, attr) |
paul@646 | 764 | values.append(Period(start, end, tzid=tzid)) |
paul@630 | 765 | return values |
paul@630 | 766 | |
paul@506 | 767 | def get_utc_datetime(d, name, date_tzid=None): |
paul@506 | 768 | |
paul@506 | 769 | """ |
paul@506 | 770 | Return the value provided by 'd' for 'name' as a datetime in the UTC zone |
paul@506 | 771 | or as a date, converting any date to a datetime if 'date_tzid' is specified. |
paul@720 | 772 | If no datetime or date is available, None is returned. |
paul@506 | 773 | """ |
paul@506 | 774 | |
paul@348 | 775 | t = get_datetime_item(d, name) |
paul@348 | 776 | if not t: |
paul@348 | 777 | return None |
paul@348 | 778 | else: |
paul@348 | 779 | dt, attr = t |
paul@720 | 780 | return dt is not None and to_utc_datetime(dt, date_tzid) or None |
paul@289 | 781 | |
paul@289 | 782 | def get_datetime_item(d, name): |
paul@562 | 783 | |
paul@562 | 784 | """ |
paul@562 | 785 | Return the value provided by 'd' for 'name' as a datetime or as a date, |
paul@562 | 786 | together with the attributes describing it. Return None if no value exists |
paul@562 | 787 | for 'name' in 'd'. |
paul@562 | 788 | """ |
paul@562 | 789 | |
paul@348 | 790 | t = get_item(d, name) |
paul@348 | 791 | if not t: |
paul@348 | 792 | return None |
paul@348 | 793 | else: |
paul@348 | 794 | value, attr = t |
paul@613 | 795 | dt = get_datetime(value, attr) |
paul@616 | 796 | tzid = get_datetime_tzid(dt) |
paul@616 | 797 | if tzid: |
paul@616 | 798 | attr["TZID"] = tzid |
paul@613 | 799 | return dt, attr |
paul@213 | 800 | |
paul@528 | 801 | # Conversion functions. |
paul@528 | 802 | |
paul@792 | 803 | def get_address_parts(values): |
paul@792 | 804 | |
paul@792 | 805 | "Return name and address tuples for each of the given 'values'." |
paul@792 | 806 | |
paul@792 | 807 | l = [] |
paul@792 | 808 | for name, address in values and email.utils.getaddresses(values) or []: |
paul@792 | 809 | if is_mailto_uri(name): |
paul@792 | 810 | name = name[7:] # strip "mailto:" |
paul@792 | 811 | l.append((name, address)) |
paul@792 | 812 | return l |
paul@792 | 813 | |
paul@213 | 814 | def get_addresses(values): |
paul@790 | 815 | |
paul@790 | 816 | """ |
paul@790 | 817 | Return only addresses from the given 'values' which may be of the form |
paul@790 | 818 | "Common Name <recipient@domain>", with the latter part being the address |
paul@790 | 819 | itself. |
paul@790 | 820 | """ |
paul@790 | 821 | |
paul@792 | 822 | return [address for name, address in get_address_parts(values)] |
paul@213 | 823 | |
paul@213 | 824 | def get_address(value): |
paul@790 | 825 | |
paul@790 | 826 | "Return an e-mail address from the given 'value'." |
paul@790 | 827 | |
paul@712 | 828 | if not value: return None |
paul@792 | 829 | return get_addresses([value])[0] |
paul@792 | 830 | |
paul@792 | 831 | def get_verbose_address(value, attr=None): |
paul@792 | 832 | |
paul@792 | 833 | """ |
paul@792 | 834 | Return a verbose e-mail address featuring any name from the given 'value' |
paul@792 | 835 | and any accompanying 'attr' dictionary. |
paul@792 | 836 | """ |
paul@792 | 837 | |
paul@810 | 838 | l = get_address_parts([value]) |
paul@810 | 839 | if not l: |
paul@810 | 840 | return value |
paul@810 | 841 | name, address = l[0] |
paul@792 | 842 | if not name: |
paul@792 | 843 | name = attr and attr.get("CN") |
paul@792 | 844 | if name and address: |
paul@792 | 845 | return "%s <%s>" % (name, address) |
paul@792 | 846 | else: |
paul@792 | 847 | return address |
paul@792 | 848 | |
paul@792 | 849 | def is_mailto_uri(value): |
paul@792 | 850 | return value.lower().startswith("mailto:") |
paul@213 | 851 | |
paul@213 | 852 | def get_uri(value): |
paul@790 | 853 | |
paul@790 | 854 | "Return a URI for the given 'value'." |
paul@790 | 855 | |
paul@712 | 856 | if not value: return None |
paul@792 | 857 | return is_mailto_uri(value) and ("mailto:%s" % value[7:]) or \ |
paul@712 | 858 | ":" in value and value or \ |
paul@790 | 859 | "mailto:%s" % get_address(value) |
paul@213 | 860 | |
paul@792 | 861 | def uri_parts(values): |
paul@792 | 862 | |
paul@792 | 863 | "Return any common name plus the URI for each of the given 'values'." |
paul@792 | 864 | |
paul@792 | 865 | return [(name, get_uri(address)) for name, address in get_address_parts(values)] |
paul@792 | 866 | |
paul@309 | 867 | uri_value = get_uri |
paul@309 | 868 | |
paul@309 | 869 | def uri_values(values): |
paul@309 | 870 | return map(get_uri, values) |
paul@309 | 871 | |
paul@213 | 872 | def uri_dict(d): |
paul@213 | 873 | return dict([(get_uri(key), value) for key, value in d.items()]) |
paul@213 | 874 | |
paul@213 | 875 | def uri_item(item): |
paul@213 | 876 | return get_uri(item[0]), item[1] |
paul@213 | 877 | |
paul@213 | 878 | def uri_items(items): |
paul@213 | 879 | return [(get_uri(value), attr) for value, attr in items] |
paul@213 | 880 | |
paul@220 | 881 | # Operations on structure data. |
paul@220 | 882 | |
paul@682 | 883 | def is_new_object(old_sequence, new_sequence, old_dtstamp, new_dtstamp, ignore_dtstamp): |
paul@220 | 884 | |
paul@220 | 885 | """ |
paul@220 | 886 | Return for the given 'old_sequence' and 'new_sequence', 'old_dtstamp' and |
paul@682 | 887 | 'new_dtstamp', and the 'ignore_dtstamp' indication, whether the object |
paul@220 | 888 | providing the new information is really newer than the object providing the |
paul@220 | 889 | old information. |
paul@220 | 890 | """ |
paul@220 | 891 | |
paul@220 | 892 | have_sequence = old_sequence is not None and new_sequence is not None |
paul@220 | 893 | is_same_sequence = have_sequence and int(new_sequence) == int(old_sequence) |
paul@220 | 894 | |
paul@220 | 895 | have_dtstamp = old_dtstamp and new_dtstamp |
paul@220 | 896 | is_old_dtstamp = have_dtstamp and new_dtstamp < old_dtstamp or old_dtstamp and not new_dtstamp |
paul@220 | 897 | |
paul@220 | 898 | is_old_sequence = have_sequence and ( |
paul@220 | 899 | int(new_sequence) < int(old_sequence) or |
paul@220 | 900 | is_same_sequence and is_old_dtstamp |
paul@220 | 901 | ) |
paul@220 | 902 | |
paul@682 | 903 | return is_same_sequence and ignore_dtstamp or not is_old_sequence |
paul@220 | 904 | |
paul@630 | 905 | def get_periods(obj, tzid, end=None, inclusive=False): |
paul@256 | 906 | |
paul@256 | 907 | """ |
paul@618 | 908 | Return periods for the given object 'obj', employing the given 'tzid' where |
paul@618 | 909 | no time zone information is available (for whole day events, for example), |
paul@630 | 910 | confining materialised periods to before the given 'end' datetime. |
paul@618 | 911 | |
paul@630 | 912 | If 'end' is omitted, only explicit recurrences and recurrences from |
paul@630 | 913 | explicitly-terminated rules will be returned. |
paul@630 | 914 | |
paul@630 | 915 | If 'inclusive' is set to a true value, any period occurring at the 'end' |
paul@630 | 916 | will be included. |
paul@256 | 917 | """ |
paul@256 | 918 | |
paul@318 | 919 | rrule = obj.get_value("RRULE") |
paul@636 | 920 | parameters = rrule and get_parameters(rrule) |
paul@318 | 921 | |
paul@318 | 922 | # Use localised datetimes. |
paul@318 | 923 | |
paul@797 | 924 | main_period = obj.get_main_period(tzid) |
paul@797 | 925 | |
paul@797 | 926 | dtstart = main_period.get_start() |
paul@797 | 927 | dtstart_attr = main_period.get_start_attr() |
paul@256 | 928 | |
paul@618 | 929 | # Attempt to get time zone details from the object, using the supplied zone |
paul@618 | 930 | # only as a fallback. |
paul@618 | 931 | |
paul@638 | 932 | obj_tzid = obj.get_tzid() |
paul@256 | 933 | |
paul@352 | 934 | if not rrule: |
paul@797 | 935 | periods = [main_period] |
paul@630 | 936 | |
paul@636 | 937 | elif end or parameters and parameters.has_key("UNTIL") or parameters.has_key("COUNT"): |
paul@630 | 938 | |
paul@352 | 939 | # Recurrence rules create multiple instances to be checked. |
paul@352 | 940 | # Conflicts may only be assessed within a period defined by policy |
paul@352 | 941 | # for the agent, with instances outside that period being considered |
paul@352 | 942 | # unchecked. |
paul@352 | 943 | |
paul@352 | 944 | selector = get_rule(dtstart, rrule) |
paul@352 | 945 | periods = [] |
paul@352 | 946 | |
paul@521 | 947 | until = parameters.get("UNTIL") |
paul@521 | 948 | if until: |
paul@650 | 949 | until_dt = to_timezone(get_datetime(until, dtstart_attr), obj_tzid) |
paul@650 | 950 | end = end and min(until_dt, end) or until_dt |
paul@521 | 951 | inclusive = True |
paul@521 | 952 | |
paul@630 | 953 | for recurrence_start in selector.materialise(dtstart, end, parameters.get("COUNT"), parameters.get("BYSETPOS"), inclusive): |
paul@630 | 954 | create = len(recurrence_start) == 3 and date or datetime |
paul@638 | 955 | recurrence_start = to_timezone(create(*recurrence_start), obj_tzid) |
paul@878 | 956 | recurrence_end = recurrence_start + main_period.get_duration() |
paul@638 | 957 | periods.append(RecurringPeriod(recurrence_start, recurrence_end, tzid, "RRULE", dtstart_attr)) |
paul@352 | 958 | |
paul@635 | 959 | else: |
paul@635 | 960 | periods = [] |
paul@635 | 961 | |
paul@352 | 962 | # Add recurrence dates. |
paul@256 | 963 | |
paul@878 | 964 | rdates = obj.get_date_value_item_periods("RDATE", tzid) |
paul@352 | 965 | if rdates: |
paul@878 | 966 | periods += rdates |
paul@424 | 967 | |
paul@424 | 968 | # Return a sorted list of the periods. |
paul@424 | 969 | |
paul@542 | 970 | periods.sort() |
paul@352 | 971 | |
paul@352 | 972 | # Exclude exception dates. |
paul@352 | 973 | |
paul@878 | 974 | exdates = obj.get_date_value_item_periods("EXDATE", tzid) |
paul@256 | 975 | |
paul@352 | 976 | if exdates: |
paul@878 | 977 | for period in exdates: |
paul@424 | 978 | i = bisect_left(periods, period) |
paul@458 | 979 | while i < len(periods) and periods[i] == period: |
paul@424 | 980 | del periods[i] |
paul@256 | 981 | |
paul@256 | 982 | return periods |
paul@256 | 983 | |
paul@606 | 984 | def get_sender_identities(mapping): |
paul@606 | 985 | |
paul@606 | 986 | """ |
paul@606 | 987 | Return a mapping from actual senders to the identities for which they |
paul@606 | 988 | have provided data, extracting this information from the given |
paul@606 | 989 | 'mapping'. |
paul@606 | 990 | """ |
paul@606 | 991 | |
paul@606 | 992 | senders = {} |
paul@606 | 993 | |
paul@606 | 994 | for value, attr in mapping.items(): |
paul@606 | 995 | sent_by = attr.get("SENT-BY") |
paul@606 | 996 | if sent_by: |
paul@606 | 997 | sender = get_uri(sent_by) |
paul@606 | 998 | else: |
paul@606 | 999 | sender = value |
paul@606 | 1000 | |
paul@606 | 1001 | if not senders.has_key(sender): |
paul@606 | 1002 | senders[sender] = [] |
paul@606 | 1003 | |
paul@606 | 1004 | senders[sender].append(value) |
paul@606 | 1005 | |
paul@606 | 1006 | return senders |
paul@606 | 1007 | |
paul@618 | 1008 | def get_window_end(tzid, days=100): |
paul@606 | 1009 | |
paul@618 | 1010 | """ |
paul@618 | 1011 | Return a datetime in the time zone indicated by 'tzid' marking the end of a |
paul@618 | 1012 | window of the given number of 'days'. |
paul@618 | 1013 | """ |
paul@618 | 1014 | |
paul@618 | 1015 | return to_timezone(datetime.now(), tzid) + timedelta(days) |
paul@606 | 1016 | |
paul@213 | 1017 | # vim: tabstop=4 expandtab shiftwidth=4 |