-
Notifications
You must be signed in to change notification settings - Fork 0
/
monzo-status
executable file
·131 lines (95 loc) · 3.92 KB
/
monzo-status
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python3
import os
import sys
import time
import json
import datetime
import yaml
import pwd
from monzo_utils.lib.config import Config
from monzo_utils.lib.db import DB
from monzo_utils.model.provider import Provider
from monzo_utils.model.account import Account
from monzo_utils.model.pot import Pot
from monzo_utils.model.transaction import Transaction
PROVIDER = 'Monzo'
FIELD_MAP = {
'date': 'created_at'
}
class Monzo:
widths = {}
def accounts(self, first=True):
transactions = False
n = 10
for i in range(0, len(sys.argv)):
if sys.argv[i] == '-t':
transactions = True
if i+1 < len(sys.argv) and sys.argv[i+1].isdigit():
n = int(sys.argv[i+1])
count = 0
providers = [Provider.one("select * from provider where name = %s", [PROVIDER])] + \
Provider.find("select * from provider where name != %s order by name asc", [PROVIDER])
for provider in providers:
for account in provider.accounts(order=Config().account_order if 'account_order' in Config().keys else None):
self.get_column_widths([account], account.keys)
for provider in providers:
accounts = provider.accounts(order=Config().account_order if 'account_order' in Config().keys else None)
for account in accounts:
if account.active == 0:
continue
self.display([account], account.keys, show_headers=(count == 0 or transactions))
if transactions:
sys.stdout.write("\n")
rows = list(reversed(account.transactions(orderby='created_at', orderdir='desc', limit=n)))
self.display(rows, Transaction.DISPLAY_KEYS)
sys.stdout.write("\n")
count += 1
def get_column_widths(self, data, columns):
for key in columns:
if key in ['balance','available']:
if key not in self.widths or self.widths[key] <8:
self.widths[key] = 8
else:
if key not in self.widths or self.widths[key] < len(key):
self.widths[key] = len(key)
for i in range(0, len(data)):
if type(data[i]) != dict:
obj = data[i].__dict__['attributes']
else:
obj = data[i]
for key in columns:
if key in FIELD_MAP:
obj[key] = obj[FIELD_MAP[key]]
if key not in obj or obj[key] is None:
obj[key] = ''
if obj[key] == datetime.datetime:
obj[key] = obj[key].strftime('%d.%m.%Y %H:%M')
if len(str(obj[key])) > self.widths[key]:
self.widths[key] = len(str(obj[key]))
def display(self, data, columns, show_headers=True):
self.get_column_widths(data, columns)
if show_headers:
for key in columns:
if key != 'name':
sys.stdout.write(self.capitalise(key).rjust(self.widths[key]+2))
else:
sys.stdout.write(self.capitalise(key).ljust(self.widths[key]+2))
sys.stdout.write("\n")
for key in columns:
sys.stdout.write('-' * (self.widths[key]+2))
sys.stdout.write("\n")
for i in range(0, len(data)):
if type(data[i]) != dict:
obj = data[i].__dict__['attributes']
else:
obj = data[i]
for key in columns:
if key != 'name':
sys.stdout.write(str(obj[key]).rjust(self.widths[key]+2))
else:
sys.stdout.write(str(obj[key]).ljust(self.widths[key]+2))
sys.stdout.write("\n")
def capitalise(self, string):
return string.title().replace('_','')
m = Monzo()
m.accounts()