Directory checking for singlefilestorage

This commit is contained in:
Markus Unterwaditzer 2014-05-19 19:40:30 +02:00
parent d015857834
commit b54c413949
3 changed files with 31 additions and 23 deletions

View file

@ -13,7 +13,7 @@ from .base import Item, Storage
import vdirsyncer.exceptions as exceptions import vdirsyncer.exceptions as exceptions
import vdirsyncer.log as log import vdirsyncer.log as log
from vdirsyncer.utils import expand_path, text_type, safe_write, \ from vdirsyncer.utils import expand_path, text_type, safe_write, \
get_etag_from_file get_etag_from_file, checkdir
logger = log.get(__name__) logger = log.get(__name__)
@ -44,16 +44,7 @@ class FilesystemStorage(Storage):
path = expand_path(path) path = expand_path(path)
if collection is not None: if collection is not None:
path = os.path.join(path, collection) path = os.path.join(path, collection)
if not os.path.isdir(path): checkdir(path, create=create)
if os.path.exists(path):
raise IOError('{} is not a directory.')
if create:
os.makedirs(path, 0o750)
else:
raise IOError('Directory {} does not exist. Use create = '
'True in your configuration to automatically '
'create it, or create it '
'yourself.'.format(path))
self.collection = collection self.collection = collection
self.path = path self.path = path
self.encoding = encoding self.encoding = encoding

View file

@ -13,7 +13,7 @@ import collections
from .base import Item, Storage from .base import Item, Storage
import vdirsyncer.exceptions as exceptions import vdirsyncer.exceptions as exceptions
import vdirsyncer.log as log import vdirsyncer.log as log
from vdirsyncer.utils import expand_path, safe_write, itervalues from vdirsyncer.utils import expand_path, safe_write, itervalues, checkfile
from vdirsyncer.utils.vobject import split_collection, join_collection from vdirsyncer.utils.vobject import split_collection, join_collection
logger = log.get(__name__) logger = log.get(__name__)
@ -44,17 +44,11 @@ class SingleFileStorage(Storage):
raise ValueError('collection is not a valid argument for {}' raise ValueError('collection is not a valid argument for {}'
.format(type(self).__name__)) .format(type(self).__name__))
if not os.path.isfile(path): checkfile(path, create=create)
if os.path.exists(path):
raise IOError('{} is not a file.'.format(path)) if create:
if create: self._write_mode = 'wb+'
self._write_mode = 'wb+' self._append_mode = 'ab+'
self._append_mode = 'ab+'
else:
raise IOError('File {} does not exist. Use create = '
'True in your configuration to automatically '
'create it, or create it '
'yourself.'.format(path))
self.path = path self.path = path
self.encoding = encoding self.encoding = encoding

View file

@ -276,3 +276,26 @@ def get_class_init_args(cls):
s_all, s_required = get_class_init_args(supercls) s_all, s_required = get_class_init_args(supercls)
return all | s_all, required | s_required return all | s_all, required | s_required
def checkdir(path, create=False):
if not os.path.isdir(path):
if os.path.exists(path):
raise IOError('{} is not a directory.'.format(path))
if create:
os.makedirs(path, 0o750)
else:
raise IOError('Directory {} does not exist. Use create = '
'True in your configuration to automatically '
'create it, or create it '
'yourself.'.format(path))
def checkfile(path, create=False):
checkdir(os.path.dirname(path), create=create)
if not os.path.isfile(path):
if os.path.exists(path):
raise IOError('{} is not a file.'.format(path))
if not create:
raise IOError('File {} does not exist. Use create = '
'True in your configuration to automatically '
'create it, or create it '
'yourself.'.format(path))