forked from garrickp/tzinfo_py
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrulecompile.py
More file actions
289 lines (252 loc) · 9.9 KB
/
rulecompile.py
File metadata and controls
289 lines (252 loc) · 9.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
"""
Compile rule data into rulesets.
Copyright (c) 2012 Garrick Peterson
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import re
# Sample rule data with header
# # Rule NAME FROM TO TYPE IN ON AT SAVE LETTER/S
# Rule US 1918 1919 - Mar lastSun 2:00 1:00 D
# Rule US 1918 1919 - Oct lastSun 2:00 0 S
# Rule US 1942 only - Feb 9 2:00 1:00 W # War
# Rule US 1945 only - Aug 14 23:00u 1:00 P # Peace
# Rule US 1945 only - Sep 30 2:00 0 S
# Rule US 1967 2006 - Oct lastSun 2:00 0 S
# Rule US 1967 1973 - Apr lastSun 2:00 1:00 D
# Rule US 1974 only - Jan 6 2:00 1:00 D
# Rule US 1975 only - Feb 23 2:00 1:00 D
# Rule US 1976 1986 - Apr lastSun 2:00 1:00 D
# Rule US 1987 2006 - Apr Sun>=1 2:00 1:00 D
# Rule US 2007 max - Mar Sun>=8 2:00 1:00 D
# Rule US 2007 max - Nov Sun>=1 2:00 0 S
# Target code should look something like this (minus comments & whitespace):
# def __rule_us(dt):
# s = 0
# l = ''
#
# # Rule US 1918 1919 - Mar lastSun 2:00 1:00 D
# if dt.year >= 1918 and dt.year <= 1919 and dt >= __lastSun(dt.year, 3, 2, 0):
# s = timedelta(hours=1)
# l = 'D'
#
# # Rule US 1942 only - Feb 9 2:00 1:00 W # War
# if dt.year == 1942 and dt >= datetime(dt.year, 2, 9, 2, 0)
# s = timedelta(hours=1)
# l = 'W'
#
# # Rule US 2007 max - Mar Sun>=8 2:00 1:00 D
# if dt.year >= 2007 and dt >= __sunGtEq(dt.year, 3, 8, 2, 0)
# s = timedelta(hours=1)
# l = 'D'
MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
INDENT = ' '
def name_to_identifier(name):
return name.replace('/', '_').replace('-', '_').replace('+', 'plus')
# TODO Clean up to attempt to match PEP8
class CompileError(Exception):
pass
# TODO Get this into some kind of common module - too much code duplication
class ASO(object):
pass
class RuleSet(ASO):
def __init__(self, n):
self.name = n
self.codename = name_to_identifier(n)
self.initial_assignments = [Assignment('s', FuncCall('timedelta')), Assignment('l', 'S')]
self.rule_elements = []
def render(self, level=0):
yield('\n')
yield(INDENT * level)
yield('def _')
yield(self.codename)
yield('(dt):')
for a in self.initial_assignments:
for x in a.render(level + 1):
yield(x)
for r_ele in self.rule_elements:
for x in r_ele.render(level + 1):
yield(x)
yield('\n')
yield(INDENT * (level + 1))
yield('return (s, l)')
class RuleElement(ASO):
def __init__(self):
self.conditions = []
self.assignments = []
def render(self, level=0):
yield('\n')
yield(INDENT * level)
yield('if ')
first=True
for c in self.conditions:
if not first:
yield(' and ')
else:
first = False
for x in c.render(level + 1):
yield x
if first:
yield('True')
yield(':')
for a in self.assignments:
for x in a.render(level + 1):
yield x
class Assignment(ASO):
def __init__(self, n, v):
self.name = n
self.value = v
def render(self, level=0):
yield('\n')
yield(INDENT * level)
yield(self.name)
yield(' = ')
if isinstance(self.value, ASO):
for x in self.value.render(level):
yield(x)
else:
yield(repr(self.value))
class Condition(ASO):
def __init__(self, n, o, v):
self.name = n
self.operator = o
self.value = v
def render(self, level=0):
yield('(')
yield(self.name)
yield(' %s ' % (self.operator,))
if isinstance(self.value, ASO):
for x in self.value.render(level):
yield(x)
else:
yield(repr(self.value))
yield(')')
class FuncCall(ASO):
def __init__(self, n, *args, **kwargs):
self.name = n
self.args = args
self.kwargs = kwargs
def render(self, level=0):
yield(self.name)
yield('(')
first = True
for a in self.args:
if not first:
yield ", "
else:
first = False
if isinstance(a, ASO):
for x in a.render(level):
yield(x)
else:
yield(repr(a))
for k, v in self.kwargs.items():
if not first:
yield ", "
else:
first = False
yield(k)
yield('=')
if isinstance(v, ASO):
for x in v.render(level):
yield(x)
else:
yield(repr(v))
yield(')')
class Identifier(ASO):
def __init__(self, n):
self.name = n
def render(self, level=0):
yield(self.name)
def compile(rules):
all_rulesets = {}
for _, rule_list in rules.items():
for rule in rule_list:
# TODO Get logic for all the re.match and ints out of here and
# into parse
r_ele = RuleElement()
try:
# Fix for systemV rule entry
if rule['from'] == 'min':
from_yr = 0
else:
from_yr = int(rule['from'])
if rule['to'] == 'only':
r_ele.conditions.append(Condition('dt.year', '==', from_yr))
elif rule['to'] == 'max':
r_ele.conditions.append(Condition('dt.year', '>=', from_yr))
else:
r_ele.conditions.append(Condition('dt.year', '>=', from_yr))
r_ele.conditions.append(Condition('dt.year', '<=', int(rule['to'])))
except ValueError:
raise CompileError("Problem creating condition for 'from %r to %r'"
% (rule['from'], rule['to']))
try:
in_mo = MONTHS.index(rule['in']) + 1
except ValueError:
raise CompileError("Not able to index month %r" % (rule['in'],))
try:
on_day = int(rule['on'])
except ValueError:
on_day = None
try:
at_h_m = re.match(r'(\d+):?(\d+)?', rule['at']).groups()
at_hour = int(at_h_m[0])
at_min = int(at_h_m[1]) if at_h_m[1] else 0
except AttributeError:
raise CompileError("unable to turn %r to hours:minutes" % (rule['at'],))
# Fix for Jordan Rule:
if at_hour >= 24:
at_hour = 24 - at_hour
if on_day:
f_call = FuncCall('datetime', Identifier('dt.year'), in_mo, on_day, int(at_hour), int(at_min))
else:
if any([x in rule['on'] for x in ('=','>','<')]):
try:
f_name, d = re.match(r'( ?[a-zA-Z<>=]+)(\d+)', rule['on']).groups()
d = int(d)
f_name = f_name.strip()
except Exception:
raise CompileError("Problem extracting day from %r" % (rule['on'],))
f_name = f_name.replace('>', 'Gt')
f_name = f_name.replace('<', 'Lt')
f_name = f_name.replace('=', 'Eq')
f_call = FuncCall('__' + f_name, Identifier('dt.year'), in_mo, d, int(at_hour), int(at_min))
else:
f_call = FuncCall('__' + rule['on'], Identifier('dt.year'), in_mo, int(at_hour), int(at_min))
r_ele.conditions.append(Condition('dt', '>=', Identifier(''.join(f_call.render()))))
r_ele.assignments.append(Assignment('l', "%s" % (rule['letter'],)))
try:
off_h_m_s = re.match(r'(-)?(\d+):?(\d+)?:?(\d+)?', rule['save']).groups()
neg = bool(off_h_m_s[0])
h = int(off_h_m_s[1])
m = int(off_h_m_s[2]) if off_h_m_s[2] else 0
s = 0
#s = int(off_h_m_s[3]) if off_h_m_s[3] else 0
except AttributeError:
raise CompileError("unable to convert save: %r" % (rule['save'],))
if neg:
h = -h
m = -m
s = -s
r_ele.assignments.append(Assignment('s', FuncCall('timedelta', hours=h, minutes=m, seconds=s)))
if rule['name'] in all_rulesets:
r_set = all_rulesets[rule['name']]
else:
r_set = RuleSet(rule['name'])
r_set.rule_elements.append(r_ele)
all_rulesets[rule['name']] = r_set
return all_rulesets