From 2d62ec9a26416d00e95391c64a5233ba5a75c906 Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Thu, 20 Aug 2015 17:18:23 +0200 Subject: [PATCH] Kill parse_pairs_args --- tests/cli/test_config.py | 16 --------------- vdirsyncer/cli/__init__.py | 42 +++++++++++++++++++++++++++++--------- vdirsyncer/cli/utils.py | 23 --------------------- 3 files changed, 32 insertions(+), 49 deletions(-) diff --git a/tests/cli/test_config.py b/tests/cli/test_config.py index 3e32760..0a76d39 100644 --- a/tests/cli/test_config.py +++ b/tests/cli/test_config.py @@ -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''' diff --git a/vdirsyncer/cli/__init__.py b/vdirsyncer/cli/__init__.py index 8c51870..7a2d9ed 100644 --- a/vdirsyncer/cli/__init__.py +++ b/vdirsyncer/cli/__init__.py @@ -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() diff --git a/vdirsyncer/cli/utils.py b/vdirsyncer/cli/utils.py index e4d0a18..3638f80 100644 --- a/vdirsyncer/cli/utils.py +++ b/vdirsyncer/cli/utils.py @@ -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.