Browse code

Pylint boobank-graphite.

Xavier G authored on10/11/2017 18:36:32
Showing1 changed files

... ...
@@ -1,49 +1,73 @@
1 1
 #!/usr/bin/env python
2 2
 # -*- coding: utf-8 -*-
3
+"""
4
+boobank-graphite takes a JSON file, as created by boobank-cacher when running
5
+and caching a "boobank list" command, and sends it to a Graphite instance.
6
+"""
3 7
 
4 8
 import sys
5 9
 import json
6 10
 import time
7 11
 import socket
8 12
 import argparse
13
+from boobank_utils import DEFAULT_CURRENCY
9 14
 
10 15
 
11 16
 def main():
17
+    """
18
+    Main function.
19
+    """
12 20
     args = parse_args()
13
-    accounts = get_accounts(args.json_file)
21
+    accounts = json.load(args.json_file)
14 22
     timestamp = time.time()
15
-    socket = graphite_connect(args.host, args.port)
23
+    graphite_socket = graphite_connect(args.host, args.port)
16 24
     for account in accounts.get('data', []):
17
-        send_account(socket, account, timestamp)
18
-    socket.close()
25
+        send_account(graphite_socket, account, timestamp)
26
+    graphite_socket.close()
19 27
 
20 28
 def parse_args():
21
-    args_parser = argparse.ArgumentParser(description='Send the contents of a boobank-cacher JSON file to Graphite.')
22
-    args_parser.add_argument('-gh', '--graphite-host', dest='host', default='::1', help='Graphite host; defaults to ::1.')
23
-    args_parser.add_argument('-gp', '--graphite-port', dest='port', default='2003', type=int, help='Graphite port; defaults to 2003.')
24
-    args_parser.add_argument('json_file', nargs='?', type=argparse.FileType('r'), default=sys.stdin, help='JSON file to send to Graphite; defaults to "-" (standard input).')
25
-    return args_parser.parse_args()
29
+    """
30
+    Parse command-line arguments.
31
+    """
32
+    description = 'Send the contents of a boobank-cacher JSON file to Graphite.'
33
+    args = argparse.ArgumentParser(description=description)
34
+    args.add_argument('-gh', '--graphite-host', dest='host', default='::1',
35
+                      help='Graphite host; defaults to ::1.')
36
+    args.add_argument('-gp', '--graphite-port', dest='port', default='2003', type=int,
37
+                      help='Graphite port; defaults to 2003.')
38
+    args.add_argument('json_file', nargs='?', type=argparse.FileType('r'), default=sys.stdin,
39
+                      help='JSON file to send to Graphite; defaults to "-" (standard input).')
40
+    return args.parse_args()
26 41
 
27 42
 def graphite_connect(host, port):
43
+    """
44
+    Connect to the Graphite instance listening on host and port.
45
+    """
28 46
     addr_infos = socket.getaddrinfo(host, port, 0, 0, socket.IPPROTO_TCP)
29 47
     _, _, _, _, sockaddr = addr_infos[0]
30 48
     graphite_socket = socket.socket(socket.AF_INET6, socket.SOCK_STREAM, 0)
31 49
     graphite_socket.connect(sockaddr)
32 50
     return graphite_socket
33 51
 
34
-def get_accounts(filedesc):
35
-    return json.load(filedesc)
36
-
37 52
 def get_path(account):
38
-    (id, bank) = account['id'].split('@')
39
-    currency = account.get('currency', 'EUR').lower()
40
-    return 'bank.%s.%s-balance-%s' % (bank, id, currency)
53
+    """
54
+    Return the path to send to Graphite for the given bank account.
55
+    The following scheme is used:
56
+    bank.<bank id>.<account id>-balance-<currency>
57
+    """
58
+    (account_id, bank) = account['id'].split('@')
59
+    currency = account.get('currency', DEFAULT_CURRENCY).lower()
60
+    return 'bank.%s.%s-balance-%s' % (bank, account_id, currency)
41 61
 
42
-def send_account(socket, account, timestamp):
62
+def send_account(graphite_socket, account, timestamp):
63
+    """
64
+    Send the balance of the given bank account to Graphite over the given TCP
65
+    socket.
66
+    """
43 67
     pattern = '%s %.2f %d\n'
44 68
     values = (get_path(account), account['balance'], int(timestamp))
45 69
     line = pattern % values
46
-    socket.send(line)
70
+    graphite_socket.send(line)
47 71
 
48 72
 if __name__ == '__main__':
49 73
     main()