50 lines
1.6 KiB
Python
50 lines
1.6 KiB
Python
from typing import List
|
|
import configparser
|
|
from pathlib import Path
|
|
import sys
|
|
|
|
miniQMTPath = r'D:\\Programs\\DTQMT\\userdata_mini' # miniQMT软件的安装路径
|
|
# miniQMTPath = ''
|
|
account_no:str = '99082560'
|
|
console_log = True
|
|
|
|
def get_config_path() -> Path:
|
|
"""获取配置文件的正确路径(兼容开发环境和打包后的可执行文件)"""
|
|
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'
|
|
|
|
def create_default_config():
|
|
"""创建默认配置文件"""
|
|
config = configparser.ConfigParser()
|
|
config['config'] = {
|
|
'miniQMTPath': r'D:/Programs/QMT/userdata_mini',
|
|
'account_no': '00000000'
|
|
}
|
|
config_path = get_config_path()
|
|
with open(config_path, 'w') as configfile:
|
|
config.write(configfile)
|
|
print(f'已创建默认配置文件: {config_path}')
|
|
|
|
def initConfig():
|
|
global miniQMTPath, grid_price, grid_volume, account_no
|
|
|
|
# 获取配置文件路径
|
|
config_path = get_config_path()
|
|
|
|
# 检查配置文件是否存在,不存在则创建
|
|
if not config_path.exists():
|
|
create_default_config()
|
|
|
|
config = configparser.ConfigParser()
|
|
config.read(config_path, encoding='utf-8')
|
|
miniQMTPath = config.get('config','miniQMTPath')
|
|
account_no = config.get('config','account_no')
|