Sun, 21 Jan 2018 00:58:06 +0000
Errors as rumps notifications
NOTE: These seem to be broken in rumps, so there's a workaround.
But the whole app crashes on the notification callback, probably due to the
same brokenness. See https://github.com/jaredks/rumps/issues/59
0 | 1 | # |
2 | # Borgend configuration loader | |
3 | # | |
4 | ||
5 | import yaml | |
6 | import io | |
7 | import os | |
8 | import xdg | |
9 | import string | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
10 | import logging |
2 | 11 | from functools import reduce |
0 | 12 | |
2 | 13 | # |
14 | # Defaults | |
15 | # | |
16 | ||
17 | defaults={ | |
21 | 18 | # borg |
19 | # Default: backup every 6 hours (21600 seconds) | |
20 | 'backup_interval': 21600, | |
21 | # Default: retry every 15 minutes if unable to connect / unfinished backup | |
22 | 'retry_interval': 900, | |
23 | # Extract passphrases at startup or on demand? | |
24 | '__extract_passphrases_at_startup': True, | |
25 | # Borg settings | |
26 | 'borg': { | |
27 | 'executable': 'borg', | |
28 | 'common_parameters': [], | |
29 | 'create_parameters': [], | |
30 | 'prune_parameters': [], | |
2 | 31 | } |
32 | } | |
33 | ||
34 | ||
35 | # | |
36 | # Type checking etc. | |
37 | # | |
38 | ||
39 | def error(x): | |
40 | raise AssertionError(x) | |
41 | ||
42 | def check_string(cfg, field, descr, loc, default=None): | |
43 | return check_field(cfg, field, descr, loc, default, | |
44 | lambda x: isinstance(x, str)) | |
45 | ||
46 | def check_dict(cfg, field, descr, loc, default=None): | |
47 | return check_field(cfg, field, descr, loc, default, | |
48 | lambda x: isinstance(x, dict)) | |
49 | ||
50 | def check_list(cfg, field, descr, loc, default=None): | |
51 | return check_field(cfg, field, descr, loc, default, | |
52 | lambda x: isinstance(x, list)) | |
53 | ||
54 | def is_list_of(x, chk): | |
55 | if x is None: | |
56 | return True | |
57 | elif isinstance(x, list): | |
58 | return reduce(lambda y, z: y and chk(z), x, True) | |
59 | else: | |
60 | return False | |
61 | ||
62 | def check_list_of_dicts(cfg, field, descr, loc, default=None): | |
63 | return check_field(cfg, field, descr, loc, default, | |
64 | lambda x: is_list_of(x, lambda z: isinstance(z, dict))) | |
65 | ||
66 | def check_list_of_strings(cfg, field, descr, loc, default=None): | |
67 | return check_field(cfg, field, descr, loc, default, | |
68 | lambda x: is_list_of(x, lambda z: isinstance(z, str))) | |
69 | ||
70 | def check_nonempty_list_of_strings(cfg, field, descr, loc): | |
71 | return check_list_of_strings(cfg, field, descr, loc) and cfg[field] | |
72 | ||
73 | ||
74 | def check_nonneg_int(cfg, field, descr, loc, default=None): | |
75 | return check_field(cfg, field, descr, loc, default, | |
76 | lambda x: isinstance(x, int) and x>=0) | |
77 | ||
78 | def check_field(cfg, field, descr, loc, default, check): | |
79 | if field in cfg: | |
80 | tmp=cfg[field] | |
81 | if not check(tmp): | |
82 | error("%s is of invalid type for %s" % (field, loc)) | |
83 | return tmp | |
84 | else: | |
85 | if default is not None: | |
86 | return default | |
87 | else: | |
88 | error("%s is not configured for %s" % (field, loc)) | |
89 | ||
90 | # | |
91 | # Conversion of config into command line | |
92 | # | |
93 | ||
94 | def arglistify(args): | |
95 | flatten=lambda l: [item for sublist in l for item in sublist] | |
96 | if args is None: | |
97 | return [] | |
98 | else: | |
99 | return flatten([['--' + key, str(d[key])] for d in args for key in d]) | |
100 | ||
101 | # | |
102 | # Load config on module load | |
103 | # | |
0 | 104 | |
105 | def expand_env(cfg, env): | |
106 | if isinstance(cfg, dict): | |
107 | out={key: expand_env(val, env) for key, val in cfg.items()} | |
108 | elif isinstance(cfg, list): | |
109 | out=[expand_env(val, env) for val in cfg] | |
110 | elif isinstance(cfg, str): | |
111 | out=string.Template(cfg).substitute(os.environ) | |
112 | else: | |
2 | 113 | out=cfg |
114 | ||
0 | 115 | return out |
116 | ||
117 | cfgfile=os.path.join(xdg.XDG_CONFIG_HOME, "borgend", "config.yaml") | |
118 | ||
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
119 | logging.info("Reading configuration file %s" % cfgfile) |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
120 | |
0 | 121 | if not (os.path.exists(cfgfile) and os.path.isfile(cfgfile)): |
122 | raise SystemExit(f'Configuration file required: {cfgfile}') | |
123 | ||
124 | with io.open(cfgfile, 'r') as file: | |
125 | settings=expand_env(yaml.load(file), os.environ); | |
1
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
126 | |
2 | 127 | # |
128 | # Verify basic settings | |
129 | # | |
130 | ||
21 | 131 | def check_and_set(cfg, field, loc, defa, fn): |
132 | cfg[field]=fn(cfg, field, field, loc, defa[field]) | |
133 | ||
134 | def check_parameters(cmd): | |
135 | check_and_set(settings['borg'], cmd+'_parameters', | |
136 | 'borg', defaults['borg'], | |
137 | check_list_of_dicts) | |
2 | 138 | |
21 | 139 | check_and_set(settings, 'backup_interval', 'top-level', defaults, check_nonneg_int) |
140 | check_and_set(settings, 'retry_interval', 'top-level', defaults, check_nonneg_int) | |
141 | check_and_set(settings, '__extract_passphrases_at_startup', 'top-level', defaults, check_nonneg_int) | |
142 | check_and_set(settings, 'borg', 'top-level', defaults, check_dict) | |
143 | # Check parameters within 'borg' | |
144 | if True: | |
145 | check_and_set(settings['borg'], 'executable', 'borg', | |
146 | defaults['borg'], check_string) | |
2 | 147 | |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
148 | check_parameters('common') |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
149 | check_parameters('create') |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
150 | check_parameters('prune') |
2 | 151 | |
152 |