Add check_option for Options

This commit is contained in:
shenjack 2023-11-05 15:08:12 +08:00
parent 90726e5812
commit 0c2857d741
Signed by: shenjack
GPG Key ID: 7B1134A979775551
3 changed files with 26 additions and 16 deletions

View File

@ -1,5 +1,10 @@
# Change log / 更新日志 # Change log / 更新日志
## 0.1.8
- 为 `types.Options` 添加了 `_check_option` 选项
- 为 `True` 时 会检查参数是否合法 (在类属性中已经定义了)
## 0.1.7 ## 0.1.7
- 修复了 `CompilerHelper` 中的问题 - 修复了 `CompilerHelper` 中的问题

View File

@ -6,16 +6,20 @@
import time import time
from string import Template from string import Template
from typing import List, Union, Optional, Dict, Tuple from typing import List, Union, Optional, Dict, Tuple, TYPE_CHECKING
from lib_not_dr.types.options import Options from lib_not_dr.types.options import Options
from lib_not_dr.logger.structure import LogMessage, FormattingMessage from lib_not_dr.logger.structure import LogMessage, FormattingMessage
if TYPE_CHECKING:
from lib_not_dr.logger.formatter.colors import BaseColorFormatter
class BaseFormatter(Options): class BaseFormatter(Options):
name = 'BaseFormatter' name = 'BaseFormatter'
sub_formatter: List['BaseFormatter'] = [] sub_formatter: List['BaseFormatter'] = []
color_formatters: List['BaseColorFormatter'] = []
default_template: str = '${log_time}|${logger_name}|${logger_tag}|${level}|${messages}' default_template: str = '${log_time}|${logger_name}|${logger_tag}|${level}|${messages}'
@classmethod @classmethod
@ -187,20 +191,19 @@ class StdFormatter(BaseFormatter):
enable_color: bool = True enable_color: bool = True
sub_formatter = [TimeFormatter(), sub_formatter: List[BaseFormatter] = [TimeFormatter(),
LevelFormatter(), LevelFormatter(),
TraceFormatter()] TraceFormatter()]
from lib_not_dr.logger.formatter.colors import (LevelColorFormatter, from lib_not_dr.logger.formatter.colors import (LevelColorFormatter,
LoggerColorFormatter, LoggerColorFormatter,
TimeColorFormatter, TimeColorFormatter,
TraceColorFormatter, TraceColorFormatter,
MessageColorFormatter) MessageColorFormatter)
color_formatters = [LevelColorFormatter(), color_formatters: List[BaseFormatter] = [LevelColorFormatter(),
LoggerColorFormatter(), LoggerColorFormatter(),
TimeColorFormatter(), TimeColorFormatter(),
TraceColorFormatter(), TraceColorFormatter(),
MessageColorFormatter()] MessageColorFormatter()]
def __init__(self, def __init__(self,
enable_color: bool = True, enable_color: bool = True,
@ -215,10 +218,12 @@ class StdFormatter(BaseFormatter):
:param kwargs: other options :param kwargs: other options
""" """
# 同 structures.LogMessage.__init__ 的注释 (逃) # 同 structures.LogMessage.__init__ 的注释 (逃)
super().__init__(enable_color=enable_color, self.enable_color = enable_color
sub_formatter=sub_formatter, if sub_formatter is not None:
color_formatters=color_formatters, self.sub_formatter = sub_formatter
**kwargs) if color_formatters is not None:
self.color_formatters = color_formatters
super().__init__(**kwargs)
def _format(self, message: FormattingMessage) -> FormattingMessage: def _format(self, message: FormattingMessage) -> FormattingMessage:
super()._format(message) super()._format(message)
@ -277,4 +282,3 @@ if __name__ == '__main__':
print(std_format.format_message(log_message), end='') print(std_format.format_message(log_message), end='')
print(std_format.as_markdown()) print(std_format.as_markdown())

View File

@ -64,6 +64,7 @@ class Options:
""" """
name = 'Option Base' name = 'Option Base'
cached_options: Dict[str, Union[str, Any]] = {} cached_options: Dict[str, Union[str, Any]] = {}
_check_options: bool = True
def __init__(self, **kwargs): def __init__(self, **kwargs):
""" """
@ -75,7 +76,7 @@ class Options:
self._options: Dict[str, Union[Callable, object]] = {} self._options: Dict[str, Union[Callable, object]] = {}
self.flush_option() self.flush_option()
for option, value in kwargs.items(): for option, value in kwargs.items():
if option not in self.cached_options: if option not in self.cached_options and self._check_options:
raise OptionNameNotDefined(f"option: {option} with value: {value} is not defined") raise OptionNameNotDefined(f"option: {option} with value: {value} is not defined")
setattr(self, option, value) setattr(self, option, value)
run_load_file = True run_load_file = True