refactor: reduce complexity
This commit is contained in:
parent
a384323bb5
commit
f2bde6659a
|
|
@ -114,12 +114,14 @@ 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:
|
|
||||||
name = nameMatch.group(1)
|
|
||||||
if name:
|
|
||||||
match = re.match(r'^[一二三四五六七八九十]、(.+)$', i)
|
|
||||||
if match:
|
if match:
|
||||||
|
name = match.group(1)
|
||||||
|
if not name:
|
||||||
|
continue
|
||||||
|
match = re.match(r'^[一二三四五六七八九十]、(.+)$', i)
|
||||||
|
if not match:
|
||||||
|
continue
|
||||||
description = match.group(1)
|
description = match.group(1)
|
||||||
if re.match(r'.*\d+月\d+日.*', description):
|
if re.match(r'.*\d+月\d+日.*', description):
|
||||||
yield name, description
|
yield name, description
|
||||||
|
|
@ -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,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user