WIP selective state sync

This commit is contained in:
lash 2022-05-02 11:21:07 +00:00
parent 53da59c06e
commit 714bf79d22
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
3 changed files with 61 additions and 7 deletions

View File

@ -144,7 +144,7 @@ class PersistedState(State):
return to_state return to_state
def sync(self, state=None): def sync(self, state=None, not_state=None):
"""Reload resources for a single state in memory from the persisted state store. """Reload resources for a single state in memory from the persisted state store.
:param state: State to load :param state: State to load
@ -153,11 +153,17 @@ class PersistedState(State):
# :todo: if sync state is none, sync all # :todo: if sync state is none, sync all
""" """
states = [] states_numeric = []
if state == None: if state == None:
states = list(self.all()) states_numeric = list(self.all(numeric=True))
else: else:
states = [self.name(state)] #states = [self.name(state)]
states_numeric = [state]
states = []
for state in states_numeric:
if not_state != None and state & not_state == 0:
states.append(self.name(state))
ks = [] ks = []
for k in states: for k in states:

View File

@ -217,14 +217,15 @@ class State:
return self.__alias(k, *args) return self.__alias(k, *args)
def all(self, pure=False): def all(self, pure=False, numeric=False):
"""Return list of all unique atomic and alias states. """Return list of all unique atomic and alias state strings.
:rtype: list of ints :rtype: list of ints
:return: states :return: states
""" """
l = [] l = []
for k in dir(self): for k in dir(self):
state = None
if k[0] == '_': if k[0] == '_':
continue continue
if k.upper() != k: if k.upper() != k:
@ -233,7 +234,12 @@ class State:
state = self.from_name(k) state = self.from_name(k)
if not self.__is_pure(state): if not self.__is_pure(state):
continue continue
l.append(k) if numeric:
if state == None:
state = self.from_name(k)
l.append(state)
else:
l.append(k)
l.sort() l.sort()
return l return l
@ -628,3 +634,25 @@ class State:
statemask = ~statemask statemask = ~statemask
statemask &= self.__limit statemask &= self.__limit
return statemask return statemask
def purge(self, key):
state = self.state(key)
state_name = self.name(state)
import sys
sys.stderr.write('foo {} {}'.format(state_name, self.__keys))
v = self.__keys.get(state)
v.remove(key)
del self.__keys_reverse[key]
try:
del self.__contents[key]
except KeyError:
pass
try:
del self.modified_last[key]
except KeyError:
pass

View File

@ -7,6 +7,7 @@ from shep import State
from shep.error import ( from shep.error import (
StateExists, StateExists,
StateInvalid, StateInvalid,
StateItemNotFound,
) )
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
@ -250,5 +251,24 @@ class TestState(unittest.TestCase):
self.assertEqual(mask, states.ALL) self.assertEqual(mask, states.ALL)
def test_remove(self):
states = State(1)
states.add('foo')
states.put('xyzzy', contents='plugh')
v = states.get('xyzzy')
self.assertEqual(v, 'plugh')
states.next('xyzzy')
v = states.state('xyzzy')
self.assertEqual(states.FOO, v)
states.purge('xyzzy')
with self.assertRaises(StateItemNotFound):
states.state('xyzzy')
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()