Actually working persistence by overloading a dict
This commit is contained in:
parent
fbba5010a3
commit
5b5bb4b30c
@ -65,9 +65,6 @@ class BotFramework():
|
|||||||
def react_chats(self):
|
def react_chats(self):
|
||||||
"""Checks unanswered messages and answers them"""
|
"""Checks unanswered messages and answers them"""
|
||||||
|
|
||||||
# HACKY: TODO remove
|
|
||||||
self.persistence.sync()
|
|
||||||
# writes persistent variables to file so that they ACTUALLY persist
|
|
||||||
num = self.telegram.fetch_updates()
|
num = self.telegram.fetch_updates()
|
||||||
for i in range(num):
|
for i in range(num):
|
||||||
self.react_command()
|
self.react_command()
|
||||||
|
@ -4,17 +4,16 @@ import clock.main
|
|||||||
import dashboard.main
|
import dashboard.main
|
||||||
# wrapper
|
# wrapper
|
||||||
import wrapper
|
import wrapper
|
||||||
|
import persistence.main
|
||||||
# misc.
|
# misc.
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
import shelve
|
|
||||||
|
|
||||||
class Launcher():
|
class Launcher():
|
||||||
"""Launches all other submodules"""
|
"""Launches all other submodules"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
""""""
|
""""""
|
||||||
self.persistence = shelve.DbfilenameShelf("persistence/prst.db", writeback = True)
|
self.persistence = persistence.main.PersistentDict("persistence/prst.json")
|
||||||
if len(self.persistence) == 0:
|
if len(self.persistence) == 0:
|
||||||
self.init_persistence()
|
self.init_persistence()
|
||||||
self.persistence["global"]["reboots"] += 1
|
self.persistence["global"]["reboots"] += 1
|
||||||
|
68
persistence/main.py
Normal file
68
persistence/main.py
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
import json
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class PersistentDict(dict):
|
||||||
|
""""""
|
||||||
|
def __init__(self, file_name, *args, **kwargs):
|
||||||
|
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
self.path = file_name
|
||||||
|
self.last_action = ""
|
||||||
|
if not os.path.exists(self.path):
|
||||||
|
with open(self.path, "a") as f:
|
||||||
|
f.write("{}")
|
||||||
|
self.read_dict()
|
||||||
|
|
||||||
|
|
||||||
|
def write_dict(self):
|
||||||
|
with open(self.path, "w") as f:
|
||||||
|
json.dump(self, f)
|
||||||
|
self.last_action = "w"
|
||||||
|
|
||||||
|
def read_dict(self):
|
||||||
|
with open(self.path) as f:
|
||||||
|
tmp = dict(json.load(f))
|
||||||
|
for key in tmp:
|
||||||
|
super().__setitem__(key, tmp[key])
|
||||||
|
self.last_action = "r"
|
||||||
|
|
||||||
|
|
||||||
|
def __setitem__(self, key, value):
|
||||||
|
if self.last_action != "r":
|
||||||
|
self.read_dict()
|
||||||
|
|
||||||
|
super().__setitem__(key,value)
|
||||||
|
self.write_dict() # writes 'self' to a json file.
|
||||||
|
|
||||||
|
def __getitem__(self, key):
|
||||||
|
if self.last_action != "r":
|
||||||
|
self.read_dict()
|
||||||
|
|
||||||
|
ret_val = super().__getitem__(key)
|
||||||
|
if type(ret_val) == dict:
|
||||||
|
ret_val = HookedDict(key, self, ret_val)
|
||||||
|
|
||||||
|
return ret_val
|
||||||
|
|
||||||
|
def clear(self):
|
||||||
|
super().clear()
|
||||||
|
self.write_dict()
|
||||||
|
return {}
|
||||||
|
|
||||||
|
class HookedDict(dict):
|
||||||
|
def __init__(self, own_name, parent_dict, *args, **kwargs):
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
self.name = own_name
|
||||||
|
self.parent = parent_dict
|
||||||
|
|
||||||
|
def __setitem__(self, key, value):
|
||||||
|
super().__setitem__(key, value)
|
||||||
|
self.parent.__setitem__(self.name, self)
|
||||||
|
|
||||||
|
def __getitem__(self, key):
|
||||||
|
ret_val = super().__getitem__(key)
|
||||||
|
if type(ret_val) == dict:
|
||||||
|
ret_val = HookedDict(key, self, ret_val)
|
||||||
|
return ret_val
|
@ -19,7 +19,6 @@ class Wrapper():
|
|||||||
while True:
|
while True:
|
||||||
action()
|
action()
|
||||||
time.sleep(sleep_delta)
|
time.sleep(sleep_delta)
|
||||||
self.own.persistence.sync()
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user