shep

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

commit 02be210eb4359408a6d8db4fb1d7dfaff679eaa3
parent 92d1ec42ed507224859e99c0d71aa4c93e8cfd19
Author: lash <dev@holbrook.no>
Date:   Wed,  2 Feb 2022 07:24:43 +0000

Add path query to persist

Diffstat:
MCHANGELOG | 2++
Msetup.cfg | 2+-
Mshep/persist.py | 6++++++
Mshep/store/file.py | 24+++++++++++++++---------
Mtests/test_file.py | 10++++++++++
5 files changed, 34 insertions(+), 10 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG @@ -1,3 +1,5 @@ +- 0.0.12 + * Add path method - 0.0.11 * Add sync from persisted store - 0.0.10 diff --git a/setup.cfg b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = shep -version = 0.0.11 +version = 0.0.12 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 @@ -90,3 +90,9 @@ class PersistedState(State): super(PersistedState, self).put(o[0], state=state, contents=o[1]) except StateItemExists: pass + + + def path(self, state, key=None): + k = self.name(state) + + return self.__stores[k].path(key=key) diff --git a/shep/store/file.py b/shep/store/file.py @@ -5,12 +5,12 @@ import os class SimpleFileStore: def __init__(self, path): - self.path = path - os.makedirs(self.path, exist_ok=True) + self.__path = path + os.makedirs(self.__path, exist_ok=True) def add(self, k, contents=None, force=False): - fp = os.path.join(self.path, k) + fp = os.path.join(self.__path, k) have_file = False try: os.stat(fp) @@ -32,12 +32,12 @@ class SimpleFileStore: def remove(self, k): - fp = os.path.join(self.path, k) + fp = os.path.join(self.__path, k) os.unlink(fp) def get(self, k): - fp = os.path.join(self.path, k) + fp = os.path.join(self.__path, k) f = open(fp, 'r') r = f.read() f.close() @@ -46,8 +46,8 @@ class SimpleFileStore: def list(self): files = [] - for p in os.listdir(self.path): - fp = os.path.join(self.path, p) + for p in os.listdir(self.__path): + fp = os.path.join(self.__path, p) f = open(fp, 'r') r = f.read() f.close() @@ -57,13 +57,19 @@ class SimpleFileStore: return files + def path(self, key=None): + if key == None: + return self.__path + return os.path.join(self.__path, key) + + class SimpleFileStoreFactory: def __init__(self, path): - self.path = path + self.__path = path def add(self, k): k = str(k) - store_path = os.path.join(self.path, k) + store_path = os.path.join(self.__path, k) return SimpleFileStore(store_path) diff --git a/tests/test_file.py b/tests/test_file.py @@ -135,6 +135,16 @@ class TestStateReport(unittest.TestCase): self.states.sync(self.states.FOO) self.assertEqual(self.states.get('yyy'), None) self.assertEqual(self.states.get('zzzz'), 'xyzzy') + + + def test_path(self): + self.states.put('yyy', state=self.states.FOO) + + d = os.path.join(self.d, 'FOO') + self.assertEqual(self.states.path(self.states.FOO), d) + + d = os.path.join(self.d, 'FOO', 'BAR') + self.assertEqual(self.states.path(self.states.FOO, key='BAR'), d) if __name__ == '__main__':