This commit is contained in:
2026-06-12 16:25:41 +08:00
parent ef4c1cca32
commit 2d8a0c3bca
23 changed files with 2904 additions and 525 deletions
+18 -58
View File
@@ -1,65 +1,25 @@
import configparser
from pathlib import Path
"""
运行时配置 — 端口、路径、账号由自动探测设置,无需配置文件。
"""
import os
import sys
from pathlib import Path
miniQMTPath = r'D:\\Programs\\DTQMT\\userdata_mini' # miniQMT软件的安装路径
# miniQMTPath = ''
account_no:str = '99082560'
console_log = True
log_level = "INFO"
# ---- 自动探测的配置项(默认值仅占位,启动时自动修正) ----
miniQMTPath: str = ''
account_no: str = ''
log_level: str = 'INFO'
console_log: bool = True
use_simulated_qmt: bool = False
def get_config_path() -> Path:
"""获取配置文件的正确路径(兼容开发环境和打包后的可执行文件)"""
def app_dir() -> Path:
"""应用根目录(兼容开发环境与打包后的 exe)"""
if getattr(sys, 'frozen', False):
# 打包后的可执行文件环境
# sys._MEIPASS是PyInstaller解压临时文件的目录
# 配置文件应该放在可执行文件同目录下
base_path = Path(sys.executable).parent
else:
# 开发环境
base_path = Path(__file__).resolve().parent
return base_path / 'config.ini'
return Path(sys.executable).parent
return Path(__file__).resolve().parent
def save_config(miniQmtPath:str, account_no:str, use_simulated_qmt: bool = False):
"""创建默认配置文件"""
config = configparser.ConfigParser()
config['config'] = {
'miniQMTPath': miniQmtPath,
'account_no': account_no,
'use_simulated_qmt': str(use_simulated_qmt),
'log_level': 'INFO'
}
config_path = get_config_path()
with open(config_path, 'w') as configfile:
config.write(configfile)
print(f'已创建默认配置文件: {config_path}')
def exist_config() -> bool:
"""检查配置文件是否存在"""
config_path = get_config_path()
return config_path.exists()
def initConfig() -> bool:
global miniQMTPath, account_no, log_level, use_simulated_qmt
# 获取配置文件路径
config_path = get_config_path()
config = configparser.ConfigParser()
config.read(config_path, encoding='utf-8')
miniQMTPath = config.get('config','miniQMTPath')
account_no = config.get('config','account_no')
log_level = config.get('config','log_level')
try:
use_simulated_qmt = config.get('config','use_simulated_qmt').lower() in ('true', '1', 'yes')
except (configparser.NoOptionError, configparser.NoSectionError):
use_simulated_qmt = False
# 判断miniQMTPath是否为空,并且目录是否存在
if not miniQMTPath or not Path(miniQMTPath).exists():
print('请先配置miniQMTPath')
return False
else:
return True
def log_file_path() -> Path:
"""日志文件路径"""
return app_dir() / 'sfgrid.log'