Add sync for persisted store backend
This commit is contained in:
parent
1e077be121
commit
92d1ec42ed
@ -1,3 +1,5 @@
|
||||
- 0.0.11
|
||||
* Add sync from persisted store
|
||||
- 0.0.10
|
||||
* Implement move, set, unset on persisted store
|
||||
- 0.0.9
|
||||
|
@ -1,6 +1,6 @@
|
||||
[metadata]
|
||||
name = shep
|
||||
version = 0.0.10
|
||||
version = 0.0.11
|
||||
description = Multi-state key stores using bit masks
|
||||
author = Louis Holbrook
|
||||
author_email = dev@holbrook.no
|
||||
|
@ -1,5 +1,6 @@
|
||||
# local imports
|
||||
from .state import State
|
||||
from .error import StateItemExists
|
||||
|
||||
|
||||
class PersistedState(State):
|
||||
@ -79,3 +80,13 @@ class PersistedState(State):
|
||||
|
||||
self.__stores[k].remove(key)
|
||||
super(PersistedState, self).purge(key)
|
||||
|
||||
|
||||
def sync(self, state):
|
||||
k = self.name(state)
|
||||
|
||||
for o in self.__stores[k].list():
|
||||
try:
|
||||
super(PersistedState, self).put(o[0], state=state, contents=o[1])
|
||||
except StateItemExists:
|
||||
pass
|
||||
|
@ -44,6 +44,19 @@ class SimpleFileStore:
|
||||
return r
|
||||
|
||||
|
||||
def list(self):
|
||||
files = []
|
||||
for p in os.listdir(self.path):
|
||||
fp = os.path.join(self.path, p)
|
||||
f = open(fp, 'r')
|
||||
r = f.read()
|
||||
f.close()
|
||||
if len(r) == 0:
|
||||
r = None
|
||||
files.append((p, r,))
|
||||
return files
|
||||
|
||||
|
||||
class SimpleFileStoreFactory:
|
||||
|
||||
def __init__(self, path):
|
||||
|
@ -117,5 +117,25 @@ class TestStateReport(unittest.TestCase):
|
||||
os.stat(fp)
|
||||
|
||||
|
||||
def test_sync(self):
|
||||
self.states.put('abcd', state=self.states.FOO, contents='foo')
|
||||
self.states.put('xxx', state=self.states.FOO)
|
||||
self.states.put('yyy', state=self.states.FOO)
|
||||
|
||||
fp = os.path.join(self.d, 'FOO', 'yyy')
|
||||
f = open(fp, 'w')
|
||||
f.write('')
|
||||
f.close()
|
||||
|
||||
fp = os.path.join(self.d, 'FOO', 'zzzz')
|
||||
f = open(fp, 'w')
|
||||
f.write('xyzzy')
|
||||
f.close()
|
||||
|
||||
self.states.sync(self.states.FOO)
|
||||
self.assertEqual(self.states.get('yyy'), None)
|
||||
self.assertEqual(self.states.get('zzzz'), 'xyzzy')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
Loading…
Reference in New Issue
Block a user