Browse code

Add a timestamp to cached data.

Xavier G authored on29/10/2017 20:41:05
Showing2 changed files

... ...
@@ -3,6 +3,7 @@
3 3
 
4 4
 import sys
5 5
 import json
6
+import time
6 7
 from prettytable import PrettyTable
7 8
 from termcolor import colored
8 9
 
... ...
@@ -21,11 +22,17 @@ def main():
21 22
     accounts = get_accounts()
22 23
     table = get_table()
23 24
     total = 0
24
-    for account in accounts:
25
+    for account in accounts.get('data', []):
25 26
         add_account(table, account)
26 27
         total += account.get('balance')
27 28
     print(table)
28
-    print (' ' * 60) + colored('Total: ', 'white', attrs=['bold']) + format_balance({'balance': total})
29
+    timestamp = accounts.get('timestamp', time.time())
30
+    humandate = time.strftime('%A %Y-%m-%d %H:%M:%S %Z', time.localtime(timestamp))
31
+    date_info = ' Data cached on %s' % humandate
32
+    last_line = '%-60s' % date_info
33
+    last_line += colored('Total: ', 'white', attrs=['bold'])
34
+    last_line += format_balance({'balance': total})
35
+    print(last_line)
29 36
 
30 37
 def get_accounts():
31 38
     try:
... ...
@@ -4,6 +4,7 @@
4 4
 import os
5 5
 import sys
6 6
 import json
7
+import time
7 8
 import errno
8 9
 import subprocess
9 10
 
... ...
@@ -33,7 +34,8 @@ def run_boobank(conf):
33 34
     boobank_path = os.path.expanduser(conf.get('boobank_path', 'boobank'))
34 35
     boobank_command = [boobank_path, 'list', '--formatter=json']
35 36
     boobank_output = subprocess.check_output(boobank_command)
36
-    return json.loads(boobank_output)
37
+    boobank_data = json.loads(boobank_output)
38
+    return {'timestamp': time.time(), 'data': boobank_data}
37 39
 
38 40
 def process_additions(conf, accounts):
39 41
     additions = conf.get('additions', {})
... ...
@@ -57,7 +59,7 @@ def write_cache(conf, accounts):
57 59
 def main():
58 60
     conf = get_conf()
59 61
     accounts = run_boobank(conf)
60
-    accounts = process_additions(conf, accounts)
62
+    accounts['data'] = process_additions(conf, accounts['data'])
61 63
     write_cache(conf, accounts)
62 64
 
63 65
 if __name__ == '__main__':