class State: def __init__(self): self.state_dao = StateDAOJSON() self.state = {} self.state_marker = {} self.load_state() def load_state(self): self.state = self.state_dao.load_state() for label_name, label_object in self.state.items(): self.state_marker[label_name] = {} self.state_marker[label_name]['marked'] = False for key_name, key_object in label_object.items(): self.state_marker[label_name][key_name] = False def save_state(self): self.state_dao.save_state(self.state) def set_state(self, label, key, value): #write through self.state[label][key] = value self.save_state() def get_state(self, label, key): return self.state[label][key] def define_state(self, label, key, default_value = None): if not label in self.state.keys(): self.state[label] = {} self.state_marker[label] = {} if not key in self.state[label].keys(): self.state[label][key] = default_value self.state_marker[label]['marked'] = True #mark the label self.state_marker[label][key] = True #mark the key def prune(self): #delete any unmarked states for label_name, label_object in self.state_marker.items(): if not self.state_marker[label_name]['marked']: del self.state[label_name] else: for key_name, key_object in label_object.items(): if not self.state_marker[label_name][key_name]: del self.state[label_name][key_name] self.save_state()
def __init__(self): self.state_dao = StateDAOJSON() self.state = {} self.state_marker = {} self.load_state()
def test(): state_dao = StateDAOJSON() state = state_dao.load_state() state_dao.save_stae(state)