paul@33 | 1 | #!/usr/bin/env python |
paul@33 | 2 | |
paul@33 | 3 | """ |
paul@33 | 4 | Recurrence rule calculation. |
paul@33 | 5 | |
paul@33 | 6 | Copyright (C) 2014 Paul Boddie <paul@boddie.org.uk> |
paul@33 | 7 | |
paul@33 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@33 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@33 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@33 | 11 | version. |
paul@33 | 12 | |
paul@33 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@33 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@33 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@33 | 16 | details. |
paul@33 | 17 | |
paul@33 | 18 | You should have received a copy of the GNU General Public License along with |
paul@33 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@33 | 20 | |
paul@33 | 21 | ---- |
paul@33 | 22 | |
paul@33 | 23 | References: |
paul@33 | 24 | |
paul@33 | 25 | RFC 5545: Internet Calendaring and Scheduling Core Object Specification |
paul@33 | 26 | (iCalendar) |
paul@33 | 27 | http://tools.ietf.org/html/rfc5545 |
paul@33 | 28 | |
paul@33 | 29 | ---- |
paul@33 | 30 | |
paul@33 | 31 | FREQ defines the selection resolution. |
paul@33 | 32 | DTSTART defines the start of the selection. |
paul@33 | 33 | INTERVAL defines the step of the selection. |
paul@33 | 34 | COUNT defines a number of instances; UNTIL defines a limit to the selection. |
paul@33 | 35 | |
paul@33 | 36 | BY... qualifiers select instances within each outer selection instance according |
paul@33 | 37 | to the recurrence of instances of the next highest resolution. For example, |
paul@33 | 38 | BYDAY selects days in weeks. Thus, if no explicit week recurrence is indicated, |
paul@33 | 39 | all weeks are selected within the selection of the next highest explicitly |
paul@33 | 40 | specified resolution, whether this is months or years. |
paul@33 | 41 | |
paul@33 | 42 | BYSETPOS in conjunction with BY... qualifiers permit the selection of specific |
paul@33 | 43 | instances. |
paul@33 | 44 | |
paul@33 | 45 | Within the FREQ resolution, BY... qualifiers refine selected instances. |
paul@33 | 46 | |
paul@33 | 47 | Outside the FREQ resolution, BY... qualifiers select instances at the resolution |
paul@33 | 48 | concerned. |
paul@33 | 49 | |
paul@33 | 50 | Thus, FREQ and BY... qualifiers need to be ordered in terms of increasing |
paul@33 | 51 | resolution (or decreasing scope). |
paul@33 | 52 | """ |
paul@33 | 53 | |
paul@34 | 54 | from calendar import monthrange |
paul@33 | 55 | from datetime import date, datetime, timedelta |
paul@33 | 56 | import operator |
paul@33 | 57 | |
paul@33 | 58 | # Frequency levels, specified by FREQ in iCalendar. |
paul@33 | 59 | |
paul@33 | 60 | freq_levels = ( |
paul@42 | 61 | "YEARLY", |
paul@42 | 62 | "MONTHLY", |
paul@42 | 63 | "WEEKLY", |
paul@44 | 64 | None, |
paul@44 | 65 | None, |
paul@33 | 66 | "DAILY", |
paul@42 | 67 | "HOURLY", |
paul@42 | 68 | "MINUTELY", |
paul@42 | 69 | "SECONDLY" |
paul@33 | 70 | ) |
paul@33 | 71 | |
paul@33 | 72 | # Enumeration levels, employed by BY... qualifiers. |
paul@33 | 73 | |
paul@33 | 74 | enum_levels = ( |
paul@42 | 75 | None, |
paul@44 | 76 | "BYMONTH", |
paul@44 | 77 | "BYWEEKNO", |
paul@44 | 78 | "BYYEARDAY", |
paul@44 | 79 | "BYMONTHDAY", |
paul@44 | 80 | "BYDAY", |
paul@44 | 81 | "BYHOUR", |
paul@44 | 82 | "BYMINUTE", |
paul@44 | 83 | "BYSECOND" |
paul@33 | 84 | ) |
paul@33 | 85 | |
paul@33 | 86 | # Map from levels to lengths of datetime tuples. |
paul@33 | 87 | |
paul@44 | 88 | lengths = [1, 2, 3, 3, 3, 3, 4, 5, 6] |
paul@33 | 89 | positions = [l-1 for l in lengths] |
paul@33 | 90 | |
paul@33 | 91 | # Map from qualifiers to interval units. Here, weeks are defined as 7 days. |
paul@33 | 92 | |
paul@33 | 93 | units = {"WEEKLY" : 7} |
paul@33 | 94 | |
paul@33 | 95 | # Make dictionaries mapping qualifiers to levels. |
paul@33 | 96 | |
paul@44 | 97 | freq = dict([(level, i) for (i, level) in enumerate(freq_levels) if level]) |
paul@44 | 98 | enum = dict([(level, i) for (i, level) in enumerate(enum_levels) if level]) |
paul@46 | 99 | weekdays = dict([(weekday, i+1) for (i, weekday) in enumerate(["MO", "TU", "WE", "TH", "FR", "SA", "SU"])]) |
paul@33 | 100 | |
paul@33 | 101 | # Functions for structuring the recurrences. |
paul@33 | 102 | |
paul@33 | 103 | def get_next(it): |
paul@33 | 104 | try: |
paul@33 | 105 | return it.next() |
paul@33 | 106 | except StopIteration: |
paul@33 | 107 | return None |
paul@33 | 108 | |
paul@46 | 109 | def get_parameters(values): |
paul@46 | 110 | |
paul@46 | 111 | "Return parameters from the given list of 'values'." |
paul@46 | 112 | |
paul@46 | 113 | d = {} |
paul@46 | 114 | for value in values: |
paul@46 | 115 | parts = value.split("=", 1) |
paul@46 | 116 | if len(parts) < 2: |
paul@46 | 117 | continue |
paul@46 | 118 | key, value = parts |
paul@46 | 119 | if key in ("COUNT", "BYSETPOS"): |
paul@46 | 120 | d[key] = int(value) |
paul@46 | 121 | return d |
paul@46 | 122 | |
paul@46 | 123 | def get_qualifiers(values): |
paul@46 | 124 | |
paul@46 | 125 | """ |
paul@46 | 126 | Process the list of 'values' of the form "key=value", returning a list of |
paul@46 | 127 | qualifiers. |
paul@46 | 128 | """ |
paul@46 | 129 | |
paul@46 | 130 | qualifiers = [] |
paul@46 | 131 | frequency = None |
paul@46 | 132 | interval = 1 |
paul@46 | 133 | |
paul@46 | 134 | for value in values: |
paul@46 | 135 | parts = value.split("=", 1) |
paul@46 | 136 | if len(parts) < 2: |
paul@46 | 137 | continue |
paul@46 | 138 | key, value = parts |
paul@46 | 139 | if key == "FREQ" and freq.has_key(value): |
paul@46 | 140 | qualifier = frequency = (value, {}) |
paul@46 | 141 | elif key == "INTERVAL": |
paul@46 | 142 | interval = int(value) |
paul@46 | 143 | continue |
paul@46 | 144 | elif enum.has_key(key): |
paul@46 | 145 | qualifier = (key, {"values" : get_qualifier_values(key, value)}) |
paul@46 | 146 | else: |
paul@46 | 147 | continue |
paul@46 | 148 | |
paul@46 | 149 | qualifiers.append(qualifier) |
paul@46 | 150 | |
paul@46 | 151 | if frequency: |
paul@46 | 152 | frequency[1]["interval"] = interval |
paul@46 | 153 | |
paul@46 | 154 | return qualifiers |
paul@46 | 155 | |
paul@46 | 156 | def get_qualifier_values(qualifier, value): |
paul@46 | 157 | |
paul@46 | 158 | """ |
paul@46 | 159 | For the given 'qualifier', process the 'value' string, returning a list of |
paul@46 | 160 | suitable values. |
paul@46 | 161 | """ |
paul@46 | 162 | |
paul@46 | 163 | if qualifier != "BYDAY": |
paul@46 | 164 | return map(int, value.split(",")) |
paul@46 | 165 | |
paul@46 | 166 | values = [] |
paul@46 | 167 | for part in value.split(","): |
paul@46 | 168 | weekday = weekdays.get(part[-2:]) |
paul@46 | 169 | if not weekday: |
paul@46 | 170 | continue |
paul@46 | 171 | index = part[:-2] |
paul@46 | 172 | if index: |
paul@46 | 173 | index = int(index) |
paul@46 | 174 | else: |
paul@46 | 175 | index = None |
paul@46 | 176 | values.append((weekday, index)) |
paul@46 | 177 | |
paul@46 | 178 | return values |
paul@46 | 179 | |
paul@33 | 180 | def order_qualifiers(qualifiers): |
paul@33 | 181 | |
paul@33 | 182 | "Return the 'qualifiers' in order of increasing resolution." |
paul@33 | 183 | |
paul@33 | 184 | l = [] |
paul@33 | 185 | |
paul@33 | 186 | for qualifier, args in qualifiers: |
paul@33 | 187 | if enum.has_key(qualifier): |
paul@33 | 188 | level = enum[qualifier] |
paul@35 | 189 | if special_enum_levels.has_key(qualifier): |
paul@33 | 190 | args["interval"] = 1 |
paul@35 | 191 | selector = special_enum_levels[qualifier] |
paul@33 | 192 | else: |
paul@33 | 193 | selector = Enum |
paul@33 | 194 | else: |
paul@33 | 195 | level = freq[qualifier] |
paul@33 | 196 | selector = Pattern |
paul@33 | 197 | |
paul@42 | 198 | l.append(selector(level, args, qualifier)) |
paul@33 | 199 | |
paul@42 | 200 | l.sort(key=lambda x: x.level) |
paul@33 | 201 | return l |
paul@33 | 202 | |
paul@33 | 203 | def get_datetime_structure(datetime): |
paul@33 | 204 | |
paul@33 | 205 | "Return the structure of 'datetime' for recurrence production." |
paul@33 | 206 | |
paul@33 | 207 | l = [] |
paul@42 | 208 | offset = 0 |
paul@42 | 209 | for level, value in enumerate(datetime): |
paul@42 | 210 | if level == 2: |
paul@44 | 211 | offset = 3 |
paul@42 | 212 | l.append(Enum(level + offset, {"values" : [value]}, "DT")) |
paul@33 | 213 | return l |
paul@33 | 214 | |
paul@33 | 215 | def combine_datetime_with_qualifiers(datetime, qualifiers): |
paul@33 | 216 | |
paul@33 | 217 | """ |
paul@33 | 218 | Combine 'datetime' with 'qualifiers' to produce a structure for recurrence |
paul@33 | 219 | production. |
paul@33 | 220 | """ |
paul@33 | 221 | |
paul@33 | 222 | iter_dt = iter(get_datetime_structure(datetime)) |
paul@33 | 223 | iter_q = iter(order_qualifiers(qualifiers)) |
paul@33 | 224 | |
paul@33 | 225 | l = [] |
paul@33 | 226 | |
paul@33 | 227 | from_dt = get_next(iter_dt) |
paul@33 | 228 | from_q = get_next(iter_q) |
paul@33 | 229 | |
paul@33 | 230 | have_q = False |
paul@33 | 231 | context = [] |
paul@39 | 232 | context.append(from_dt.args["values"][0]) |
paul@33 | 233 | |
paul@33 | 234 | # Consume from both lists, merging entries. |
paul@33 | 235 | |
paul@33 | 236 | while from_dt and from_q: |
paul@42 | 237 | _level = from_dt.level |
paul@42 | 238 | level = from_q.level |
paul@33 | 239 | |
paul@33 | 240 | # Datetime value at wider resolution. |
paul@33 | 241 | |
paul@42 | 242 | if _level < level: |
paul@39 | 243 | from_dt = get_next(iter_dt) |
paul@38 | 244 | context.append(from_dt.args["values"][0]) |
paul@33 | 245 | |
paul@33 | 246 | # Qualifier at wider or same resolution as datetime value. |
paul@33 | 247 | |
paul@33 | 248 | else: |
paul@33 | 249 | if not have_q: |
paul@42 | 250 | if isinstance(from_q, Enum) and level > 0: |
paul@45 | 251 | repeat = Pattern(level - 1, {"interval" : 1}, None) |
paul@38 | 252 | repeat.context = tuple(context) |
paul@33 | 253 | l.append(repeat) |
paul@33 | 254 | have_q = True |
paul@33 | 255 | |
paul@43 | 256 | from_q.context = tuple(context) |
paul@43 | 257 | l.append(from_q) |
paul@43 | 258 | from_q = get_next(iter_q) |
paul@33 | 259 | |
paul@43 | 260 | if _level == level: |
paul@33 | 261 | from_dt = get_next(iter_dt) |
paul@39 | 262 | context.append(from_dt.args["values"][0]) |
paul@33 | 263 | |
paul@33 | 264 | # Complete the list. |
paul@33 | 265 | |
paul@33 | 266 | while from_dt: |
paul@33 | 267 | l.append(from_dt) |
paul@33 | 268 | from_dt = get_next(iter_dt) |
paul@33 | 269 | |
paul@33 | 270 | while from_q: |
paul@33 | 271 | if not have_q: |
paul@42 | 272 | if isinstance(from_q, Enum) and level > 0: |
paul@45 | 273 | repeat = Pattern(level - 1, {"interval" : 1}, None) |
paul@38 | 274 | repeat.context = tuple(context) |
paul@33 | 275 | l.append(repeat) |
paul@33 | 276 | have_q = True |
paul@43 | 277 | |
paul@43 | 278 | from_q.context = tuple(context) |
paul@33 | 279 | l.append(from_q) |
paul@33 | 280 | from_q = get_next(iter_q) |
paul@33 | 281 | |
paul@33 | 282 | return l |
paul@33 | 283 | |
paul@33 | 284 | # Datetime arithmetic. |
paul@33 | 285 | |
paul@33 | 286 | def combine(t1, t2): |
paul@33 | 287 | return tuple(map(lambda x, y: x or y, t1, t2)) |
paul@33 | 288 | |
paul@33 | 289 | def scale(interval, pos): |
paul@33 | 290 | return (0,) * pos + (interval,) |
paul@33 | 291 | |
paul@33 | 292 | def get_seconds(t): |
paul@33 | 293 | |
paul@33 | 294 | "Convert the sub-day part of 't' into seconds." |
paul@33 | 295 | |
paul@33 | 296 | t = t + (0,) * (6 - len(t)) |
paul@33 | 297 | return (t[3] * 60 + t[4]) * 60 + t[5] |
paul@33 | 298 | |
paul@33 | 299 | def update(t, step): |
paul@33 | 300 | |
paul@33 | 301 | "Update 't' by 'step' at the resolution of 'step'." |
paul@33 | 302 | |
paul@33 | 303 | i = len(step) |
paul@33 | 304 | |
paul@33 | 305 | # Years only. |
paul@33 | 306 | |
paul@33 | 307 | if i == 1: |
paul@33 | 308 | return (t[0] + step[0],) + tuple(t[1:]) |
paul@33 | 309 | |
paul@33 | 310 | # Years and months. |
paul@33 | 311 | |
paul@33 | 312 | elif i == 2: |
paul@33 | 313 | month = t[1] + step[1] |
paul@33 | 314 | return (t[0] + step[0] + (month - 1) / 12, (month - 1) % 12 + 1) + tuple(t[2:]) |
paul@33 | 315 | |
paul@33 | 316 | # Dates and datetimes. |
paul@33 | 317 | |
paul@33 | 318 | else: |
paul@33 | 319 | updated_for_months = update(t, step[:2]) |
paul@33 | 320 | |
paul@33 | 321 | # Dates only. |
paul@33 | 322 | |
paul@33 | 323 | if i == 3: |
paul@33 | 324 | d = datetime(*updated_for_months) |
paul@33 | 325 | s = timedelta(step[2]) |
paul@33 | 326 | |
paul@33 | 327 | # Datetimes. |
paul@33 | 328 | |
paul@33 | 329 | else: |
paul@33 | 330 | d = datetime(*updated_for_months) |
paul@33 | 331 | s = timedelta(step[2], get_seconds(step)) |
paul@33 | 332 | |
paul@39 | 333 | return to_tuple(d + s, len(t)) |
paul@39 | 334 | |
paul@46 | 335 | def to_tuple(d, n=None): |
paul@46 | 336 | if not isinstance(d, date): |
paul@46 | 337 | return d |
paul@46 | 338 | if n is None: |
paul@46 | 339 | if isinstance(d, datetime): |
paul@46 | 340 | n = 6 |
paul@46 | 341 | else: |
paul@46 | 342 | n = 3 |
paul@39 | 343 | return d.timetuple()[:n] |
paul@39 | 344 | |
paul@39 | 345 | def get_first_day(first_day, weekday): |
paul@39 | 346 | first_day = date(*first_day) |
paul@39 | 347 | first_weekday = first_day.isoweekday() |
paul@39 | 348 | if first_weekday > weekday: |
paul@39 | 349 | return first_day + timedelta(7 - first_weekday + weekday) |
paul@39 | 350 | else: |
paul@39 | 351 | return first_day + timedelta(weekday - first_weekday) |
paul@39 | 352 | |
paul@39 | 353 | def get_last_day(last_day, weekday): |
paul@39 | 354 | last_day = date(*last_day) |
paul@39 | 355 | last_weekday = last_day.isoweekday() |
paul@39 | 356 | if last_weekday < weekday: |
paul@39 | 357 | return last_day - timedelta(last_weekday + 7 - weekday) |
paul@39 | 358 | else: |
paul@39 | 359 | return last_day - timedelta(last_weekday - weekday) |
paul@33 | 360 | |
paul@33 | 361 | # Classes for producing instances from recurrence structures. |
paul@33 | 362 | |
paul@33 | 363 | class Selector: |
paul@42 | 364 | def __init__(self, level, args, qualifier, selecting=None): |
paul@42 | 365 | self.level = level |
paul@42 | 366 | self.pos = positions[level] |
paul@33 | 367 | self.args = args |
paul@33 | 368 | self.qualifier = qualifier |
paul@33 | 369 | self.context = () |
paul@33 | 370 | self.selecting = selecting |
paul@33 | 371 | |
paul@33 | 372 | def __repr__(self): |
paul@42 | 373 | return "%s(%r, %r, %r, %r)" % (self.__class__.__name__, self.level, self.args, self.qualifier, self.context) |
paul@33 | 374 | |
paul@45 | 375 | def materialise(self, start, end, count=None, setpos=None): |
paul@46 | 376 | start = to_tuple(start) |
paul@46 | 377 | end = to_tuple(end) |
paul@33 | 378 | counter = count and [0, count] |
paul@45 | 379 | results = self.materialise_items(self.context, start, end, counter, setpos) |
paul@39 | 380 | results.sort() |
paul@41 | 381 | return results[:count] |
paul@33 | 382 | |
paul@45 | 383 | def materialise_item(self, current, last, next, counter, setpos=None): |
paul@45 | 384 | if self.selecting: |
paul@45 | 385 | return self.selecting.materialise_items(current, last, next, counter, setpos) |
paul@45 | 386 | elif last <= current: |
paul@45 | 387 | return [current] |
paul@45 | 388 | else: |
paul@45 | 389 | return [] |
paul@45 | 390 | |
paul@45 | 391 | def convert_positions(self, setpos): |
paul@45 | 392 | l = [] |
paul@45 | 393 | for pos in setpos: |
paul@45 | 394 | lower = pos < 0 and pos or pos - 1 |
paul@45 | 395 | upper = pos > 0 and pos or pos < -1 and pos + 1 or None |
paul@45 | 396 | l.append((lower, upper)) |
paul@45 | 397 | return l |
paul@45 | 398 | |
paul@45 | 399 | def select_positions(self, results, setpos): |
paul@45 | 400 | results.sort() |
paul@45 | 401 | l = [] |
paul@45 | 402 | for lower, upper in self.convert_positions(setpos): |
paul@45 | 403 | l += results[lower:upper] |
paul@45 | 404 | return l |
paul@45 | 405 | |
paul@45 | 406 | def filter_by_period(self, results, start, end): |
paul@45 | 407 | l = [] |
paul@45 | 408 | for result in results: |
paul@45 | 409 | if start <= result < end: |
paul@45 | 410 | l.append(result) |
paul@45 | 411 | return l |
paul@33 | 412 | |
paul@33 | 413 | class Pattern(Selector): |
paul@45 | 414 | def materialise_items(self, context, start, end, counter, setpos=None): |
paul@38 | 415 | first = scale(self.context[self.pos], self.pos) |
paul@34 | 416 | |
paul@34 | 417 | # Define the step between items. |
paul@34 | 418 | |
paul@33 | 419 | interval = self.args.get("interval", 1) * units.get(self.qualifier, 1) |
paul@33 | 420 | step = scale(interval, self.pos) |
paul@34 | 421 | |
paul@34 | 422 | # Define the scale of a single item. |
paul@34 | 423 | |
paul@33 | 424 | unit_interval = units.get(self.qualifier, 1) |
paul@33 | 425 | unit_step = scale(unit_interval, self.pos) |
paul@34 | 426 | |
paul@34 | 427 | current = combine(context, first) |
paul@33 | 428 | results = [] |
paul@34 | 429 | |
paul@33 | 430 | while current < end and (counter is None or counter[0] < counter[1]): |
paul@33 | 431 | next = update(current, step) |
paul@33 | 432 | current_end = update(current, unit_step) |
paul@45 | 433 | interval_results = self.materialise_item(current, max(current, start), min(current_end, end), counter, setpos) |
paul@45 | 434 | if counter is not None: |
paul@45 | 435 | counter[0] += len(interval_results) |
paul@45 | 436 | results += interval_results |
paul@33 | 437 | current = next |
paul@34 | 438 | |
paul@33 | 439 | return results |
paul@33 | 440 | |
paul@35 | 441 | class WeekDayFilter(Selector): |
paul@45 | 442 | def materialise_items(self, context, start, end, counter, setpos=None): |
paul@39 | 443 | step = scale(1, 2) |
paul@33 | 444 | results = [] |
paul@34 | 445 | |
paul@39 | 446 | # Get weekdays in the year. |
paul@39 | 447 | |
paul@39 | 448 | if len(context) == 1: |
paul@39 | 449 | first_day = (context[0], 1, 1) |
paul@39 | 450 | last_day = (context[0], 12, 31) |
paul@39 | 451 | |
paul@39 | 452 | # Get weekdays in the month. |
paul@39 | 453 | |
paul@39 | 454 | elif len(context) == 2: |
paul@39 | 455 | first_day = (context[0], context[1], 1) |
paul@39 | 456 | last_day = update((context[0], context[1], 1), (0, 1, 0)) |
paul@39 | 457 | last_day = update(last_day, (0, 0, -1)) |
paul@39 | 458 | |
paul@39 | 459 | # Get weekdays in the week. |
paul@39 | 460 | |
paul@39 | 461 | else: |
paul@39 | 462 | current = context |
paul@39 | 463 | values = [value for (value, index) in self.args["values"]] |
paul@39 | 464 | |
paul@45 | 465 | while current < end: |
paul@39 | 466 | next = update(current, step) |
paul@39 | 467 | if date(*current).isoweekday() in values: |
paul@39 | 468 | results += self.materialise_item(current, max(current, start), min(next, end), counter) |
paul@39 | 469 | current = next |
paul@45 | 470 | |
paul@45 | 471 | if setpos: |
paul@45 | 472 | return self.select_positions(results, setpos) |
paul@45 | 473 | else: |
paul@45 | 474 | return results |
paul@39 | 475 | |
paul@39 | 476 | # Find each of the given days. |
paul@39 | 477 | |
paul@39 | 478 | for value, index in self.args["values"]: |
paul@39 | 479 | if index is not None: |
paul@39 | 480 | offset = timedelta(7 * (abs(index) - 1)) |
paul@39 | 481 | |
paul@39 | 482 | if index < 0: |
paul@39 | 483 | current = to_tuple(get_last_day(last_day, value) - offset, 3) |
paul@39 | 484 | else: |
paul@39 | 485 | current = to_tuple(get_first_day(first_day, value) + offset, 3) |
paul@39 | 486 | |
paul@45 | 487 | next = update(current, step) |
paul@45 | 488 | |
paul@45 | 489 | # To support setpos, only current and next bound the search, not |
paul@45 | 490 | # the period in addition. |
paul@45 | 491 | |
paul@45 | 492 | results += self.materialise_item(current, current, next, counter) |
paul@39 | 493 | |
paul@39 | 494 | else: |
paul@39 | 495 | if index < 0: |
paul@39 | 496 | current = to_tuple(get_last_day(last_day, value), 3) |
paul@39 | 497 | direction = operator.sub |
paul@39 | 498 | else: |
paul@39 | 499 | current = to_tuple(get_first_day(first_day, value), 3) |
paul@39 | 500 | direction = operator.add |
paul@39 | 501 | |
paul@39 | 502 | while first_day <= current <= last_day: |
paul@45 | 503 | next = update(current, step) |
paul@45 | 504 | |
paul@45 | 505 | # To support setpos, only current and next bound the search, not |
paul@45 | 506 | # the period in addition. |
paul@45 | 507 | |
paul@45 | 508 | results += self.materialise_item(current, current, next, counter) |
paul@39 | 509 | current = to_tuple(direction(date(*current), timedelta(7)), 3) |
paul@34 | 510 | |
paul@45 | 511 | # Extract selected positions and remove out-of-period instances. |
paul@45 | 512 | |
paul@45 | 513 | if setpos: |
paul@45 | 514 | results = self.select_positions(results, setpos) |
paul@45 | 515 | |
paul@45 | 516 | return self.filter_by_period(results, start, end) |
paul@33 | 517 | |
paul@33 | 518 | class Enum(Selector): |
paul@45 | 519 | def materialise_items(self, context, start, end, counter, setpos=None): |
paul@33 | 520 | step = scale(1, self.pos) |
paul@33 | 521 | results = [] |
paul@33 | 522 | for value in self.args["values"]: |
paul@33 | 523 | current = combine(context, scale(value, self.pos)) |
paul@45 | 524 | next = update(current, step) |
paul@45 | 525 | |
paul@45 | 526 | # To support setpos, only current and next bound the search, not |
paul@45 | 527 | # the period in addition. |
paul@45 | 528 | |
paul@45 | 529 | results += self.materialise_item(current, current, next, counter, setpos) |
paul@45 | 530 | |
paul@45 | 531 | # Extract selected positions and remove out-of-period instances. |
paul@45 | 532 | |
paul@45 | 533 | if setpos: |
paul@45 | 534 | results = self.select_positions(results, setpos) |
paul@45 | 535 | |
paul@45 | 536 | return self.filter_by_period(results, start, end) |
paul@35 | 537 | |
paul@35 | 538 | class MonthDayFilter(Enum): |
paul@45 | 539 | def materialise_items(self, context, start, end, counter, setpos=None): |
paul@35 | 540 | last_day = monthrange(context[0], context[1])[1] |
paul@35 | 541 | step = scale(1, self.pos) |
paul@35 | 542 | results = [] |
paul@35 | 543 | for value in self.args["values"]: |
paul@35 | 544 | if value < 0: |
paul@35 | 545 | value = last_day + 1 + value |
paul@35 | 546 | current = combine(context, scale(value, self.pos)) |
paul@45 | 547 | next = update(current, step) |
paul@45 | 548 | |
paul@45 | 549 | # To support setpos, only current and next bound the search, not |
paul@45 | 550 | # the period in addition. |
paul@45 | 551 | |
paul@45 | 552 | results += self.materialise_item(current, current, next, counter) |
paul@45 | 553 | |
paul@45 | 554 | # Extract selected positions and remove out-of-period instances. |
paul@45 | 555 | |
paul@45 | 556 | if setpos: |
paul@45 | 557 | results = self.select_positions(results, setpos) |
paul@45 | 558 | |
paul@45 | 559 | return self.filter_by_period(results, start, end) |
paul@33 | 560 | |
paul@37 | 561 | class YearDayFilter(Enum): |
paul@45 | 562 | def materialise_items(self, context, start, end, counter, setpos=None): |
paul@37 | 563 | first_day = date(context[0], 1, 1) |
paul@37 | 564 | next_first_day = date(context[0] + 1, 1, 1) |
paul@37 | 565 | year_length = (next_first_day - first_day).days |
paul@37 | 566 | step = scale(1, self.pos) |
paul@37 | 567 | results = [] |
paul@37 | 568 | for value in self.args["values"]: |
paul@37 | 569 | if value < 0: |
paul@37 | 570 | value = year_length + 1 + value |
paul@39 | 571 | current = to_tuple(first_day + timedelta(value - 1), 3) |
paul@45 | 572 | next = update(current, step) |
paul@45 | 573 | |
paul@45 | 574 | # To support setpos, only current and next bound the search, not |
paul@45 | 575 | # the period in addition. |
paul@45 | 576 | |
paul@45 | 577 | results += self.materialise_item(current, current, next, counter) |
paul@45 | 578 | |
paul@45 | 579 | # Extract selected positions and remove out-of-period instances. |
paul@45 | 580 | |
paul@45 | 581 | if setpos: |
paul@45 | 582 | results = self.select_positions(results, setpos) |
paul@45 | 583 | |
paul@45 | 584 | return self.filter_by_period(results, start, end) |
paul@37 | 585 | |
paul@46 | 586 | special_enum_levels = { |
paul@46 | 587 | "BYDAY" : WeekDayFilter, |
paul@46 | 588 | "BYMONTHDAY" : MonthDayFilter, |
paul@46 | 589 | "BYYEARDAY" : YearDayFilter, |
paul@46 | 590 | } |
paul@46 | 591 | |
paul@46 | 592 | # Public functions. |
paul@46 | 593 | |
paul@46 | 594 | def connect_selectors(selectors): |
paul@33 | 595 | current = selectors[0] |
paul@33 | 596 | for selector in selectors[1:]: |
paul@33 | 597 | current.selecting = selector |
paul@33 | 598 | current = selector |
paul@33 | 599 | return selectors[0] |
paul@33 | 600 | |
paul@46 | 601 | def get_selector(dt, qualifiers): |
paul@46 | 602 | dt = to_tuple(dt) |
paul@46 | 603 | return connect_selectors(combine_datetime_with_qualifiers(dt, qualifiers)) |
paul@46 | 604 | |
paul@46 | 605 | def get_rule(dt, rule): |
paul@46 | 606 | qualifiers = get_qualifiers(rule) |
paul@46 | 607 | return get_selector(dt, qualifiers) |
paul@35 | 608 | |
paul@33 | 609 | # vim: tabstop=4 expandtab shiftwidth=4 |