-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
159 lines (137 loc) · 6.48 KB
/
run.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
import sys
import getopt
import csv
import time
import re
import configparser
# author: lastlink
settings = configparser.ConfigParser()
settings._interpolation = configparser.ExtendedInterpolation()
settings.read('settings.ini')
def main(argv):
inputfile = ''
outputfile = ''
bank = ''
validBanks = ['wells', 'chase']
try:
opts, args = getopt.getopt(
argv, "hi:o:b:", ["ifile=", "ofile=", "bank="])
except getopt.GetoptError:
print('test.py -i <inputfile> -o <outputfile> -b <bankname>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print('test.py -i <inputfile> -o <outputfile>')
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt in ("-o", "--ofile"):
outputfile = arg
elif opt in ("-b", "--bank"):
bank = arg
try:
validBanks.index(bank)
except ValueError:
print(bank + " not in " + str(validBanks))
sys.exit()
if bank is None or bank == '':
bank = settings.get('CurrentBank', 'bank')
try:
validBanks.index(bank)
except ValueError:
print(bank + " not in " + str(validBanks))
sys.exit()
print('Input file is "', inputfile)
print('Output file is "', outputfile)
print('Bank type is "'+bank)
start = time.time()
print("read by line timer:")
lineNum = 0
with open(inputfile) as f:
# place the csv export name here, note when importing into sql database this will be name of table
with open(outputfile, "w", newline='') as file:
csv_file = csv.writer(file)
outputFormat = ['Notes', 'Posting Date', 'Description',
'Amount (Debit)', 'Amount (Credit)', 'Budget', 'Balance', 'Account']
csv_file.writerow(outputFormat)
for line in f:
# print(line)
lineArr = line.split(",")
rowResult = [''] * len(outputFormat)
if bank == 'chase':
if lineNum == 0:
baseHeader = lineArr
print(baseHeader)
else:
if lineArr[baseHeader.index('Details')] == 'DEBIT':
rowResult[outputFormat.index(
'Amount (Debit)')] = lineArr[baseHeader.index('Amount')]
elif lineArr[baseHeader.index('Details')] == 'CREDIT':
rowResult[outputFormat.index(
'Amount (Credit)')] = lineArr[baseHeader.index('Amount')]
else:
print(
"Missing credit/debit:" + lineArr[baseHeader.index('Details')] + " on line:" + lineNum)
rowResult[outputFormat.index(
'Notes')] = lineArr[baseHeader.index('Details')]
rowResult[outputFormat.index('Description')] = re.sub(
"\s\s+", " ", lineArr[baseHeader.index('Description')])
# need to clean out junk
rowResult[outputFormat.index(
'Account')] = cleanAccount(rowResult[outputFormat.index('Description')])
rowResult[outputFormat.index(
'Posting Date')] = lineArr[baseHeader.index('Posting Date')]
rowResult[outputFormat.index(
'Balance')] = lineArr[baseHeader.index('Balance')]
# budget logic
pass
elif bank == 'wells':
if lineNum == 0:
baseHeader = ['Posting Date','Amount','Star','Blank','Description']
print(baseHeader)
rowResult[outputFormat.index('Description')] = re.sub(
"\s\s+", " ", lineArr[baseHeader.index('Description')].replace('"', '').rstrip())
if float(lineArr[baseHeader.index('Amount')].replace('"', '')) > 0:
rowResult[outputFormat.index(
'Amount (Credit)')] = lineArr[baseHeader.index('Amount')].replace('"', '')
else:
rowResult[outputFormat.index(
'Amount (Debit)')] = abs(float(lineArr[baseHeader.index('Amount')].replace('"', '')))
rowResult[outputFormat.index(
'Posting Date')] = lineArr[baseHeader.index('Posting Date')].replace('"', '')
rowResult[outputFormat.index(
'Account')] = cleanAccount(rowResult[outputFormat.index('Description')])
pass
else:
print('bank not implemented')
sys.exit()
rowResult[outputFormat.index(
'Budget')] = determineBudget(rowResult[outputFormat.index('Account')])
csv_file.writerow(rowResult)
lineNum += 1
end = time.time()
print(end - start, "line num", lineNum)
def cleanAccount(account):
tmpAccount = account
# remove m/d
tmpAccount = re.sub("((0|1)\d{1})\/((0|1|2)\d{1})", " ", tmpAccount)
# remove double spaces
tmpAccount = re.sub("\s\s+", " ", tmpAccount)
for accountType in settings.get('Account', 'accounts').split(','):
accountSearch = [element.upper() for element in settings.get('Account', accountType+'_search').split(',')]
if any(word in tmpAccount.upper() for word in accountSearch):
return accountType
# could possibly do matching from setting ini as well, but would be better to clean unique ideas and do a map
return tmpAccount
def determineBudget(account):
for category in settings.get('Budget', 'categories').split(','):
if settings.has_option('Budget', category+'_search'):
# make array uppercase
categorySearch = [element.upper() for element in settings.get('Budget', category+'_search').split(',')]
if any(word in account.upper() for word in categorySearch):
return settings.get('Budget', category+'_name') if settings.has_option('Budget', category+'_name') else category
return ''
# sys.exit()
# call main function
if __name__ == "__main__":
main(sys.argv[1:])