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