While checking boobank-cacher with pylint, it was deemed necesssary to move the
configuration file from boobank-cacher/conf.json to boobank-utils/cacher.json.
This way, it makes senses to expose a DEFAULT_CONF_DIR constant in the
boobank_utils module.
... | ... |
@@ -6,7 +6,7 @@ boobank-cacher: run 1 to n boobank commands and save their JSON output to file, |
6 | 6 |
along with a timestamp. boobank-cacher also handles "additions", i.e. the |
7 | 7 |
ability to insert simple, static data in the JSON output. boobank-cacher takes |
8 | 8 |
no command-line arguments: everything is read from |
9 |
-~/.config/boobank-cacher/conf.json. See the conf subdirectory for a sample |
|
9 |
+~/.config/boobank-utils/cacher.json. See the conf subdirectory for a sample |
|
10 | 10 |
configuration file. |
11 | 11 |
|
12 | 12 |
boobank-graphite: take a JSON file created by the "boobank list" command and |
... | ... |
@@ -1,23 +1,35 @@ |
1 | 1 |
#!/usr/bin/env python |
2 | 2 |
# -*- coding: utf-8 -*- |
3 |
+""" |
|
4 |
+boobank-cacher runs a list of boobank commands defined in its configuration |
|
5 |
+file. For each command, it has the ability to inject simple, additional data to |
|
6 |
+the result before saving it to a JSON file. |
|
7 |
+That JSON file can then act as a local cache, which other tools can leverage |
|
8 |
+for various purposes without emitting too many requests to bank websites. |
|
9 |
+""" |
|
3 | 10 |
|
4 | 11 |
import os |
5 | 12 |
import re |
6 |
-import sys |
|
7 | 13 |
import json |
8 | 14 |
import copy |
9 | 15 |
import time |
10 |
-import errno |
|
11 | 16 |
import argparse |
12 | 17 |
import subprocess |
13 |
-from boobank_utils import * |
|
18 |
+from boobank_utils import mkdir, DEFAULT_CONF_DIR |
|
14 | 19 |
|
15 | 20 |
def get_conf(conf_path): |
21 |
+ """ |
|
22 |
+ Return the contents of the boobank-cacher configuration. |
|
23 |
+ """ |
|
16 | 24 |
conf_path = os.path.expanduser(conf_path) |
17 | 25 |
with open(conf_path, 'r') as filedesc: |
18 | 26 |
return json.load(filedesc) |
19 | 27 |
|
20 | 28 |
def run_boobank(conf): |
29 |
+ """ |
|
30 |
+ Pipe the commands found in the given boobank-cacher configuration to a |
|
31 |
+ single boobank process and return their results. |
|
32 |
+ """ |
|
21 | 33 |
results = copy.deepcopy(conf) |
22 | 34 |
# Ensure there is at least one command to run: |
23 | 35 |
commands = results.get('commands', []) |
... | ... |
@@ -25,7 +37,8 @@ def run_boobank(conf): |
25 | 37 |
return results |
26 | 38 |
boobank_path = os.path.expanduser(conf.get('boobank_path', 'boobank')) |
27 | 39 |
boobank_command = [boobank_path, '--formatter=json'] |
28 |
- boobank_process = subprocess.Popen(boobank_command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, close_fds=True, bufsize=-1) |
|
40 |
+ boobank_process = subprocess.Popen(boobank_command, stdin=subprocess.PIPE, |
|
41 |
+ stdout=subprocess.PIPE, close_fds=True, bufsize=-1) |
|
29 | 42 |
# Send all commands at once: |
30 | 43 |
for command in commands: |
31 | 44 |
boobank_process.stdin.write(command['command'] + "\n") |
... | ... |
@@ -48,6 +61,10 @@ def run_boobank(conf): |
48 | 61 |
return results |
49 | 62 |
|
50 | 63 |
def process_additions(results): |
64 |
+ """ |
|
65 |
+ Inject additional data described in the boobank-cacher configuration to the |
|
66 |
+ given results. |
|
67 |
+ """ |
|
51 | 68 |
for command in results.get('commands', []): |
52 | 69 |
additions = command.get('additions', {}) |
53 | 70 |
result = command.get('result', {}) |
... | ... |
@@ -64,6 +81,10 @@ def process_additions(results): |
64 | 81 |
return results |
65 | 82 |
|
66 | 83 |
def write_cache(results): |
84 |
+ """ |
|
85 |
+ Write the given results to cache files as described in the boobank-cacher |
|
86 |
+ configuration. |
|
87 |
+ """ |
|
67 | 88 |
for command in results.get('commands', []): |
68 | 89 |
if 'result' not in command or 'cache_path' not in command: |
69 | 90 |
continue |
... | ... |
@@ -73,11 +94,20 @@ def write_cache(results): |
73 | 94 |
json.dump(command['result'], filedesc, indent=4) |
74 | 95 |
|
75 | 96 |
def parse_args(): |
76 |
- args_parser = argparse.ArgumentParser(description='Cache the output of boobank commands to JSON files.') |
|
77 |
- args_parser.add_argument('-c', '--config', default=DEFAULT_CONF_FILE, help='configuration file to use') |
|
97 |
+ """ |
|
98 |
+ Parse command-line arguments. |
|
99 |
+ """ |
|
100 |
+ description = 'Cache the output of boobank commands to JSON files.' |
|
101 |
+ args_parser = argparse.ArgumentParser(description=description) |
|
102 |
+ default_conf = os.path.join(DEFAULT_CONF_DIR, 'cacher.json') |
|
103 |
+ args_parser.add_argument('-c', '--config', default=default_conf, |
|
104 |
+ help='configuration file to use') |
|
78 | 105 |
return args_parser.parse_args() |
79 | 106 |
|
80 | 107 |
def main(): |
108 |
+ """ |
|
109 |
+ Main function. |
|
110 |
+ """ |
|
81 | 111 |
args = parse_args() |
82 | 112 |
conf = get_conf(args.config) |
83 | 113 |
results = run_boobank(conf) |