refactor: reduce complexity

This commit is contained in:
NateScarlet 2019-07-16 23:40:11 +08:00
parent a384323bb5
commit f2bde6659a
No known key found for this signature in database
GPG Key ID: 5C242793B070309C

View File

@ -114,15 +114,17 @@ def get_patch_rules(lines: Iterator[str]) -> Iterator[Tuple[str, str]]:
""" """
name = None name = None
for i in lines: for i in lines:
nameMatch = re.match(r'.*\d+年(.{2,})(?:假期|放假)安排.*', i) match = re.match(r'.*\d+年(.{2,})(?:假期|放假)安排.*', i)
if nameMatch: if match:
name = nameMatch.group(1) name = match.group(1)
if name: if not name:
match = re.match(r'^[一二三四五六七八九十]、(.+)$', i) continue
if match: match = re.match(r'^[一二三四五六七八九十]、(.+)$', i)
description = match.group(1) if not match:
if re.match(r'.*\d+月\d+日.*', description): continue
yield name, description description = match.group(1)
if re.match(r'.*\d+月\d+日.*', description):
yield name, description
def _cast_int(value): def _cast_int(value):
@ -301,6 +303,28 @@ class SentenceParser:
] ]
def parse_paper(year: int, url: str) -> Iterator[dict]:
"""Parse one paper
Args:
year (int): Year
url (str): Paper url
Returns:
Iterator[dict]: Days
"""
paper = get_paper(url)
rules = get_rules(paper)
ret = ({'name': name, **i}
for name, description in rules
for i in DescriptionParser(description, year).parse())
try:
for i in ret:
yield i
except NotImplementedError as ex:
raise RuntimeError('Can not parse paper', url) from ex
def fetch_holiday(year: int): def fetch_holiday(year: int):
"""Fetch holiday data. """ """Fetch holiday data. """
@ -308,15 +332,12 @@ def fetch_holiday(year: int):
papers.reverse() papers.reverse()
days = dict() days = dict()
for i in papers:
paper = get_paper(i) for k in (j
try: for i in papers
rules = get_rules(paper) for j in parse_paper(year, i)):
for name, description in rules: days[k['date']] = k
for j in DescriptionParser(description, year).parse():
days[j['date']] = {'name': name, **j}
except NotImplementedError as ex:
raise RuntimeError('Can not extract rules', i) from ex
return { return {
'year': year, 'year': year,
'papers': papers, 'papers': papers,