shep

Multi-state key stores using bit masks for python3
git clone git://git.defalsify.org/shep.git
Log | Files | Refs | LICENSE

commit 714bf79d22e3aa236e02da84fa6553a26d635649
parent 53da59c06e56d2b67a999f9310e87a416c1ed68e
Author: lash <dev@holbrook.no>
Date:   Mon,  2 May 2022 11:21:07 +0000

WIP selective state sync

Diffstat:
Mshep/persist.py | 14++++++++++----
Mshep/state.py | 34+++++++++++++++++++++++++++++++---
Mtests/test_state.py | 20++++++++++++++++++++
3 files changed, 61 insertions(+), 7 deletions(-)

diff --git a/shep/persist.py b/shep/persist.py @@ -144,7 +144,7 @@ class PersistedState(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. :param state: State to load @@ -153,11 +153,17 @@ class PersistedState(State): # :todo: if sync state is none, sync all """ - states = [] + states_numeric = [] if state == None: - states = list(self.all()) + states_numeric = list(self.all(numeric=True)) 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 = [] for k in states: diff --git a/shep/state.py b/shep/state.py @@ -217,14 +217,15 @@ class State: return self.__alias(k, *args) - def all(self, pure=False): - """Return list of all unique atomic and alias states. + def all(self, pure=False, numeric=False): + """Return list of all unique atomic and alias state strings. :rtype: list of ints :return: states """ l = [] for k in dir(self): + state = None if k[0] == '_': continue if k.upper() != k: @@ -233,7 +234,12 @@ class State: state = self.from_name(k) if not self.__is_pure(state): continue - l.append(k) + if numeric: + if state == None: + state = self.from_name(k) + l.append(state) + else: + l.append(k) l.sort() return l @@ -628,3 +634,25 @@ class State: statemask = ~statemask statemask &= self.__limit 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 diff --git a/tests/test_state.py b/tests/test_state.py @@ -7,6 +7,7 @@ from shep import State from shep.error import ( StateExists, StateInvalid, + StateItemNotFound, ) logging.basicConfig(level=logging.DEBUG) @@ -250,5 +251,24 @@ class TestState(unittest.TestCase): 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__': unittest.main()