-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransformer.py
40 lines (30 loc) · 1.35 KB
/
transformer.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
from lark import Transformer, Tree
from undate import Undate, Calendar
class HebrewUndate(Undate):
"""Undate convience subclass; sets default calendar to Hebrew."""
calendar = Calendar.HEBREW
class HebrewDateTransformer(Transformer):
"""Transform a Hebrew date parse tree and return an Undate or
UndateInterval."""
def hebrew_date(self, items):
parts = {}
for child in items:
if child.data in ["year", "month", "day"]:
# in each case we expect one integer value;
# anonymous tokens convert to their value and cast as int
value = int(child.children[0])
parts[str(child.data)] = value
# initialize and return an undate with islamic year, month, day and
# islamic calendar
return HebrewUndate(**parts)
# year translation is not needed since we want a tree with name year
# this is equivalent to a no-op
# def year(self, items):
# return Tree(data="year", children=[items[0]])
def month(self, items):
# month has a nested tree for the rule and the value
# the name of the rule (month_1, month_2, etc) gives us the
# number of the month needed for converting the date
tree = items[0]
month_n = tree.data.split("_")[-1]
return Tree(data="month", children=[month_n])