diff --git a/borax/calendars/dataset/__init__.py b/borax/calendars/dataset/__init__.py index 6db80ab..5226672 100644 --- a/borax/calendars/dataset/__init__.py +++ b/borax/calendars/dataset/__init__.py @@ -11,4 +11,6 @@ def get_festival_dataset_path(identifier: str) -> Path: """Return the full path for festival dataset csv file.""" + if identifier not in _FILE_DICT: + raise ValueError(f'Festival Dataset {identifier} not found!') return Path(__file__).parent / _FILE_DICT.get(identifier) diff --git a/borax/calendars/festivals2.py b/borax/calendars/festivals2.py index f0d8f3e..ce4fa17 100644 --- a/borax/calendars/festivals2.py +++ b/borax/calendars/festivals2.py @@ -1222,3 +1222,14 @@ def load_builtin(cls, identifier: str = 'basic') -> 'FestivalLibrary': if identifier == 'empty': return FestivalLibrary() return cls.load_file(get_festival_dataset_path(identifier)) + + @classmethod + def load(cls, identifier_or_path: Union[str, Path]) -> 'FestivalLibrary': + """Create a FestivalLibrary object from borax builtin dataset or custom file.""" + 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) diff --git a/docs/changelog.md b/docs/changelog.md index 729631f..e7a7276 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -9,7 +9,8 @@ - 功能更新 - 新增 [Borax日历应用](/guides/borax_calendar_app) - 包 `borax.apps` 变更为 `borax.capp` - - 新增方法 `FestivalLibrary.extend_term_festivals` + - 新增方法 `FestivalLibrary.extend_term_festivals` + - 新增方法 `FestivalLibrary.load` - 新增 `borax.ui.widgets.MessageLabel` 类 - `Festival` 类新增 `code` 属性 - 项目构建 diff --git a/docs/guides/festivals2.md b/docs/guides/festivals2.md index a8d7cd7..0d5aa2c 100644 --- a/docs/guides/festivals2.md +++ b/docs/guides/festivals2.md @@ -488,6 +488,24 @@ FestivalLibrary.delete_by_indexes(indexes:List[int]) 按照位置删除多个元素。 +### load + +> Add in 4.1.0 + +```python +FestivalLibrary.load(cls, identifier_or_path: Union[str, Path]) -> 'FestivalLibrary' +``` + +加载Borax内部数据或自定义文件。 + +```python +fl = FestivalLibrary.load('basic') + +fl2 = FestivalLibrary.load('/usr/my/my_festivals.csv') +``` + + + ### load_file ```python diff --git a/tests/test_festival_library.py b/tests/test_festival_library.py index fceaa49..97f11a2 100644 --- a/tests/test_festival_library.py +++ b/tests/test_festival_library.py @@ -23,6 +23,12 @@ def test_library(self): self.assertIn('元旦', [g.name for g in gd_days]) + def test_new_load(self): + fl = FestivalLibrary.load('basic') + self.assertEqual(33, len(fl)) + with self.assertRaises(FileNotFoundError): + fl2 = FestivalLibrary.load('not-found') + def test_list_days(self): fl = FestivalLibrary.load_builtin() fes_list = []