... | ... |
@@ -1,25 +1,33 @@ |
1 | 1 |
#!/usr/bin/env python |
2 | 2 |
# -*- coding: utf-8 -*- |
3 |
+""" |
|
4 |
+boobank-accounts takes one or several JSON files, as created by boobank-cacher |
|
5 |
+when running and caching a "boobank list" command, and display their contents. |
|
6 |
+""" |
|
3 | 7 |
|
4 |
-import os |
|
5 |
-import sys |
|
6 |
-import json |
|
7 | 8 |
import time |
8 | 9 |
import argparse |
9 | 10 |
from prettytable import PrettyTable |
10 | 11 |
from termcolor import colored |
11 |
-from boobank_utils import * |
|
12 |
+from boobank_utils import format_amount, format_timestamp_colored, load_json |
|
13 |
+from boobank_utils import DEFAULT_CURRENCY, COLORING_THRESHOLD, MAX_AGE |
|
12 | 14 |
|
13 | 15 |
def parse_args(): |
14 |
- args_parser = argparse.ArgumentParser(description='Display the contents of cached "boobank list" JSON files.') |
|
16 |
+ """ |
|
17 |
+ Parse command-line arguments. |
|
18 |
+ """ |
|
19 |
+ description = 'Display the contents of cached "boobank list" JSON files.' |
|
20 |
+ args_parser = argparse.ArgumentParser(description) |
|
15 | 21 |
args_parser.add_argument('json_file', nargs='*', default=['-'], help='JSON files to display') |
16 | 22 |
return args_parser.parse_args() |
17 | 23 |
|
18 | 24 |
def main(): |
25 |
+ """ |
|
26 |
+ Main function. |
|
27 |
+ """ |
|
19 | 28 |
args = parse_args() |
20 | 29 |
table = get_table() |
21 | 30 |
total = 0 |
22 |
- timestamps = {} |
|
23 | 31 |
filepaths = set(args.json_file) |
24 | 32 |
for filepath in filepaths: |
25 | 33 |
accounts = load_json(filepath) |
... | ... |
@@ -34,21 +42,27 @@ def main(): |
34 | 42 |
print(total_line) |
35 | 43 |
|
36 | 44 |
def get_table(): |
37 |
- pt = PrettyTable() |
|
38 |
- pt.field_names = ['Account ID', 'Account name', 'Balance', 'Data cached on'] |
|
39 |
- pt.field_names = [colored(x, 'white', attrs=['bold']) for x in pt.field_names] |
|
40 |
- pt.align = 'r' |
|
41 |
- pt.align[pt.field_names[1]] = 'l' |
|
42 |
- pt.align[pt.field_names[3]] = 'c' |
|
43 |
- return pt |
|
45 |
+ """ |
|
46 |
+ Create and return the PrettyTable instance used to display data. |
|
47 |
+ """ |
|
48 |
+ ptable = PrettyTable() |
|
49 |
+ ptable.field_names = ['Account ID', 'Account name', 'Balance', 'Data cached on'] |
|
50 |
+ ptable.field_names = [colored(x, 'white', attrs=['bold']) for x in ptable.field_names] |
|
51 |
+ ptable.align = 'r' |
|
52 |
+ ptable.align[ptable.field_names[1]] = 'l' |
|
53 |
+ ptable.align[ptable.field_names[3]] = 'c' |
|
54 |
+ return ptable |
|
44 | 55 |
|
45 | 56 |
def add_account(table, account, cache_timestamp): |
46 |
- id = colored(account.get('id'), 'cyan') |
|
57 |
+ """ |
|
58 |
+ Add the given bank account to the given PrettyTable instance. |
|
59 |
+ """ |
|
60 |
+ account_id = colored(account.get('id'), 'cyan') |
|
47 | 61 |
label = colored(account.get('label'), 'cyan') |
48 | 62 |
currency = account.get('currency', DEFAULT_CURRENCY) |
49 | 63 |
balance = format_amount(account['balance'], currency, COLORING_THRESHOLD) |
50 | 64 |
cache_date = format_timestamp_colored(cache_timestamp, MAX_AGE) |
51 |
- table.add_row([id, label, balance, cache_date]) |
|
65 |
+ table.add_row([account_id, label, balance, cache_date]) |
|
52 | 66 |
|
53 | 67 |
if __name__ == '__main__': |
54 | 68 |
main() |