1.1 --- a/tools/make_freebusy.py Mon Mar 02 18:38:25 2015 +0100
1.2 +++ b/tools/make_freebusy.py Mon Mar 02 18:52:07 2015 +0100
1.3 @@ -1,6 +1,6 @@
1.4 #!/usr/bin/env python
1.5
1.6 -from imiptools.data import get_freebusy_period, get_datetime_item, get_utc_datetime, get_value, get_value_map
1.7 +from imiptools.data import get_window_end, Object
1.8 from imiptools.dates import format_datetime, get_default_timezone
1.9 from imiptools.profile import Preferences
1.10 from imip_store import FileStore, FilePublisher
1.11 @@ -8,59 +8,82 @@
1.12
1.13 try:
1.14 user = sys.argv[1]
1.15 + store_and_publish = "-s" in sys.argv[2:]
1.16 except IndexError:
1.17 - print >>sys.stderr, "Need a user."
1.18 + print >>sys.stderr, "Need a user, along with the -s option if updating the store."
1.19 sys.exit(1)
1.20
1.21 preferences = Preferences(user)
1.22 tzid = preferences.get("TZID") or get_default_timezone()
1.23
1.24 -s = FileStore()
1.25 -p = FilePublisher()
1.26 +# Get the size of the free/busy window.
1.27
1.28 -events = s.get_events(user)
1.29 +try:
1.30 + window_size = int(preferences.get("window_size"))
1.31 +except (TypeError, ValueError):
1.32 + window_size = 100
1.33 +window_end = get_window_end(tzid, window_size)
1.34 +
1.35 +store = FileStore()
1.36 +publisher = FilePublisher()
1.37 +
1.38 +# Get all identifiers for events.
1.39 +
1.40 +uids = store.get_events(user)
1.41
1.42 all_events = set()
1.43 -for uid in events:
1.44 +for uid in uids:
1.45 all_events.add((uid, None))
1.46 - all_events.update([(uid, recurrenceid) for recurrenceid in s.get_recurrences(user, uid)])
1.47 + all_events.update([(uid, recurrenceid) for recurrenceid in store.get_recurrences(user, uid)])
1.48 +
1.49 +# Filter out cancelled events.
1.50
1.51 -cancelled = s.get_cancellations(user) or []
1.52 +cancelled = store.get_cancellations(user) or []
1.53 all_events.difference_update(cancelled)
1.54
1.55 +# Obtain event objects.
1.56 +
1.57 objs = []
1.58 for uid, recurrenceid in all_events:
1.59 print >>sys.stderr, uid, recurrenceid
1.60 - objs.append(s.get_event(user, uid, recurrenceid))
1.61 + event = store.get_event(user, uid, recurrenceid)
1.62 + if event:
1.63 + objs.append(Object(event))
1.64 +
1.65 +# Build a free/busy collection for the given user.
1.66
1.67 fb = []
1.68 for obj in objs:
1.69 - if not obj:
1.70 - continue
1.71 - details, details_attr = obj.values()[0]
1.72 -
1.73 participants = {}
1.74 - participants.update(get_value_map(details, "ATTENDEE"))
1.75 - participants.update(get_value_map(details, "ORGANIZER"))
1.76 + participants.update(obj.get_value_map("ATTENDEE"))
1.77 + participants.update(obj.get_value_map("ORGANIZER"))
1.78
1.79 for participant, participant_attr in participants.items():
1.80 +
1.81 + # Only consider events where this user actually participates.
1.82 +
1.83 if participant == user:
1.84 - if participant_attr.get("PARTSTAT") != "DECLINED":
1.85 - dtstart, dtstart_attr = get_datetime_item(details, "DTSTART")
1.86 - dtend, dtend_attr = get_datetime_item(details, "DTEND")
1.87 - event_tzid = dtstart_attr.get("TZID") or dtend_attr.get("TZID") or tzid
1.88 - dtstart, dtend = get_freebusy_period(dtstart, dtend, event_tzid)
1.89 - fb.append((format_datetime(dtstart),
1.90 - format_datetime(dtend),
1.91 - get_value(details, "UID"),
1.92 - get_value(details, "TRANSP") or "OPAQUE",
1.93 - format_datetime(get_utc_datetime(details, "RECURRENCE-ID")) or "",
1.94 + if participant_attr.get("PARTSTAT") not in ("DECLINED", "DELEGATED", "NEEDS-ACTION"):
1.95 +
1.96 + # Obtain the actual periods associated with the event.
1.97 +
1.98 + for start, end in obj.get_periods_for_freebusy(tzid, window_end):
1.99 + fb.append((start, end,
1.100 + obj.get_value("UID"),
1.101 + obj.get_value("TRANSP") or "OPAQUE",
1.102 + format_datetime(obj.get_utc_datetime("RECURRENCE-ID")) or "",
1.103 ))
1.104 break
1.105
1.106 fb.sort()
1.107
1.108 -s.set_freebusy(user, fb)
1.109 -p.set_freebusy(user, fb)
1.110 +# Store and publish the free/busy collection.
1.111 +
1.112 +if store_and_publish:
1.113 + store.set_freebusy(user, fb)
1.114 + publisher.set_freebusy(user, fb)
1.115 +else:
1.116 + for item in fb:
1.117 + print "\t".join(item)
1.118
1.119 # vim: tabstop=4 expandtab shiftwidth=4