paul@33 | 1 | #!/usr/bin/env python |
paul@33 | 2 | |
paul@33 | 3 | """ |
paul@33 | 4 | Recurrence rule calculation. |
paul@33 | 5 | |
paul@1237 | 6 | Copyright (C) 2014, 2015, 2017 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@521 | 34 | COUNT defines a number of instances |
paul@521 | 35 | UNTIL defines a limit to the selection. |
paul@33 | 36 | |
paul@33 | 37 | BY... qualifiers select instances within each outer selection instance according |
paul@33 | 38 | to the recurrence of instances of the next highest resolution. For example, |
paul@33 | 39 | BYDAY selects days in weeks. Thus, if no explicit week recurrence is indicated, |
paul@33 | 40 | all weeks are selected within the selection of the next highest explicitly |
paul@33 | 41 | specified resolution, whether this is months or years. |
paul@33 | 42 | |
paul@33 | 43 | BYSETPOS in conjunction with BY... qualifiers permit the selection of specific |
paul@33 | 44 | instances. |
paul@33 | 45 | |
paul@33 | 46 | Within the FREQ resolution, BY... qualifiers refine selected instances. |
paul@33 | 47 | |
paul@33 | 48 | Outside the FREQ resolution, BY... qualifiers select instances at the resolution |
paul@33 | 49 | concerned. |
paul@33 | 50 | |
paul@33 | 51 | Thus, FREQ and BY... qualifiers need to be ordered in terms of increasing |
paul@33 | 52 | resolution (or decreasing scope). |
paul@33 | 53 | """ |
paul@33 | 54 | |
paul@34 | 55 | from calendar import monthrange |
paul@33 | 56 | from datetime import date, datetime, timedelta |
paul@33 | 57 | import operator |
paul@33 | 58 | |
paul@33 | 59 | # Frequency levels, specified by FREQ in iCalendar. |
paul@33 | 60 | |
paul@33 | 61 | freq_levels = ( |
paul@42 | 62 | "YEARLY", |
paul@42 | 63 | "MONTHLY", |
paul@42 | 64 | "WEEKLY", |
paul@44 | 65 | None, |
paul@44 | 66 | None, |
paul@33 | 67 | "DAILY", |
paul@42 | 68 | "HOURLY", |
paul@42 | 69 | "MINUTELY", |
paul@42 | 70 | "SECONDLY" |
paul@33 | 71 | ) |
paul@33 | 72 | |
paul@1359 | 73 | # Symbols corresponding to resolution levels. |
paul@1359 | 74 | |
paul@1359 | 75 | YEARS, MONTHS, WEEKS, DAYS, HOURS, MINUTES, SECONDS = 0, 1, 2, 5, 6, 7, 8 |
paul@1359 | 76 | |
paul@33 | 77 | # Enumeration levels, employed by BY... qualifiers. |
paul@33 | 78 | |
paul@33 | 79 | enum_levels = ( |
paul@42 | 80 | None, |
paul@44 | 81 | "BYMONTH", |
paul@44 | 82 | "BYWEEKNO", |
paul@44 | 83 | "BYYEARDAY", |
paul@44 | 84 | "BYMONTHDAY", |
paul@44 | 85 | "BYDAY", |
paul@44 | 86 | "BYHOUR", |
paul@44 | 87 | "BYMINUTE", |
paul@44 | 88 | "BYSECOND" |
paul@33 | 89 | ) |
paul@33 | 90 | |
paul@1241 | 91 | # Levels defining days. |
paul@1241 | 92 | |
paul@1241 | 93 | daylevels = [2, 3, 4, 5] |
paul@1241 | 94 | |
paul@33 | 95 | # Map from levels to lengths of datetime tuples. |
paul@33 | 96 | |
paul@44 | 97 | lengths = [1, 2, 3, 3, 3, 3, 4, 5, 6] |
paul@33 | 98 | positions = [l-1 for l in lengths] |
paul@33 | 99 | |
paul@1241 | 100 | # Define the lowest values at each resolution (years, months, days... hours, |
paul@1241 | 101 | # minutes, seconds). |
paul@1241 | 102 | |
paul@1241 | 103 | firstvalues = [0, 1, 1, 1, 1, 1, 0, 0, 0] |
paul@1241 | 104 | |
paul@1359 | 105 | # Map from qualifiers to interval multiples. Here, weeks are defined as 7 days. |
paul@33 | 106 | |
paul@1359 | 107 | multiples = {"WEEKLY" : 7} |
paul@33 | 108 | |
paul@33 | 109 | # Make dictionaries mapping qualifiers to levels. |
paul@33 | 110 | |
paul@1359 | 111 | freq = {} |
paul@1359 | 112 | for i, level in enumerate(freq_levels): |
paul@1359 | 113 | if level: |
paul@1359 | 114 | freq[level] = i |
paul@1359 | 115 | |
paul@1359 | 116 | enum = {} |
paul@1359 | 117 | for i, level in enumerate(enum_levels): |
paul@1359 | 118 | if level: |
paul@1359 | 119 | enum[level] = i |
paul@1359 | 120 | |
paul@1359 | 121 | # Weekdays: name -> 1-based value |
paul@1359 | 122 | |
paul@1359 | 123 | weekdays = {} |
paul@1359 | 124 | for i, weekday in enumerate(["MO", "TU", "WE", "TH", "FR", "SA", "SU"]): |
paul@1359 | 125 | weekdays[weekday] = i + 1 |
paul@33 | 126 | |
paul@33 | 127 | # Functions for structuring the recurrences. |
paul@33 | 128 | |
paul@33 | 129 | def get_next(it): |
paul@1359 | 130 | |
paul@1359 | 131 | "Return the next value from iterator 'it' or None if no more values exist." |
paul@1359 | 132 | |
paul@33 | 133 | try: |
paul@33 | 134 | return it.next() |
paul@33 | 135 | except StopIteration: |
paul@33 | 136 | return None |
paul@33 | 137 | |
paul@46 | 138 | def get_parameters(values): |
paul@46 | 139 | |
paul@46 | 140 | "Return parameters from the given list of 'values'." |
paul@46 | 141 | |
paul@46 | 142 | d = {} |
paul@46 | 143 | for value in values: |
paul@46 | 144 | parts = value.split("=", 1) |
paul@46 | 145 | if len(parts) < 2: |
paul@46 | 146 | continue |
paul@46 | 147 | key, value = parts |
paul@46 | 148 | if key in ("COUNT", "BYSETPOS"): |
paul@46 | 149 | d[key] = int(value) |
paul@521 | 150 | else: |
paul@521 | 151 | d[key] = value |
paul@46 | 152 | return d |
paul@46 | 153 | |
paul@46 | 154 | def get_qualifiers(values): |
paul@46 | 155 | |
paul@46 | 156 | """ |
paul@46 | 157 | Process the list of 'values' of the form "key=value", returning a list of |
paul@358 | 158 | qualifiers of the form (qualifier name, args). |
paul@46 | 159 | """ |
paul@46 | 160 | |
paul@46 | 161 | qualifiers = [] |
paul@46 | 162 | frequency = None |
paul@46 | 163 | interval = 1 |
paul@46 | 164 | |
paul@46 | 165 | for value in values: |
paul@46 | 166 | parts = value.split("=", 1) |
paul@46 | 167 | if len(parts) < 2: |
paul@46 | 168 | continue |
paul@46 | 169 | key, value = parts |
paul@1239 | 170 | |
paul@1239 | 171 | # Accept frequency indicators as qualifiers. |
paul@1239 | 172 | |
paul@46 | 173 | if key == "FREQ" and freq.has_key(value): |
paul@46 | 174 | qualifier = frequency = (value, {}) |
paul@1239 | 175 | |
paul@1239 | 176 | # Accept interval indicators for frequency qualifier parameterisation. |
paul@1239 | 177 | |
paul@46 | 178 | elif key == "INTERVAL": |
paul@46 | 179 | interval = int(value) |
paul@46 | 180 | continue |
paul@1239 | 181 | |
paul@1239 | 182 | # Accept enumerators as qualifiers. |
paul@1239 | 183 | |
paul@46 | 184 | elif enum.has_key(key): |
paul@46 | 185 | qualifier = (key, {"values" : get_qualifier_values(key, value)}) |
paul@1239 | 186 | |
paul@1239 | 187 | # Ignore other items. |
paul@1239 | 188 | |
paul@46 | 189 | else: |
paul@46 | 190 | continue |
paul@46 | 191 | |
paul@46 | 192 | qualifiers.append(qualifier) |
paul@46 | 193 | |
paul@1239 | 194 | # Parameterise any frequency qualifier with the interval. |
paul@1239 | 195 | |
paul@46 | 196 | if frequency: |
paul@46 | 197 | frequency[1]["interval"] = interval |
paul@46 | 198 | |
paul@46 | 199 | return qualifiers |
paul@46 | 200 | |
paul@46 | 201 | def get_qualifier_values(qualifier, value): |
paul@46 | 202 | |
paul@46 | 203 | """ |
paul@46 | 204 | For the given 'qualifier', process the 'value' string, returning a list of |
paul@46 | 205 | suitable values. |
paul@46 | 206 | """ |
paul@46 | 207 | |
paul@1359 | 208 | # For non-weekday selection, obtain a list of day numbers. |
paul@1359 | 209 | |
paul@46 | 210 | if qualifier != "BYDAY": |
paul@46 | 211 | return map(int, value.split(",")) |
paul@46 | 212 | |
paul@1359 | 213 | # For weekday selection, obtain the weekday number and instance number. |
paul@1359 | 214 | |
paul@46 | 215 | values = [] |
paul@1359 | 216 | |
paul@46 | 217 | for part in value.split(","): |
paul@46 | 218 | weekday = weekdays.get(part[-2:]) |
paul@46 | 219 | if not weekday: |
paul@46 | 220 | continue |
paul@46 | 221 | index = part[:-2] |
paul@46 | 222 | if index: |
paul@46 | 223 | index = int(index) |
paul@46 | 224 | else: |
paul@46 | 225 | index = None |
paul@46 | 226 | values.append((weekday, index)) |
paul@46 | 227 | |
paul@46 | 228 | return values |
paul@46 | 229 | |
paul@33 | 230 | def order_qualifiers(qualifiers): |
paul@33 | 231 | |
paul@33 | 232 | "Return the 'qualifiers' in order of increasing resolution." |
paul@33 | 233 | |
paul@33 | 234 | l = [] |
paul@33 | 235 | |
paul@33 | 236 | for qualifier, args in qualifiers: |
paul@1237 | 237 | |
paul@1237 | 238 | # Distinguish between enumerators, used to select particular periods, |
paul@1237 | 239 | # and frequencies, used to select repeating periods. |
paul@1237 | 240 | |
paul@33 | 241 | if enum.has_key(qualifier): |
paul@33 | 242 | level = enum[qualifier] |
paul@35 | 243 | if special_enum_levels.has_key(qualifier): |
paul@33 | 244 | args["interval"] = 1 |
paul@35 | 245 | selector = special_enum_levels[qualifier] |
paul@33 | 246 | else: |
paul@33 | 247 | selector = Enum |
paul@33 | 248 | else: |
paul@33 | 249 | level = freq[qualifier] |
paul@33 | 250 | selector = Pattern |
paul@33 | 251 | |
paul@42 | 252 | l.append(selector(level, args, qualifier)) |
paul@33 | 253 | |
paul@42 | 254 | l.sort(key=lambda x: x.level) |
paul@33 | 255 | return l |
paul@33 | 256 | |
paul@33 | 257 | def get_datetime_structure(datetime): |
paul@33 | 258 | |
paul@33 | 259 | "Return the structure of 'datetime' for recurrence production." |
paul@33 | 260 | |
paul@33 | 261 | l = [] |
paul@42 | 262 | offset = 0 |
paul@1237 | 263 | |
paul@1359 | 264 | for pos, value in enumerate(datetime): |
paul@1237 | 265 | |
paul@1237 | 266 | # At the day number, adjust the frequency level offset to reference |
paul@1237 | 267 | # days (and then hours, minutes, seconds). |
paul@1237 | 268 | |
paul@1359 | 269 | if pos == 2: |
paul@44 | 270 | offset = 3 |
paul@1237 | 271 | |
paul@1359 | 272 | l.append(Enum(pos + offset, {"values" : [value]}, "DT")) |
paul@1237 | 273 | |
paul@33 | 274 | return l |
paul@33 | 275 | |
paul@33 | 276 | def combine_datetime_with_qualifiers(datetime, qualifiers): |
paul@33 | 277 | |
paul@33 | 278 | """ |
paul@33 | 279 | Combine 'datetime' with 'qualifiers' to produce a structure for recurrence |
paul@33 | 280 | production. |
paul@1241 | 281 | |
paul@1241 | 282 | Initial datetime values appearing at broader resolutions than any qualifiers |
paul@1241 | 283 | are ignored, since their details will be used when materialising the |
paul@1241 | 284 | results. |
paul@1241 | 285 | |
paul@1241 | 286 | Qualifiers are accumulated in order to define a selection. Datetime values |
paul@1241 | 287 | occurring between qualifiers or at the same resolution as qualifiers are |
paul@1241 | 288 | ignored. |
paul@1241 | 289 | |
paul@1241 | 290 | Any remaining datetime values are introduced as enumerators, provided that |
paul@1241 | 291 | they do not conflict with qualifiers. For example, specific day values |
paul@1241 | 292 | conflict with day selectors and weekly qualifiers. |
paul@1241 | 293 | |
paul@1241 | 294 | The purpose of the remaining datetime values is to define a result within |
paul@1241 | 295 | a period selected by the most precise qualifier. For example, the selection |
paul@1241 | 296 | of a day and month in a year recurrence. |
paul@33 | 297 | """ |
paul@33 | 298 | |
paul@33 | 299 | iter_dt = iter(get_datetime_structure(datetime)) |
paul@33 | 300 | iter_q = iter(order_qualifiers(qualifiers)) |
paul@33 | 301 | |
paul@33 | 302 | l = [] |
paul@33 | 303 | |
paul@33 | 304 | from_dt = get_next(iter_dt) |
paul@33 | 305 | from_q = get_next(iter_q) |
paul@33 | 306 | have_q = False |
paul@1237 | 307 | |
paul@33 | 308 | # Consume from both lists, merging entries. |
paul@33 | 309 | |
paul@33 | 310 | while from_dt and from_q: |
paul@42 | 311 | _level = from_dt.level |
paul@42 | 312 | level = from_q.level |
paul@33 | 313 | |
paul@1241 | 314 | # Datetime value at wider resolution. |
paul@33 | 315 | |
paul@42 | 316 | if _level < level: |
paul@39 | 317 | from_dt = get_next(iter_dt) |
paul@33 | 318 | |
paul@33 | 319 | # Qualifier at wider or same resolution as datetime value. |
paul@33 | 320 | |
paul@33 | 321 | else: |
paul@33 | 322 | if not have_q: |
paul@1241 | 323 | add_initial_qualifier(from_q, level, l) |
paul@33 | 324 | have_q = True |
paul@33 | 325 | |
paul@1241 | 326 | # Add the qualifier to the combined list. |
paul@1237 | 327 | |
paul@43 | 328 | l.append(from_q) |
paul@1237 | 329 | |
paul@1241 | 330 | # Datetime value at same resolution. |
paul@33 | 331 | |
paul@43 | 332 | if _level == level: |
paul@33 | 333 | from_dt = get_next(iter_dt) |
paul@33 | 334 | |
paul@1237 | 335 | # Get the next qualifier. |
paul@1237 | 336 | |
paul@1237 | 337 | from_q = get_next(iter_q) |
paul@1237 | 338 | |
paul@1237 | 339 | # Complete the list by adding remaining datetime enumerators. |
paul@33 | 340 | |
paul@33 | 341 | while from_dt: |
paul@1241 | 342 | |
paul@1241 | 343 | # Ignore datetime values that conflict with day-level qualifiers. |
paul@1241 | 344 | |
paul@1359 | 345 | if not l or from_dt.level != freq["DAILY"] or \ |
paul@1359 | 346 | l[-1].level not in daylevels: |
paul@1359 | 347 | |
paul@1241 | 348 | l.append(from_dt) |
paul@1241 | 349 | |
paul@33 | 350 | from_dt = get_next(iter_dt) |
paul@33 | 351 | |
paul@1237 | 352 | # Complete the list by adding remaining qualifiers. |
paul@1237 | 353 | |
paul@33 | 354 | while from_q: |
paul@33 | 355 | if not have_q: |
paul@1241 | 356 | add_initial_qualifier(from_q, level, l) |
paul@33 | 357 | have_q = True |
paul@43 | 358 | |
paul@1241 | 359 | # Add the qualifier to the combined list. |
paul@1237 | 360 | |
paul@33 | 361 | l.append(from_q) |
paul@1237 | 362 | |
paul@1237 | 363 | # Get the next qualifier. |
paul@1237 | 364 | |
paul@33 | 365 | from_q = get_next(iter_q) |
paul@33 | 366 | |
paul@33 | 367 | return l |
paul@33 | 368 | |
paul@1241 | 369 | def add_initial_qualifier(from_q, level, l): |
paul@1237 | 370 | |
paul@1237 | 371 | """ |
paul@1237 | 372 | Take the first qualifier 'from_q' at the given resolution 'level', using it |
paul@1241 | 373 | to create an initial qualifier, adding it to the combined list 'l' if |
paul@1241 | 374 | required. |
paul@1237 | 375 | """ |
paul@1237 | 376 | |
paul@1237 | 377 | if isinstance(from_q, Enum) and level > 0: |
paul@1237 | 378 | repeat = Pattern(level - 1, {"interval" : 1}, None) |
paul@1237 | 379 | l.append(repeat) |
paul@1237 | 380 | |
paul@1359 | 381 | def get_multiple(qualifier): |
paul@1359 | 382 | |
paul@1359 | 383 | "Return the time unit multiple for 'qualifier'." |
paul@1359 | 384 | |
paul@1359 | 385 | return multiples.get(qualifier, 1) |
paul@1359 | 386 | |
paul@33 | 387 | # Datetime arithmetic. |
paul@33 | 388 | |
paul@1359 | 389 | def is_year_only(t): |
paul@1359 | 390 | |
paul@1359 | 391 | "Return if 't' describes a year." |
paul@1359 | 392 | |
paul@1359 | 393 | return len(t) == lengths[YEARS] |
paul@1359 | 394 | |
paul@1359 | 395 | def is_month_only(t): |
paul@1359 | 396 | |
paul@1359 | 397 | "Return if 't' describes a month within a year." |
paul@1359 | 398 | |
paul@1359 | 399 | return len(t) == lengths[MONTHS] |
paul@1359 | 400 | |
paul@1359 | 401 | def start_of_year(t): |
paul@1359 | 402 | |
paul@1359 | 403 | "Return the start of the year referenced by 't'." |
paul@1359 | 404 | |
paul@1359 | 405 | return (t[0], 1, 1) |
paul@1359 | 406 | |
paul@1359 | 407 | def end_of_year(t): |
paul@1359 | 408 | |
paul@1359 | 409 | "Return the end of the year referenced by 't'." |
paul@1359 | 410 | |
paul@1359 | 411 | return (t[0], 12, 31) |
paul@1359 | 412 | |
paul@1359 | 413 | def start_of_month(t): |
paul@1359 | 414 | |
paul@1359 | 415 | "Return the start of the month referenced by 't'." |
paul@1359 | 416 | |
paul@1359 | 417 | year, month = t[:2] |
paul@1359 | 418 | return (year, month, 1) |
paul@1359 | 419 | |
paul@1359 | 420 | def end_of_month(t): |
paul@1359 | 421 | |
paul@1359 | 422 | "Return the end of the month referenced by 't'." |
paul@1359 | 423 | |
paul@1359 | 424 | year, month = t[:2] |
paul@1359 | 425 | return update(update((year, month, 1), (0, 1, 0)), (0, 0, -1)) |
paul@1359 | 426 | |
paul@1359 | 427 | def day_in_year(t, number): |
paul@1359 | 428 | |
paul@1359 | 429 | "Return the day in the year referenced by 't' with the given 'number'." |
paul@1359 | 430 | |
paul@1359 | 431 | return to_tuple(date(*start_of_year(t)) + timedelta(number - 1)) |
paul@1359 | 432 | |
paul@1359 | 433 | def get_year_length(t): |
paul@1359 | 434 | |
paul@1359 | 435 | "Return the length of the year referenced by 't'." |
paul@1359 | 436 | |
paul@1359 | 437 | first_day = date(*start_of_year(t)) |
paul@1359 | 438 | last_day = date(*end_of_year(t)) |
paul@1359 | 439 | return (last_day - first_day).days + 1 |
paul@1359 | 440 | |
paul@1359 | 441 | def get_weekday(t): |
paul@1359 | 442 | |
paul@1359 | 443 | "Return the 1-based weekday for the month referenced by 't'." |
paul@1359 | 444 | |
paul@1359 | 445 | year, month = t[:2] |
paul@1359 | 446 | return monthrange(year, month)[0] + 1 |
paul@1359 | 447 | |
paul@1359 | 448 | def get_ordered_weekdays(t): |
paul@322 | 449 | |
paul@322 | 450 | """ |
paul@1359 | 451 | Return the 1-based weekday sequence describing the first week of the month |
paul@1359 | 452 | referenced by 't'. |
paul@322 | 453 | """ |
paul@322 | 454 | |
paul@1359 | 455 | first = get_weekday(t) |
paul@1359 | 456 | return range(first, 8) + range(1, first) |
paul@1359 | 457 | |
paul@1359 | 458 | def get_last_weekday_instance(weekday, first_day, last_day): |
paul@1359 | 459 | |
paul@1359 | 460 | """ |
paul@1359 | 461 | Return the last instance number for 'weekday' within the period from |
paul@1359 | 462 | 'first_day' to 'last_day' inclusive. |
paul@33 | 463 | |
paul@1359 | 464 | Here, 'weekday' is 1-based (starting on Monday), the returned limit is |
paul@1359 | 465 | 1-based. |
paul@1359 | 466 | """ |
paul@1359 | 467 | |
paul@1359 | 468 | weekday0 = get_first_day(first_day, weekday) |
paul@1359 | 469 | days = (date(*last_day) - weekday0).days |
paul@1359 | 470 | return days / 7 + 1 |
paul@1359 | 471 | |
paul@1359 | 472 | def precision(t, level, value=None): |
paul@322 | 473 | |
paul@322 | 474 | """ |
paul@1359 | 475 | Return 't' trimmed in resolution to the indicated resolution 'level', |
paul@1359 | 476 | setting 'value' at the given resolution if indicated. |
paul@322 | 477 | """ |
paul@322 | 478 | |
paul@1359 | 479 | pos = positions[level] |
paul@1359 | 480 | |
paul@1359 | 481 | if value is None: |
paul@1359 | 482 | return t[:pos + 1] |
paul@1359 | 483 | else: |
paul@1359 | 484 | return t[:pos] + (value,) |
paul@1359 | 485 | |
paul@1359 | 486 | def scale(interval, level): |
paul@1359 | 487 | |
paul@1359 | 488 | """ |
paul@1359 | 489 | Scale the given 'interval' value in resolution to the indicated resolution |
paul@1359 | 490 | 'level', returning a tuple with leading zero elements and 'interval' at the |
paul@1359 | 491 | stated position. |
paul@1359 | 492 | """ |
paul@1359 | 493 | |
paul@1359 | 494 | pos = positions[level] |
paul@33 | 495 | return (0,) * pos + (interval,) |
paul@33 | 496 | |
paul@33 | 497 | def get_seconds(t): |
paul@33 | 498 | |
paul@33 | 499 | "Convert the sub-day part of 't' into seconds." |
paul@33 | 500 | |
paul@33 | 501 | t = t + (0,) * (6 - len(t)) |
paul@33 | 502 | return (t[3] * 60 + t[4]) * 60 + t[5] |
paul@33 | 503 | |
paul@33 | 504 | def update(t, step): |
paul@33 | 505 | |
paul@33 | 506 | "Update 't' by 'step' at the resolution of 'step'." |
paul@33 | 507 | |
paul@33 | 508 | i = len(step) |
paul@33 | 509 | |
paul@33 | 510 | # Years only. |
paul@33 | 511 | |
paul@33 | 512 | if i == 1: |
paul@33 | 513 | return (t[0] + step[0],) + tuple(t[1:]) |
paul@33 | 514 | |
paul@33 | 515 | # Years and months. |
paul@33 | 516 | |
paul@33 | 517 | elif i == 2: |
paul@33 | 518 | month = t[1] + step[1] |
paul@33 | 519 | return (t[0] + step[0] + (month - 1) / 12, (month - 1) % 12 + 1) + tuple(t[2:]) |
paul@33 | 520 | |
paul@33 | 521 | # Dates and datetimes. |
paul@33 | 522 | |
paul@33 | 523 | else: |
paul@33 | 524 | updated_for_months = update(t, step[:2]) |
paul@33 | 525 | |
paul@33 | 526 | # Dates only. |
paul@33 | 527 | |
paul@33 | 528 | if i == 3: |
paul@33 | 529 | d = datetime(*updated_for_months) |
paul@33 | 530 | s = timedelta(step[2]) |
paul@33 | 531 | |
paul@33 | 532 | # Datetimes. |
paul@33 | 533 | |
paul@33 | 534 | else: |
paul@33 | 535 | d = datetime(*updated_for_months) |
paul@33 | 536 | s = timedelta(step[2], get_seconds(step)) |
paul@33 | 537 | |
paul@1359 | 538 | return to_tuple(d + s)[:len(t)] |
paul@39 | 539 | |
paul@1359 | 540 | def to_tuple(d): |
paul@322 | 541 | |
paul@1359 | 542 | "Return date or datetime 'd' as a tuple." |
paul@322 | 543 | |
paul@46 | 544 | if not isinstance(d, date): |
paul@46 | 545 | return d |
paul@1359 | 546 | if isinstance(d, datetime): |
paul@1359 | 547 | n = 6 |
paul@1359 | 548 | else: |
paul@1359 | 549 | n = 3 |
paul@39 | 550 | return d.timetuple()[:n] |
paul@39 | 551 | |
paul@39 | 552 | def get_first_day(first_day, weekday): |
paul@322 | 553 | |
paul@1359 | 554 | """ |
paul@1359 | 555 | Return the first occurrence at or after 'first_day' of 'weekday' as a date |
paul@1359 | 556 | instance. |
paul@1359 | 557 | """ |
paul@322 | 558 | |
paul@39 | 559 | first_day = date(*first_day) |
paul@39 | 560 | first_weekday = first_day.isoweekday() |
paul@39 | 561 | if first_weekday > weekday: |
paul@39 | 562 | return first_day + timedelta(7 - first_weekday + weekday) |
paul@39 | 563 | else: |
paul@39 | 564 | return first_day + timedelta(weekday - first_weekday) |
paul@39 | 565 | |
paul@39 | 566 | def get_last_day(last_day, weekday): |
paul@322 | 567 | |
paul@1359 | 568 | """ |
paul@1359 | 569 | Return the last occurrence at or before 'last_day' of 'weekday' as a date |
paul@1359 | 570 | instance. |
paul@1359 | 571 | """ |
paul@322 | 572 | |
paul@39 | 573 | last_day = date(*last_day) |
paul@39 | 574 | last_weekday = last_day.isoweekday() |
paul@39 | 575 | if last_weekday < weekday: |
paul@39 | 576 | return last_day - timedelta(last_weekday + 7 - weekday) |
paul@39 | 577 | else: |
paul@39 | 578 | return last_day - timedelta(last_weekday - weekday) |
paul@33 | 579 | |
paul@1359 | 580 | # Value expansion and sorting. |
paul@1359 | 581 | |
paul@1359 | 582 | def sort_values(values, limit=None): |
paul@1359 | 583 | |
paul@1359 | 584 | """ |
paul@1359 | 585 | Sort the given 'values' using 'limit' to determine the positions of negative |
paul@1359 | 586 | values. |
paul@1359 | 587 | """ |
paul@1359 | 588 | |
paul@1359 | 589 | # Convert negative values to positive values according to the value limit. |
paul@1359 | 590 | |
paul@1359 | 591 | if limit is not None: |
paul@1359 | 592 | l = map(lambda x, limit=limit: x < 0 and x + 1 + limit or x, values) |
paul@1359 | 593 | else: |
paul@1359 | 594 | l = values[:] |
paul@1359 | 595 | |
paul@1359 | 596 | l.sort() |
paul@1359 | 597 | return l |
paul@1359 | 598 | |
paul@1359 | 599 | def compare_weekday_selectors(x, y, weekdays): |
paul@1359 | 600 | |
paul@1359 | 601 | """ |
paul@1359 | 602 | Compare 'x' and 'y' values of the form (weekday number, instance number) |
paul@1359 | 603 | using 'weekdays' to define an ordering of the weekday numbers. |
paul@1359 | 604 | """ |
paul@1359 | 605 | |
paul@1359 | 606 | return cmp((x[1], weekdays.index(x[0])), (y[1], weekdays.index(y[0]))) |
paul@1359 | 607 | |
paul@1359 | 608 | def sort_weekdays(values, first_day, last_day): |
paul@1359 | 609 | |
paul@1359 | 610 | """ |
paul@1359 | 611 | Return a sorted copy of the given 'values', each having the form (weekday |
paul@1359 | 612 | number, instance number) using 'weekdays' to define the ordering of the |
paul@1359 | 613 | weekday numbers and 'limit' to determine the positions of negative instance |
paul@1359 | 614 | numbers. |
paul@1359 | 615 | """ |
paul@1359 | 616 | |
paul@1359 | 617 | weekdays = get_ordered_weekdays(first_day) |
paul@1359 | 618 | |
paul@1359 | 619 | # Expand the values to incorporate specific weekday instances. |
paul@1359 | 620 | |
paul@1359 | 621 | l = [] |
paul@1359 | 622 | |
paul@1359 | 623 | for weekday, index in values: |
paul@1359 | 624 | |
paul@1359 | 625 | # Obtain the last instance number of the weekday in the period. |
paul@1359 | 626 | |
paul@1359 | 627 | limit = get_last_weekday_instance(weekday, first_day, last_day) |
paul@1359 | 628 | |
paul@1359 | 629 | # For specific instances, convert negative selections. |
paul@1359 | 630 | |
paul@1359 | 631 | if index is not None: |
paul@1359 | 632 | l.append((weekday, index < 0 and index + 1 + limit or index)) |
paul@1359 | 633 | |
paul@1359 | 634 | # For None, introduce selections for all instances of the weekday. |
paul@1359 | 635 | |
paul@1359 | 636 | else: |
paul@1359 | 637 | index = 1 |
paul@1359 | 638 | while index <= limit: |
paul@1359 | 639 | l.append((weekday, index)) |
paul@1359 | 640 | index += 1 |
paul@1359 | 641 | |
paul@1359 | 642 | # Sort the values so that the resulting dates are ordered. |
paul@1359 | 643 | |
paul@1359 | 644 | fn = lambda x, y, weekdays=weekdays: compare_weekday_selectors(x, y, weekdays) |
paul@1359 | 645 | l.sort(cmp=fn) |
paul@1359 | 646 | return l |
paul@1359 | 647 | |
paul@1360 | 648 | def convert_positions(setpos): |
paul@1360 | 649 | |
paul@1360 | 650 | "Convert 'setpos' to 0-based indexes." |
paul@1360 | 651 | |
paul@1360 | 652 | l = [] |
paul@1360 | 653 | for pos in setpos: |
paul@1360 | 654 | index = pos < 0 and pos or pos - 1 |
paul@1360 | 655 | l.append(index) |
paul@1360 | 656 | return l |
paul@1360 | 657 | |
paul@33 | 658 | # Classes for producing instances from recurrence structures. |
paul@33 | 659 | |
paul@33 | 660 | class Selector: |
paul@358 | 661 | |
paul@358 | 662 | "A generic selector." |
paul@358 | 663 | |
paul@1241 | 664 | def __init__(self, level, args, qualifier, selecting=None, first=False): |
paul@358 | 665 | |
paul@358 | 666 | """ |
paul@358 | 667 | Initialise at the given 'level' a selector employing the given 'args' |
paul@358 | 668 | defined in the interpretation of recurrence rule qualifiers, with the |
paul@358 | 669 | 'qualifier' being the name of the rule qualifier, and 'selecting' being |
paul@358 | 670 | an optional selector used to find more specific instances from those |
paul@358 | 671 | found by this selector. |
paul@358 | 672 | """ |
paul@358 | 673 | |
paul@42 | 674 | self.level = level |
paul@33 | 675 | self.args = args |
paul@33 | 676 | self.qualifier = qualifier |
paul@1237 | 677 | self.selecting = selecting |
paul@1241 | 678 | self.first = first |
paul@1237 | 679 | |
paul@33 | 680 | def __repr__(self): |
paul@1359 | 681 | return "%s(%r, %r, %r, %r)" % (self.__class__.__name__, self.level, |
paul@1359 | 682 | self.args, self.qualifier, self.first) |
paul@33 | 683 | |
paul@359 | 684 | def materialise(self, start, end, count=None, setpos=None, inclusive=False): |
paul@358 | 685 | |
paul@358 | 686 | """ |
paul@358 | 687 | Starting at 'start', materialise instances up to but not including any |
paul@358 | 688 | at 'end' or later, returning at most 'count' if specified, and returning |
paul@358 | 689 | only the occurrences indicated by 'setpos' if specified. A list of |
paul@358 | 690 | instances is returned. |
paul@359 | 691 | |
paul@359 | 692 | If 'inclusive' is specified, the selection of instances will include the |
paul@359 | 693 | end of the search period if present in the results. |
paul@358 | 694 | """ |
paul@358 | 695 | |
paul@46 | 696 | start = to_tuple(start) |
paul@46 | 697 | end = to_tuple(end) |
paul@1359 | 698 | counter = Counter(count) |
paul@1241 | 699 | results = self.materialise_items(start, start, end, counter, setpos, inclusive) |
paul@41 | 700 | return results[:count] |
paul@33 | 701 | |
paul@359 | 702 | def materialise_item(self, current, earliest, next, counter, setpos=None, inclusive=False): |
paul@358 | 703 | |
paul@358 | 704 | """ |
paul@358 | 705 | Given the 'current' instance, the 'earliest' acceptable instance, the |
paul@358 | 706 | 'next' instance, an instance 'counter', and the optional 'setpos' |
paul@358 | 707 | criteria, return a list of result items. Where no selection within the |
paul@358 | 708 | current instance occurs, the current instance will be returned as a |
paul@358 | 709 | result if the same or later than the earliest acceptable instance. |
paul@358 | 710 | """ |
paul@358 | 711 | |
paul@45 | 712 | if self.selecting: |
paul@1359 | 713 | return self.selecting.materialise_items(current, earliest, next, |
paul@1359 | 714 | counter, setpos, inclusive) |
paul@1360 | 715 | else: |
paul@45 | 716 | return [current] |
paul@45 | 717 | |
paul@45 | 718 | def select_positions(self, results, setpos): |
paul@358 | 719 | |
paul@358 | 720 | "Select in 'results' the 1-based positions given by 'setpos'." |
paul@358 | 721 | |
paul@45 | 722 | l = [] |
paul@1360 | 723 | for index in convert_positions(setpos): |
paul@1359 | 724 | l.append(results[index]) |
paul@45 | 725 | return l |
paul@45 | 726 | |
paul@1359 | 727 | def filter_by_period(self, result, start, end, inclusive): |
paul@359 | 728 | |
paul@1359 | 729 | "Return whether 'result' occurs at or after 'start' and before 'end'." |
paul@358 | 730 | |
paul@1359 | 731 | return start <= result and (inclusive and result <= end or result < end) |
paul@33 | 732 | |
paul@33 | 733 | class Pattern(Selector): |
paul@358 | 734 | |
paul@1237 | 735 | "A selector of time periods according to a repeating pattern." |
paul@358 | 736 | |
paul@359 | 737 | def materialise_items(self, context, start, end, counter, setpos=None, inclusive=False): |
paul@1237 | 738 | |
paul@1237 | 739 | """ |
paul@1237 | 740 | Bounded by the given 'context', return periods within 'start' to 'end', |
paul@1237 | 741 | updating the 'counter', selecting only the indexes specified by 'setpos' |
paul@1237 | 742 | (if given). |
paul@1237 | 743 | |
paul@1237 | 744 | If 'inclusive' is specified, the selection of periods will include those |
paul@1237 | 745 | starting at the end of the search period, if present in the results. |
paul@1237 | 746 | """ |
paul@1237 | 747 | |
paul@1237 | 748 | # Define the step between result periods. |
paul@34 | 749 | |
paul@1359 | 750 | multiple = get_multiple(self.qualifier) |
paul@1359 | 751 | interval = self.args.get("interval", 1) * multiple |
paul@1359 | 752 | step = scale(interval, self.level) |
paul@34 | 753 | |
paul@1237 | 754 | # Define the scale of a single period. |
paul@34 | 755 | |
paul@1359 | 756 | unit_step = scale(multiple, self.level) |
paul@34 | 757 | |
paul@1241 | 758 | # Employ the context as the current period if this is the first |
paul@1241 | 759 | # qualifier in the selection chain. |
paul@1241 | 760 | |
paul@1241 | 761 | if self.first: |
paul@1359 | 762 | current = precision(context, self.level) |
paul@1237 | 763 | |
paul@1241 | 764 | # Otherwise, obtain the first value at this resolution within the |
paul@1241 | 765 | # context period. |
paul@1241 | 766 | |
paul@1241 | 767 | else: |
paul@1359 | 768 | current = precision(context, self.level, firstvalues[self.level]) |
paul@1241 | 769 | |
paul@33 | 770 | results = [] |
paul@34 | 771 | |
paul@1237 | 772 | # Obtain periods before the end (and also at the end if inclusive), |
paul@1237 | 773 | # provided that any limit imposed by the counter has not been exceeded. |
paul@1237 | 774 | |
paul@1237 | 775 | while (inclusive and current <= end or current < end) and \ |
paul@1359 | 776 | not counter.at_limit(): |
paul@1237 | 777 | |
paul@1237 | 778 | # Increment the current datetime by the step for the next period. |
paul@1237 | 779 | |
paul@33 | 780 | next = update(current, step) |
paul@1237 | 781 | |
paul@1237 | 782 | # Determine the end point of the current period. |
paul@1237 | 783 | |
paul@33 | 784 | current_end = update(current, unit_step) |
paul@1237 | 785 | |
paul@1237 | 786 | # Obtain any period or periods within the bounds defined by the |
paul@1237 | 787 | # current period and any contraining start and end points, plus |
paul@1237 | 788 | # counter, setpos and inclusive details. |
paul@1237 | 789 | |
paul@1359 | 790 | interval_results = self.materialise_item(current, |
paul@1359 | 791 | max(current, start), min(current_end, end), |
paul@1359 | 792 | counter, setpos, inclusive) |
paul@1237 | 793 | |
paul@1237 | 794 | # Update the counter with the number of identified results. |
paul@1237 | 795 | |
paul@1359 | 796 | counter += len(interval_results) |
paul@1237 | 797 | |
paul@1237 | 798 | # Accumulate the results. |
paul@1237 | 799 | |
paul@45 | 800 | results += interval_results |
paul@1237 | 801 | |
paul@1237 | 802 | # Visit the next instance. |
paul@1237 | 803 | |
paul@33 | 804 | current = next |
paul@34 | 805 | |
paul@33 | 806 | return results |
paul@33 | 807 | |
paul@35 | 808 | class WeekDayFilter(Selector): |
paul@358 | 809 | |
paul@358 | 810 | "A selector of instances specified in terms of day numbers." |
paul@358 | 811 | |
paul@359 | 812 | def materialise_items(self, context, start, end, counter, setpos=None, inclusive=False): |
paul@1359 | 813 | step = scale(1, WEEKS) |
paul@33 | 814 | results = [] |
paul@34 | 815 | |
paul@39 | 816 | # Get weekdays in the year. |
paul@39 | 817 | |
paul@1359 | 818 | if is_year_only(context): |
paul@1359 | 819 | first_day = start_of_year(context) |
paul@1359 | 820 | last_day = end_of_year(context) |
paul@39 | 821 | |
paul@39 | 822 | # Get weekdays in the month. |
paul@39 | 823 | |
paul@1359 | 824 | elif is_month_only(context): |
paul@1359 | 825 | first_day = start_of_month(context) |
paul@1359 | 826 | last_day = end_of_month(context) |
paul@39 | 827 | |
paul@39 | 828 | # Get weekdays in the week. |
paul@39 | 829 | |
paul@39 | 830 | else: |
paul@39 | 831 | current = context |
paul@39 | 832 | values = [value for (value, index) in self.args["values"]] |
paul@39 | 833 | |
paul@359 | 834 | while (inclusive and current <= end or current < end): |
paul@39 | 835 | next = update(current, step) |
paul@1359 | 836 | |
paul@39 | 837 | if date(*current).isoweekday() in values: |
paul@1359 | 838 | results += self.materialise_item(current, |
paul@1359 | 839 | max(current, start), min(next, end), |
paul@1359 | 840 | counter, inclusive=inclusive) |
paul@39 | 841 | current = next |
paul@45 | 842 | |
paul@45 | 843 | if setpos: |
paul@45 | 844 | return self.select_positions(results, setpos) |
paul@45 | 845 | else: |
paul@45 | 846 | return results |
paul@39 | 847 | |
paul@39 | 848 | # Find each of the given days. |
paul@39 | 849 | |
paul@1359 | 850 | for value, index in sort_weekdays(self.args["values"], first_day, last_day): |
paul@1360 | 851 | offset = timedelta(7 * (index - 1)) |
paul@1359 | 852 | |
paul@1359 | 853 | current = precision(to_tuple(get_first_day(first_day, value) + offset), DAYS) |
paul@1359 | 854 | next = update(current, step) |
paul@39 | 855 | |
paul@1359 | 856 | # To support setpos, only current and next bound the search, not |
paul@1359 | 857 | # the period in addition. |
paul@39 | 858 | |
paul@1359 | 859 | results += self.materialise_item(current, current, next, counter, |
paul@1359 | 860 | inclusive=inclusive) |
paul@45 | 861 | |
paul@1359 | 862 | # Extract selected positions and remove out-of-period instances. |
paul@45 | 863 | |
paul@1359 | 864 | if setpos: |
paul@1359 | 865 | results = self.select_positions(results, setpos) |
paul@39 | 866 | |
paul@1359 | 867 | return filter(lambda result: |
paul@1359 | 868 | self.filter_by_period(result, start, end, inclusive), |
paul@1359 | 869 | results) |
paul@1359 | 870 | |
paul@1359 | 871 | class Enum(Selector): |
paul@1359 | 872 | |
paul@1359 | 873 | "A generic value selector." |
paul@1359 | 874 | |
paul@1359 | 875 | def materialise_items(self, context, start, end, counter, setpos=None, inclusive=False): |
paul@1359 | 876 | step = scale(1, self.level) |
paul@1359 | 877 | results = [] |
paul@39 | 878 | |
paul@1359 | 879 | # Select each value at the current resolution. |
paul@1359 | 880 | |
paul@1359 | 881 | for value in sort_values(self.args["values"]): |
paul@1359 | 882 | current = precision(context, self.level, value) |
paul@1359 | 883 | next = update(current, step) |
paul@45 | 884 | |
paul@1359 | 885 | # To support setpos, only current and next bound the search, not |
paul@1359 | 886 | # the period in addition. |
paul@45 | 887 | |
paul@1359 | 888 | results += self.materialise_item(current, current, next, counter, |
paul@1359 | 889 | setpos, inclusive) |
paul@34 | 890 | |
paul@45 | 891 | # Extract selected positions and remove out-of-period instances. |
paul@45 | 892 | |
paul@45 | 893 | if setpos: |
paul@45 | 894 | results = self.select_positions(results, setpos) |
paul@45 | 895 | |
paul@1359 | 896 | return filter(lambda result: |
paul@1359 | 897 | self.filter_by_period(result, start, end, inclusive), |
paul@1359 | 898 | results) |
paul@33 | 899 | |
paul@1359 | 900 | class MonthDayFilter(Enum): |
paul@1359 | 901 | |
paul@1359 | 902 | "A selector of month days." |
paul@1359 | 903 | |
paul@359 | 904 | def materialise_items(self, context, start, end, counter, setpos=None, inclusive=False): |
paul@1359 | 905 | step = scale(1, self.level) |
paul@33 | 906 | results = [] |
paul@1359 | 907 | |
paul@1359 | 908 | last_day = end_of_month(context)[2] |
paul@1359 | 909 | |
paul@1359 | 910 | for value in sort_values(self.args["values"], last_day): |
paul@1359 | 911 | current = precision(context, self.level, value) |
paul@45 | 912 | next = update(current, step) |
paul@45 | 913 | |
paul@45 | 914 | # To support setpos, only current and next bound the search, not |
paul@45 | 915 | # the period in addition. |
paul@45 | 916 | |
paul@1359 | 917 | results += self.materialise_item(current, current, next, counter, |
paul@1359 | 918 | inclusive=inclusive) |
paul@45 | 919 | |
paul@45 | 920 | # Extract selected positions and remove out-of-period instances. |
paul@45 | 921 | |
paul@45 | 922 | if setpos: |
paul@45 | 923 | results = self.select_positions(results, setpos) |
paul@45 | 924 | |
paul@1359 | 925 | return filter(lambda result: |
paul@1359 | 926 | self.filter_by_period(result, start, end, inclusive), |
paul@1359 | 927 | results) |
paul@35 | 928 | |
paul@1359 | 929 | class YearDayFilter(Enum): |
paul@1359 | 930 | |
paul@1359 | 931 | "A selector of days in years." |
paul@1359 | 932 | |
paul@359 | 933 | def materialise_items(self, context, start, end, counter, setpos=None, inclusive=False): |
paul@1359 | 934 | step = scale(1, self.level) |
paul@35 | 935 | results = [] |
paul@1359 | 936 | |
paul@1359 | 937 | year_length = get_year_length(context) |
paul@1359 | 938 | |
paul@1359 | 939 | for value in sort_values(self.args["values"], year_length): |
paul@1359 | 940 | current = day_in_year(context, value) |
paul@45 | 941 | next = update(current, step) |
paul@45 | 942 | |
paul@45 | 943 | # To support setpos, only current and next bound the search, not |
paul@45 | 944 | # the period in addition. |
paul@45 | 945 | |
paul@1359 | 946 | results += self.materialise_item(current, current, next, counter, |
paul@1359 | 947 | inclusive=inclusive) |
paul@45 | 948 | |
paul@45 | 949 | # Extract selected positions and remove out-of-period instances. |
paul@45 | 950 | |
paul@45 | 951 | if setpos: |
paul@45 | 952 | results = self.select_positions(results, setpos) |
paul@45 | 953 | |
paul@1359 | 954 | return filter(lambda result: |
paul@1359 | 955 | self.filter_by_period(result, start, end, inclusive), |
paul@1359 | 956 | results) |
paul@37 | 957 | |
paul@46 | 958 | special_enum_levels = { |
paul@46 | 959 | "BYDAY" : WeekDayFilter, |
paul@46 | 960 | "BYMONTHDAY" : MonthDayFilter, |
paul@46 | 961 | "BYYEARDAY" : YearDayFilter, |
paul@46 | 962 | } |
paul@46 | 963 | |
paul@1359 | 964 | class Counter: |
paul@1359 | 965 | |
paul@1359 | 966 | "A counter to track instance quantities." |
paul@1359 | 967 | |
paul@1359 | 968 | def __init__(self, limit): |
paul@1359 | 969 | self.current = 0 |
paul@1359 | 970 | self.limit = limit |
paul@1359 | 971 | |
paul@1359 | 972 | def __iadd__(self, n): |
paul@1359 | 973 | self.current += n |
paul@1359 | 974 | return self |
paul@1359 | 975 | |
paul@1359 | 976 | def at_limit(self): |
paul@1359 | 977 | return self.limit is not None and self.current >= self.limit |
paul@1359 | 978 | |
paul@46 | 979 | # Public functions. |
paul@46 | 980 | |
paul@46 | 981 | def connect_selectors(selectors): |
paul@358 | 982 | |
paul@358 | 983 | """ |
paul@358 | 984 | Make the 'selectors' reference each other in a hierarchy so that |
paul@358 | 985 | materialising the principal selector causes the more specific ones to be |
paul@358 | 986 | employed in the operation. |
paul@358 | 987 | """ |
paul@358 | 988 | |
paul@33 | 989 | current = selectors[0] |
paul@1241 | 990 | current.first = True |
paul@33 | 991 | for selector in selectors[1:]: |
paul@33 | 992 | current.selecting = selector |
paul@33 | 993 | current = selector |
paul@33 | 994 | return selectors[0] |
paul@33 | 995 | |
paul@46 | 996 | def get_selector(dt, qualifiers): |
paul@322 | 997 | |
paul@322 | 998 | """ |
paul@322 | 999 | Combine the initial datetime 'dt' with the given 'qualifiers', returning an |
paul@322 | 1000 | object that can be used to select recurrences described by the 'qualifiers'. |
paul@322 | 1001 | """ |
paul@322 | 1002 | |
paul@46 | 1003 | dt = to_tuple(dt) |
paul@46 | 1004 | return connect_selectors(combine_datetime_with_qualifiers(dt, qualifiers)) |
paul@46 | 1005 | |
paul@46 | 1006 | def get_rule(dt, rule): |
paul@317 | 1007 | |
paul@317 | 1008 | """ |
paul@317 | 1009 | Using the given initial datetime 'dt', interpret the 'rule' (a semicolon- |
paul@317 | 1010 | separated collection of "key=value" strings), and return the resulting |
paul@317 | 1011 | selector object. |
paul@317 | 1012 | """ |
paul@317 | 1013 | |
paul@351 | 1014 | if not isinstance(rule, tuple): |
paul@351 | 1015 | rule = rule.split(";") |
paul@351 | 1016 | qualifiers = get_qualifiers(rule) |
paul@46 | 1017 | return get_selector(dt, qualifiers) |
paul@35 | 1018 | |
paul@33 | 1019 | # vim: tabstop=4 expandtab shiftwidth=4 |