feat | 添加自动格式化文件名的功能

This commit is contained in:
shenjack 2023-12-13 09:42:25 +08:00
parent 987631c0f3
commit a4e63ef73f
Signed by: shenjack
GPG Key ID: 7B1134A979775551
2 changed files with 21 additions and 3 deletions

View File

@ -115,6 +115,7 @@ class FileCacheOutputStream(BaseOutputStream):
flush_timer: Optional[threading.Timer] = None
file_path: Path = Path("./logs")
# if contain {time} -> time.strftime("%Y-%m-%d %H-%M-%S", time.gmtime(time.time)
file_name: str
# file mode: always 'a'
file_encoding: str = "utf-8"
@ -138,9 +139,17 @@ class FileCacheOutputStream(BaseOutputStream):
file_swap_on_both: bool = False # swap file when both size and time limit reached
def init(self, **kwargs) -> bool:
# 时间取整
self.file_start_time = round(time.time())
# 初始化文件名
if "{time}" in self.file_name:
self.file_name = self.file_name.format(time=time.strftime(
"%Y-%m-%d %H-%M-%S", time.gmtime(self.file_start_time)
))
# 初始化缓存
if self.text_cache is None:
self.text_cache = io.StringIO()
# 检查文件名
self.get_file_path()
return False
@ -224,8 +233,7 @@ class FileCacheOutputStream(BaseOutputStream):
if file_time > self.file_time_limit:
time_pass = False
if (self.file_swap_on_both and size_pass and time_pass) or (
not self.file_swap_on_both and (size_pass or time_pass)
):
not self.file_swap_on_both and (size_pass or time_pass)):
# 两个都满足
# 或者只有一个满足
if size_pass and time_pass:
@ -243,6 +251,7 @@ class FileCacheOutputStream(BaseOutputStream):
if text == "":
return None
current_file = self.check_flush()
# 检查文件是否存在
if not current_file.exists():
current_file.parent.mkdir(parents=True, exist_ok=True)
current_file.touch(exist_ok=True)

View File

@ -13,4 +13,13 @@ nuitka_config_type = Dict[str, Union[str, bool, List[Union[str, tuple]]]]
raw_config_type = Dict[str, Union[nuitka_config_type, str]]
parse_config_function = Callable[[raw_config_type], nuitka_config_type]
__all__ = ["reader", "compile", "raw_config_type", "parse_config_function", "nuitka_config_type"]
# fmt: off
__all__ = [
"reader",
"compile",
"raw_config_type",
"parse_config_function",
"nuitka_config_type"
]
# fmt: on