1.1 --- a/imip_resource.py Wed Oct 22 14:20:59 2014 +0200
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,272 +0,0 @@
1.4 -#!/usr/bin/env python
1.5 -
1.6 -"""
1.7 -Handlers for a resource.
1.8 -"""
1.9 -
1.10 -from datetime import date, datetime, timedelta
1.11 -from imiptools.content import Handler, format_datetime
1.12 -from imiptools.period import insert_period, period_overlaps, remove_period
1.13 -from vCalendar import to_node
1.14 -from vRecurrence import get_parameters, get_rule
1.15 -
1.16 -class Event(Handler):
1.17 -
1.18 - "An event handler."
1.19 -
1.20 - def add(self):
1.21 - pass
1.22 -
1.23 - def cancel(self):
1.24 - pass
1.25 -
1.26 - def counter(self):
1.27 -
1.28 - "Since this handler does not send requests, it will not handle replies."
1.29 -
1.30 - pass
1.31 -
1.32 - def declinecounter(self):
1.33 -
1.34 - """
1.35 - Since this handler does not send counter proposals, it will not handle
1.36 - replies to such proposals.
1.37 - """
1.38 -
1.39 - pass
1.40 -
1.41 - def publish(self):
1.42 - pass
1.43 -
1.44 - def refresh(self):
1.45 - pass
1.46 -
1.47 - def reply(self):
1.48 -
1.49 - "Since this handler does not send requests, it will not handle replies."
1.50 -
1.51 - pass
1.52 -
1.53 - def request(self):
1.54 -
1.55 - """
1.56 - Respond to a request by preparing a reply containing accept/decline
1.57 - information for each indicated attendee.
1.58 -
1.59 - No support for countering requests is implemented.
1.60 - """
1.61 -
1.62 - oa = self.require_organiser_and_attendees()
1.63 - if not oa:
1.64 - return None
1.65 -
1.66 - (organiser, organiser_attr), attendees = oa
1.67 -
1.68 - # Process each attendee separately.
1.69 -
1.70 - calendar = []
1.71 -
1.72 - for attendee, attendee_attr in attendees.items():
1.73 -
1.74 - # Check for event using UID.
1.75 -
1.76 - if not self.have_new_object(attendee, "VEVENT"):
1.77 - continue
1.78 -
1.79 - # If newer than any old version, discard old details from the
1.80 - # free/busy record and check for suitability.
1.81 -
1.82 - dtstart = self.get_utc_datetime("DTSTART")
1.83 - dtend = self.get_utc_datetime("DTEND")
1.84 -
1.85 - # NOTE: Need also DURATION support.
1.86 -
1.87 - duration = dtend - dtstart
1.88 -
1.89 - # Recurrence rules create multiple instances to be checked.
1.90 - # Conflicts may only be assessed within a period defined by policy
1.91 - # for the agent, with instances outside that period being considered
1.92 - # unchecked.
1.93 -
1.94 - # NOTE: Need to expose the 100 day window in the configuration.
1.95 -
1.96 - window_end = datetime.now() + timedelta(100)
1.97 -
1.98 - # NOTE: Need also RDATE and EXDATE support.
1.99 -
1.100 - rrule = self.get_value("RRULE")
1.101 -
1.102 - if rrule:
1.103 - selector = get_rule(dtstart, rrule)
1.104 - parameters = get_parameters(rrule)
1.105 - periods = []
1.106 - for start in selector.materialise(dtstart, window_end, parameters.get("COUNT"), parameters.get("BYSETPOS")):
1.107 - start = datetime(*start, tzinfo=timezone("UTC"))
1.108 - end = start + duration
1.109 - periods.append((format_datetime(start), format_datetime(end)))
1.110 - else:
1.111 - periods = [(format_datetime(dtstart), format_datetime(dtend))]
1.112 -
1.113 - conflict = False
1.114 - freebusy = self.store.get_freebusy(attendee)
1.115 -
1.116 - if freebusy:
1.117 - remove_period(freebusy, self.uid)
1.118 - conflict = True
1.119 - for start, end in periods:
1.120 - if period_overlaps(freebusy, (start, end)):
1.121 - break
1.122 - else:
1.123 - conflict = False
1.124 - else:
1.125 - freebusy = []
1.126 -
1.127 - # If the event can be scheduled, it is registered and a reply sent
1.128 - # accepting the event. (The attendee has PARTSTAT=ACCEPTED as an
1.129 - # attribute.)
1.130 -
1.131 - if not conflict:
1.132 - for start, end in periods:
1.133 - insert_period(freebusy, (start, end, self.uid))
1.134 -
1.135 - if self.get_value("TRANSP") in (None, "OPAQUE"):
1.136 - self.store.set_freebusy(attendee, freebusy)
1.137 -
1.138 - if self.publisher:
1.139 - self.publisher.set_freebusy(attendee, freebusy)
1.140 -
1.141 - self.store.set_event(attendee, self.uid, to_node(
1.142 - {"VEVENT" : [(self.details, {})]}
1.143 - ))
1.144 - attendee_attr["PARTSTAT"] = "ACCEPTED"
1.145 -
1.146 - # If the event cannot be scheduled, it is not registered and a reply
1.147 - # sent declining the event. (The attendee has PARTSTAT=DECLINED as an
1.148 - # attribute.)
1.149 -
1.150 - else:
1.151 - attendee_attr["PARTSTAT"] = "DECLINED"
1.152 -
1.153 - self.details["ATTENDEE"] = [(attendee, attendee_attr)]
1.154 - calendar.append(to_node(
1.155 - {"VEVENT" : [(self.details, {})]}
1.156 - ))
1.157 -
1.158 - return calendar
1.159 -
1.160 -class Freebusy(Handler):
1.161 -
1.162 - "A free/busy handler."
1.163 -
1.164 - def publish(self):
1.165 - pass
1.166 -
1.167 - def reply(self):
1.168 -
1.169 - "Since this handler does not send requests, it will not handle replies."
1.170 -
1.171 - pass
1.172 -
1.173 - def request(self):
1.174 -
1.175 - """
1.176 - Respond to a request by preparing a reply containing free/busy
1.177 - information for each indicated attendee.
1.178 - """
1.179 -
1.180 - oa = self.require_organiser_and_attendees()
1.181 - if not oa:
1.182 - return None
1.183 -
1.184 - (organiser, organiser_attr), attendees = oa
1.185 -
1.186 - # Construct an appropriate fragment.
1.187 -
1.188 - calendar = []
1.189 - cwrite = calendar.append
1.190 -
1.191 - # Get the details for each attendee.
1.192 -
1.193 - for attendee, attendee_attr in attendees.items():
1.194 - freebusy = self.store.get_freebusy(attendee)
1.195 -
1.196 - if freebusy:
1.197 - record = []
1.198 - rwrite = record.append
1.199 -
1.200 - rwrite(("ORGANIZER", organiser_attr, organiser))
1.201 - rwrite(("ATTENDEE", attendee_attr, attendee))
1.202 - rwrite(("UID", {}, self.uid))
1.203 -
1.204 - for start, end, uid in freebusy:
1.205 - rwrite(("FREEBUSY", {"FBTYPE" : "BUSY"}, [start, end]))
1.206 -
1.207 - cwrite(("VFREEBUSY", {}, record))
1.208 -
1.209 - # Return the reply.
1.210 -
1.211 - return calendar
1.212 -
1.213 -class Journal(Handler):
1.214 -
1.215 - "A journal entry handler."
1.216 -
1.217 - def add(self):
1.218 - pass
1.219 -
1.220 - def cancel(self):
1.221 - pass
1.222 -
1.223 - def publish(self):
1.224 - pass
1.225 -
1.226 -class Todo(Handler):
1.227 -
1.228 - "A to-do item handler."
1.229 -
1.230 - def add(self):
1.231 - pass
1.232 -
1.233 - def cancel(self):
1.234 - pass
1.235 -
1.236 - def counter(self):
1.237 -
1.238 - "Since this handler does not send requests, it will not handle replies."
1.239 -
1.240 - pass
1.241 -
1.242 - def declinecounter(self):
1.243 -
1.244 - """
1.245 - Since this handler does not send counter proposals, it will not handle
1.246 - replies to such proposals.
1.247 - """
1.248 -
1.249 - pass
1.250 -
1.251 - def publish(self):
1.252 - pass
1.253 -
1.254 - def refresh(self):
1.255 - pass
1.256 -
1.257 - def reply(self):
1.258 -
1.259 - "Since this handler does not send requests, it will not handle replies."
1.260 -
1.261 - pass
1.262 -
1.263 - def request(self):
1.264 - pass
1.265 -
1.266 -# Handler registry.
1.267 -
1.268 -handlers = [
1.269 - ("VFREEBUSY", Freebusy),
1.270 - ("VEVENT", Event),
1.271 - ("VTODO", Todo),
1.272 - ("VJOURNAL", Journal),
1.273 - ]
1.274 -
1.275 -# vim: tabstop=4 expandtab shiftwidth=4
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/imiptools/handlers/resource.py Wed Oct 22 15:39:21 2014 +0200
2.3 @@ -0,0 +1,272 @@
2.4 +#!/usr/bin/env python
2.5 +
2.6 +"""
2.7 +Handlers for a resource.
2.8 +"""
2.9 +
2.10 +from datetime import date, datetime, timedelta
2.11 +from imiptools.content import Handler, format_datetime
2.12 +from imiptools.period import insert_period, period_overlaps, remove_period
2.13 +from vCalendar import to_node
2.14 +from vRecurrence import get_parameters, get_rule
2.15 +
2.16 +class Event(Handler):
2.17 +
2.18 + "An event handler."
2.19 +
2.20 + def add(self):
2.21 + pass
2.22 +
2.23 + def cancel(self):
2.24 + pass
2.25 +
2.26 + def counter(self):
2.27 +
2.28 + "Since this handler does not send requests, it will not handle replies."
2.29 +
2.30 + pass
2.31 +
2.32 + def declinecounter(self):
2.33 +
2.34 + """
2.35 + Since this handler does not send counter proposals, it will not handle
2.36 + replies to such proposals.
2.37 + """
2.38 +
2.39 + pass
2.40 +
2.41 + def publish(self):
2.42 + pass
2.43 +
2.44 + def refresh(self):
2.45 + pass
2.46 +
2.47 + def reply(self):
2.48 +
2.49 + "Since this handler does not send requests, it will not handle replies."
2.50 +
2.51 + pass
2.52 +
2.53 + def request(self):
2.54 +
2.55 + """
2.56 + Respond to a request by preparing a reply containing accept/decline
2.57 + information for each indicated attendee.
2.58 +
2.59 + No support for countering requests is implemented.
2.60 + """
2.61 +
2.62 + oa = self.require_organiser_and_attendees()
2.63 + if not oa:
2.64 + return None
2.65 +
2.66 + (organiser, organiser_attr), attendees = oa
2.67 +
2.68 + # Process each attendee separately.
2.69 +
2.70 + calendar = []
2.71 +
2.72 + for attendee, attendee_attr in attendees.items():
2.73 +
2.74 + # Check for event using UID.
2.75 +
2.76 + if not self.have_new_object(attendee, "VEVENT"):
2.77 + continue
2.78 +
2.79 + # If newer than any old version, discard old details from the
2.80 + # free/busy record and check for suitability.
2.81 +
2.82 + dtstart = self.get_utc_datetime("DTSTART")
2.83 + dtend = self.get_utc_datetime("DTEND")
2.84 +
2.85 + # NOTE: Need also DURATION support.
2.86 +
2.87 + duration = dtend - dtstart
2.88 +
2.89 + # Recurrence rules create multiple instances to be checked.
2.90 + # Conflicts may only be assessed within a period defined by policy
2.91 + # for the agent, with instances outside that period being considered
2.92 + # unchecked.
2.93 +
2.94 + # NOTE: Need to expose the 100 day window in the configuration.
2.95 +
2.96 + window_end = datetime.now() + timedelta(100)
2.97 +
2.98 + # NOTE: Need also RDATE and EXDATE support.
2.99 +
2.100 + rrule = self.get_value("RRULE")
2.101 +
2.102 + if rrule:
2.103 + selector = get_rule(dtstart, rrule)
2.104 + parameters = get_parameters(rrule)
2.105 + periods = []
2.106 + for start in selector.materialise(dtstart, window_end, parameters.get("COUNT"), parameters.get("BYSETPOS")):
2.107 + start = datetime(*start, tzinfo=timezone("UTC"))
2.108 + end = start + duration
2.109 + periods.append((format_datetime(start), format_datetime(end)))
2.110 + else:
2.111 + periods = [(format_datetime(dtstart), format_datetime(dtend))]
2.112 +
2.113 + conflict = False
2.114 + freebusy = self.store.get_freebusy(attendee)
2.115 +
2.116 + if freebusy:
2.117 + remove_period(freebusy, self.uid)
2.118 + conflict = True
2.119 + for start, end in periods:
2.120 + if period_overlaps(freebusy, (start, end)):
2.121 + break
2.122 + else:
2.123 + conflict = False
2.124 + else:
2.125 + freebusy = []
2.126 +
2.127 + # If the event can be scheduled, it is registered and a reply sent
2.128 + # accepting the event. (The attendee has PARTSTAT=ACCEPTED as an
2.129 + # attribute.)
2.130 +
2.131 + if not conflict:
2.132 + for start, end in periods:
2.133 + insert_period(freebusy, (start, end, self.uid))
2.134 +
2.135 + if self.get_value("TRANSP") in (None, "OPAQUE"):
2.136 + self.store.set_freebusy(attendee, freebusy)
2.137 +
2.138 + if self.publisher:
2.139 + self.publisher.set_freebusy(attendee, freebusy)
2.140 +
2.141 + self.store.set_event(attendee, self.uid, to_node(
2.142 + {"VEVENT" : [(self.details, {})]}
2.143 + ))
2.144 + attendee_attr["PARTSTAT"] = "ACCEPTED"
2.145 +
2.146 + # If the event cannot be scheduled, it is not registered and a reply
2.147 + # sent declining the event. (The attendee has PARTSTAT=DECLINED as an
2.148 + # attribute.)
2.149 +
2.150 + else:
2.151 + attendee_attr["PARTSTAT"] = "DECLINED"
2.152 +
2.153 + self.details["ATTENDEE"] = [(attendee, attendee_attr)]
2.154 + calendar.append(to_node(
2.155 + {"VEVENT" : [(self.details, {})]}
2.156 + ))
2.157 +
2.158 + return calendar
2.159 +
2.160 +class Freebusy(Handler):
2.161 +
2.162 + "A free/busy handler."
2.163 +
2.164 + def publish(self):
2.165 + pass
2.166 +
2.167 + def reply(self):
2.168 +
2.169 + "Since this handler does not send requests, it will not handle replies."
2.170 +
2.171 + pass
2.172 +
2.173 + def request(self):
2.174 +
2.175 + """
2.176 + Respond to a request by preparing a reply containing free/busy
2.177 + information for each indicated attendee.
2.178 + """
2.179 +
2.180 + oa = self.require_organiser_and_attendees()
2.181 + if not oa:
2.182 + return None
2.183 +
2.184 + (organiser, organiser_attr), attendees = oa
2.185 +
2.186 + # Construct an appropriate fragment.
2.187 +
2.188 + calendar = []
2.189 + cwrite = calendar.append
2.190 +
2.191 + # Get the details for each attendee.
2.192 +
2.193 + for attendee, attendee_attr in attendees.items():
2.194 + freebusy = self.store.get_freebusy(attendee)
2.195 +
2.196 + if freebusy:
2.197 + record = []
2.198 + rwrite = record.append
2.199 +
2.200 + rwrite(("ORGANIZER", organiser_attr, organiser))
2.201 + rwrite(("ATTENDEE", attendee_attr, attendee))
2.202 + rwrite(("UID", {}, self.uid))
2.203 +
2.204 + for start, end, uid in freebusy:
2.205 + rwrite(("FREEBUSY", {"FBTYPE" : "BUSY"}, [start, end]))
2.206 +
2.207 + cwrite(("VFREEBUSY", {}, record))
2.208 +
2.209 + # Return the reply.
2.210 +
2.211 + return calendar
2.212 +
2.213 +class Journal(Handler):
2.214 +
2.215 + "A journal entry handler."
2.216 +
2.217 + def add(self):
2.218 + pass
2.219 +
2.220 + def cancel(self):
2.221 + pass
2.222 +
2.223 + def publish(self):
2.224 + pass
2.225 +
2.226 +class Todo(Handler):
2.227 +
2.228 + "A to-do item handler."
2.229 +
2.230 + def add(self):
2.231 + pass
2.232 +
2.233 + def cancel(self):
2.234 + pass
2.235 +
2.236 + def counter(self):
2.237 +
2.238 + "Since this handler does not send requests, it will not handle replies."
2.239 +
2.240 + pass
2.241 +
2.242 + def declinecounter(self):
2.243 +
2.244 + """
2.245 + Since this handler does not send counter proposals, it will not handle
2.246 + replies to such proposals.
2.247 + """
2.248 +
2.249 + pass
2.250 +
2.251 + def publish(self):
2.252 + pass
2.253 +
2.254 + def refresh(self):
2.255 + pass
2.256 +
2.257 + def reply(self):
2.258 +
2.259 + "Since this handler does not send requests, it will not handle replies."
2.260 +
2.261 + pass
2.262 +
2.263 + def request(self):
2.264 + pass
2.265 +
2.266 +# Handler registry.
2.267 +
2.268 +handlers = [
2.269 + ("VFREEBUSY", Freebusy),
2.270 + ("VEVENT", Event),
2.271 + ("VTODO", Todo),
2.272 + ("VJOURNAL", Journal),
2.273 + ]
2.274 +
2.275 +# vim: tabstop=4 expandtab shiftwidth=4