修复了大量存在的问题,增加了假鸭子类型等等机制

This commit is contained in:
2026-06-25 14:49:46 +08:00
parent 19f2787db0
commit d88d11b646
827 changed files with 32617 additions and 18316 deletions

View File

@@ -1,20 +1,27 @@
# 工具函数
from __future__ import annotations
import os
import subprocess
from lib.constants.config import ERROR_MESSAGES
from lib.core.VLogger import get_logger as _vlog
def DetectFileType(FilePath):
def DetectFileType(FilePath: str) -> str:
"""检测文件类型"""
_: str
ext: str
_, ext = os.path.splitext(FilePath)
return ext.lower()
def ExecuteCommand(command, shell=True, CaptureOutput=True, text=True):
"""执行命令"""
def ExecuteCommand(command: list[str], shell: bool = False, CaptureOutput: bool = True, text: bool = True) -> subprocess.CompletedProcess[str] | None:
"""执行命令
command 应该是列表形式(如 ['gcc', 'file.c', '-o', 'file.exe']
避免字符串拼接以防止 shell 注入"""
try:
result = subprocess.run(
result: subprocess.CompletedProcess[str] = subprocess.run(
command,
shell=shell,
check=True,
@@ -23,13 +30,13 @@ def ExecuteCommand(command, shell=True, CaptureOutput=True, text=True):
)
return result
except subprocess.CalledProcessError as e:
print(f'{ERROR_MESSAGES["COMPILE_FAILED"]}: {e}')
_vlog().error(f'编译失败: {e}')
if e.stderr:
print(f'Error output: {e.stderr}')
_vlog().error(f'错误输出: {e.stderr}')
return None
def AppendFileContent(FilePath, content):
def AppendFileContent(FilePath: str, content: str) -> bool:
"""追加文件内容"""
try:
with open(FilePath, 'a', encoding='utf-8') as f:
@@ -37,5 +44,5 @@ def AppendFileContent(FilePath, content):
f.write('\n')
return True
except Exception as e:
print(f'Failed to append to file {FilePath}: {e}')
_vlog().error(f'写入文件失败 {FilePath}: {e}')
return False