Add simple file store backend

This commit is contained in:
lash 2022-01-31 15:53:33 +00:00
parent d232632c36
commit 3de0ad8ee3
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
3 changed files with 76 additions and 0 deletions

View File

@ -1,5 +1,6 @@
- 0.0.6
* Update description
* Implement simple file store backend (add only)
- 0.0.5
* Add persistence wrapper
- 0.0.4

33
shep/store/file.py Normal file
View File

@ -0,0 +1,33 @@
# standard imports
import os
class SimpleFileStore:
def __init__(self, path):
self.path = path
os.makedirs(self.path, exist_ok=True)
def add(self, v, contents=''):
fp = os.path.join(self.path, v)
try:
os.stat(fp)
raise FileExistsError(fp)
except FileNotFoundError:
pass
f = open(fp, 'w')
f.write(contents)
f.close()
class SimpleFileStoreFactory:
def __init__(self, path):
self.path = path
def add(self, k):
k = str(k)
store_path = os.path.join(self.path, k)
return SimpleFileStore(store_path)

42
tests/test_file.py Normal file
View File

@ -0,0 +1,42 @@
# standard imports
import unittest
import tempfile
import os
# local imports
from shep.persist import PersistedState
from shep.store.file import SimpleFileStoreFactory
from shep.error import (
StateExists,
StateInvalid,
)
class TestStateReport(unittest.TestCase):
def setUp(self):
self.d = tempfile.mkdtemp()
self.factory = SimpleFileStoreFactory(self.d)
self.states = PersistedState(self.factory.add, 4)
self.states.add('foo')
self.states.add('bar')
self.states.add('baz')
def test_add(self):
self.states.put('abcd', self.states.FOO)
fp = os.path.join(self.d, 'FOO', 'abcd')
f = open(fp, 'r')
v = f.read()
f.close()
self.assertEqual(len(v), 0)
def test_dup(self):
self.states.put('abcd', self.states.FOO)
with self.assertRaises(FileExistsError):
self.states.put('abcd', self.states.FOO)
if __name__ == '__main__':
unittest.main()