From 46910dbc4f02c5dbb3fd7d81f59c8f5a3118adb9 Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Sun, 6 Apr 2014 13:39:57 +0200 Subject: [PATCH] Fix #20 --- example.cfg | 2 ++ tests/storage/test_filesystem.py | 8 ++++++++ vdirsyncer/storage/filesystem.py | 11 ++++++++++- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/example.cfg b/example.cfg index c3c0a0b..f88f0c1 100644 --- a/example.cfg +++ b/example.cfg @@ -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 diff --git a/tests/storage/test_filesystem.py b/tests/storage/test_filesystem.py index 2673935..ac8f8ba 100644 --- a/tests/storage/test_filesystem.py +++ b/tests/storage/test_filesystem.py @@ -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')] diff --git a/vdirsyncer/storage/filesystem.py b/vdirsyncer/storage/filesystem.py index 743b0c8..7e5852b 100644 --- a/vdirsyncer/storage/filesystem.py +++ b/vdirsyncer/storage/filesystem.py @@ -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