This commit is contained in:
Markus Unterwaditzer 2014-04-06 13:39:57 +02:00
parent 8f6b3003cc
commit 46910dbc4f
3 changed files with 20 additions and 1 deletions

View file

@ -21,6 +21,8 @@ collections = private,coworkers # collections = addressbooks in this case
type = filesystem
path = ~/.contacts/
fileext = .vcf
#create = True # create directory if it doesn't exist
#encoding = utf-8
[storage bob_contacts_remote]
type = carddav

View file

@ -26,3 +26,11 @@ class TestFilesystemStorage(StorageTests):
if collection is not None:
os.makedirs(os.path.join(path, collection))
return {'path': path, 'fileext': '.txt', 'collection': collection}
def test_create_directory(self, tmpdir):
with pytest.raises(ValueError):
self.storage_class(str(tmpdir), '.txt', collection='lol',
create=False)
self.storage_class(str(tmpdir), '.txt', collection='asd')
assert tmpdir.listdir() == [tmpdir.join('asd')]

View file

@ -28,7 +28,7 @@ class FilesystemStorage(Storage):
_repr_attributes = ('path',)
def __init__(self, path, fileext, collection=None, encoding='utf-8',
**kwargs):
create=True, **kwargs):
'''
:param path: Absolute path to a vdir or collection, depending on the
collection parameter (see
@ -38,10 +38,19 @@ class FilesystemStorage(Storage):
will trigger a re-download of everything (but *should* not cause
data-loss of any kind).
:param encoding: File encoding for items.
:param create: Create directories if they don't exist.
'''
super(FilesystemStorage, self).__init__(**kwargs)
if collection is not None:
path = os.path.join(path, collection)
if not os.path.isdir(path):
if create:
os.makedirs(path, 0750)
else:
raise ValueError('Directory {} does not exist. Use create = '
'True in your configuration to automatically '
'create it, or create it '
'yourself.'.format(path))
self.collection = collection
self.path = expand_path(path)
self.encoding = encoding