-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzonecompile.py
346 lines (303 loc) · 10.9 KB
/
zonecompile.py
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
"""
Compile zone data into zone classes.
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
# # Zone NAME GMTOFF RULES FORMAT [UNTIL]
# Zone America/Detroit -5:32:11 - LMT 1905
# -6:00 - CST 1915 May 15 2:00
# -5:00 - EST 1942
# -5:00 US E%sT 1946
# -5:00 Detroit E%sT 1973
# -5:00 US E%sT 1975
# -5:00 - EST 1975 Apr 27 2:00
# -5:00 US E%sT
# Target code should look something like this (minus comments and whitespace):
# class America_Detroit(tzinfo):
# def __from_rules(self, dt):
# offset = None
# rule = None
# save = None
# format = None
# letter = None
#
# # Here's the meat and bones of the generated content
# if dt.year <= 1905:
# offset = timedelta(hours=-5, minutes=-32, seconds=-11)
# format = "LMT"
# rule = None
# elif dt <= datetime(1915, 5, 15, 2, 0):
# offset = timedelta(hours=-6)
# format = "CST"
# rule = None
# elif dt.year <= 1946:
# offset = timedelta(hours=-5)
# format = "E%sT"
# rule = __us
# else:
# offset = timedelta(hours=-5)
# format = "E%sT"
# rule = __us
#
# # The following is pretty much constant
# if rule is not None:
# save, letter = rule()
# if offset is not None and save is not None:
# offset = offset + save
# if format is not None and letter is not None:
# format = format % letter
# return offset, save, format
#
# def utcoffset(self, dt):
# offset, _, _ = self.__from_rules(dt)
# return offset.total_seconds // 60
# def dst(self, dt):
# _, save, _ = self.__from_rules(dt)
# return 0 - (save.total_seconds // 60)
# def tzname(self, dt):
# _, _, format = self.__from_rules(dt)
# return format
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')
class CompileError(Exception):
pass
class ASO(object):
pass
class Zone(ASO):
def __init__(self, n):
self.name = n
self.code_name = name_to_identifier(n)
self.offsets = []
def render(self, level=0):
yield('\n')
yield(INDENT * level)
yield('class ')
yield(self.code_name)
yield('(tzinfo):')
level += 1
yield('\n')
yield(INDENT * level)
yield('def __from_rules(self, dt):')
level += 1
for x in ('rule','format','letter'):
yield('\n')
yield(INDENT * level)
yield(x)
yield(' = None')
for x in ('offset', 'save'):
yield('\n')
yield(INDENT * level)
yield(x)
yield(' = timedelta()')
yield('\n')
yield(INDENT * level)
yield('if dt:')
level += 1
yield('\n')
yield(INDENT * level)
yield('dt = datetime(dt.year, dt.month, dt.day, dt.hour, dt.minute)')
level -= 1
yield('\n')
yield(INDENT * level)
yield('else:')
level += 1
yield('\n')
yield(INDENT * level)
yield('dt = datetime.now()')
level -= 1
first = True
for o in self.offsets:
for x in o.render(level, first):
yield(x)
first = False
for line in ["if rule is not None:",
" save, letter = rule(dt)",
"if offset is not None and save is not None:",
" offset = offset - save",
"if format is not None and letter is not None and '%' in format:",
" format = format % letter",
"return offset, save, format"]:
yield('\n')
yield(INDENT * level)
yield(line)
level -= 1
for line in ["def utcoffset(self, dt):",
" offset, _, _ = self.__from_rules(dt)",
" return offset",
"def dst(self, dt):",
" _, save, _ = self.__from_rules(dt)",
" return save",
"def tzname(self, dt):",
" _, _, format = self.__from_rules(dt)",
" return format"]:
yield('\n')
yield(INDENT * level)
yield(line)
class Offset(ASO):
def __init__(self):
self.condition = None
self.assignments = []
def render(self, level=0, first=False):
if self.condition:
yield('\n')
yield(INDENT * level)
if first:
yield('if ')
else:
yield('elif')
for x in self.condition.render(level):
yield(x)
yield(':')
level += 1
else:
if not first:
yield('\n')
yield(INDENT * level)
yield('else:')
level += 1
for a in self.assignments:
for x in a.render(level):
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(zones):
all_zones = {}
for name, zone in zones.items():
offsets = zone['offsets']
z_obj = Zone(name)
for offset in offsets:
# Get and set up gmt offset
o_obj = Offset()
try:
gmt_off = re.match(r'( ?-?)?(\d+):?(\d+)?:?(\d+)?', offset['gmtoff']).groups()
except AttributeError:
raise CompileError("Error parsing gmtoff: %r" % (offset['gmtoff'],))
neg = gmt_off[0] == '-'
hours = int(gmt_off[1])
mins = int(gmt_off[2]) if gmt_off[2] else 0
secs = 0
#secs = int(gmt_off[3]) if gmt_off[3] else 0
if neg:
hours = -hours
mins = -mins
secs = -secs
o_obj.assignments.append(Assignment('offset', FuncCall('timedelta',
hours=hours, minutes=mins, seconds=secs)))
# Get and set up rule assignment
rule = offset['rules']
if not rule or rule.strip() == '-':
rule_name = None
elif re.match(r'\d{1,2}:?\d{0,2}:?\d{0,2}', rule):
rule_name = {'1:00': '_rule_constant_1hour',
'0:30': '_rule_constant_30min',
'0:20': '_rule_constant_20min'}[rule.strip()]
else:
rule_name = '_' + name_to_identifier(rule)
o_obj.assignments.append(Assignment('rule', Identifier(rule_name)))
# Get and set up format
fmt = offset['format']
o_obj.assignments.append(Assignment('format', fmt))
# Get and set up the condition
if offset['until']:
# TODO These times appear to be local in the file, which could
# wreak all sorts of havok with conversions. Fix this!
try:
u_parts = re.match(r'(\d{4}) ?(\w{3})? ?(\d+)? ?(\d+)?:?(\d+)?',
offset['until']).groups()
except AttributeError:
raise CompileError("Error parsing until: %r" % (offset['until'],))
u_year = int(u_parts[0])
u_mon = u_parts[1]
u_day = int(u_parts[2]) if u_parts[2] else 1
u_hour = int(u_parts[3]) if u_parts[3] else 0
u_mins = int(u_parts[4]) if u_parts[4] else 0
try:
u_mon_i = MONTHS.index(u_mon) + 1 if u_mon else 1
except ValueError:
raise CompileError("Not able to index month %r" % (rule['in'],))
comp = Condition('dt', '<', Identifier('datetime(%s,%s,%s,%s,%s)' % (u_year, u_mon_i, u_day, u_hour, u_mins)))
o_obj.condition = comp
z_obj.offsets.append(o_obj)
all_zones[name] = z_obj
return all_zones