| 1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- import os
- import json
- from Kikka.KikkaConst import *
- from Kikka.Utils.Singleton import singleton
- class Storageable():
- def toDict(self):
- raise NotImplementedError
- @singleton
- class KikkaMemory():
- def __init__(self):
- self._memory = None
- def awake(self):
- if os.path.exists(MEMORY_FILE):
- with open(MEMORY_FILE, "r", encoding="utf8") as fp:
- self._memory = json.load(fp)
-
- def storage(self):
- def data2dict(obj):
- if isinstance(obj, Storageable):
- return obj.toDict()
-
- json_str = json.dumps(self._memory, default=data2dict, ensure_ascii=False, indent=4)
- with open(MEMORY_FILE, mode="w", newline="", encoding="utf8") as fp:
- fp.write(json_str)
-
- def getAction(self):
- return self._memory.get("action", {})
-
- def updateAction(self, action_data):
- self._memory["action"] = action_data
- self.storage()
-
- def getConfig(self):
- return self._memory.get("config", {})
-
- def updateConfig(self, config_data):
- self._memory["config"] = config_data
- self.storage()
|