diff --git a/vdirsyncer/cli/utils.py b/vdirsyncer/cli/utils.py index eed430d..1a31e66 100644 --- a/vdirsyncer/cli/utils.py +++ b/vdirsyncer/cli/utils.py @@ -184,17 +184,13 @@ def _discover_from_config(config): return rv -def _get_coll(pair_name, storage_name, collection, discovered, config): - try: - return discovered[collection] - except KeyError: - return _handle_collection_not_found(config, collection) - - -def _handle_collection_not_found(config, collection): +def _handle_collection_not_found(config, collection, e=None): storage_name = config.get('instance_name', None) - cli_logger.error('No collection {} found for storage {}.' - .format(collection, storage_name)) + + cli_logger.error('{}No collection {} found for storage {}.' + .format('{}\n'.format(e) if e else '', + collection, storage_name)) + if click.confirm('Should vdirsyncer attempt to create it?'): storage_type = config['type'] cls, config = storage_class_from_config(config) @@ -230,10 +226,16 @@ def _collections_for_pair_impl(status_path, name_a, name_b, pair_name, collections = [shortcut] for collection in collections: - a_args = _get_coll(pair_name, name_a, collection, a_discovered, - config_a) - b_args = _get_coll(pair_name, name_b, collection, b_discovered, - config_b) + try: + a_args = a_discovered[collection] + except KeyError: + a_args = _handle_collection_not_found(config_a, collection) + + try: + b_args = b_discovered[collection] + except KeyError: + b_args = _handle_collection_not_found(config_b, collection) + yield collection, (a_args, b_args) @@ -412,9 +414,9 @@ def storage_instance_from_config(config, create=True): try: return cls(**new_config) except exceptions.CollectionNotFound as e: - cli_logger.error(str(e)) if create: - _handle_collection_not_found(config, None) + config = _handle_collection_not_found( + config, config.get('collection', None), e=str(e)) return storage_instance_from_config(config, create=False) else: raise diff --git a/vdirsyncer/storage/base.py b/vdirsyncer/storage/base.py index 2f6d4c8..83011e3 100644 --- a/vdirsyncer/storage/base.py +++ b/vdirsyncer/storage/base.py @@ -104,10 +104,14 @@ class Storage(with_metaclass(StorageMeta)): @classmethod def create_collection(cls, collection, **kwargs): - '''Create the specified collection and return the new arguments. + ''' + Create the specified collection and return the new arguments. ``collection=None`` means the arguments are already pointing to a - possible collection location.''' + possible collection location. + + The returned args should contain the collection name, for UI purposes. + ''' raise NotImplementedError() def __repr__(self): diff --git a/vdirsyncer/storage/filesystem.py b/vdirsyncer/storage/filesystem.py index 773b4fc..0272a3c 100644 --- a/vdirsyncer/storage/filesystem.py +++ b/vdirsyncer/storage/filesystem.py @@ -72,6 +72,7 @@ class FilesystemStorage(Storage): if collection is not None: kwargs['path'] = os.path.join(kwargs['path'], collection) checkdir(kwargs['path'], create=True) + kwargs['collection'] = collection return kwargs def _get_filepath(self, href):