imip-agent

Annotated imiptools/data.py

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