Add simple file store backend
This commit is contained in:
parent
d232632c36
commit
3de0ad8ee3
@ -1,5 +1,6 @@
|
|||||||
- 0.0.6
|
- 0.0.6
|
||||||
* Update description
|
* Update description
|
||||||
|
* Implement simple file store backend (add only)
|
||||||
- 0.0.5
|
- 0.0.5
|
||||||
* Add persistence wrapper
|
* Add persistence wrapper
|
||||||
- 0.0.4
|
- 0.0.4
|
||||||
|
33
shep/store/file.py
Normal file
33
shep/store/file.py
Normal 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
42
tests/test_file.py
Normal 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()
|
Loading…
Reference in New Issue
Block a user