feat: 增加 ZMQ 调试服务器并完善设置功能
This commit is contained in:
@@ -9,17 +9,38 @@ from heurams.services.logger import get_logger
|
||||
from heurams.services.exceptions import WTFException
|
||||
|
||||
# 我们的流程是: 找到文件名: 返回文件名里头的数据; 找不到: 继续查索引; 所以 self.data 除了存本级各种索引球用没得
|
||||
# 递归就是这么吊
|
||||
logger = get_logger(__name__)
|
||||
|
||||
class ConfigDict(UserDict): # 舒服了
|
||||
_instances = {} # 必须使用单例模式, 不然有严重的多实例导致的配置无法持久化问题
|
||||
|
||||
def __new__(cls, config_path: pathlib.Path, dict=None):
|
||||
if dict:
|
||||
raise WTFException("不要放默认值...")
|
||||
|
||||
# 规范化路径, 免得单例存在"别名"
|
||||
path_key = config_path.resolve()
|
||||
|
||||
if path_key in cls._instances:
|
||||
return cls._instances[path_key]
|
||||
|
||||
instance = super().__new__(cls)
|
||||
cls._instances[path_key] = instance
|
||||
return instance
|
||||
|
||||
def __init__(self, config_path: pathlib.Path, dict = None): # 需要自己把自己提起来
|
||||
# 避免重复初始化
|
||||
if hasattr(self, '_initialized'):
|
||||
return
|
||||
self._initialized = True
|
||||
if dict:
|
||||
raise WTFException("不要放默认值...")
|
||||
super().__init__(dict)
|
||||
self.logger = get_logger(__name__)
|
||||
logger = get_logger(__name__)
|
||||
self.path = config_path
|
||||
self.is_dir = self.path.is_dir()
|
||||
if self.is_dir:
|
||||
self.update_index() # 狗儿要唱狗儿歌
|
||||
self.update_index()
|
||||
else:
|
||||
with open(self.path, 'r+') as f: #TODO: 给这个做缓存
|
||||
try:
|
||||
@@ -35,11 +56,6 @@ class ConfigDict(UserDict): # 舒服了
|
||||
return ConfigDict(value)
|
||||
return value
|
||||
|
||||
def converter(self, s):
|
||||
if (self.path / s).exists:
|
||||
return self.path / s
|
||||
return self.path / s+'.toml'
|
||||
|
||||
def __contains__(self, key):
|
||||
return super().__contains__(key)
|
||||
|
||||
@@ -67,22 +83,16 @@ class ConfigDict(UserDict): # 舒服了
|
||||
if i.suffix == '.toml':
|
||||
self.data[i.stem] = i
|
||||
else:
|
||||
self.logger.log(f"配置目录中有无效的文件 {i.stem}") # what's up bro
|
||||
logger.debug(f"配置目录中有无效的文件 {i.stem}") # what's up bro
|
||||
|
||||
def persist(self):
|
||||
if self.is_dir:
|
||||
raise WTFException("不准这样浪费性能...")
|
||||
for i in self.data.keys():
|
||||
j = self[i]
|
||||
if isinstance(j, ConfigDict):
|
||||
j.persist()
|
||||
logger.debug("完成配置持久化")
|
||||
return
|
||||
|
||||
with open(self.path, 'w+') as f:
|
||||
toml.dump(self.data, f)
|
||||
|
||||
def __del__(self):
|
||||
if not self.is_dir:
|
||||
self.persist() # 不准循环引用, 懂了吧
|
||||
|
||||
@staticmethod
|
||||
def titleize(objt):
|
||||
if isinstance(objt, pathlib.Path):
|
||||
return objt.stem
|
||||
else:
|
||||
return objt
|
||||
toml.dump(self.data, f)
|
||||
Reference in New Issue
Block a user