1 | 1 |
new file mode 100755 |
... | ... |
@@ -0,0 +1,64 @@ |
1 |
+#!/usr/bin/env python |
|
2 |
+# -*- coding: utf-8 -*- |
|
3 |
+ |
|
4 |
+import os |
|
5 |
+import sys |
|
6 |
+import json |
|
7 |
+import errno |
|
8 |
+import subprocess |
|
9 |
+ |
|
10 |
+CONF_FILE = '~/.config/boobank-cacher/conf.json' |
|
11 |
+ |
|
12 |
+def get_conf_path(): |
|
13 |
+ return os.path.expanduser(CONF_FILE) |
|
14 |
+ |
|
15 |
+def get_conf(): |
|
16 |
+ with open(get_conf_path(), 'r') as filedesc: |
|
17 |
+ return json.load(filedesc) |
|
18 |
+ |
|
19 |
+def mkdir(path, mode): |
|
20 |
+ try: |
|
21 |
+ os.makedirs(path, mode) |
|
22 |
+ except OSError as exc: |
|
23 |
+ if exc.errno == errno.EEXIST and os.path.isdir(path): |
|
24 |
+ pass |
|
25 |
+ else: |
|
26 |
+ raise |
|
27 |
+ |
|
28 |
+def get_cache_path(conf): |
|
29 |
+ cache_path = conf.get('cache_path', '~/.cache/boobank-cacher/cache.json') |
|
30 |
+ return os.path.expanduser(cache_path) |
|
31 |
+ |
|
32 |
+def run_boobank(conf): |
|
33 |
+ boobank_path = os.path.expanduser(conf.get('boobank_path', 'boobank')) |
|
34 |
+ boobank_command = [boobank_path, 'list', '--formatter=json'] |
|
35 |
+ boobank_output = subprocess.check_output(boobank_command) |
|
36 |
+ return json.loads(boobank_output) |
|
37 |
+ |
|
38 |
+def process_additions(conf, accounts): |
|
39 |
+ additions = conf.get('additions', {}) |
|
40 |
+ if not additions: |
|
41 |
+ return accounts |
|
42 |
+ |
|
43 |
+ processed_accounts = [] |
|
44 |
+ for account in accounts: |
|
45 |
+ processed_accounts.append(account) |
|
46 |
+ account_id = account.get('id') |
|
47 |
+ if account_id is not None and account_id in additions: |
|
48 |
+ processed_accounts.append(additions.get(account_id)) |
|
49 |
+ return processed_accounts |
|
50 |
+ |
|
51 |
+def write_cache(conf, accounts): |
|
52 |
+ cache_path = get_cache_path(conf) |
|
53 |
+ mkdir(os.path.dirname(cache_path), 0700) |
|
54 |
+ with open(cache_path, 'w') as filedesc: |
|
55 |
+ json.dump(accounts, filedesc, indent=4) |
|
56 |
+ |
|
57 |
+def main(): |
|
58 |
+ conf = get_conf() |
|
59 |
+ accounts = run_boobank(conf) |
|
60 |
+ accounts = process_additions(conf, accounts) |
|
61 |
+ write_cache(conf, accounts) |
|
62 |
+ |
|
63 |
+if __name__ == '__main__': |
|
64 |
+ main() |