1.1 --- a/tests/qualifiers.py Sat Oct 04 01:07:46 2014 +0200
1.2 +++ b/tests/qualifiers.py Sat Oct 04 16:04:55 2014 +0200
1.3 @@ -20,7 +20,7 @@
1.4 show(l)
1.5
1.6 s = process(l)
1.7 -l = s.materialise((2003, 12, 24))
1.8 +l = s.materialise(dt, (2003, 12, 24))
1.9 print len(l) == 7, len(l)
1.10 print l[0] == (1997, 11, 2), l[0]
1.11 print l[-1] == (2003, 11, 2), l[-1]
1.12 @@ -43,9 +43,9 @@
1.13 show(l)
1.14
1.15 s = process(l)
1.16 -l = s.materialise((2003, 12, 24, 0, 0, 0))
1.17 -print len(l) == 34, len(l)
1.18 -print l[0] == (1997, 1, 4, 8, 30, 0), l[0]
1.19 +l = s.materialise(dt, (2003, 12, 24, 0, 0, 0))
1.20 +print len(l) == 32, len(l)
1.21 +print l[0] == (1997, 1, 11, 8, 30, 0), l[0]
1.22 print l[-1] == (2003, 1, 25, 9, 30, 0), l[-1]
1.23 print
1.24
1.25 @@ -62,7 +62,7 @@
1.26 show(l)
1.27
1.28 s = process(l)
1.29 -l = s.materialise((1997, 12, 24), 10)
1.30 +l = s.materialise(dt, (1997, 12, 24), 10)
1.31 print len(l) == 10, len(l)
1.32 print l[0] == (1997, 9, 2, 9, 0, 0), l[0]
1.33 print l[-1] == (1997, 9, 11, 9, 0, 0), l[-1]
1.34 @@ -81,7 +81,7 @@
1.35 show(l)
1.36
1.37 s = process(l)
1.38 -l = s.materialise((1997, 12, 24, 0, 0, 0))
1.39 +l = s.materialise(dt, (1997, 12, 24, 0, 0, 0))
1.40 print len(l) == 113, len(l)
1.41 print l[0] == (1997, 9, 2, 9, 0, 0), l[0]
1.42 print l[-1] == (1997, 12, 23, 9, 0, 0), l[-1]
1.43 @@ -100,7 +100,7 @@
1.44 show(l)
1.45
1.46 s = process(l)
1.47 -l = s.materialise((1997, 12, 24, 0, 0, 0))
1.48 +l = s.materialise(dt, (1997, 12, 24, 0, 0, 0))
1.49 print len(l) == 57, len(l)
1.50 print l[0] == (1997, 9, 2, 9, 0, 0), l[0]
1.51 print l[-1] == (1997, 12, 23, 9, 0, 0), l[-1]
1.52 @@ -119,7 +119,7 @@
1.53 show(l)
1.54
1.55 s = process(l)
1.56 -l = s.materialise((1997, 12, 24, 0, 0, 0))
1.57 +l = s.materialise(dt, (1997, 12, 24, 0, 0, 0))
1.58 print len(l) == 17, len(l)
1.59 print l[0] == (1997, 9, 2, 9, 0, 0), l[0]
1.60 print l[-1] == (1997, 12, 23, 9, 0, 0), l[-1]
1.61 @@ -138,7 +138,7 @@
1.62 show(l)
1.63
1.64 s = process(l)
1.65 -l = s.materialise((1997, 12, 24, 0, 0, 0), 5)
1.66 +l = s.materialise(dt, (1997, 12, 24, 0, 0, 0), 5)
1.67 print len(l) == 5, len(l)
1.68 print l[0] == (1997, 9, 2, 9, 0, 0), l[0]
1.69 print l[-1] == (1997, 10, 12, 9, 0, 0), l[-1]
1.70 @@ -159,7 +159,7 @@
1.71 show(l)
1.72
1.73 s = process(l)
1.74 -l = s.materialise((2000, 1, 31, 14, 0, 0))
1.75 +l = s.materialise(dt, (2000, 1, 31, 14, 0, 0))
1.76 print len(l) == 93, len(l)
1.77 print l[0] == (1998, 1, 1, 9, 0, 0), l[0]
1.78 print l[-1] == (2000, 1, 31, 9, 0, 0), l[-1]
1.79 @@ -179,7 +179,7 @@
1.80 show(l)
1.81
1.82 s = process(l)
1.83 -l = s.materialise((2000, 1, 31, 14, 0, 0))
1.84 +l = s.materialise(dt, (2000, 1, 31, 14, 0, 0))
1.85 print len(l) == 93, len(l)
1.86 print l[0] == (1998, 1, 1, 9, 0, 0), l[0]
1.87 print l[-1] == (2000, 1, 31, 9, 0, 0), l[-1]
1.88 @@ -198,7 +198,7 @@
1.89 show(l)
1.90
1.91 s = process(l)
1.92 -l = s.materialise((1997, 12, 24, 0, 0, 0), 10)
1.93 +l = s.materialise(dt, (1997, 12, 24, 0, 0, 0), 10)
1.94 print len(l) == 10, len(l)
1.95 print l[0] == (1997, 9, 2, 9, 0, 0), l[0]
1.96 print l[-1] == (1997, 11, 4, 9, 0, 0), l[-1]
1.97 @@ -217,7 +217,7 @@
1.98 show(l)
1.99
1.100 s = process(l)
1.101 -l = s.materialise((1997, 12, 24, 0, 0, 0))
1.102 +l = s.materialise(dt, (1997, 12, 24, 0, 0, 0))
1.103 print len(l) == 17, len(l)
1.104 print l[0] == (1997, 9, 2, 9, 0, 0), l[0]
1.105 print l[-1] == (1997, 12, 23, 9, 0, 0), l[-1]
1.106 @@ -236,7 +236,7 @@
1.107 show(l)
1.108
1.109 s = process(l)
1.110 -l = s.materialise((1998, 2, 20, 0, 0, 0))
1.111 +l = s.materialise(dt, (1998, 2, 20, 0, 0, 0))
1.112 print len(l) == 13, len(l)
1.113 print l[0] == (1997, 9, 2, 9, 0, 0), l[0]
1.114 print l[-1] == (1998, 2, 17, 9, 0, 0), l[-1]
1.115 @@ -256,7 +256,7 @@
1.116 show(l)
1.117
1.118 s = process(l)
1.119 -l = s.materialise((1997, 10, 7, 9, 0, 0))
1.120 +l = s.materialise(dt, (1997, 10, 7, 9, 0, 0))
1.121 print len(l) == 10, len(l)
1.122 print l[0] == (1997, 9, 2, 9, 0, 0), l[0]
1.123 print l[-1] == (1997, 10, 2, 9, 0, 0), l[-1]
1.124 @@ -276,7 +276,7 @@
1.125 show(l)
1.126
1.127 s = process(l)
1.128 -l = s.materialise((1997, 12, 24, 0, 0, 0), 10)
1.129 +l = s.materialise(dt, (1997, 12, 24, 0, 0, 0), 10)
1.130 print len(l) == 10, len(l)
1.131 print l[0] == (1997, 9, 2, 9, 0, 0), l[0]
1.132 print l[-1] == (1997, 10, 2, 9, 0, 0), l[-1]
1.133 @@ -296,7 +296,7 @@
1.134 show(l)
1.135
1.136 s = process(l)
1.137 -l = s.materialise((1997, 12, 24, 0, 0, 0))
1.138 +l = s.materialise(dt, (1997, 12, 24, 0, 0, 0))
1.139 print len(l) == 25, len(l)
1.140 print l[0] == (1997, 9, 1, 9, 0, 0), l[0]
1.141 print l[-1] == (1997, 12, 22, 9, 0, 0), l[-1]
1.142 @@ -316,7 +316,7 @@
1.143 show(l)
1.144
1.145 s = process(l)
1.146 -l = s.materialise((1997, 12, 24, 0, 0, 0), 8)
1.147 +l = s.materialise(dt, (1997, 12, 24, 0, 0, 0), 8)
1.148 print len(l) == 8, len(l)
1.149 print l[0] == (1997, 9, 2, 9, 0, 0), l[0]
1.150 print l[-1] == (1997, 10, 16, 9, 0, 0), l[-1]
1.151 @@ -336,7 +336,7 @@
1.152 show(l)
1.153
1.154 s = process(l)
1.155 -l = s.materialise((1998, 12, 24, 0, 0, 0), 10)
1.156 +l = s.materialise(dt, (1998, 12, 24, 0, 0, 0), 10)
1.157 print len(l) == 10, len(l)
1.158 print l[0] == (1997, 9, 5, 9, 0, 0), l[0]
1.159 print l[-1] == (1998, 6, 5, 9, 0, 0), l[-1]
1.160 @@ -356,7 +356,7 @@
1.161 show(l)
1.162
1.163 s = process(l)
1.164 -l = s.materialise((1997, 12, 24, 0, 0, 0))
1.165 +l = s.materialise(dt, (1997, 12, 24, 0, 0, 0))
1.166 print len(l) == 4, len(l)
1.167 print l[0] == (1997, 9, 5, 9, 0, 0), l[0]
1.168 print l[-1] == (1997, 12, 5, 9, 0, 0), l[-1]
1.169 @@ -376,7 +376,7 @@
1.170 show(l)
1.171
1.172 s = process(l)
1.173 -l = s.materialise((1998, 12, 24, 0, 0, 0), 10)
1.174 +l = s.materialise(dt, (1998, 12, 24, 0, 0, 0), 10)
1.175 print len(l) == 10, len(l)
1.176 print l[0] == (1997, 9, 7, 9, 0, 0), l[0]
1.177 print l[-1] == (1998, 5, 31, 9, 0, 0), l[-1]
1.178 @@ -396,10 +396,70 @@
1.179 show(l)
1.180
1.181 s = process(l)
1.182 -l = s.materialise((1998, 12, 24, 0, 0, 0), 6)
1.183 +l = s.materialise(dt, (1998, 12, 24, 0, 0, 0), 6)
1.184 print len(l) == 6, len(l)
1.185 print l[0] == (1997, 9, 22, 9, 0, 0), l[0]
1.186 print l[-1] == (1998, 2, 16, 9, 0, 0), l[-1]
1.187 print
1.188
1.189 +qualifiers = [
1.190 + ("MONTHLY", {"interval" : 1}),
1.191 + ("BYMONTHDAY", {"values" : [-3]})
1.192 + ]
1.193 +
1.194 +l = order_qualifiers(qualifiers)
1.195 +show(l)
1.196 +dt = (1997, 9, 28, 9, 0, 0)
1.197 +l = get_datetime_structure(dt)
1.198 +show(l)
1.199 +l = combine_datetime_with_qualifiers(dt, qualifiers)
1.200 +show(l)
1.201 +
1.202 +s = process(l)
1.203 +l = s.materialise(dt, (1998, 12, 24, 0, 0, 0), 6)
1.204 +print len(l) == 6, len(l)
1.205 +print l[0] == (1997, 9, 28, 9, 0, 0), l[0]
1.206 +print l[-1] == (1998, 2, 26, 9, 0, 0), l[-1]
1.207 +print
1.208 +
1.209 +qualifiers = [
1.210 + ("MONTHLY", {"interval" : 1}),
1.211 + ("BYMONTHDAY", {"values" : [2, 15]})
1.212 + ]
1.213 +
1.214 +l = order_qualifiers(qualifiers)
1.215 +show(l)
1.216 +dt = (1997, 9, 2, 9, 0, 0)
1.217 +l = get_datetime_structure(dt)
1.218 +show(l)
1.219 +l = combine_datetime_with_qualifiers(dt, qualifiers)
1.220 +show(l)
1.221 +
1.222 +s = process(l)
1.223 +l = s.materialise(dt, (1998, 12, 24, 0, 0, 0), 10)
1.224 +print len(l) == 10, len(l)
1.225 +print l[0] == (1997, 9, 2, 9, 0, 0), l[0]
1.226 +print l[-1] == (1998, 1, 15, 9, 0, 0), l[-1]
1.227 +print
1.228 +
1.229 +qualifiers = [
1.230 + ("MONTHLY", {"interval" : 1}),
1.231 + ("BYMONTHDAY", {"values" : [1, -1]})
1.232 + ]
1.233 +
1.234 +l = order_qualifiers(qualifiers)
1.235 +show(l)
1.236 +dt = (1997, 9, 30, 9, 0, 0)
1.237 +l = get_datetime_structure(dt)
1.238 +show(l)
1.239 +l = combine_datetime_with_qualifiers(dt, qualifiers)
1.240 +show(l)
1.241 +
1.242 +s = process(l)
1.243 +l = s.materialise(dt, (1998, 12, 24, 0, 0, 0), 10)
1.244 +print len(l) == 10, len(l)
1.245 +print l[0] == (1997, 9, 30, 9, 0, 0), l[0]
1.246 +print l[-1] == (1998, 2, 1, 9, 0, 0), l[-1]
1.247 +print
1.248 +
1.249 # vim: tabstop=4 expandtab shiftwidth=4
2.1 --- a/vRecurrence.py Sat Oct 04 01:07:46 2014 +0200
2.2 +++ b/vRecurrence.py Sat Oct 04 16:04:55 2014 +0200
2.3 @@ -78,8 +78,6 @@
2.4 ("BYMONTH",)
2.5 )
2.6
2.7 -special_enum_levels = ("BYDAY", "BYMONTHDAY", "BYYEARDAY")
2.8 -
2.9 # Map from levels to lengths of datetime tuples.
2.10
2.11 lengths = [6, 5, 4, 3, 3, 2, 1]
2.12 @@ -118,9 +116,9 @@
2.13 for qualifier, args in qualifiers:
2.14 if enum.has_key(qualifier):
2.15 level = enum[qualifier]
2.16 - if qualifier in special_enum_levels:
2.17 + if special_enum_levels.has_key(qualifier):
2.18 args["interval"] = 1
2.19 - selector = PatternFilter
2.20 + selector = special_enum_levels[qualifier]
2.21 else:
2.22 selector = Enum
2.23 else:
2.24 @@ -295,23 +293,22 @@
2.25 def __repr__(self):
2.26 return "%s(%r, %r, %r, %r)" % (self.__class__.__name__, self.pos, self.args, self.qualifier, self.context)
2.27
2.28 - def materialise(self, end, count=None):
2.29 + def materialise(self, start, end, count=None):
2.30 counter = count and [0, count]
2.31 - return self.materialise_items(self.context, end, counter)
2.32 + return self.materialise_items(self.context, start, end, counter)
2.33
2.34 - def materialise_item(self, current, next, counter):
2.35 + def materialise_item(self, current, last, next, counter):
2.36 if counter is None or counter[0] < counter[1]:
2.37 if self.selecting:
2.38 - return self.selecting.materialise_items(current, next, counter)
2.39 - else:
2.40 + return self.selecting.materialise_items(current, last, next, counter)
2.41 + elif last <= current:
2.42 if counter is not None:
2.43 counter[0] += 1
2.44 return [current]
2.45 - else:
2.46 - return []
2.47 + return []
2.48
2.49 class Pattern(Selector):
2.50 - def materialise_items(self, context, end, counter):
2.51 + def materialise_items(self, context, start, end, counter):
2.52 first = scale(self.args["values"][0], self.pos)
2.53
2.54 # Define the step between items.
2.55 @@ -330,13 +327,13 @@
2.56 while current < end and (counter is None or counter[0] < counter[1]):
2.57 next = update(current, step)
2.58 current_end = update(current, unit_step)
2.59 - results += self.materialise_item(current, min(current_end, end), counter)
2.60 + results += self.materialise_item(current, max(current, start), min(current_end, end), counter)
2.61 current = next
2.62
2.63 return results
2.64
2.65 -class PatternFilter(Selector):
2.66 - def materialise_items(self, context, end, counter):
2.67 +class WeekDayFilter(Selector):
2.68 + def materialise_items(self, context, start, end, counter):
2.69 first = scale(bases[self.pos], self.pos)
2.70
2.71 # Define the step between items to be tested.
2.72 @@ -348,36 +345,49 @@
2.73
2.74 while current < end and (counter is None or counter[0] < counter[1]):
2.75 next = update(current, step)
2.76 - results += self.materialise_item(current, min(next, end), counter)
2.77 + results += self.materialise_item(current, max(current, start), min(next, end), counter)
2.78 current = next
2.79
2.80 return results
2.81
2.82 - def materialise_item(self, current, next, counter):
2.83 + def materialise_item(self, current, last, next, counter):
2.84 values = self.args["values"]
2.85
2.86 # Select by day in week, also by occurrence in month.
2.87
2.88 - if self.qualifier == "BYDAY":
2.89 - for value, index in values:
2.90 - if datetime(*current).isoweekday() == value:
2.91 - last_day = monthrange(current[0], current[1])[1]
2.92 - index_from_start = (current[2] - 1) / 7
2.93 - index_from_end = -(1 + (last_day - current[2]) / 7)
2.94 - if index is None or index in (index_from_start, index_from_end):
2.95 - return Selector.materialise_item(self, current, next, counter)
2.96 + for value, index in values:
2.97 + if datetime(*current).isoweekday() == value:
2.98 + last_day = monthrange(current[0], current[1])[1]
2.99 + index_from_start = (current[2] - 1) / 7
2.100 + index_from_end = -(1 + (last_day - current[2]) / 7)
2.101 + if index is None or index in (index_from_start, index_from_end):
2.102 + return Selector.materialise_item(self, current, last, next, counter)
2.103
2.104 return []
2.105
2.106 class Enum(Selector):
2.107 - def materialise_items(self, context, end, counter):
2.108 + def materialise_items(self, context, start, end, counter):
2.109 step = scale(1, self.pos)
2.110 results = []
2.111 for value in self.args["values"]:
2.112 current = combine(context, scale(value, self.pos))
2.113 if current < end and (counter is None or counter[0] < counter[1]):
2.114 next = update(current, step)
2.115 - results += self.materialise_item(current, min(next, end), counter)
2.116 + results += self.materialise_item(current, max(current, start), min(next, end), counter)
2.117 + return results
2.118 +
2.119 +class MonthDayFilter(Enum):
2.120 + def materialise_items(self, context, start, end, counter):
2.121 + last_day = monthrange(context[0], context[1])[1]
2.122 + step = scale(1, self.pos)
2.123 + results = []
2.124 + for value in self.args["values"]:
2.125 + if value < 0:
2.126 + value = last_day + 1 + value
2.127 + current = combine(context, scale(value, self.pos))
2.128 + if current < end and (counter is None or counter[0] < counter[1]):
2.129 + next = update(current, step)
2.130 + results += self.materialise_item(current, max(current, start), min(next, end), counter)
2.131 return results
2.132
2.133 def process(selectors):
2.134 @@ -387,4 +397,10 @@
2.135 current = selector
2.136 return selectors[0]
2.137
2.138 +special_enum_levels = {
2.139 + "BYDAY" : WeekDayFilter,
2.140 + "BYMONTHDAY" : MonthDayFilter,
2.141 + #"BYYEARDAY" : YearDayFilter,
2.142 + }
2.143 +
2.144 # vim: tabstop=4 expandtab shiftwidth=4