commit 2f951678950b2abdfb925e909dba122f174ee131
parent 1d0e31d10df8120b368b91e634ad331dd06b9c16
Author: lash <dev@holbrook.no>
Date: Sun, 6 Nov 2022 23:10:44 +0000
Ensure persisted states even if empty
Diffstat:
5 files changed, 20 insertions(+), 7 deletions(-)
diff --git a/CHANGELOG b/CHANGELOG
@@ -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
diff --git a/shep/persist.py b/shep/persist.py
@@ -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)
diff --git a/shep/state.py b/shep/state.py
@@ -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
diff --git a/tests/test_file.py b/tests/test_file.py
@@ -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):
diff --git a/tests/test_state.py b/tests/test_state.py
@@ -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)