From 02be210eb4359408a6d8db4fb1d7dfaff679eaa3 Mon Sep 17 00:00:00 2001 From: lash Date: Wed, 2 Feb 2022 07:24:43 +0000 Subject: [PATCH] Add path query to persist --- CHANGELOG | 2 ++ setup.cfg | 2 +- shep/persist.py | 6 ++++++ shep/store/file.py | 24 +++++++++++++++--------- tests/test_file.py | 10 ++++++++++ 5 files changed, 34 insertions(+), 10 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index efd9b8c..9e698a1 100644 --- 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 index 2decfd5..924b972 100644 --- 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 index 0c43c92..83f67c8 100644 --- 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 index 421a4ea..a85ecbb 100644 --- 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 index 2ae8331..44825a7 100644 --- 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__':