From d84dabea44750931c0b5dd17c6f1c9391d348ec4 Mon Sep 17 00:00:00 2001 From: NateScarlet Date: Sat, 8 Feb 2020 16:52:45 +0800 Subject: [PATCH] fix: handle 2020-02 patch rule fix #50 --- fetch_holidays.py | 16 +++++++++++++--- tests/description_parsing_cases.json | 9 +++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/fetch_holidays.py b/fetch_holidays.py index e803d3b..fc71b4d 100755 --- a/fetch_holidays.py +++ b/fetch_holidays.py @@ -115,7 +115,7 @@ def get_patch_rules(lines: Iterator[str]) -> Iterator[Tuple[str, str]]: """ name = None for i in lines: - match = re.match(r'.*\d+年(.{2,})(?:假期|放假)安排.*', i) + match = re.match(r'.*\d+年([^和、]{2,})(?:假期|放假).*安排', i) if match: name = match.group(1) if not name: @@ -187,6 +187,13 @@ class DescriptionParser: class SentenceParser: """Parser for holiday shift description sentence. """ + special_cases = { + '延长2020年春节假期至2月2日(农历正月初九': [ + {"date": date(2020, 2, 1), "isOffDay": True}, + {"date": date(2020, 2, 2), "isOffDay": True}, + ], + } + def __init__(self, parent: DescriptionParser, sentence): self.parent = parent self.sentence = sentence @@ -260,7 +267,6 @@ class SentenceParser: Returns: Iterator[dict]: Days without name field. """ - for method in self.parsing_methods: for i in method(self): yield i @@ -297,10 +303,15 @@ class SentenceParser: 'isOffDay': True } + def _parse_special(self): + for i in self.special_cases.get(self.sentence, []): + yield i + parsing_methods = [ _parse_rest_1, _parse_work_1, _parse_shift_1, + _parse_special, ] @@ -330,7 +341,6 @@ def fetch_holiday(year: int): """Fetch holiday data. """ papers = get_paper_urls(year) - papers.reverse() days = dict() diff --git a/tests/description_parsing_cases.json b/tests/description_parsing_cases.json index d21881c..632acab 100644 --- a/tests/description_parsing_cases.json +++ b/tests/description_parsing_cases.json @@ -1,4 +1,13 @@ [ + { + "year": 2020, + "description": "延长2020年春节假期至2月2日(农历正月初九,星期日),2月3日(星期一)起正常上班。", + "expected": [ + { "date": "2020-02-01", "isOffDay": true }, + { "date": "2020-02-02", "isOffDay": true }, + { "date": "2020-02-03", "isOffDay": false } + ] + }, { "year": 2019, "description": "2018年12月30日至2019年1月1日放假调休,共3天。2018年12月29日(星期六)上班。",