57 lines
1.3 KiB
Python
57 lines
1.3 KiB
Python
import json
|
|
import os
|
|
from helpers.os_environment import resolve_external_file_path
|
|
|
|
|
|
class StateManager:
|
|
filepath = None
|
|
__state = {}
|
|
|
|
def __init__(self, name, default_state=None):
|
|
self.filepath = resolve_external_file_path("/state/" + name + ".json")
|
|
if not os.path.isfile(self.filepath):
|
|
self.log("No file found for " + self.filepath)
|
|
try:
|
|
# Try creating the file.
|
|
open(self.filepath, "x")
|
|
except:
|
|
self.log("failed to create state file")
|
|
return
|
|
|
|
self.log("Saving state to " + self.filepath)
|
|
|
|
file = open(self.filepath, 'r')
|
|
|
|
file_state = file.read()
|
|
file.close()
|
|
|
|
# TODO: also check for invalid JSON state
|
|
if file_state == "":
|
|
print("file empty")
|
|
|
|
self.state = default_state
|
|
|
|
else:
|
|
self.__state = json.loads(file_state)
|
|
|
|
@property
|
|
def state(self):
|
|
return self.__state
|
|
|
|
@state.setter
|
|
def state(self, state):
|
|
self.__state = state
|
|
|
|
file = open(self.filepath, "w")
|
|
|
|
file.write(json.dumps(state, indent=2, sort_keys=True))
|
|
|
|
file.close()
|
|
|
|
def update(self, key, value):
|
|
state = self.state
|
|
state[key] = value
|
|
self.state = state
|
|
|
|
def log(self, msg):
|
|
print(msg)
|