shep

Multi-state key stores using bit masks for python3
git clone git://git.defalsify.org/shep.git
Log | Files | Refs | LICENSE

commit 92d1ec42ed507224859e99c0d71aa4c93e8cfd19
parent 1e077be1211335d26058f742dfbccb2e48acf3a8
Author: lash <dev@holbrook.no>
Date:   Tue,  1 Feb 2022 14:38:24 +0000

Add sync for persisted store backend

Diffstat:
MCHANGELOG | 2++
Msetup.cfg | 2+-
Mshep/persist.py | 11+++++++++++
Mshep/store/file.py | 13+++++++++++++
Mtests/test_file.py | 20++++++++++++++++++++
5 files changed, 47 insertions(+), 1 deletion(-)

diff --git a/CHANGELOG b/CHANGELOG @@ -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 diff --git a/setup.cfg b/setup.cfg @@ -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 diff --git a/shep/persist.py b/shep/persist.py @@ -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 diff --git a/shep/store/file.py b/shep/store/file.py @@ -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): diff --git a/tests/test_file.py b/tests/test_file.py @@ -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()