paul@497 | 1 | #!/usr/bin/env python |
paul@497 | 2 | |
paul@497 | 3 | """ |
paul@497 | 4 | Web interface data abstractions. |
paul@497 | 5 | |
paul@497 | 6 | Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk> |
paul@497 | 7 | |
paul@497 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@497 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@497 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@497 | 11 | version. |
paul@497 | 12 | |
paul@497 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@497 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@497 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@497 | 16 | details. |
paul@497 | 17 | |
paul@497 | 18 | You should have received a copy of the GNU General Public License along with |
paul@497 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@497 | 20 | """ |
paul@497 | 21 | |
paul@497 | 22 | from datetime import datetime, timedelta |
paul@498 | 23 | from imiptools.dates import format_datetime, get_datetime, get_start_of_day, to_date |
paul@497 | 24 | from imiptools.period import Period |
paul@497 | 25 | |
paul@498 | 26 | class PeriodError(Exception): |
paul@498 | 27 | pass |
paul@498 | 28 | |
paul@497 | 29 | class EventPeriod(Period): |
paul@497 | 30 | |
paul@498 | 31 | """ |
paul@498 | 32 | A simple period plus attribute details, compatible with RecurringPeriod, and |
paul@498 | 33 | intended to represent information obtained from an iCalendar resource. |
paul@498 | 34 | """ |
paul@497 | 35 | |
paul@498 | 36 | def __init__(self, start, end, start_attr=None, end_attr=None, form_start=None, form_end=None): |
paul@497 | 37 | Period.__init__(self, start, end) |
paul@497 | 38 | self.start_attr = start_attr |
paul@497 | 39 | self.end_attr = end_attr |
paul@498 | 40 | self.form_start = form_start |
paul@498 | 41 | self.form_end = form_end |
paul@498 | 42 | |
paul@498 | 43 | def as_tuple(self): |
paul@498 | 44 | return self.start, self.end, self.start_attr, self.end_attr, self.form_start, self.form_end |
paul@498 | 45 | |
paul@498 | 46 | def __repr__(self): |
paul@498 | 47 | return "EventPeriod(%r, %r, %r, %r, %r, %r)" % self.as_tuple() |
paul@498 | 48 | |
paul@498 | 49 | def get_start(self): |
paul@498 | 50 | return self.start |
paul@498 | 51 | |
paul@498 | 52 | def get_end(self): |
paul@498 | 53 | return self.end |
paul@498 | 54 | |
paul@498 | 55 | def as_event_period(self): |
paul@498 | 56 | return self |
paul@498 | 57 | |
paul@498 | 58 | def get_form_start(self): |
paul@498 | 59 | if not self.form_start: |
paul@498 | 60 | self.form_start = self.get_form_date(self.start, self.start_attr) |
paul@498 | 61 | return self.form_start |
paul@498 | 62 | |
paul@498 | 63 | def get_form_end(self): |
paul@498 | 64 | if not self.form_end: |
paul@498 | 65 | self.form_end = self.get_form_date(self.end, self.end_attr) |
paul@498 | 66 | return self.form_end |
paul@498 | 67 | |
paul@498 | 68 | def as_form_period(self): |
paul@498 | 69 | return FormPeriod( |
paul@498 | 70 | self.get_form_date(self.start, self.start_attr), |
paul@498 | 71 | self.get_form_date(self.end, self.end_attr), |
paul@498 | 72 | isinstance(self.end, datetime) or self.start != self.end - timedelta(1), |
paul@498 | 73 | isinstance(self.start, datetime) or isinstance(self.end, datetime) |
paul@498 | 74 | ) |
paul@498 | 75 | |
paul@498 | 76 | def get_form_date(self, dt, attr=None): |
paul@498 | 77 | return FormDate( |
paul@498 | 78 | format_datetime(to_date(dt)), |
paul@498 | 79 | isinstance(dt, datetime) and str(dt.hour) or None, |
paul@498 | 80 | isinstance(dt, datetime) and str(dt.minute) or None, |
paul@498 | 81 | isinstance(dt, datetime) and str(dt.second) or None, |
paul@498 | 82 | attr and attr.get("TZID") or None, |
paul@498 | 83 | dt, attr |
paul@498 | 84 | ) |
paul@498 | 85 | |
paul@498 | 86 | def event_period_from_recurrence_period(period): |
paul@498 | 87 | return EventPeriod(period.start, period.end, period.start_attr, period.end_attr) |
paul@498 | 88 | |
paul@498 | 89 | class FormPeriod: |
paul@498 | 90 | |
paul@498 | 91 | "A period whose information originates from a form." |
paul@498 | 92 | |
paul@498 | 93 | def __init__(self, start, end, end_enabled=True, times_enabled=True): |
paul@498 | 94 | self.start = start |
paul@498 | 95 | self.end = end |
paul@498 | 96 | self.end_enabled = end_enabled |
paul@498 | 97 | self.times_enabled = times_enabled |
paul@497 | 98 | |
paul@497 | 99 | def as_tuple(self): |
paul@498 | 100 | return self.start, self.end, self.end_enabled, self.times_enabled |
paul@497 | 101 | |
paul@497 | 102 | def __repr__(self): |
paul@498 | 103 | return "FormPeriod(%r, %r, %r, %r)" % self.as_tuple() |
paul@498 | 104 | |
paul@498 | 105 | def _get_start(self): |
paul@498 | 106 | t = self.start.as_datetime_item(self.times_enabled) |
paul@498 | 107 | if t: |
paul@498 | 108 | return t |
paul@498 | 109 | else: |
paul@498 | 110 | return None |
paul@498 | 111 | |
paul@498 | 112 | def _get_end(self): |
paul@498 | 113 | |
paul@498 | 114 | # Handle specified end datetimes. |
paul@498 | 115 | |
paul@498 | 116 | if self.end_enabled: |
paul@498 | 117 | t = self.end.as_datetime_item(self.times_enabled) |
paul@498 | 118 | if t: |
paul@498 | 119 | dtend, dtend_attr = t |
paul@498 | 120 | else: |
paul@498 | 121 | return None |
paul@498 | 122 | |
paul@498 | 123 | # Otherwise, treat the end date as the start date. Datetimes are |
paul@498 | 124 | # handled by making the event occupy the rest of the day. |
paul@498 | 125 | |
paul@498 | 126 | else: |
paul@498 | 127 | t = self._get_start() |
paul@498 | 128 | if t: |
paul@498 | 129 | dtstart, dtstart_attr = t |
paul@498 | 130 | dtend = dtstart + timedelta(1) |
paul@498 | 131 | dtend_attr = dtstart_attr |
paul@498 | 132 | |
paul@498 | 133 | if isinstance(dtstart, datetime): |
paul@498 | 134 | dtend = get_start_of_day(dtend, dtend_attr["TZID"]) |
paul@498 | 135 | else: |
paul@498 | 136 | return None |
paul@498 | 137 | |
paul@498 | 138 | return dtend, dtend_attr |
paul@498 | 139 | |
paul@498 | 140 | def get_start(self): |
paul@498 | 141 | t = self._get_start() |
paul@498 | 142 | if t: |
paul@498 | 143 | dtstart, dtstart_attr = t |
paul@498 | 144 | return dtstart |
paul@498 | 145 | else: |
paul@498 | 146 | return None |
paul@498 | 147 | |
paul@498 | 148 | def get_end(self): |
paul@498 | 149 | t = self._get_end() |
paul@498 | 150 | if t: |
paul@498 | 151 | dtend, dtend_attr = t |
paul@498 | 152 | return dtend |
paul@498 | 153 | else: |
paul@498 | 154 | return None |
paul@498 | 155 | |
paul@498 | 156 | def as_event_period(self, index=None): |
paul@498 | 157 | t = self._get_start() |
paul@498 | 158 | if t: |
paul@498 | 159 | dtstart, dtstart_attr = t |
paul@498 | 160 | else: |
paul@498 | 161 | raise PeriodError(*[index is not None and ("dtstart", index) or "dtstart"]) |
paul@498 | 162 | |
paul@498 | 163 | t = self._get_end() |
paul@498 | 164 | if t: |
paul@498 | 165 | dtend, dtend_attr = t |
paul@498 | 166 | else: |
paul@498 | 167 | raise PeriodError(*[index is not None and ("dtend", index) or "dtend"]) |
paul@498 | 168 | |
paul@498 | 169 | if dtstart > dtend: |
paul@498 | 170 | raise PeriodError(*[ |
paul@498 | 171 | index is not None and ("dtstart", index) or "dtstart", |
paul@498 | 172 | index is not None and ("dtend", index) or "dtend" |
paul@498 | 173 | ]) |
paul@498 | 174 | |
paul@498 | 175 | return EventPeriod(dtstart, dtend, dtstart_attr, dtend_attr, self.start, self.end) |
paul@498 | 176 | |
paul@498 | 177 | def get_form_start(self): |
paul@498 | 178 | return self.start |
paul@498 | 179 | |
paul@498 | 180 | def get_form_end(self): |
paul@498 | 181 | return self.end |
paul@498 | 182 | |
paul@498 | 183 | def as_form_period(self): |
paul@498 | 184 | return self |
paul@497 | 185 | |
paul@498 | 186 | class FormDate: |
paul@498 | 187 | |
paul@498 | 188 | "Date information originating from form information." |
paul@498 | 189 | |
paul@498 | 190 | def __init__(self, date=None, hour=None, minute=None, second=None, tzid=None, dt=None, attr=None): |
paul@498 | 191 | self.date = date |
paul@498 | 192 | self.hour = hour |
paul@498 | 193 | self.minute = minute |
paul@498 | 194 | self.second = second |
paul@498 | 195 | self.tzid = tzid |
paul@498 | 196 | self.dt = dt |
paul@498 | 197 | self.attr = attr |
paul@498 | 198 | |
paul@498 | 199 | def as_tuple(self): |
paul@498 | 200 | return self.date, self.hour, self.minute, self.second, self.tzid, self.dt, self.attr |
paul@498 | 201 | |
paul@498 | 202 | def __repr__(self): |
paul@498 | 203 | return "FormDate(%r, %r, %r, %r, %r, %r, %r)" % self.as_tuple() |
paul@498 | 204 | |
paul@498 | 205 | def get_component(self, value): |
paul@498 | 206 | return (value or "").rjust(2, "0")[:2] |
paul@498 | 207 | |
paul@498 | 208 | def get_hour(self): |
paul@498 | 209 | return self.get_component(self.hour) |
paul@498 | 210 | |
paul@498 | 211 | def get_minute(self): |
paul@498 | 212 | return self.get_component(self.minute) |
paul@498 | 213 | |
paul@498 | 214 | def get_second(self): |
paul@498 | 215 | return self.get_component(self.second) |
paul@498 | 216 | |
paul@498 | 217 | def get_date_string(self): |
paul@498 | 218 | return self.date or "" |
paul@498 | 219 | |
paul@498 | 220 | def get_datetime_string(self): |
paul@498 | 221 | if not self.date: |
paul@498 | 222 | return "" |
paul@498 | 223 | |
paul@498 | 224 | hour = self.hour; minute = self.minute; second = self.second |
paul@498 | 225 | |
paul@498 | 226 | if hour or minute or second: |
paul@498 | 227 | time = "T%s%s%s" % tuple(map(self.get_component, (hour, minute, second))) |
paul@498 | 228 | else: |
paul@498 | 229 | time = "" |
paul@498 | 230 | |
paul@498 | 231 | return "%s%s" % (self.date, time) |
paul@498 | 232 | |
paul@498 | 233 | def get_tzid(self): |
paul@498 | 234 | return self.tzid |
paul@498 | 235 | |
paul@498 | 236 | def as_datetime_item(self, with_time=True): |
paul@498 | 237 | |
paul@498 | 238 | """ |
paul@498 | 239 | Return a (datetime, attr) tuple for the datetime information provided by |
paul@498 | 240 | this object, or None if the fields cannot be used to construct a |
paul@498 | 241 | datetime object. |
paul@498 | 242 | """ |
paul@498 | 243 | |
paul@498 | 244 | # Return any original datetime details. |
paul@498 | 245 | |
paul@498 | 246 | if self.dt: |
paul@498 | 247 | return self.dt, self.attr |
paul@498 | 248 | |
paul@498 | 249 | # Otherwise, construct a datetime and attributes. |
paul@498 | 250 | |
paul@498 | 251 | if not self.date: |
paul@498 | 252 | return None |
paul@498 | 253 | elif with_time: |
paul@498 | 254 | attr = {"TZID" : self.get_tzid(), "VALUE" : "DATE-TIME"} |
paul@498 | 255 | dt = get_datetime(self.get_datetime_string(), attr) |
paul@498 | 256 | else: |
paul@498 | 257 | dt = None |
paul@498 | 258 | |
paul@498 | 259 | # Interpret incomplete datetimes as dates. |
paul@498 | 260 | |
paul@498 | 261 | if not dt: |
paul@498 | 262 | attr = {"VALUE" : "DATE"} |
paul@498 | 263 | dt = get_datetime(self.get_date_string(), attr) |
paul@498 | 264 | |
paul@498 | 265 | if dt: |
paul@498 | 266 | return dt, attr |
paul@498 | 267 | |
paul@498 | 268 | return None |
paul@498 | 269 | |
paul@498 | 270 | def end_date_to_calendar(dt): |
paul@497 | 271 | |
paul@497 | 272 | """ |
paul@498 | 273 | Change end dates to refer to the actual dates, not the iCalendar "next day" |
paul@498 | 274 | dates. |
paul@497 | 275 | """ |
paul@497 | 276 | |
paul@498 | 277 | if not isinstance(dt, datetime): |
paul@498 | 278 | return dt + timedelta(1) |
paul@497 | 279 | else: |
paul@498 | 280 | return dt |
paul@497 | 281 | |
paul@498 | 282 | def end_date_from_calendar(dt): |
paul@497 | 283 | |
paul@498 | 284 | """ |
paul@498 | 285 | Change end dates to refer to the actual dates, not the iCalendar "next day" |
paul@498 | 286 | dates. |
paul@498 | 287 | """ |
paul@497 | 288 | |
paul@498 | 289 | if not isinstance(dt, datetime): |
paul@498 | 290 | return dt - timedelta(1) |
paul@497 | 291 | else: |
paul@498 | 292 | return dt |
paul@497 | 293 | |
paul@497 | 294 | # vim: tabstop=4 expandtab shiftwidth=4 |