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