Skip to content

Commit

Permalink
Add an option to FileSystemDatabase to create directories if they don…
Browse files Browse the repository at this point in the history
…'t already exist
  • Loading branch information
JohnVinyard committed Jun 9, 2016
1 parent 1bb1afd commit d732009
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
2 changes: 1 addition & 1 deletion featureflow/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '1.13.10'
__version__ = '1.14.10'

from model import BaseModel

Expand Down
4 changes: 3 additions & 1 deletion featureflow/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,11 @@ def __delitem__(self, key):


class FileSystemDatabase(Database):
def __init__(self, path=None, key_builder=None):
def __init__(self, path=None, key_builder=None, createdirs=False):
super(FileSystemDatabase, self).__init__(key_builder=key_builder)
self._path = path
if createdirs and not os.path.exists(self._path):
os.makedirs(self._path)

def write_stream(self, key, content_type):
return open(os.path.join(self._path, key), 'wb')
Expand Down
36 changes: 35 additions & 1 deletion featureflow/test_data.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import unittest2
from data import InMemoryDatabase, UserSpecifiedIdProvider
from uuid import uuid4
from data import \
InMemoryDatabase, UserSpecifiedIdProvider, FileSystemDatabase, \
StringDelimitedKeyBuilder
import shutil


class InMemoryDatabaseTest(unittest2.TestCase):
Expand Down Expand Up @@ -34,3 +38,33 @@ def test_can_overwrite_key(self):
class UserSpecifiedIdProviderTest(unittest2.TestCase):
def test_raises_when_no_key_is_provided(self):
self.assertRaises(ValueError, lambda: UserSpecifiedIdProvider())


class FileSystemDatabaseTests(unittest2.TestCase):
def setUp(self):
self._key_builder = StringDelimitedKeyBuilder()
self._path = '/tmp/{path}'.format(path=uuid4().hex)

def tearDown(self):
try:
shutil.rmtree(self._path)
except OSError:
pass

def _make_db(self, createdirs):
return FileSystemDatabase(
path=self._path,
key_builder=self._key_builder,
createdirs=createdirs)

def test_creates_path_when_asked(self):
db = self._make_db(createdirs=True)
with db.write_stream('key', 'text/plain') as s:
s.write('text')

with db.read_stream('key') as s:
self.assertEqual('text', s.read())

def test_does_not_create_path_when_not_asked(self):
db = self._make_db(createdirs=False)
self.assertRaises(IOError, lambda: db.write_stream('key', 'text/plain'))

0 comments on commit d732009

Please sign in to comment.