Create Config object

This commit is contained in:
Markus Unterwaditzer 2015-08-20 15:19:25 +02:00
parent 32abaae9b9
commit ccb94a1c29
4 changed files with 37 additions and 31 deletions

View file

@ -126,16 +126,15 @@ def sync(ctx, pairs, force_delete, max_workers):
''' '''
from .tasks import prepare_pair, sync_collection from .tasks import prepare_pair, sync_collection
from .utils import parse_pairs_args, WorkerQueue from .utils import parse_pairs_args, WorkerQueue
general, all_pairs, all_storages = ctx.config 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, all_pairs): for pair_name, collections in parse_pairs_args(pairs, config.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,
general=general, all_pairs=all_pairs, config=config,
all_storages=all_storages,
force_delete=force_delete, force_delete=force_delete,
callback=sync_collection)) callback=sync_collection))
wq.spawn_worker() wq.spawn_worker()
@ -154,16 +153,14 @@ def metasync(ctx, pairs, max_workers):
''' '''
from .tasks import prepare_pair, metasync_collection from .tasks import prepare_pair, metasync_collection
from .utils import parse_pairs_args, WorkerQueue from .utils import parse_pairs_args, WorkerQueue
general, all_pairs, all_storages = ctx.config 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, all_pairs): for pair_name, collections in parse_pairs_args(pairs, config.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,
general=general, all_pairs=all_pairs,
all_storages=all_storages,
callback=metasync_collection)) callback=metasync_collection))
wq.spawn_worker() wq.spawn_worker()
@ -179,23 +176,28 @@ def discover(ctx, pairs, max_workers):
''' '''
from .tasks import discover_collections from .tasks import discover_collections
from .utils import WorkerQueue from .utils import WorkerQueue
general, all_pairs, all_storages = ctx.config config = ctx.config
wq = WorkerQueue(max_workers) wq = WorkerQueue(max_workers)
with wq.join(): with wq.join():
for pair in (pairs or all_pairs): for pair in (pairs or config.pairs):
try: try:
name_a, name_b, pair_options = all_pairs[pair] name_a, name_b, pair_options = config.pairs[pair]
except KeyError: except KeyError:
raise CliError('Pair not found: {}\n' raise CliError('Pair not found: {}\n'
'These are the pairs found: {}' 'These are the pairs found: {}'
.format(pair, list(all_pairs))) .format(pair, list(config.pairs)))
wq.put(functools.partial( wq.put(functools.partial(
discover_collections, status_path=general['status_path'], discover_collections,
name_a=name_a, name_b=name_b, pair_name=pair, status_path=config.general['status_path'],
config_a=all_storages[name_a], config_b=all_storages[name_b], name_a=name_a,
pair_options=pair_options, skip_cache=True name_b=name_b,
pair_name=pair,
config_a=config.storages[name_a],
config_b=config.storages[name_b],
pair_options=pair_options,
skip_cache=True,
)) ))
wq.spawn_worker() wq.spawn_worker()
@ -217,13 +219,12 @@ def repair(ctx, collection):
collection of the `calendars_local` storage. collection of the `calendars_local` storage.
''' '''
from .tasks import repair_collection from .tasks import repair_collection
general, all_pairs, all_storages = ctx.config
cli_logger.warning('This operation will take a very long time.') cli_logger.warning('This operation will take a very long time.')
cli_logger.warning('It\'s recommended to turn off other client\'s ' cli_logger.warning('It\'s recommended to turn off other client\'s '
'synchronization features.') 'synchronization features.')
click.confirm('Do you want to continue?', abort=True) click.confirm('Do you want to continue?', abort=True)
repair_collection(general, all_pairs, all_storages, collection) repair_collection(ctx.config, collection)
# Not sure if useful. I originally wanted it because: # Not sure if useful. I originally wanted it because:
# * my password manager has a timeout for caching the master password # * my password manager has a timeout for caching the master password

View file

@ -79,7 +79,7 @@ def load_config():
raise CliError('Error during reading config {}: {}' raise CliError('Error during reading config {}: {}'
.format(fname, e)) .format(fname, e))
return general, pairs, storages return Config(general, pairs, storages)
def read_config(f): def read_config(f):
@ -175,3 +175,10 @@ def parse_options(items, section=None):
except ValueError as e: except ValueError as e:
raise ValueError('Section "{}", option "{}": {}' raise ValueError('Section "{}", option "{}": {}'
.format(section, key, e)) .format(section, key, e))
class Config(object):
def __init__(self, general, pairs, storages):
self.general = general
self.pairs = pairs
self.storages = storages

View file

@ -10,19 +10,18 @@ from .utils import CliError, JobFailed, cli_logger, collections_for_pair, \
from ..sync import sync from ..sync import sync
def prepare_pair(wq, pair_name, collections, general, all_pairs, all_storages, def prepare_pair(wq, pair_name, collections, config, callback, **kwargs):
callback, **kwargs): a_name, b_name, pair_options = config.pairs[pair_name]
a_name, b_name, pair_options = all_pairs[pair_name]
try: try:
config_a, config_b = all_storages[a_name], all_storages[b_name] config_a, config_b = config.storages[a_name], config.storages[b_name]
except KeyError as e: except KeyError as e:
raise CliError('Pair {}: Storage {} not found. These are the ' raise CliError('Pair {}: Storage {} not found. These are the '
'configured storages: {}' 'configured storages: {}'
.format(pair_name, str(e), list(all_storages))) .format(pair_name, str(e), list(config.storages)))
all_collections = dict(collections_for_pair( all_collections = dict(collections_for_pair(
general['status_path'], a_name, b_name, pair_name, config.general['status_path'], a_name, b_name, pair_name,
config_a, config_b, pair_options config_a, config_b, pair_options
)) ))
@ -39,7 +38,7 @@ def prepare_pair(wq, pair_name, collections, general, all_pairs, all_storages,
wq.put(functools.partial( wq.put(functools.partial(
callback, pair_name=pair_name, collection=collection, callback, pair_name=pair_name, collection=collection,
config_a=config_a, config_b=config_b, pair_options=pair_options, config_a=config_a, config_b=config_b, pair_options=pair_options,
general=general, **kwargs general=config.general, **kwargs
)) ))
for i in range(new_workers): for i in range(new_workers):
@ -81,14 +80,14 @@ def discover_collections(wq, pair_name, **kwargs):
.format(pair_name, json.dumps(collections))) .format(pair_name, json.dumps(collections)))
def repair_collection(general, all_pairs, all_storages, collection): def repair_collection(config, collection):
from ..repair import repair_storage from ..repair import repair_storage
storage_name, collection = collection, None storage_name, collection = collection, None
if '/' in storage_name: if '/' in storage_name:
storage_name, collection = storage_name.split('/') storage_name, collection = storage_name.split('/')
config = all_storages[storage_name] config = config.storages[storage_name]
storage_type = config['type'] storage_type = config['type']
if collection is not None: if collection is not None:

View file

@ -125,8 +125,7 @@ def _password_from_command(username, host):
return None return None
try: try:
general, _, _ = ctx.config command = ctx.config.general['password_command'].split()
command = general['password_command'].split()
except KeyError: except KeyError:
return None return None