Skip to content

Commit

Permalink
✨ Refactor FestivalLibrary.load
Browse files Browse the repository at this point in the history
  • Loading branch information
kinegratii committed Jan 27, 2024
1 parent 7cc0a84 commit 39a9a1c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
25 changes: 18 additions & 7 deletions borax/calendars/festivals2.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
'FreqConst', 'Festival', 'FestivalSchema',
'SolarFestival', 'LunarFestival', 'WeekFestival', 'TermFestival',
'encode', 'decode', 'decode_festival',
'FestivalLibrary', 'ConditionUtils'
'FestivalLibrary', 'ConditionUtils', 'FestivalDatasetNotExist'
]

MixedDate = Union[date, LunarDate]
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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()

Check warning on line 1237 in borax/calendars/festivals2.py

View check run for this annotation

Codecov / codecov/patch

borax/calendars/festivals2.py#L1237

Added line #L1237 was not covered by tests
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

Check warning on line 1244 in borax/calendars/festivals2.py

View check run for this annotation

Codecov / codecov/patch

borax/calendars/festivals2.py#L1244

Added line #L1244 was not covered by tests
if not path_o.exists():
raise FestivalDatasetNotExist(f'FestivalDataset does not exist:{identifier_or_path}')
return cls.load_file(path_o)
7 changes: 4 additions & 3 deletions tests/test_festival_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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()
Expand Down

0 comments on commit 39a9a1c

Please sign in to comment.