mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-09 13:50:00 +08:00
Update data_class.py
This commit is contained in:
@@ -1,10 +1,11 @@
|
|||||||
from typing import Union, Optional
|
from typing import Union, Optional
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from ruamel.yaml import YAML
|
from ruamel.yaml import YAML
|
||||||
|
from ruamel import yaml
|
||||||
import ujson as json
|
import ujson as json
|
||||||
import copy
|
import copy
|
||||||
|
|
||||||
yaml = YAML(typ="safe")
|
_yaml = YAML(typ="safe")
|
||||||
|
|
||||||
|
|
||||||
class StaticData:
|
class StaticData:
|
||||||
@@ -26,16 +27,17 @@ class StaticData:
|
|||||||
if f.read().strip():
|
if f.read().strip():
|
||||||
raise ValueError(f"{file} 文件加载错误,请检查文件内容格式.")
|
raise ValueError(f"{file} 文件加载错误,请检查文件内容格式.")
|
||||||
elif file.name.endswith("yaml"):
|
elif file.name.endswith("yaml"):
|
||||||
self._data = yaml.load(f)
|
self._data = _yaml.load(f)
|
||||||
|
|
||||||
def set(self, key, value):
|
def set(self, key, value):
|
||||||
self._data[key] = value
|
self._data[key] = value
|
||||||
self.save()
|
self.save()
|
||||||
|
|
||||||
def set_module_data(self, module, key, value):
|
def set_module_data(self, module, key, value, auto_save: bool = True):
|
||||||
if module in self._data.keys():
|
if module in self._data.keys():
|
||||||
self._data[module][key] = value
|
self._data[module][key] = value
|
||||||
self.save()
|
if auto_save:
|
||||||
|
self.save()
|
||||||
|
|
||||||
def get(self, key):
|
def get(self, key):
|
||||||
return self._data.get(key)
|
return self._data.get(key)
|
||||||
@@ -51,19 +53,22 @@ class StaticData:
|
|||||||
return copy.deepcopy(self._data)
|
return copy.deepcopy(self._data)
|
||||||
|
|
||||||
def save(self, path: Union[str, Path] = None):
|
def save(self, path: Union[str, Path] = None):
|
||||||
path = path if path else self.file
|
path = path or self.file
|
||||||
if isinstance(path, str):
|
if isinstance(path, str):
|
||||||
path = Path(path)
|
path = Path(path)
|
||||||
if path:
|
if path:
|
||||||
with open(path, "w", encoding="utf8") as f:
|
with open(path, "w", encoding="utf8") as f:
|
||||||
json.dump(self._data, f, ensure_ascii=False, indent=4)
|
if path.name.endswith("yaml"):
|
||||||
|
yaml.dump(self._data, f, indent=2, Dumper=yaml.RoundTripDumper, allow_unicode=True)
|
||||||
|
else:
|
||||||
|
json.dump(self._data, f, ensure_ascii=False, indent=4)
|
||||||
|
|
||||||
def reload(self):
|
def reload(self):
|
||||||
if self.file.exists():
|
if self.file.exists():
|
||||||
if self.file.name.endswith("json"):
|
if self.file.name.endswith("json"):
|
||||||
self._data: dict = json.load(open(self.file, "r", encoding="utf8"))
|
self._data: dict = json.load(open(self.file, "r", encoding="utf8"))
|
||||||
elif self.file.name.endswith("yaml"):
|
elif self.file.name.endswith("yaml"):
|
||||||
self._data: dict = yaml.load(open(self.file, "r", encoding="utf8"))
|
self._data: dict = _yaml.load(open(self.file, "r", encoding="utf8"))
|
||||||
|
|
||||||
def is_exists(self):
|
def is_exists(self):
|
||||||
return self.file.exists()
|
return self.file.exists()
|
||||||
|
|||||||
Reference in New Issue
Block a user