Improve CLI

This commit is contained in:
Markus Unterwaditzer 2014-03-13 20:15:31 +01:00
parent 8db16454f8
commit 18fe8096f5

View file

@ -101,6 +101,33 @@ def main():
_main(env, cfg) _main(env, cfg)
def parse_pairs_args(pairs_args, all_pairs):
if not pairs_args:
pairs_args = list(all_pairs)
for pair_and_collection in pairs_args:
pair, collection = pair_and_collection, None
if '/' in pair:
pair, collection = pair.split('/')
try:
a_name, b_name, pair_options, storage_defaults = \
all_pairs[pair]
except KeyError:
cli_logger.critical('Pair not found: {}'.format(pair))
cli_logger.critical('These are the pairs found: ')
cli_logger.critical(list(all_pairs))
sys.exit(1)
if collection is None:
collections = [x.strip() for x in
pair_options.get('collections', '').split(',')]
else:
collections = [collection]
for c in collections:
yield pair, c
def _main(env, file_cfg): def _main(env, file_cfg):
general, all_pairs, all_storages = file_cfg general, all_pairs, all_storages = file_cfg
app = argvard.Argvard() app = argvard.Argvard()
@ -124,43 +151,40 @@ def _main(env, file_cfg):
@sync_command.main('[pairs...]') @sync_command.main('[pairs...]')
def sync_main(context, pairs=None): def sync_main(context, pairs=None):
'''Syncronize the given pairs. If no pairs are given, all will be '''
synchronized.''' Syncronize the given pairs. If no pairs are given, all will be
if pairs is None: synchronized.
pairs = list(all_pairs)
Examples:
`vdirsyncer sync` will sync everything configured.
`vdirsyncer sync bob frank` will sync the pairs "bob" and "frank".
`vdirsyncer sync bob/first_collection` will sync "first_collection"
from the pair "bob".
'''
actions = [] actions = []
for pair_name in pairs: for pair_name, collection in parse_pairs_args(pairs, all_pairs):
try: a_name, b_name, pair_options, storage_defaults = \
a_name, b_name, pair_options, storage_defaults = \ all_pairs[pair_name]
all_pairs[pair_name] if collection:
except KeyError: storage_defaults['collection'] = collection
cli_logger.critical('Pair not found: {}'.format(pair_name)) config_a = dict(storage_defaults)
cli_logger.critical('These are the pairs found: ') config_a.update(all_storages[a_name])
cli_logger.critical(list(all_pairs)) config_b = dict(storage_defaults)
sys.exit(1) config_b.update(all_storages[b_name])
collections = pair_options.get('collections', '').split(',') a = storage_instance_from_config(config_a)
for collection in collections: b = storage_instance_from_config(config_b)
collection = collection.strip()
if collection:
storage_defaults['collection'] = collection
config_a = dict(storage_defaults)
config_a.update(all_storages[a_name])
config_b = dict(storage_defaults)
config_b.update(all_storages[b_name])
a = storage_instance_from_config(config_a)
b = storage_instance_from_config(config_b)
def x(a=a, b=b, pair_name=pair_name, collection=collection): def x(a=a, b=b, pair_name=pair_name, collection=collection):
status_name = \ status_name = \
'_'.join(filter(bool, (pair_name, collection))) '_'.join(filter(bool, (pair_name, collection)))
pair_description = \ pair_description = \
' from '.join(filter(bool, (collection, pair_name))) ' from '.join(filter(bool, (collection, pair_name)))
cli_logger.debug('Syncing {}'.format(pair_description)) cli_logger.debug('Syncing {}'.format(pair_description))
status = load_status(general['status_path'], status_name) status = load_status(general['status_path'], status_name)
sync(a, b, status, sync(a, b, status,
pair_options.get('conflict_resolution', None)) pair_options.get('conflict_resolution', None))
save_status(general['status_path'], status_name, status) save_status(general['status_path'], status_name, status)
actions.append(x) actions.append(x)
for action in actions: for action in actions:
action() action()