commit f0ac792c4a39d7f036403e93a089ff67592b2b0f
parent b9925a5038efd117f70ea7199c69f8338452578c
Author: lash <dev@holbrook.no>
Date: Wed, 2 Feb 2022 09:27:57 +0000
Add next state advancer
Diffstat:
6 files changed, 51 insertions(+), 6 deletions(-)
diff --git a/CHANGELOG b/CHANGELOG
@@ -1,3 +1,5 @@
+- 0.0.14
+ * Add next state advancer
- 0.0.13
* Add next state peek
- 0.0.12
diff --git a/setup.cfg b/setup.cfg
@@ -1,6 +1,6 @@
[metadata]
name = shep
-version = 0.0.13
+version = 0.0.14
description = Multi-state key stores using bit masks
author = Louis Holbrook
author_email = dev@holbrook.no
diff --git a/shep/persist.py b/shep/persist.py
@@ -58,11 +58,14 @@ class PersistedState(State):
def move(self, key, to_state):
from_state = self.state(key)
- k_from = self.name(from_state)
-
to_state = super(PersistedState, self).move(key, to_state)
+ return self.__movestore(key, from_state, to_state)
+
+ def __movestore(self, key, from_state, to_state):
+ k_from = self.name(from_state)
k_to = self.name(to_state)
+
self.__ensure_store(k_to)
contents = self.__stores[k_from].get(key)
@@ -96,3 +99,9 @@ class PersistedState(State):
k = self.name(state)
return self.__stores[k].path(key=key)
+
+
+ def next(self, key=None):
+ from_state = self.state(key)
+ to_state = super(PersistedState, self).next(key)
+ return self.__movestore(key, from_state, to_state)
diff --git a/shep/state.py b/shep/state.py
@@ -132,8 +132,8 @@ class State:
def name(self, v):
- if v == None:
- return self.NEW
+ if v == None or v == 0:
+ return 'NEW'
k = self.__reverse.get(v)
if k == None:
raise StateInvalid(v)
@@ -269,6 +269,14 @@ class State:
return self.__keys[state]
+ def sync(self, state):
+ pass
+
+
+ def path(self, state, key=None):
+ return None
+
+
def peek(self, key):
state = self.__keys_reverse.get(key)
if state == None:
@@ -284,3 +292,9 @@ class State:
raise StateInvalid('unknown state {}'.format(state))
return state
+
+
+ def next(self, key):
+ from_state = self.state(key)
+ new_state = self.peek(key)
+ return self.__move(key, from_state, new_state)
diff --git a/shep/store/file.py b/shep/store/file.py
@@ -23,7 +23,7 @@ class SimpleFileStore:
raise FileExistsError(fp)
if contents == None:
raise FileExistsError('will not overwrite empty content on existing file {}. Use rm then add instead'.format(fp))
- elif contents == None:
+ if contents == None:
contents = ''
f = open(fp, 'w')
diff --git a/tests/test_file.py b/tests/test_file.py
@@ -145,7 +145,27 @@ class TestStateReport(unittest.TestCase):
d = os.path.join(self.d, 'FOO', 'BAR')
self.assertEqual(self.states.path(self.states.FOO, key='BAR'), d)
+
+
+ def test_next(self):
+ self.states.put('abcd')
+
+ self.states.next('abcd')
+ self.assertEqual(self.states.state('abcd'), self.states.FOO)
+ self.states.next('abcd')
+ self.assertEqual(self.states.state('abcd'), self.states.BAR)
+
+ with self.assertRaises(StateInvalid):
+ self.states.next('abcd')
+
+ fp = os.path.join(self.d, 'FOO', 'abcd')
+ with self.assertRaises(FileNotFoundError):
+ os.stat(fp)
+
+ fp = os.path.join(self.d, 'BAR', 'abcd')
+ os.stat(fp)
+
if __name__ == '__main__':
unittest.main()