even better
This commit is contained in:
@@ -1 +0,0 @@
|
||||
{"messages_read": 66, "messages_sent": 41, "commands_executed": 31, "photos_sent": 6, "log": [], "chat_members": {"364520272": "Remy Moll"}, "reboots": 57}
|
1
persistence/persistent_init.json
Normal file
1
persistence/persistent_init.json
Normal file
@@ -0,0 +1 @@
|
||||
{"messages_read": 0, "messages_sent": 0, "commands_executed": 0, "photos_sent": 0, "log": [], "chat_members": {}, "reboots": 0}
|
@@ -1,21 +1,19 @@
|
||||
import json
|
||||
import time
|
||||
|
||||
import os
|
||||
class Variables():
|
||||
""""""
|
||||
|
||||
def __init__(self,savefile_path):
|
||||
def __init__(self,savefile_path, init_path):
|
||||
self.path = savefile_path
|
||||
self.init_path = init_path
|
||||
self.last_action = ""
|
||||
# last performed action, if only reads are made, then the underlying var has not been changed
|
||||
# and doesn't need to be read again
|
||||
self.savefile = {}
|
||||
|
||||
def write(self, name, value):
|
||||
if self.last_action == "read":
|
||||
pre = self.savefile
|
||||
else:
|
||||
pre = self.read()
|
||||
pre = self.read()
|
||||
pre[name] = value
|
||||
try:
|
||||
file = open(self.path,"w")
|
||||
@@ -25,35 +23,33 @@ class Variables():
|
||||
except:
|
||||
print("Config not written - critical")
|
||||
|
||||
|
||||
def read(self, name=""):
|
||||
if self.last_action == "read":
|
||||
vars = self.savefile
|
||||
else:
|
||||
try:
|
||||
file = open(self.path,"r")
|
||||
vars = json.load(file)
|
||||
file.close()
|
||||
self.savefile = vars
|
||||
self.last_action = "read"
|
||||
except:
|
||||
return None
|
||||
|
||||
if len(name) != 0:
|
||||
vars = vars[name]
|
||||
|
||||
return vars
|
||||
|
||||
def increment(self, name=""):
|
||||
if self.last_action == "read":
|
||||
pre = self.savefile
|
||||
else:
|
||||
pre = self.read()
|
||||
if name == "":
|
||||
return self.savefile
|
||||
else:
|
||||
return self.savefile[name]
|
||||
|
||||
try:
|
||||
pre[name] += 1
|
||||
file = open(self.path,"w")
|
||||
json.dump(pre, file)
|
||||
if os.path.exists(self.path):
|
||||
file_path = self.path
|
||||
else:
|
||||
file_path = self.init_path
|
||||
file = open(file_path,"r")
|
||||
vars = json.load(file)
|
||||
file.close()
|
||||
self.last_action = "write"
|
||||
self.savefile = vars
|
||||
self.last_action = "read"
|
||||
except:
|
||||
print("Config not written - critical")
|
||||
return None
|
||||
|
||||
if name != "":
|
||||
vars = vars[name]
|
||||
return vars
|
||||
|
||||
|
||||
def increment(self, name=""):
|
||||
pre = self.read(name)
|
||||
plus1 = pre + 1
|
||||
self.write(name, plus1)
|
||||
|
Reference in New Issue
Block a user