config.py

changeset 74
4f56142e7497
parent 43
8f3ac19f11b6
child 79
b075b3db3044
equal deleted inserted replaced
73:4f0e9cf8f230 74:4f56142e7497
6 import yaml 6 import yaml
7 import io 7 import io
8 import os 8 import os
9 import string 9 import string
10 import logging 10 import logging
11 import borgend
12 import platform 11 import platform
13 from functools import reduce 12 from functools import reduce
13 import borgend
14 14
15 logger=borgend.logger.getChild(__name__) 15 logger=borgend.logger.getChild(__name__)
16 16
17 # 17 #
18 # Defaults 18 # Defaults
57 # 57 #
58 58
59 def error(x): 59 def error(x):
60 raise AssertionError(x) 60 raise AssertionError(x)
61 61
62 def check_field(cfg, field, descr, loc, default, check):
63 if field in cfg:
64 tmp=cfg[field]
65 if not check(tmp):
66 error("%s is of invalid type for %s" % (field, loc))
67 return tmp
68 else:
69 if default is not None:
70 return default
71 else:
72 error("%s is not configured for %s" % (field, loc))
73
62 def check_bool(cfg, field, descr, loc, default=None): 74 def check_bool(cfg, field, descr, loc, default=None):
63 return check_field(cfg, field, descr, loc, default, 75 return check_field(cfg, field, descr, loc, default,
64 lambda x: isinstance(x, bool)) 76 lambda x: isinstance(x, bool))
65 77
66 def check_string(cfg, field, descr, loc, default=None): 78 def check_string(cfg, field, descr, loc, default=None):
97 109
98 def check_nonneg_int(cfg, field, descr, loc, default=None): 110 def check_nonneg_int(cfg, field, descr, loc, default=None):
99 return check_field(cfg, field, descr, loc, default, 111 return check_field(cfg, field, descr, loc, default,
100 lambda x: isinstance(x, int) and x>=0) 112 lambda x: isinstance(x, int) and x>=0)
101 113
102 def check_field(cfg, field, descr, loc, default, check):
103 if field in cfg:
104 tmp=cfg[field]
105 if not check(tmp):
106 error("%s is of invalid type for %s" % (field, loc))
107 return tmp
108 else:
109 if default is not None:
110 return default
111 else:
112 error("%s is not configured for %s" % (field, loc))
113 114
114 # 115 #
115 # Conversion of config into command line 116 # Borg command line parameter configuration helper routines and classes
116 # 117 #
117 118
118 def arglistify(args): 119 class BorgParameters:
119 flatten=lambda l: [item for sublist in l for item in sublist] 120 def __init__(self, common, create, prune):
120 if args is None: 121 self.common=common or []
121 return [] 122 self.create=create or []
122 else: 123 self.prune=prune or []
123 return flatten([['--' + key, str(d[key])] for d in args for key in d]) 124
125 def from_config(cfg, loc):
126 common=check_list_of_dicts(cfg, 'common_parameters',
127 'Borg parameters', loc, default=[])
128
129 create=check_list_of_dicts(cfg, 'create_parameters',
130 'Create parameters', loc, default=[])
131
132 prune=check_list_of_dicts(cfg, 'prune_parameters',
133 'Prune parameters', loc, default=[])
134
135 return BorgParameters(common, create, prune)
136
137 def __add__(self, other):
138 common=self.common+other.common
139 create=self.create+other.create
140 prune=self.prune+other.prune
141 return BorgParameters(common, create, prune)
124 142
125 # 143 #
126 # Load config on module load 144 # Load config on module load
127 # 145 #
128 146
151 # 169 #
152 170
153 def check_and_set(cfg, field, loc, defa, fn): 171 def check_and_set(cfg, field, loc, defa, fn):
154 cfg[field]=fn(cfg, field, field, loc, defa[field]) 172 cfg[field]=fn(cfg, field, field, loc, defa[field])
155 173
156 def check_parameters(cmd):
157 check_and_set(settings['borg'], cmd+'_parameters',
158 'borg', defaults['borg'],
159 check_list_of_dicts)
160
161 check_and_set(settings, 'backup_interval', 'top-level', defaults, check_nonneg_int) 174 check_and_set(settings, 'backup_interval', 'top-level', defaults, check_nonneg_int)
162 check_and_set(settings, 'retry_interval', 'top-level', defaults, check_nonneg_int) 175 check_and_set(settings, 'retry_interval', 'top-level', defaults, check_nonneg_int)
163 check_and_set(settings, 'extract_passphrases_at_startup', 'top-level', defaults, check_nonneg_int) 176 check_and_set(settings, 'extract_passphrases_at_startup', 'top-level', defaults, check_nonneg_int)
164 check_and_set(settings, 'no_quit_menu_entry', 'top-level', defaults, check_bool) 177 check_and_set(settings, 'no_quit_menu_entry', 'top-level', defaults, check_bool)
165 check_and_set(settings, 'borg', 'top-level', defaults, check_dict) 178 check_and_set(settings, 'borg', 'top-level', defaults, check_dict)
166 # Check parameters within 'borg' 179 # Check parameters within 'borg'
167 if True: 180 if True:
168 check_and_set(settings['borg'], 'executable', 'borg', 181 check_and_set(settings['borg'], 'executable', 'borg',
169 defaults['borg'], check_string) 182 defaults['borg'], check_string)
170 183
171 check_parameters('common') 184 borg_parameters=BorgParameters.from_config(settings['borg'], "top-level")
172 check_parameters('create')
173 check_parameters('prune')
174 185
175

mercurial