--- a/config.py Fri Jan 26 10:35:00 2018 +0000 +++ b/config.py Fri Jan 26 19:04:04 2018 +0000 @@ -8,9 +8,9 @@ import os import string import logging -import borgend import platform from functools import reduce +import borgend logger=borgend.logger.getChild(__name__) @@ -59,6 +59,18 @@ def error(x): raise AssertionError(x) +def check_field(cfg, field, descr, loc, default, check): + if field in cfg: + tmp=cfg[field] + if not check(tmp): + error("%s is of invalid type for %s" % (field, loc)) + return tmp + else: + if default is not None: + return default + else: + error("%s is not configured for %s" % (field, loc)) + def check_bool(cfg, field, descr, loc, default=None): return check_field(cfg, field, descr, loc, default, lambda x: isinstance(x, bool)) @@ -99,28 +111,34 @@ return check_field(cfg, field, descr, loc, default, lambda x: isinstance(x, int) and x>=0) -def check_field(cfg, field, descr, loc, default, check): - if field in cfg: - tmp=cfg[field] - if not check(tmp): - error("%s is of invalid type for %s" % (field, loc)) - return tmp - else: - if default is not None: - return default - else: - error("%s is not configured for %s" % (field, loc)) # -# Conversion of config into command line +# Borg command line parameter configuration helper routines and classes # -def arglistify(args): - flatten=lambda l: [item for sublist in l for item in sublist] - if args is None: - return [] - else: - return flatten([['--' + key, str(d[key])] for d in args for key in d]) +class BorgParameters: + def __init__(self, common, create, prune): + self.common=common or [] + self.create=create or [] + self.prune=prune or [] + + def from_config(cfg, loc): + common=check_list_of_dicts(cfg, 'common_parameters', + 'Borg parameters', loc, default=[]) + + create=check_list_of_dicts(cfg, 'create_parameters', + 'Create parameters', loc, default=[]) + + prune=check_list_of_dicts(cfg, 'prune_parameters', + 'Prune parameters', loc, default=[]) + + return BorgParameters(common, create, prune) + + def __add__(self, other): + common=self.common+other.common + create=self.create+other.create + prune=self.prune+other.prune + return BorgParameters(common, create, prune) # # Load config on module load @@ -153,11 +171,6 @@ def check_and_set(cfg, field, loc, defa, fn): cfg[field]=fn(cfg, field, field, loc, defa[field]) -def check_parameters(cmd): - check_and_set(settings['borg'], cmd+'_parameters', - 'borg', defaults['borg'], - check_list_of_dicts) - check_and_set(settings, 'backup_interval', 'top-level', defaults, check_nonneg_int) check_and_set(settings, 'retry_interval', 'top-level', defaults, check_nonneg_int) check_and_set(settings, 'extract_passphrases_at_startup', 'top-level', defaults, check_nonneg_int) @@ -168,8 +181,5 @@ check_and_set(settings['borg'], 'executable', 'borg', defaults['borg'], check_string) - check_parameters('common') - check_parameters('create') - check_parameters('prune') + borg_parameters=BorgParameters.from_config(settings['borg'], "top-level") -