Add next state advancer
This commit is contained in:
parent
b9925a5038
commit
f0ac792c4a
@ -1,3 +1,5 @@
|
||||
- 0.0.14
|
||||
* Add next state advancer
|
||||
- 0.0.13
|
||||
* Add next state peek
|
||||
- 0.0.12
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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')
|
||||
|
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user