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

View file

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

View file

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

View file

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