Make pair config more strongly typed

This commit is contained in:
Markus Unterwaditzer 2016-10-04 18:51:11 +02:00
parent 1c030d40ac
commit 15bf13cfe1
4 changed files with 22 additions and 21 deletions

View file

@ -45,8 +45,6 @@ def test_read_config(read_config):
[pair bob]
a = bob_a
b = bob_b
foo = bar
bam = true
collections = null
[storage bob_a]
@ -64,7 +62,7 @@ def test_read_config(read_config):
assert set(c.pairs) == {'bob'}
bob = c.pairs['bob']
assert bob.options == {'bam': True, 'foo': 'bar', 'collections': None}
assert bob.collections is None
assert c.storages == {
'bob_a': {'type': 'filesystem', 'path': '/tmp/contacts/', 'fileext':

View file

@ -251,14 +251,20 @@ class PairConfig(object):
self.name = name
self.name_a = options.pop('a')
self.name_b = options.pop('b')
self.options = options
self._set_conflict_resolution()
self._set_partial_sync()
self._set_collections()
self._set_conflict_resolution(options)
self._set_partial_sync(options)
self._set_collections(options)
self.metadata = options.pop('metadata', None) or ()
def _set_conflict_resolution(self):
conflict_resolution = self.options.pop('conflict_resolution', None)
if options:
raise ValueError('Unknown options: {}'.format(', '.join(options)))
self.config_a = self._config.get_storage_args(self.name_a)
self.config_b = self._config.get_storage_args(self.name_b)
def _set_conflict_resolution(self, options):
conflict_resolution = options.pop('conflict_resolution', None)
if conflict_resolution in (None, 'a wins', 'b wins'):
self.conflict_resolution = conflict_resolution
elif isinstance(conflict_resolution, list) and \
@ -279,14 +285,14 @@ class PairConfig(object):
else:
raise ValueError('Invalid value for `conflict_resolution`.')
def _set_partial_sync(self):
self.partial_sync = self.options.pop('partial_sync', 'revert')
def _set_partial_sync(self, options):
self.partial_sync = options.pop('partial_sync', 'revert')
if self.partial_sync not in ('ignore', 'revert', 'error'):
raise ValueError('Invalid value for `partial_sync`.')
def _set_collections(self):
def _set_collections(self, options):
try:
collections = self.options['collections']
self.collections = options.pop('collections')
except KeyError:
raise ValueError(
'collections parameter missing.\n\n'
@ -294,10 +300,7 @@ class PairConfig(object):
'Set `collections = null` explicitly in your pair config.'
)
else:
_validate_collections_param(collections)
self.config_a = self._config.get_storage_args(self.name_a)
self.config_b = self._config.get_storage_args(self.name_b)
_validate_collections_param(self.collections)
class CollectionConfig(object):

View file

@ -139,8 +139,8 @@ def metasync_collection(wq, collection, general):
metasync(
a, b, status,
conflict_resolution=pair.options.get('conflict_resolution', None),
keys=pair.options.get('metadata', None) or ()
conflict_resolution=pair.conflict_resolution,
keys=pair.metadata
)
except BaseException:
handle_cli_error(status_name)

View file

@ -176,7 +176,7 @@ def _get_collections_cache_key(pair):
m = hashlib.sha256()
j = json.dumps([
DISCOVERY_CACHE_VERSION,
pair.options.get('collections', None),
pair.collections,
pair.config_a,
pair.config_b,
], sort_keys=True)
@ -325,7 +325,7 @@ def _print_collections(base_config, discovered):
def _collections_for_pair_impl(status_path, pair, list_collections=False):
handled_collections = set()
shortcuts = pair.options['collections']
shortcuts = pair.collections
if shortcuts is None:
shortcuts = [None]