Kill parse_pairs_args

This commit is contained in:
Markus Unterwaditzer 2015-08-20 17:18:23 +02:00
parent 8e2070e42d
commit 2d62ec9a26
3 changed files with 32 additions and 49 deletions

View file

@ -69,22 +69,6 @@ def test_storage_instance_from_config(monkeypatch):
assert cli.utils.storage_instance_from_config(config) == 'OK'
def test_parse_pairs_args():
pairs = {
'foo': ('bar', 'baz', {'conflict_resolution': 'a wins'},
{'storage_option': True}),
'one': ('two', 'three', {'collections': 'a,b,c'}, {}),
'eins': ('zwei', 'drei', {'ha': True}, {})
}
assert sorted(
cli.utils.parse_pairs_args(['foo/foocoll', 'one', 'eins'], pairs)
) == [
('eins', set()),
('foo', {'foocoll'}),
('one', set()),
]
def test_missing_general_section(read_config):
with pytest.raises(cli.CliError) as excinfo:
read_config(u'''

View file

@ -99,8 +99,31 @@ max_workers_option = click.option(
)
def pairs_arg_callback(ctx, param, value):
'''
Expand the various CLI shortforms ("pair, pair/collection") to an iterable
of (pair, collections).
'''
# XXX: Ugly! pass_context should work everywhere.
config = ctx.find_object(AppContext).config
rv = {}
for pair_and_collection in (value or config.pairs):
pair, collection = pair_and_collection, None
if '/' in pair:
pair, collection = pair.split('/')
collections = rv.setdefault(pair, set())
if collection:
collections.add(collection)
return rv.items()
pairs_arg = click.argument('pairs', nargs=-1, callback=pairs_arg_callback)
@app.command()
@click.argument('pairs', nargs=-1)
@pairs_arg
@click.option('--force-delete/--no-force-delete',
help=('Do/Don\'t abort synchronization when all items are about '
'to be deleted from both sides.'))
@ -125,23 +148,22 @@ def sync(ctx, pairs, force_delete, max_workers):
from the pair "bob".
'''
from .tasks import prepare_pair, sync_collection
from .utils import parse_pairs_args, WorkerQueue
config = ctx.config
from .utils import WorkerQueue
wq = WorkerQueue(max_workers)
with wq.join():
for pair_name, collections in parse_pairs_args(pairs, config.pairs):
for pair_name, collections in pairs:
wq.put(functools.partial(prepare_pair, pair_name=pair_name,
collections=collections,
config=config,
config=ctx.config,
force_delete=force_delete,
callback=sync_collection))
wq.spawn_worker()
@app.command()
@click.argument('pairs', nargs=-1)
@pairs_arg
@max_workers_option
@pass_context
@catch_errors
@ -152,15 +174,15 @@ def metasync(ctx, pairs, max_workers):
See the `sync` command regarding the PAIRS argument.
'''
from .tasks import prepare_pair, metasync_collection
from .utils import parse_pairs_args, WorkerQueue
config = ctx.config
from .utils import WorkerQueue
wq = WorkerQueue(max_workers)
with wq.join():
for pair_name, collections in parse_pairs_args(pairs, config.pairs):
for pair_name, collections in pairs:
wq.put(functools.partial(prepare_pair, pair_name=pair_name,
collections=collections, config=config,
collections=collections,
config=ctx.config,
callback=metasync_collection))
wq.spawn_worker()

View file

@ -390,29 +390,6 @@ def handle_storage_init_error(cls, config):
problems=problems)
def parse_pairs_args(pairs_args, all_pairs):
'''
Expand the various CLI shortforms ("pair, pair/collection") to an iterable
of (pair, collections).
'''
rv = {}
for pair_and_collection in (pairs_args or all_pairs):
pair, collection = pair_and_collection, None
if '/' in pair:
pair, collection = pair.split('/')
if pair not in all_pairs:
raise CliError('Pair not found: {}\n'
'These are the pairs found: {}'
.format(pair, list(all_pairs)))
collections = rv.setdefault(pair, set())
if collection:
collections.add(collection)
return rv.items()
class WorkerQueue(object):
'''
A simple worker-queue setup.