-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculate_data.py
executable file
·146 lines (123 loc) · 3.85 KB
/
calculate_data.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
#!/usr/bin/env python3
import sys, csv
from typing import Dict, List, TypedDict
from splendidmoons.event_helpers import (CalendarEvent, CalendarAssocEvent, parse_annual_events_csv, year_moondays,
year_moondays_associated_events)
ASSOC_EVENTS: Dict[str, List[CalendarAssocEvent]] = {
"magha": [
CalendarAssocEvent(
note = "\\xMaghaPuja",
label = "magha",
day_text = "\\FullMoon",
)
],
"vesakha": [
CalendarAssocEvent(
note = "\\xVesakhaPuja",
label = "vesakha",
day_text = "\\FullMoon",
)
],
"asalha": [
CalendarAssocEvent(
note = "\\xAsalhaPuja",
label = "asalha",
day_text = "\\FullMoon"
),
CalendarAssocEvent(
note = "\\xFirstDayOfVassa",
label = "first-day",
day_text = "",
),
],
"pavarana": [
CalendarAssocEvent(
note = "\\xPavarana",
label = "pavarana",
day_text = "\\FullMoon",
),
CalendarAssocEvent(
note = "\\xLastDayOfVassa",
label = "last-day",
day_text = "\\FullMoon",
),
]
}
MOON_PHASE_DAY_TEXT: Dict[str, str] = {
"new": "\\NewMoon",
"waxing": "\\FirstQuarter",
"full": "\\FullMoon",
"waning": "\\LastQuarter",
}
class CsvEvent(TypedDict):
date: str
day_text: str
note: str
label: str
phase: str
season: str
season_number: int
season_total: int
days: int
def _to_csv_event(e: CalendarEvent) -> CsvEvent:
return CsvEvent(
date = e['date'].isoformat(),
day_text = e['day_text'],
note = e['note'],
label = e['label'],
phase = e['phase'],
season = e['season'],
season_number = e['season_number'],
season_total = e['season_total'],
days = e['days'],
)
def _remove_uposatha_info(e: CalendarEvent) -> CalendarEvent:
e['phase'] = ''
e['season'] = ''
e['season_number'] = 0
e['season_total'] = 0
e['days'] = 0
return e
def _season_texts(e: CalendarEvent) -> CalendarEvent:
s = e['season']
if s == "Vassāna":
e['season'] = "\\xVassana"
else:
e['season'] = "\\x" + s
return e
def _collect_events(from_year: int, to_year: int) -> List[CsvEvent]:
events: List[CalendarEvent] = []
year = from_year
while year <= to_year:
a = [_season_texts(x) for x in year_moondays(year, MOON_PHASE_DAY_TEXT)]
events.extend(a)
# Uposathas are already added with phase.
#
# The event filtering in Lua scripts (isUposatha()) determines uposathas
# by phase == 'full' or 'new', so remove phase from the other events
# such as major moons, first- and last day of Vassa, etc.
a = [_remove_uposatha_info(x) for x in year_moondays_associated_events(year, ASSOC_EVENTS)]
events.extend(a)
events.extend(parse_annual_events_csv(year, "./data/annual-events.csv"))
year += 1
events = sorted(events, key=lambda x: x['date'])
csv_events = [_to_csv_event(x) for x in events]
return csv_events
def events_csv(year: int, csv_path: str):
events = _collect_events(year, year)
with open(csv_path, 'w', newline='', encoding='utf-8') as f:
writer = csv.DictWriter(f,
fieldnames=events[0].keys(),
delimiter=';')
writer.writeheader()
for row in events:
writer.writerow(row)
def main():
if len(sys.argv) < 3:
print("First argument should be the year to generate csv data for. Second argument is the csv_path.")
sys.exit(2)
year = int(sys.argv[1])
csv_path = sys.argv[2]
events_csv(year, csv_path)
if __name__ == "__main__":
main()