Rename items to keys in state file

This commit is contained in:
lash 2022-02-01 06:29:51 +00:00
parent 3de0ad8ee3
commit aff0da4d84
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746

View File

@ -16,8 +16,9 @@ class State:
self.NEW = 0
self.__reverse = {0: self.NEW}
self.__items = {self.NEW: []}
self.__items_reverse = {}
self.__keys = {self.NEW: []}
self.__keys_reverse = {}
self.__contents = {}
def __is_pure(self, v):
@ -72,16 +73,16 @@ class State:
self.__c += 1
def __check_item(self, item):
if self.__items_reverse.get(item) != None:
def __check_key(self, item):
if self.__keys_reverse.get(item) != None:
raise StateItemExists(item)
def __add_state_list(self, state, item):
if self.__items.get(state) == None:
self.__items[state] = []
self.__items[state].append(item)
self.__items_reverse[item] = state
if self.__keys.get(state) == None:
self.__keys[state] = []
self.__keys[state].append(item)
self.__keys_reverse[item] = state
def __state_list_index(self, item, state_list):
@ -152,53 +153,54 @@ class State:
return (alias, r,)
def put(self, item, state=None):
def put(self, key, state=None, contents=None):
if state == None:
state = self.NEW
elif self.__reverse.get(state) == None:
raise StateInvalid(state)
self.__check_item(item)
self.__add_state_list(state, item)
self.__check_key(key)
self.__add_state_list(state, key)
self.__contents[key] = contents
def move(self, item, to_state):
current_state = self.__items_reverse.get(item)
def move(self, key, to_state):
current_state = self.__keys_reverse.get(key)
if current_state == None:
raise StateItemNotFound(item)
raise StateItemNotFound(key)
new_state = self.__reverse.get(to_state)
if new_state == None:
raise StateInvalid(to_state)
current_state_list = self.__items.get(current_state)
current_state_list = self.__keys.get(current_state)
if current_state_list == None:
raise StateCorruptionError(current_state)
idx = self.__state_list_index(item, current_state_list)
idx = self.__state_list_index(key, current_state_list)
new_state_list = self.__items.get(to_state)
new_state_list = self.__keys.get(to_state)
if current_state_list == None:
raise StateCorruptionError(to_state)
self.__add_state_list(to_state, item)
self.__add_state_list(to_state, key)
current_state_list.pop(idx)
def purge(self, item):
current_state = self.__items_reverse.get(item)
def purge(self, key):
current_state = self.__keys_reverse.get(key)
if current_state == None:
raise StateItemNotFound(item)
del self.__items_reverse[item]
raise StateItemNotFound(key)
del self.__keys_reverse[key]
current_state_list = self.__items.get(current_state)
current_state_list = self.__keys.get(current_state)
idx = self.__state_list_index(item, current_state_list)
idx = self.__state_list_index(key, current_state_list)
current_state_list.pop(idx)
def state(self, item):
state = self.__items_reverse.get(item)
def state(self, key):
state = self.__keys_reverse.get(key)
if state == None:
raise StateItemNotFound(item)
raise StateItemNotFound(key)
return state