Compare commits

...

2 Commits

Author SHA1 Message Date
716f735fe7
Update version to 0.3.12 2023-12-22 00:40:51 +08:00
e8b040ab97
Add | 添加了 Option 的 OptionNotFilled 选项 2023-12-22 00:40:17 +08:00
4 changed files with 35 additions and 2 deletions

View File

@ -6,7 +6,7 @@ A python lib came from [Difficult Rocket](https://github.com/shenjackyuanjie/Dif
## Information/信息
- Version / 版本: 0.3.11
- Version / 版本: 0.3.12
- Author / 作者: shenjackyuanjie <3695888@qq.com>
[shenjackyuanjie](https://github.com/shenjackyuanjie)

View File

@ -1,5 +1,16 @@
# lndl 0.3
## 0.3.12
- `Options`
- 又回来维护了哈
- 添加了 `_check_filled` 预定义选项
- 如果为 `True`
- 会在 `Options` 初始化时检查是否有未填写的选项
- 如果有, 则会抛出 `OptionNotFilled`
- 添加 `OptionNotFilled` 异常
- 用于在 `Options` 初始化时检查是否有未填写的选项
## 0.3.11
- 修复了 `Logger` 任意等级消息 `flush` 默认为 `True`

View File

@ -9,7 +9,7 @@ from typing import TYPE_CHECKING
if TYPE_CHECKING:
from lib_not_dr import loggers, nuitka, types, command
_version_ = "0.3.11"
_version_ = "0.3.12"
# fmt: off
__all__ = [

View File

@ -21,13 +21,16 @@ from typing import (
Iterable,
)
# fmt: off
__all__ = [
"get_type_hints_",
"Options",
"OptionsError",
"OptionNotFound",
"OptionNotFilled",
"OptionNameNotDefined",
]
# fmt: on
def get_type_hints_(cls: Type):
@ -61,6 +64,10 @@ class OptionNotFound(OptionsError):
"""某个选项没有找到"""
class OptionNotFilled(OptionsError):
"""某个选项没有填写"""
class Options:
"""
一个用于存储选项 / 提供 API 定义 的类
@ -73,11 +80,17 @@ class Options:
定义 一些需要的方法
子类: 继承 新的 Options
实现定义的方法
_check_options: 用于检查是否输入的选项都是已经定义的选项
_check_filled: 用于检查是否所有的选项都已经填写
"""
name = "Option Base"
cached_options: Dict[str, Union[str, Any]] = {}
# 用于检查是否输入的选项都是已经定义的选项
_check_options: bool = True
# 用于检查是否所有的选项都已经填写
_check_filled: bool = False
def __init__(self, **kwargs):
"""
@ -88,12 +101,21 @@ class Options:
if TYPE_CHECKING:
self._options: Dict[str, Union[Callable, object]] = {}
self.flush_option()
miss_list = []
if self._check_filled:
miss_list = [key for key in self.cached_options]
for option, value in kwargs.items():
if option not in self.cached_options and self._check_options:
raise OptionNameNotDefined(
f"option: {option} with value: {value} is not defined"
)
setattr(self, option, value)
if self._check_filled:
miss_list.remove(option)
if self._check_filled and miss_list:
raise OptionNotFilled(
f"option: {miss_list} is not filled in {self.name}"
)
run_load_file = True
if hasattr(self, "init"):
run_load_file = self.init(**kwargs) # 默认 False/None