Browse code

boobank-accounts now handle several input files.

Xavier G authored on05/11/2017 18:38:44
Showing1 changed files

... ...
@@ -11,6 +11,7 @@ from termcolor import colored
11 11
 UNUSUAL_BALANCE_THRESHOLD  = 500.0
12 12
 WORRYING_BALANCE_THRESHOLD = 100.0
13 13
 HOLYSHIT_BALANCE_THRESHOLD = 0
14
+MAX_AGE = 24 * 60 * 60
14 15
 
15 16
 CURRENCIES = {
16 17
     'EUR': '€',
... ...
@@ -20,26 +21,23 @@ CURRENCIES = {
20 21
 }
21 22
 
22 23
 def main():
23
-    accounts = get_accounts()
24 24
     table = get_table()
25 25
     total = 0
26
-    for account in accounts.get('data', []):
27
-        add_account(table, account)
28
-        total += account.get('balance')
26
+    timestamps = {}
27
+    filepaths = set(sys.argv[1:])
28
+    for filepath in filepaths:
29
+        accounts = get_accounts(filepath)
30
+        timestamp = accounts.get('timestamp', time.time())
31
+        for account in accounts.get('data', []):
32
+            add_account(table, account, timestamp)
33
+            total += account.get('balance')
29 34
     print(table)
30
-    timestamp = accounts.get('timestamp', time.time())
31
-    humandate = time.strftime('%A %Y-%m-%d %H:%M:%S %Z', time.localtime(timestamp))
32
-    date_info = ' Data cached on %s' % humandate
33
-    last_line = '%-60s' % date_info
34
-    last_line += colored('Total: ', 'white', attrs=['bold'])
35
-    last_line += format_balance({'balance': total})
36
-    print(last_line)
35
+    total_line = '%-60s' % ''
36
+    total_line += colored('Total: ', 'white', attrs=['bold'])
37
+    total_line += format_balance({'balance': total})
38
+    print(total_line)
37 39
 
38
-def get_accounts():
39
-    try:
40
-        accounts_json_path = sys.argv[1]
41
-    except IndexError:
42
-        accounts_json_path = os.path.expanduser('~/.cache/boobank-cacher/cache.json')
40
+def get_accounts(accounts_json_path):
43 41
     if accounts_json_path == '-':
44 42
         return json.load(sys.stdin)
45 43
     else:
... ...
@@ -48,17 +46,19 @@ def get_accounts():
48 46
 
49 47
 def get_table():
50 48
     pt = PrettyTable()
51
-    pt.field_names = ['Account ID', 'Account name', 'Balance']
49
+    pt.field_names = ['Account ID', 'Account name', 'Balance', 'Data cached on']
52 50
     pt.field_names = [colored(x, 'white', attrs=['bold']) for x in pt.field_names]
53 51
     pt.align = 'r'
54 52
     pt.align[pt.field_names[1]] = 'l'
53
+    pt.align[pt.field_names[3]] = 'c'
55 54
     return pt
56 55
 
57
-def add_account(table, account):
56
+def add_account(table, account, cache_timestamp):
58 57
     id = colored(account.get('id'), 'cyan')
59 58
     label = colored(account.get('label'), 'cyan')
60 59
     balance = format_balance(account)
61
-    table.add_row([id, label, balance])
60
+    cache_date = format_timestamp_colored(cache_timestamp, MAX_AGE)
61
+    table.add_row([id, label, balance, cache_date])
62 62
 
63 63
 def format_balance(account):
64 64
     balance = account.get('balance')
... ...
@@ -77,5 +77,13 @@ def format_balance(account):
77 77
         balance_color = 'yellow'
78 78
     return colored(balance_format % balance, balance_color, attrs=balance_attr)
79 79
 
80
+def format_timestamp_colored(timestamp, max_age):
81
+    result = format_timestamp(timestamp)
82
+    color = 'red' if timestamp + max_age < time.time() else 'green'
83
+    return colored(result, color)
84
+
85
+def format_timestamp(timestamp):
86
+    return time.strftime('%A %Y-%m-%d %H:%M:%S %Z', time.localtime(timestamp))
87
+
80 88
 if __name__ == '__main__':
81 89
     main()