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