Ensure persisted states even if empty

This commit is contained in:
lash 2022-11-06 23:10:44 +00:00
parent 1d0e31d10d
commit 2f95167895
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
5 changed files with 20 additions and 7 deletions

View File

@ -2,6 +2,7 @@
* Clean up lingering keys in lists when moving from alias state
* Properly set default state when set through instantiation
* pass key to verifier (breaking change)
* ensure all states persisted even if empty
- 0.2.10
* Add count active states method
* Enable complete replace of NEW state on state instantiation

View File

@ -24,12 +24,15 @@ class PersistedState(State):
super(PersistedState, self).__init__(bits, logger=logger, verifier=verifier, check_alias=check_alias, event_callback=event_callback, default_state=default_state)
self.__store_factory = factory
self.__stores = {}
self.__ensure_store(self.base_state_name)
# Create state store container if missing.
def __ensure_store(self, k):
k = k.upper()
if self.__stores.get(k) == None:
self.__stores[k] = self.__store_factory(k)
print('ensure {}'.format(k))
def put(self, key, contents=None, state=None):
@ -74,7 +77,7 @@ class PersistedState(State):
return to_state
def unset(self, key, not_state):
def unset(self, key, not_state, allow_base=False):
"""Persist a new state for a key or key/content.
See shep.state.State.unset
@ -82,7 +85,7 @@ class PersistedState(State):
from_state = self.state(key)
k_from = self.name(from_state)
to_state = super(PersistedState, self).unset(key, not_state)
to_state = super(PersistedState, self).unset(key, not_state, allow_base=allow_base)
k_to = self.name(to_state)
self.__ensure_store(k_to)
@ -237,3 +240,8 @@ class PersistedState(State):
state = self.state(key)
k = self.name(state)
return self.__stores[k].modified(key)
def add(self, key):
self.__ensure_store(key)
super(PersistedState, self).add(key)

View File

@ -473,12 +473,14 @@ class State:
def unset(self, key, not_state, allow_base=False):
"""Unset a single bit, moving to a pure or alias state.
The resulting state cannot be State.base_state_name (0).
If allow_base is set to False, The resulting state cannot be State.base_state_name (0).
:param key: Content key to modify state for
:type key: str
:param or_state: Atomic stat to add
:type or_state: int
:paran allow_base: Allow state to be reset to 0
:type allow_base: bool
:raises ValueError: State is not a single bit state, or attempts to revert to State.base_state_name
:raises StateItemNotFound: Content key is not registered
:raises StateInvalid: Resulting state after addition of atomic state is unknown

View File

@ -244,18 +244,21 @@ class TestFileStore(unittest.TestCase):
def test_factory_ls(self):
r = self.factory.ls()
self.assertEqual(len(r), 4)
self.states.put('abcd')
self.states.put('xxxx', state=self.states.BAZ)
r = self.factory.ls()
self.assertEqual(len(r), 2)
self.assertEqual(len(r), 4)
self.states.put('yyyy', state=self.states.BAZ)
r = self.factory.ls()
self.assertEqual(len(r), 2)
self.assertEqual(len(r), 4)
self.states.put('zzzz', state=self.states.BAR)
r = self.factory.ls()
self.assertEqual(len(r), 3)
self.assertEqual(len(r), 4)
def test_lock(self):

View File

@ -323,7 +323,6 @@ class TestState(unittest.TestCase):
states.state('FOO')
states.put('bar')
r = states.list(states.FOO)
print(r)
self.assertEqual(len(r), 1)