update
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
# Global configuration variables
|
||||
# Define these BEFORE imports to avoid circular dependency issues with logger
|
||||
console_log = True
|
||||
miniQMTPath = None
|
||||
miniQMTAccount = None
|
||||
log_level = "1"
|
||||
|
||||
from pathlib import Path
|
||||
from core.config.config_model import ConfigModel, CfgKeyLogLevel, CfgKeyMiniQmtPath, CfgKeyMiniQmtAccount, CfgKeyConsoleLog
|
||||
from core.database import db
|
||||
|
||||
def initConfig() -> bool:
|
||||
"""Initialize configuration from database"""
|
||||
global miniQMTPath, miniQMTAccount, log_level, console_log
|
||||
|
||||
# Ensure connection and tables
|
||||
db.connect(reuse_if_open=True)
|
||||
if not db.table_exists(ConfigModel._meta.table_name):
|
||||
db.create_tables([ConfigModel])
|
||||
|
||||
# Check and initialize keys
|
||||
_init_key(CfgKeyLogLevel, "1")
|
||||
_init_key(CfgKeyConsoleLog, "True")
|
||||
_init_key(CfgKeyMiniQmtPath, None)
|
||||
_init_key(CfgKeyMiniQmtAccount, None)
|
||||
|
||||
# Load values
|
||||
try:
|
||||
miniQMTPath = _get_value(CfgKeyMiniQmtPath)
|
||||
miniQMTAccount = _get_value(CfgKeyMiniQmtAccount)
|
||||
log_level = _get_value(CfgKeyLogLevel) or "1"
|
||||
console_log = _get_value(CfgKeyConsoleLog) or "True"
|
||||
console_log = console_log.lower() == "true"
|
||||
|
||||
# console_log is not in DB currently, keeping default True or could add to DB
|
||||
except Exception as e:
|
||||
print(f"Error loading config: {e}")
|
||||
return False
|
||||
|
||||
# Validate path
|
||||
if not miniQMTPath or not Path(miniQMTPath).exists():
|
||||
print('请先配置miniQMTPath')
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def _init_key(key: str, default_value: str | None):
|
||||
"""Helper to initialize a key if it doesn't exist"""
|
||||
try:
|
||||
ConfigModel.get(ConfigModel.key == key)
|
||||
except ConfigModel.DoesNotExist:
|
||||
ConfigModel.create(key=key, value=default_value)
|
||||
|
||||
def _get_value(key: str) -> str | None:
|
||||
"""Helper to get value safely"""
|
||||
try:
|
||||
return ConfigModel.get(ConfigModel.key == key).value
|
||||
except ConfigModel.DoesNotExist:
|
||||
return None
|
||||
|
||||
def save_config(key: str, value: str):
|
||||
"""Save configuration to database"""
|
||||
_update_key(key, value)
|
||||
print(f'配置已更新: {key}={value}')
|
||||
|
||||
def _update_key(key: str, value: str):
|
||||
try:
|
||||
record = ConfigModel.get(ConfigModel.key == key)
|
||||
record.value = value
|
||||
record.save()
|
||||
except ConfigModel.DoesNotExist:
|
||||
ConfigModel.create(key=key, value=value)
|
||||
|
||||
def exist_config() -> bool:
|
||||
"""Check if essential config exists"""
|
||||
path = _get_value(CfgKeyMiniQmtPath)
|
||||
account = _get_value(CfgKeyMiniQmtAccount)
|
||||
return bool(path and account)
|
||||
|
||||
def getLogLevel() -> str:
|
||||
"""获取配置中的日志级别"""
|
||||
return log_level
|
||||
|
||||
def getConsoleLog() -> bool:
|
||||
"""获取配置中的控制台日志设置"""
|
||||
return console_log
|
||||
|
||||
def getMiniQMTPath() -> str | None:
|
||||
"""获取配置中的miniQMT路径"""
|
||||
return miniQMTPath
|
||||
|
||||
def getMiniQMTAccount() -> str | None:
|
||||
"""获取配置的miniQMT账号"""
|
||||
return miniQMTAccount
|
||||
@@ -0,0 +1,11 @@
|
||||
from peewee import CharField
|
||||
from core.database import BaseModel, db
|
||||
|
||||
CfgKeyLogLevel = "log_level"
|
||||
CfgKeyConsoleLog = "console_log"
|
||||
CfgKeyMiniQmtPath = "miniQMTPath"
|
||||
CfgKeyMiniQmtAccount = "miniQMTAccount"
|
||||
|
||||
class ConfigModel(BaseModel):
|
||||
key = CharField(unique=True)
|
||||
value = CharField(null=True)
|
||||
Reference in New Issue
Block a user