diff --git a/borax/calendars/festivals2.py b/borax/calendars/festivals2.py index e84de7f..11aab60 100644 --- a/borax/calendars/festivals2.py +++ b/borax/calendars/festivals2.py @@ -18,7 +18,7 @@ 'FreqConst', 'Festival', 'FestivalSchema', 'SolarFestival', 'LunarFestival', 'WeekFestival', 'TermFestival', 'encode', 'decode', 'decode_festival', - 'FestivalLibrary', 'ConditionUtils' + 'FestivalLibrary', 'ConditionUtils', 'FestivalDatasetNotExist' ] MixedDate = Union[date, LunarDate] @@ -970,6 +970,10 @@ def description_contains(festival: Festival, description_contains: str): return description_contains in festival.description +class FestivalDatasetNotExist(Exception): + pass + + class FestivalLibrary(collections.UserList): """A festival collection. @@ -1226,11 +1230,18 @@ def load_builtin(cls, identifier: str = 'basic') -> 'FestivalLibrary': @classmethod def load(cls, identifier_or_path: Union[str, Path]) -> 'FestivalLibrary': - """Create a FestivalLibrary object from borax builtin dataset or custom file.""" + """Create a FestivalLibrary object from borax builtin dataset or custom file. + If dataset does not exist,a FestivalDatasetNotExist is raised. + """ if identifier_or_path == 'empty': return FestivalLibrary() - try: - path_o = get_festival_dataset_path(identifier_or_path) - return cls.load_file(path_o) - except ValueError: - return cls.load_file(identifier_or_path) + if isinstance(identifier_or_path, str): + try: + path_o = get_festival_dataset_path(identifier_or_path) + except ValueError: + path_o = Path(identifier_or_path) + else: + path_o = identifier_or_path + if not path_o.exists(): + raise FestivalDatasetNotExist(f'FestivalDataset does not exist:{identifier_or_path}') + return cls.load_file(path_o) diff --git a/tests/test_festival_library.py b/tests/test_festival_library.py index 97f11a2..095ee70 100644 --- a/tests/test_festival_library.py +++ b/tests/test_festival_library.py @@ -3,7 +3,8 @@ from io import StringIO from unittest.mock import MagicMock, patch -from borax.calendars.festivals2 import LunarFestival, TermFestival, FestivalLibrary, FestivalSchema +from borax.calendars.festivals2 import (LunarFestival, TermFestival, FestivalLibrary, FestivalSchema, + FestivalDatasetNotExist) class FestivalLibraryTestCase(unittest.TestCase): @@ -26,8 +27,8 @@ def test_library(self): def test_new_load(self): fl = FestivalLibrary.load('basic') self.assertEqual(33, len(fl)) - with self.assertRaises(FileNotFoundError): - fl2 = FestivalLibrary.load('not-found') + with self.assertRaises(FestivalDatasetNotExist): + FestivalLibrary.load('not-found') def test_list_days(self): fl = FestivalLibrary.load_builtin()