launcher/launcher/sys_config/__init__.py
2025-01-25 17:49:17 +08:00

58 lines
1.5 KiB
Python

import json
import os
import sys
class SysConfig:
"""
读取系统配置文件
"""
# 版本
version: str
# ui路径
url: str
# 日志输出级别
log_level: str
# 是否开启ui调试
debug: bool
def __init__(self):
self.config = {}
self.config_path = self.get_config_path()
self.load_config()
def get_config_path(self):
if getattr(sys, "frozen", False):
# logger.error("打包配置")
# 如果是打包后的可执行文件
base_path = sys._MEIPASS
config_subdir = "launcher/sys_config" # 添加子目录
config_filename = "config_dist.json"
else:
# logger.error("本地配置")
# 如果是本地开发环境
base_path = os.path.dirname(__file__)
config_subdir = "" # 本地开发环境不需要子目录
config_filename = "config_local.json"
config_path = os.path.join(base_path, config_subdir, config_filename)
return config_path
def load_config(self):
try:
with open(self.config_path, "r", encoding="utf-8") as file:
self.config = json.load(file)
except FileNotFoundError:
pass
# logger.error(f"配置文件未找到: {self.config_path}")
except Exception:
pass
# logger.error(f"加载配置文件时出错: {repr(e)}")
def get(self, key):
return self.config.get(key)
# 创建一个全局配置实例
sys_config = SysConfig()