-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.py
593 lines (479 loc) · 14.4 KB
/
parser.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
# Calendar, Graphical calendar applet with novel interface
#
# parser.py
#
# Copyright (c) 2010, Brandon Lewis <[email protected]>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
import recurrence as ast
tokens = (
'AT',
'THE',
'TIME',
'DATE',
'AND',
'COMMA',
'EACH',
'DAY',
'WEEK',
'EXCEPT',
'MONTH',
'THIS',
'NEXT',
'LAST',
'ONCE',
'TWICE',
'THRICE',
'TIMES',
'REPEATING',
'OF',
'TO',
'FOR',
'FROM',
'UNTIL',
'MONTHNAME',
'ORDINAL',
'CARDINAL',
'WEEKDAY',
'TODAY',
'TOMORROW',
'LPAREN',
'RPAREN',
'HOURS',
'MINUTES',
'BEFORE',
'AFTER'
)
t_AT = r"@|at"
t_AND = r"and"
t_COMMA = r","
t_EACH = r'every|each'
t_DAY = r'days?'
t_WEEK = r'weeks?'
t_EXCEPT = r"but|except"
t_FOR = r"for"
t_HOURS = r"hours?"
t_MINUTES = r"minutes?"
t_BEFORE = r"before"
t_AFTER = r"after"
def t_ignore_THE(t):
r"the"
pass
import re
def t_MONTH(t):
r"month"
return t
def t_TIME(t):
r"(\d{1,2})(((:\d{2})(am|pm)?)|(am|pm))"
value = t.value.strip("apm").split(":")
h = int(value[0])
if h < 12 and t.value.endswith("pm"):
h = (h + 12) % 24
if len(value) == 1:
m = 0
else:
m = int (value[1])
t.value = datetime.time(hour=h, minute=m)
return t
dateregex = re.compile(r"(\d\d?)[-./](\d\d?)[-./](\d{2,4})")
def t_DATE(t):
r"(\d\d?)[-./](\d\d?)[-./](\d{2,4})"
# assuming M/D/Y for now
m, d, y = dateregex.match(t.value).groups()
t.value = datetime.date(int(y), int(m), int(d))
return t
def t_WEEKDAY(t):
(
r"monday"
r"|tuesday"
r"|wednesday"
r"|thursday"
r"|friday"
r"|saturday"
r"|sunday"
r"|sun"
r"|mon"
r"|tue"
r"|wed"
r"|thu"
r"|fri"
r"|sat"
r"|sun"
)
t.value = day_name_to_num(t.value)
return t
def t_MONTHNAME(t):
(
r"january"
r"|february"
r"|march"
r"|april"
r"|may"
r"|june"
r"|july"
r"|august"
r"|september"
r"|october"
r"|november"
r"|december"
r"|jan"
r"|feb"
r"|mar"
r"|apr"
r"|may"
r"|jun"
r"|jul"
r"|aug"
r"|sep"
r"|sept"
r"|oct"
r"|nov"
r"|dec"
)
t.value = month_name_to_num(t.value)
return t
import string
def t_ORDINAL(t):
r"\d*(1st|2nd|3rd|\dth)"
t.value = int("".join((c for c in t.value if c in string.digits)))
return t
def t_CARDINAL(t):
r"\d+"
t.value = int(t.value)
return t
def t_THIS(t):
r"this"
return t
t_NEXT = r"next"
t_LAST = r"last"
t_OF = r"of"
t_TO = r"to"
t_FROM = r"starting|from"
t_TIMES = r"times?"
t_ONCE = r"once"
t_TWICE = r"twice"
t_THRICE = r"thrice"
t_REPEATING = r"repeating?"
t_UNTIL = r"ending|until|til"
t_TODAY = r"now|today"
t_TOMORROW = r"tomorrow"
t_LPAREN = r'\('
t_RPAREN = r'\)'
t_ignore = " \t"
class ParseError(Exception):
def __init__(self, position=None):
self.position = position
def t_error(t):
raise ParseError(t.lexpos)
import ply.lex as lex
lex.lex()
import datetime
def day_name_to_num(name):
days = {
"mon" : 0,
"tue" : 1,
"wed" : 2,
"thu" : 3,
"fri" : 4,
"sat" : 5,
"sun" : 6,
}
return days[name[:3]]
def make_date(year=None, month=None, day=None):
today = datetime.date.today()
return datetime.date(
year if year else today.year,
month if month else today.month,
day if day else today.day)
def days_from_now(weekday):
return (weekday - datetime.date.today().weekday()) % 7
def from_day_of_week(weekday, relative='this'):
offsets = {
'last' : -7,
'this' : 0,
'next' : 7
}
return datetime.date.fromordinal(
datetime.date.today().toordinal() + days_from_now(weekday) +
offsets[relative])
def month_name_to_num(name):
months = {
"jan" : 1,
"feb" : 2,
"mar" : 3,
"apr" : 4,
"may" : 5,
"jun" : 6,
"jul" : 7,
"aug" : 8,
"sep" : 9,
"oct" : 10,
"nov" : 11,
"dec" : 12,
}
return months[name[:3]]
## misc
def p_and_(t):
'''and : AND
| COMMA'''
t[0] = t[1]
def p_until(t):
'''until : UNTIL
| TO'''
t[0] = t[1]
## numbers
def p_number(t):
'''number : ORDINAL
| CARDINAL'''
t[0] = t[1]
## counts
def p_count_n_times(t):
'''count : CARDINAL TIMES'''
t[0] = t[1]
def p_count_once(t):
'''count : ONCE'''
t[0] = 2
def p_count_twice(t):
'''count : TWICE'''
t[0] = 3
def p_count_thrice(t):
'''count : THRICE'''
t[0] = 4
## durations
def p_duration_hours(t):
'''duration : CARDINAL HOURS'''
t[0] = datetime.timedelta(hours=t[1])
def p_duration_hours_minutes(t):
'''duration : CARDINAL HOURS CARDINAL MINUTES'''
t[0] = datetime.timedelta(hours=t[1], minutes=t[3])
def p_duration_minutes(t):
'''duration : CARDINAL MINUTES'''
t[0] = datetime.timedelta(minutes=t[1])
def p_duration_days(t):
'''duration : CARDINAL DAY'''
t[0] = datetime.timedelta(days=t[1])
def p_duration_duration_and_duration(t):
'''duration : duration and duration'''
t[0] = t[1] + t[3]
## weekdays
def p_weekday_unqualified(t):
'''weekday : WEEKDAY'''
t[0] = (t[1], 'this')
def p_weekday_qualified(t):
'''weekday : THIS WEEKDAY
| LAST WEEKDAY
| NEXT WEEKDAY
'''
t[0] = (t[2], t[1])
def p_weekdays_recursive(t):
'''weekdays : WEEKDAY and weekdays'''
t[0] = (t[1],) + t[3]
def p_weekdays_terminal(t):
'''weekdays : WEEKDAY'''
t[0] = (t[1],)
## date productions
def p_daate_DATE(t):
'date : DATE'
t[0] = t[1]
def p_date_today(t):
'date : TODAY'
t[0] = datetime.date.today()
def p_date_tomorrow(t):
'date : TOMORROW'
t[0] = datetime.date.today() + datetime.timedelta(days=1)
def p_date_weekday(t):
'date : weekday'
t[0] = from_day_of_week(*t[1])
def p_date_month(t):
'date : MONTHNAME'
t[0] = make_date(month=t[1],
day=1)
def p_date_ordinal(t):
'date : ORDINAL'
t[0] = make_date(day=t[1])
def p_date_month_day(t):
'''date : MONTHNAME number'''
month, day = t[1], t[2]
t[0] = make_date(month=month, day=day)
def p_date_day_month(t):
'''date : number OF MONTHNAME'''
month, day = t[3], t[1]
t[0] = make_date(month=month, day=day)
def p_dates_recursive(t):
'''dates : date and dates'''
t[0] = (t[1],) + t[3]
def p_dates_terminal(t):
'''dates : date'''
t[0] = (t[1],)
## datetimeset productions
def p_datetimeset_dates(t):
'''datetimeset : dates'''
t[0] = ast.DateSet(*t[1])
def p_datetimeset_every_day(t):
'''datetimeset : EACH DAY'''
t[0] = ast.Daily(datetime.date.today(), 1)
def p_datetimeset_every_day_from(t):
'''datetimeset : EACH DAY FROM date'''
t[0] = ast.Daily(t[4], 1)
def p_datetimeset_daily(t):
'''datetimeset : EACH CARDINAL DAY'''
t[0] = ast.Daily(datetime.date.today(), t[2])
def p_datetimeset_daily_from(t):
'''datetimeset : EACH CARDINAL DAY FROM date'''
t[0] = ast.Daily(t[5], t[2])
def p_datetimeset_weekly(t):
'''datetimeset : EACH weekdays'''
t[0] = ast.Weekly(*t[2])
def p_datetimeset_nth_weekday(t):
'''datetimeset : EACH ORDINAL weekdays'''
t[0] = ast.NthWeekday(t[2], None, *t[3])
def p_datetimeset_nth_weekday_of(t):
'''datetimeset : EACH ORDINAL weekdays OF MONTHNAME'''
t[0] = ast.NthWeekday(t[2], t[5], *t[3])
def p_datetimeset_last_weekday_of(t):
'''datetimeset : EACH LAST weekdays OF MONTHNAME'''
t[0] = ast.NthWeekday(-1, t[5], *t[3])
def p_datetimeset_last_weekday(t):
'''datetimeset : EACH LAST weekdays'''
t[0] = ast.NthWeekday(-1, None, *t[3])
def p_datetimeset_nth_to_last_wekday(t):
'''datetimeset : EACH ORDINAL TO LAST weekdays'''
t[0] = ast.NthWeekday(-t[2], None, *t[5])
def p_datetimeset_nth_to_last_weekday_of(t):
'''datetimeset : EACH ORDINAL TO LAST weekdays OF MONTHNAME'''
t[0] = ast.NthWeekday(-t[2], t[7], *t[5])
def p_datetimeset_ordinal_of_month(t):
'''datetimeset : number OF EACH MONTH'''
t[0] = ast.Monthly(None, t[1])
def p_datetimeset_ordinal_of_monthname(t):
'''datetimeset : number OF EACH MONTHNAME'''
t[0] = ast.Monthly(t[4], t[1])
def p_datetimeset_month_ordinal(t):
'''datetimeset : EACH MONTHNAME number'''
t[0] = ast.Monthly(t[2], t[3])
def p_datetimeset_except(t):
'''datetimeset : datetimeset EXCEPT datetimeset'''
t[0] = ast.Except(t[1], t[3])
def p_datetimeset_and_datetimeset(t):
'''datetimeset : datetimeset and datetimeset'''
t[0] = ast.And(t[1], t[3])
def p_datetimeset_from(t):
'''datetimeset : datetimeset FROM date'''
t[0] = ast.From(t[1], t[3])
def p_datetimeset_until(t):
'''datetimeset : datetimeset until date'''
t[0] = ast.Until(t[1], t[3])
def p_datetimeset_for_days(t):
'''datetimeset : datetimeset FOR CARDINAL DAY'''
t[0] = ast.Until(t[1], datetime.date.today() +
datetime.timedelta(days=t[3]))
def p_datetimeset_for_weeks(t):
'''datetimeset : datetimeset FOR CARDINAL WEEK'''
t[0] = ast.Until(t[1], datetime.date.today() +
datetime.timedelta(days=t[3] * 7))
def p_datetimeset_repeating(t):
'''datetimeset : datetimeset REPEATING count'''
t[0] = ast.For(t[1], t[3])
def p_datetimeset_group(t):
'''datetimeset : LPAREN datetimeset RPAREN'''
t[0] = t[2]
def p_datetimeset_before(t):
'''datetimeset : duration BEFORE datetimeset'''
t[0] = ast.Offset(t[3], -t[1])
def p_datetimeset_after(t):
'''datetimeset : duration AFTER datetimeset'''
t[0] = ast.Offset(t[3], t[1])
def p_period_time_duration(t):
'''period : TIME FOR duration
| TIME until TIME'''
t[0] = (t[1], t[3])
def p_periods_period(t):
'''periods : period'''
t[0] = (t[1],)
def p_periods_period_and_periods(t):
'''periods : period and periods'''
t[0] = (t[1],) + t[3]
def p_datetimeset_at_time_for_duration(t):
'''datetimeset : datetimeset AT periods
| datetimeset FROM periods'''
periods = [ast.Period(t[1], start, end) for start, end in t[3]]
t[0] = ast.And(*periods) if len(periods) > 1 else periods[0]
def p_error(t):
if t:
pos = t.lexpos
else:
pos = None
raise ParseError(pos)
start = 'datetimeset'
import ply.yacc as yacc
yacc.yacc()
def parse(s):
return yacc.parse(s.lower())
if __name__ == '__main__':
def test_parse(s, expected):
got = parse(s)
if not (got == expected):
print "Expected: %s\nGot: %s" % (expected, got)
test_parse("10/20/2010", ast.DateSet(datetime.date(2010, 10, 20)))
test_parse("wed", ast.DateSet(from_day_of_week(2)))
test_parse("5th", ast.DateSet(make_date(day=5)))
test_parse("oct 5th and nov 6th",
ast.DateSet(make_date(month=10, day=5),
make_date(month=11, day=6)))
test_parse("every wed", ast.Weekly(2))
test_parse("every wed, thu and fri", ast.Weekly(2, 3, 4))
test_parse("every wed except every last wed",
ast.Except(ast.Weekly(2), ast.NthWeekday(-1, None, 2)))
test_parse("(every wed) and (every 2nd thu)",
ast.And(ast.Weekly(2), ast.NthWeekday(2, None, 3)))
test_parse("wednesday at 5PM for 1 hour",
ast.Period(ast.DateSet(from_day_of_week(2)), datetime.time(hour=17),
datetime.timedelta(hours=1)))
test_parse("the 25 of each month", ast.Monthly(None, 25))
test_parse("25th of each october from 12:45PM to 4:45PM",
ast.Period(ast.Monthly(10, 25), datetime.time(hour=12, minute=45),
datetime.time(hour=16, minute=45)))
test_parse("25th of each october from 12PM to 4PM and 8PM for 1 hour",
ast.And(ast.Period(ast.Monthly(10, 25),
datetime.time(hour=12),
datetime.time(hour=16)),
ast.Period(ast.Monthly(10, 25),
datetime.time(hour=20),
datetime.timedelta(hours=1))))
test_parse("25th of each october from 12PM to 4PM at 8PM for 1 hour",
ast.Period(ast.Period(ast.Monthly(10, 25),
datetime.time(hour=12),
datetime.time(hour=16)),
datetime.time(hour=20),
datetime.timedelta(hours=1)))
test_parse("every day from today until october",
ast.Until(ast.Daily(datetime.date.today(), 1),
make_date(month=10, day=1)))
test_parse("every wed from today until january 25",
ast.Until(ast.From(ast.Weekly(2), datetime.date.today()),
make_date(month=1, day=25)))
test_parse("every 2 days repeating twice",
ast.For(ast.Daily(datetime.date.today(), 2), 3))
test_parse("every 2 days for 3 weeks",
ast.Until(ast.Daily(datetime.date.today(), 2),
datetime.date.today() + datetime.timedelta(days=21)))
test_parse("1 day before 21st of each month",
ast.Offset(ast.Monthly(None, 21), datetime.timedelta(days=-1)))
test_parse("1 day after 21st of each month",
ast.Offset(ast.Monthly(None, 21), datetime.timedelta(days=1)))
test_parse("1 day, 1 hour and 1 minute before every 2nd wednesday",
ast.Offset(ast.NthWeekday(2, None, 2),
-datetime.timedelta(days=1, hours=1, minutes=1)))