nas-tools/app/conf/systemconfig.py
2023-02-14 22:15:43 +08:00

57 lines
1.4 KiB
Python

import json
from app.helper import DictHelper
from app.utils.commons import singleton
@singleton
class SystemConfig:
# 系统设置
systemconfig = {}
def __init__(self):
self.dicthelper = DictHelper()
self.init_config()
def init_config(self):
"""
缓存系统设置
"""
for item in self.dicthelper.list("SystemConfig"):
if not item:
continue
if self.__is_obj(item.VALUE):
self.systemconfig[item.KEY] = json.loads(item.VALUE)
else:
self.systemconfig[item.KEY] = item.VALUE
@staticmethod
def __is_obj(obj):
if isinstance(obj, list) or isinstance(obj, dict):
return True
else:
return str(obj).startswith("{") or str(obj).startswith("[")
def set_system_config(self, key, value):
"""
设置系统设置
"""
# 更新内存
self.systemconfig[key] = value
# 写入数据库
if self.__is_obj(value):
if value:
value = json.dumps(value)
else:
value = ''
self.dicthelper.set("SystemConfig", key, value)
def get_system_config(self, key=None):
"""
获取系统设置
"""
if not key:
return self.systemconfig
return self.systemconfig.get(key)