Add sync for persisted store backend

This commit is contained in:
lash 2022-02-01 14:38:24 +00:00
parent 1e077be121
commit 92d1ec42ed
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
5 changed files with 47 additions and 1 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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):

View File

@ -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()