-
Notifications
You must be signed in to change notification settings - Fork 1
/
prepare-downloaded-data.py
176 lines (138 loc) · 4.93 KB
/
prepare-downloaded-data.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
'''
Takes a ANZ CSV download and produces two output CSV.
One file is of payments out of the account, the other
is of receipts into the account.
The column ordering of each is specific to our needs
with the existing spreadsheet
'''
import os
import csv
import pprint
from configparser import ConfigParser
from decimal import Decimal
from datetime import datetime
CREDITKEYS = ['DateISO', 'Amount', 'DateISO', 'Type', 'Details', 'Code', 'Particulars', 'Reference', 'Amount', 'DateISO']
DEBITKEYS = ['DateISO', 'Amount', 'Type', 'Details', 'Code', 'Particulars', 'Reference', 'Amount', 'DateISO', 'ForeignCurrencyAmount']
def getConfig():
'''
Read the standard and 'local only' configs
and build a corresponding dictionary of values
'''
parser = ConfigParser()
parser.read('anzaccountinfomessage.ini')
dic_out = {}
dic_out['OUTPUTPAYMENTSFILENAME'] = parser.get('DEFAULT', 'OUTPUTPAYMENTSFILENAME')
dic_out['INPUTRECEIPTSFILENAME'] = parser.get('DEFAULT', 'INPUTRECEIPTSFILENAME')
# Switch to local only config
parserlocal = ConfigParser()
parserlocal.read('anzaccountinfomessage-localonly.ini')
dic_out['DATADIR'] = parserlocal.get('DEFAULT', 'DATADIR')
dic_out['INPUTFILENAME'] = parserlocal.get('DEFAULT', 'INPUTFILENAME')
return dic_out
def makeFileNames(dic_config):
'''
Manipulate config values into full paths
'''
dic_out = {
'IN': os.path.join(dic_config['DATADIR'], dic_config['INPUTFILENAME']),
'OUTPAYMENTS': os.path.join(dic_config['DATADIR'], dic_config['OUTPUTPAYMENTSFILENAME']),
'INRECEIPTS': os.path.join(dic_config['DATADIR'], dic_config['INPUTRECEIPTSFILENAME'])
}
return dic_out
def provideISOVersionOfDate(strdmy):
'''
Take a string containging a dmy date
and output it as YYYY-MM-DD
Assumes 'y' in input is a four figure
year
'''
lst_dt_elems = strdmy.split("/")
strdtout = "{}-{}-{}".format(lst_dt_elems[2], lst_dt_elems[1], lst_dt_elems[0])
return strdtout
def getDateTimeAsISO():
'''
Return current datetime in ISO
'''
d = datetime.now()
return d.strftime('%Y%m%dT%H%M%S')
def makeentrysequence(dic_entry, key_seq):
'''
Prepare a list of dictionary keys
with which to select elements from
the entry dictionaries
Also do something weird in order to provide
an empty column to categories the entries
by when they're used in the target workbook.
'''
colcnt = 0
lst_out = []
for k in key_seq:
lst_out.append(dic_entry[k])
# This is the idiosyncratic bit of processing
# where we insert an empty column immediately
# after the first column
lst_out_final = lst_out[0:1]
lst_out_final.extend([''])
lst_out_final.extend(lst_out[1:])
return lst_out_final
def renameFileIfNess(path):
'''
If the `path` file already exists rename it
with an the current datetime in ISO formate
appended onto the end of the file name
'''
if os.path.isfile(path):
new_path = path + "." + getDateTimeAsISO()
print("About to rename : " + path + " to " + new_path)
os.rename(path, new_path)
def manageProcessing(dic_file_paths):
'''
Read inputs and output outputs !
'''
lst_entries = []
cnt_in = 0
cnt_crd_out = 0
cnt_dbt_out = 0
# Preprocess the input data
with open(dic_file_paths['IN'], 'r') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
cnt_in += 1
tmprow = row
tmprow['DateISO'] = provideISOVersionOfDate(tmprow['Date'])
lst_entries.append(tmprow)
import pprint
pprint.pprint(lst_entries)
# Rename any existing output credits file if necesary
renameFileIfNess(dic_file_paths['OUTPAYMENTS'])
# Output the credits
lst_credits = []
with open(dic_file_paths['OUTPAYMENTS'], 'wb') as csvfile:
writer = csv.writer(csvfile)
for dic_entry in lst_entries:
if Decimal(dic_entry['Amount']) > 0:
cnt_crd_out += 1
writer.writerow(makeentrysequence(dic_entry, CREDITKEYS))
# Rename any existing output debits file if necesary
renameFileIfNess(dic_file_paths['INRECEIPTS'])
# Output the debits
lst_debits = []
with open(dic_file_paths['INRECEIPTS'], 'wb') as csvfile:
writer = csv.writer(csvfile)
for dic_entry in lst_entries:
if Decimal(dic_entry['Amount']) < 0:
cnt_dbt_out += 1
writer.writerow(makeentrysequence(dic_entry, DEBITKEYS))
# Report row counts
print("")
print("Count of rows in : " + str(cnt_in))
print("Count of credit rows out : " + str(cnt_crd_out))
print("Count of debit rows out : " + str(cnt_dbt_out))
def main():
print("Version 2")
print("")
config = getConfig()
dic_file_paths = makeFileNames(config)
manageProcessing(dic_file_paths)
if __name__ == "__main__":
main()