-
Notifications
You must be signed in to change notification settings - Fork 10
/
festivals.py
329 lines (259 loc) · 9.88 KB
/
festivals.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
# coding=utf8
import calendar
import csv
from collections import defaultdict
from datetime import date
from pathlib import Path
from typing import Union, List, Iterator, Tuple, Optional
from .lunardate import LunarDate, LCalendars, TERMS_CN
from .store import (
Field, EncoderMixin, f_year, f_month, f_day, f_leap, f_index, f_reverse, f_schema
)
MDate = Union[date, LunarDate]
FestivalCountdownIterable = Iterator[Tuple[int, List]]
# Const values for date schema parameters
YEAR_ANY = 0
class DateSchema(EncoderMixin):
date_class = None
schema = None
def __init__(self, *args, **kwargs):
"""Any subclass MUST call this initial in its own initial.
"""
self._ignore_year = kwargs.get('ignore_year', True)
self._schema = kwargs.get('schema', None)
self.name = kwargs.get('name')
self.raw = kwargs.get('raw')
for k, v in kwargs.items():
setattr(self, k, v)
@property
def fuzzy(self):
return self.year == YEAR_ANY
def __key(self):
return list(map(int, self.encode()))
def __hash__(self):
return hash(self.__key())
def __eq__(self, other):
return isinstance(self, type(other)) and self.__key() == other.__key()
def __str__(self):
return '<{}: {}>'.format(self.__class__.__name__, self.encode())
# -------------------------- API -----------------------------------
def match(self, date_obj: MDate) -> bool:
date_obj = self._normalize(date_obj)
return (self.resolve(date_obj.year) - date_obj).days == 0
def delta(self, date_obj: Optional[MDate] = None) -> int:
if date_obj is None:
date_obj = self._get_date_class().today()
date_obj = self._normalize(date_obj)
return (self.resolve(date_obj.year) - date_obj).days
def countdown(self, date_obj: Optional[MDate] = None) -> int:
if date_obj is None:
date_obj = self.date_class.today()
return self._countdown(self._normalize(date_obj))
def resolve(self, year: int = YEAR_ANY) -> MDate:
"""Return the date object in the solar / lunar year.
:param year:
:return:
"""
year = year or self.year
if year:
return self._resolve(year)
raise ValueError('Unable resolve the date without a specified year.')
def resolve_solar(self, year: int) -> date:
"""Return the date object in a solar year.
:param year:
:return:
"""
if self.date_class == date:
return self.resolve(year)
else:
solar_date = LCalendars.cast_date(self.resolve(year), date)
offset = solar_date.year - year
if offset:
solar_date = LCalendars.cast_date(self.resolve(year - offset), date)
return solar_date
# -------------------- Internal Methods -------------------------------
def _normalize(self, date_obj):
date_class = self._get_date_class()
return LCalendars.cast_date(date_obj, date_class)
# Interfaces in the subclasses
def _get_date_class(self):
return self.date_class
def _resolve(self, year):
raise NotImplementedError('Any subclass MUST overwrite this method.')
def _countdown(self, date_obj):
date_obj = self._normalize(date_obj)
d = self.resolve(date_obj.year)
offset = (d - date_obj).days
if offset < 0:
d = self._resolve(date_obj.year + 1)
return (d - date_obj).days
class SolarSchema(DateSchema):
date_class = date
schema = 0
fields = [f_schema, f_year, f_month, f_day, f_reverse]
def __init__(self, month, day, year=YEAR_ANY, reverse=0, **kwargs):
self.year = year
self.month = month
self.day = day
self.reverse = reverse
super().__init__(**kwargs)
def _resolve(self, year):
if self.reverse == 0:
day = self.day
else:
day = calendar.monthrange(year, self.month)[1] - self.day + 1
return date(year, self.month, day)
class LunarSchema(DateSchema):
date_class = LunarDate
schema = 1
fields = [f_schema, f_year, f_month, f_day, f_leap]
def __init__(self, month, day, year=YEAR_ANY, leap=0, ignore_leap=1, **kwargs):
self.year = year
self.month = month
self.day = day
self.leap = leap
self._ignore_leap = ignore_leap
super().__init__(**kwargs)
def match(self, date_obj):
date_obj = self._normalize(date_obj)
if self._ignore_leap and date_obj.leap == 1:
date_obj = date_obj.replace(leap=0)
return super().match(date_obj)
def _resolve(self, year):
return LunarDate(year, self.month, self.day, self.leap)
class WeekSchema(DateSchema):
date_class = date
schema = 2
fields = [f_schema, f_year, f_month, f_index, Field(name='week', length=1)]
def __init__(self, month, index, week, year=YEAR_ANY, **kwargs):
self.year = year
self.month = month
self.index = index
self.week = week
super().__init__(**kwargs)
def _resolve(self, year):
day = WeekSchema.week_day(year, self.month, self.index, self.week)
return date(year, self.month, day)
@staticmethod
def week_day(year: int, month: int, index: int, week: int) -> int:
w, ndays = calendar.monthrange(year, month)
if week >= w:
d0 = week - w + 1
else:
d0 = 8 - (w - week)
d = d0 + 7 * (index - 1)
if not (1 <= d <= ndays):
raise ValueError('Invalid day for this month.')
return d
def __hash__(self):
return hash((self.year, self.month, self.index, self.week))
class DayLunarSchema(DateSchema):
date_class = LunarDate
schema = 3
fields = [f_schema, f_year, f_month, f_day, f_reverse]
def __init__(self, month, day, year=YEAR_ANY, reverse=0, **kwargs):
self.year = year
self.month = month
self.day = day
self._reverse = reverse
super().__init__(**kwargs)
def _resolve(self, year):
if self._reverse == 0:
day = self.day
else:
day = LCalendars.ndays(year, self.month) - self.day + 1
return LunarDate(year, self.month, day)
class TermSchema(DateSchema):
date_class = date
schema = 4
fields = [f_schema, f_year, Field(name=None, length=2), f_index, Field(name=None, length=1)]
def __init__(self, index, year=YEAR_ANY, **kwargs):
self.year = year
self.index = index
super().__init__(**kwargs)
def _resolve(self, year):
return LCalendars.create_solar_date(year, term_index=self.index)
class DateSchemaFactory:
schema_dict = {
0: SolarSchema,
1: LunarSchema,
2: WeekSchema,
3: DayLunarSchema,
4: TermSchema
}
@classmethod
def from_string(cls, raw, **kwargs):
lg = len(raw)
if lg == 10:
short = False
elif lg == 6:
short = True
else:
raise ValueError('Length expects 6 or 10, but {} got'.format(lg))
schema_code = int(raw[0])
schema_class = cls.schema_dict.get(schema_code)
schema = schema_class.decode(raw, short)
for k, v in kwargs.items():
setattr(schema, k, v)
return schema
@classmethod
def decode(cls, raw):
return cls.from_string(raw)
# -------------------- Festival Dataset ---------------------------------
LANG_FILES = {
'zh-Hans': 'FestivalData.txt'
}
class FestivalFactory:
"""A container including a collection of festivals group by language or file.
"""
def __init__(self, *, lang=None, file_path=None):
if lang:
file_path = Path(__file__).parent / LANG_FILES.get(lang)
if isinstance(file_path, str):
file_path = Path(file_path)
if file_path:
self._schema_list = list(read_dataset(file_path))
else:
self._schema_list = []
def add_festivals(self, festivals):
self._schema_list.extend(festivals)
def iter_festivals(self):
for festival in self._schema_list:
yield festival
def get_festival(self, name: str) -> DateSchema:
for schema in self._schema_list:
if schema.name == name:
return schema
def get_festival_names(self, date_obj: MDate) -> list:
return [schema.name for schema in self._schema_list if schema.match(date_obj)]
def iter_festival_countdown(self, countdown: Optional[int] = None,
date_obj: MDate = None) -> FestivalCountdownIterable:
festival_names = defaultdict(list)
for schema in self._schema_list:
_offset = schema.countdown(date_obj)
if countdown is None or _offset <= countdown:
festival_names[_offset].append(schema.name)
for offset in sorted(festival_names.keys()):
yield offset, festival_names[offset]
def read_dataset(file_path=None):
field_names = ['src', 'name']
with file_path.open(encoding='utf8') as f:
reader = csv.DictReader(f, fieldnames=field_names)
for row in reader:
try:
yield DateSchemaFactory.from_string(raw=row['src'], name=row['name'])
except ValueError:
continue
# ---------- Shortcuts Method ----------
def get_festival(name: str, lang: str = 'zh-Hans') -> DateSchema:
factory = FestivalFactory(lang=lang)
return factory.get_festival(name)
def get_term(name: str) -> TermSchema:
index = TERMS_CN.index(name)
return TermSchema(index)
def iter_festival_countdown(countdown: Optional[int] = None, date_obj: MDate = None,
lang: str = 'zh-Hans') -> FestivalCountdownIterable:
"""Return countdown of festivals.
"""
factory = FestivalFactory(lang=lang)
return factory.iter_festival_countdown(countdown, date_obj)