完成 Ruff 全量清理

This commit is contained in:
yml2213
2026-08-31 10:55:44 +08:00
parent 840af3108e
commit 09ab80e062
68 changed files with 323 additions and 291 deletions
+11 -9
View File
@@ -8,7 +8,7 @@ import os
import re
import shutil
import sys
from datetime import datetime, timedelta
from datetime import UTC, datetime, timedelta
from logging.handlers import BaseRotatingHandler
from pathlib import Path
@@ -35,7 +35,9 @@ def _parse_positive_int(value: str | None, default: int) -> int:
def _parse_size(value: str | None, default: int = _DEFAULT_ROTATION_SIZE) -> int:
"""解析 50M、1GiB 等易读大小;非法值保持安全默认值。"""
matched = re.fullmatch(r"\s*(\d+)\s*([kmgt]?i?b?)?\s*", str(value or ""), re.IGNORECASE)
matched = re.fullmatch(
r"\s*(\d+)\s*([kmgt]?i?b?)?\s*", str(value or ""), re.IGNORECASE
)
if not matched:
return default
amount = int(matched.group(1))
@@ -101,7 +103,7 @@ class _SizeAndDayRotatingFileHandler(BaseRotatingHandler):
super().__init__(str(filename), "a", encoding="utf-8", delay=True)
self.max_bytes = max_bytes
self.retention_days = retention_days
self._active_day = datetime.now().date()
self._active_day = datetime.now(UTC).date()
path = Path(filename)
self._log_dir = path.parent
self._suffix = path.suffix
@@ -113,7 +115,7 @@ class _SizeAndDayRotatingFileHandler(BaseRotatingHandler):
)
def shouldRollover(self, record: logging.LogRecord) -> bool:
if datetime.now().date() != self._active_day:
if datetime.now(UTC).date() != self._active_day:
return True
if self.stream is None:
self.stream = self._open()
@@ -126,7 +128,7 @@ class _SizeAndDayRotatingFileHandler(BaseRotatingHandler):
self.stream.close()
self.stream = None
source = Path(self.baseFilename)
current_day = datetime.now().date()
current_day = datetime.now(UTC).date()
if current_day != self._active_day:
# 每日文件本身已带日期,跨日时直接切换到新文件,无需再移动旧文件。
self.baseFilename = os.fspath(self._path_for_day(current_day).resolve())
@@ -134,7 +136,7 @@ class _SizeAndDayRotatingFileHandler(BaseRotatingHandler):
self._delete_expired_archives()
return
if source.exists() and source.stat().st_size:
stamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
stamp = datetime.now(UTC).strftime("%Y-%m-%d_%H-%M-%S")
archive = source.with_name(f"{source.name}.{stamp}.{os.getpid()}.gz")
sequence = 1
while archive.exists():
@@ -148,10 +150,10 @@ class _SizeAndDayRotatingFileHandler(BaseRotatingHandler):
self._delete_expired_archives()
def _delete_expired_archives(self) -> None:
cutoff = datetime.now() - timedelta(days=self.retention_days)
cutoff = datetime.now(UTC) - timedelta(days=self.retention_days)
for archive in self._log_dir.glob(f"{self._filename_prefix}-*.log*.gz"):
try:
if datetime.fromtimestamp(archive.stat().st_mtime) < cutoff:
if datetime.fromtimestamp(archive.stat().st_mtime, UTC) < cutoff:
archive.unlink()
except OSError:
continue
@@ -226,7 +228,7 @@ def setup_logger(
if log_file:
file_path = Path(log_file).expanduser()
elif log_dir:
file_path = Path(log_dir).expanduser() / f"app-{datetime.now():%Y-%m-%d}.log"
file_path = Path(log_dir).expanduser() / f"app-{datetime.now(UTC):%Y-%m-%d}.log"
else:
return
file_path.parent.mkdir(parents=True, exist_ok=True)