1 | 1 |
new file mode 100755 |
... | ... |
@@ -0,0 +1,73 @@ |
1 |
+#!/usr/bin/env python |
|
2 |
+# -*- coding: utf-8 -*- |
|
3 |
+ |
|
4 |
+import sys |
|
5 |
+import json |
|
6 |
+from prettytable import PrettyTable |
|
7 |
+from termcolor import colored |
|
8 |
+ |
|
9 |
+UNUSUAL_BALANCE_THRESHOLD = 500.0 |
|
10 |
+WORRYING_BALANCE_THRESHOLD = 100.0 |
|
11 |
+HOLYSHIT_BALANCE_THRESHOLD = 0 |
|
12 |
+ |
|
13 |
+CURRENCIES = { |
|
14 |
+ 'EUR': '€', |
|
15 |
+ 'USD': '$', |
|
16 |
+ 'GBP': '£', |
|
17 |
+ 'YEN': '¥' |
|
18 |
+} |
|
19 |
+ |
|
20 |
+def main(): |
|
21 |
+ accounts = get_accounts() |
|
22 |
+ table = get_table() |
|
23 |
+ total = 0 |
|
24 |
+ for account in accounts: |
|
25 |
+ add_account(table, account) |
|
26 |
+ total += account.get('balance') |
|
27 |
+ print(table) |
|
28 |
+ print (' ' * 60) + colored('Total: ', 'white', attrs=['bold']) + format_balance({'balance': total}) |
|
29 |
+ |
|
30 |
+def get_accounts(): |
|
31 |
+ try: |
|
32 |
+ accounts_json_path = sys.argv[1] |
|
33 |
+ except IndexError: |
|
34 |
+ accounts_json_path = '-' |
|
35 |
+ if accounts_json_path == '-': |
|
36 |
+ return json.load(sys.stdin) |
|
37 |
+ else: |
|
38 |
+ with open(accounts_json_path, 'r') as filedesc: |
|
39 |
+ return json.load(filedesc) |
|
40 |
+ |
|
41 |
+def get_table(): |
|
42 |
+ pt = PrettyTable() |
|
43 |
+ pt.field_names = ['Account ID', 'Account name', 'Balance'] |
|
44 |
+ pt.field_names = [colored(x, 'white', attrs=['bold']) for x in pt.field_names] |
|
45 |
+ pt.align = 'r' |
|
46 |
+ pt.align[pt.field_names[1]] = 'l' |
|
47 |
+ return pt |
|
48 |
+ |
|
49 |
+def add_account(table, account): |
|
50 |
+ id = colored(account.get('id'), 'cyan') |
|
51 |
+ label = colored(account.get('label'), 'cyan') |
|
52 |
+ balance = format_balance(account) |
|
53 |
+ table.add_row([id, label, balance]) |
|
54 |
+ |
|
55 |
+def format_balance(account): |
|
56 |
+ balance = account.get('balance') |
|
57 |
+ currency = account.get('currency', 'EUR') |
|
58 |
+ balance_format = '%12.2f ' |
|
59 |
+ balance_format += CURRENCIES.get(currency, currency) |
|
60 |
+ |
|
61 |
+ balance_color = 'green' |
|
62 |
+ balance_attr = [] |
|
63 |
+ if balance < HOLYSHIT_BALANCE_THRESHOLD: |
|
64 |
+ balance_color = 'red' |
|
65 |
+ balance_attr = ['blink'] |
|
66 |
+ elif balance < WORRYING_BALANCE_THRESHOLD: |
|
67 |
+ balance_color = 'red' |
|
68 |
+ elif balance < UNUSUAL_BALANCE_THRESHOLD: |
|
69 |
+ balance_color = 'yellow' |
|
70 |
+ return colored(balance_format % balance, balance_color, attrs=balance_attr) |
|
71 |
+ |
|
72 |
+if __name__ == '__main__': |
|
73 |
+ main() |