mirror of
https://github.com/samsonjs/vdirsyncer.git
synced 2026-04-27 14:57:41 +00:00
Kill parse_pairs_args
This commit is contained in:
parent
8e2070e42d
commit
2d62ec9a26
3 changed files with 32 additions and 49 deletions
|
|
@ -69,22 +69,6 @@ def test_storage_instance_from_config(monkeypatch):
|
||||||
assert cli.utils.storage_instance_from_config(config) == 'OK'
|
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):
|
def test_missing_general_section(read_config):
|
||||||
with pytest.raises(cli.CliError) as excinfo:
|
with pytest.raises(cli.CliError) as excinfo:
|
||||||
read_config(u'''
|
read_config(u'''
|
||||||
|
|
|
||||||
|
|
@ -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()
|
@app.command()
|
||||||
@click.argument('pairs', nargs=-1)
|
@pairs_arg
|
||||||
@click.option('--force-delete/--no-force-delete',
|
@click.option('--force-delete/--no-force-delete',
|
||||||
help=('Do/Don\'t abort synchronization when all items are about '
|
help=('Do/Don\'t abort synchronization when all items are about '
|
||||||
'to be deleted from both sides.'))
|
'to be deleted from both sides.'))
|
||||||
|
|
@ -125,23 +148,22 @@ def sync(ctx, pairs, force_delete, max_workers):
|
||||||
from the pair "bob".
|
from the pair "bob".
|
||||||
'''
|
'''
|
||||||
from .tasks import prepare_pair, sync_collection
|
from .tasks import prepare_pair, sync_collection
|
||||||
from .utils import parse_pairs_args, WorkerQueue
|
from .utils import WorkerQueue
|
||||||
config = ctx.config
|
|
||||||
|
|
||||||
wq = WorkerQueue(max_workers)
|
wq = WorkerQueue(max_workers)
|
||||||
|
|
||||||
with wq.join():
|
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,
|
wq.put(functools.partial(prepare_pair, pair_name=pair_name,
|
||||||
collections=collections,
|
collections=collections,
|
||||||
config=config,
|
config=ctx.config,
|
||||||
force_delete=force_delete,
|
force_delete=force_delete,
|
||||||
callback=sync_collection))
|
callback=sync_collection))
|
||||||
wq.spawn_worker()
|
wq.spawn_worker()
|
||||||
|
|
||||||
|
|
||||||
@app.command()
|
@app.command()
|
||||||
@click.argument('pairs', nargs=-1)
|
@pairs_arg
|
||||||
@max_workers_option
|
@max_workers_option
|
||||||
@pass_context
|
@pass_context
|
||||||
@catch_errors
|
@catch_errors
|
||||||
|
|
@ -152,15 +174,15 @@ def metasync(ctx, pairs, max_workers):
|
||||||
See the `sync` command regarding the PAIRS argument.
|
See the `sync` command regarding the PAIRS argument.
|
||||||
'''
|
'''
|
||||||
from .tasks import prepare_pair, metasync_collection
|
from .tasks import prepare_pair, metasync_collection
|
||||||
from .utils import parse_pairs_args, WorkerQueue
|
from .utils import WorkerQueue
|
||||||
config = ctx.config
|
|
||||||
|
|
||||||
wq = WorkerQueue(max_workers)
|
wq = WorkerQueue(max_workers)
|
||||||
|
|
||||||
with wq.join():
|
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,
|
wq.put(functools.partial(prepare_pair, pair_name=pair_name,
|
||||||
collections=collections, config=config,
|
collections=collections,
|
||||||
|
config=ctx.config,
|
||||||
callback=metasync_collection))
|
callback=metasync_collection))
|
||||||
wq.spawn_worker()
|
wq.spawn_worker()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -390,29 +390,6 @@ def handle_storage_init_error(cls, config):
|
||||||
problems=problems)
|
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):
|
class WorkerQueue(object):
|
||||||
'''
|
'''
|
||||||
A simple worker-queue setup.
|
A simple worker-queue setup.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue