/
bank.py
162 lines (110 loc) · 4.03 KB
/
bank.py
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# For the GUI
from tkinter import * # pylint: disable=unused-wildcard-import
# import tkFileDialog
# import tkMessageBox
from tkinter import filedialog, messagebox
from ofxparse import OfxParser
import csv
import sys
import time
import re
# from pprint import pprint
my_name = "py-bank-manager "
my_version = "Version 0.1 "
my_author = "Andrew Robinson "
full_list = []
cats_dict = {}
def new_file():
global full_list
full_list = []
print("New File!")
def read_cats():
global cats_dict
name = filedialog.askopenfilename()
with open(name, 'r') as foo:
reader = csv.reader(foo)
cats_dict = {}
for k, v in reader:
cats_dict[k] = v
print("meeowwww!!")
def apply_cats():
global full_list
for entry in full_list:
if entry["Description"] not in cats_dict:
cats_dict[entry["Description"]] = ""
entry["Category"] = cats_dict[entry["Description"]]
print('woooooof!!')
def read_ofx():
global full_list
names = filedialog.askopenfilenames()
for name in names:
print(name)
with open(name, 'r') as ofx_file:
ofx = OfxParser.parse(ofx_file)
accounts = ofx.accounts
for a in accounts:
print(a.account_id, a.institution, a.account_type)
transactions = a.statement.transactions
for t in transactions:
entry = {"Account": a.number, "Date": t.date, "Amount": t.amount}
k = t.memo + " " + t.payee
# replace multiple spaces with single
k = re.sub(r'\s\s+', " ", k)
entry["Description"] = k
if entry["Description"] not in cats_dict:
cats_dict[entry["Description"]] = ""
entry["Category"] = cats_dict[entry["Description"]]
full_list.append(entry)
# sort the list in ascending date order
full_list = sorted(full_list, key=lambda dum: dum["Date"])
def write_file():
with open('eggs.csv', 'w', newline='') as csvfile:
spamwriter = csv.writer(csvfile)
for entry in full_list:
print(entry)
spamwriter.writerow([entry["Account"], time.strftime('%d-%b-%Y', entry["Date"].timetuple()),
entry["Amount"], entry["Description"], entry["Category"]])
csvfile.close()
with open('cats.csv', 'w', newline='') as csvfile:
spamwriter = csv.writer(csvfile)
for k, v in cats_dict.items():
spamwriter.writerow([k, v])
csvfile.close()
print("Exported!")
def about():
messagebox.showinfo("About", my_name + "\n" + my_version + "\n" + my_author)
root = Tk()
root.title('py-bank-manager - bank statement processor')
root.iconbitmap(default='flag.ico')
t1 = Text(root)
sb = Scrollbar(root)
t1.pack(side=LEFT, fill=Y)
sb.pack(side=RIGHT, fill=Y)
sb.config(command=t1.yview)
t1.config(yscrollcommand=sb.set)
# Redirect print to the root widow text box
class PrintToT1(object):
@staticmethod
def write(s):
t1.insert(END, s)
t1.see(END)
sys.stdout = PrintToT1()
sys.stderr = PrintToT1()
menu = Menu(root)
root.config(menu=menu)
filemenu = Menu(menu, tearoff=0)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="New", command=new_file)
filemenu.add_separator()
filemenu.add_command(label="Import OFX...", command=read_ofx)
filemenu.add_separator()
filemenu.add_command(label="Export ...", command=write_file)
filemenu.add_separator()
filemenu.add_command(label="Read Cats ...", command=read_cats)
filemenu.add_command(label="Apply Cats ...", command=apply_cats)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.quit)
helpmenu = Menu(menu, tearoff=0)
menu.add_cascade(label="Help", menu=helpmenu)
helpmenu.add_command(label="About...", command=about)
mainloop()