-
Notifications
You must be signed in to change notification settings - Fork 0
/
converter.py
332 lines (273 loc) · 11.2 KB
/
converter.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
"""
Connor Callison
8/18/17
Alma Bursar Export -> Data Conversoin Script
***PRODUCTION VERSION***
*** 1.0.2 ***
This script will read the specified Alma export file, and convert
it to the required format to be importe into PeopleSoft.
"""
from lxml import etree
import csv
import time
from os import chdir
from os import rename as mv
from os import listdir
import shutil
import smtplib
archival_path = 'old_xml/'
# Path for converted files to be written.
student_output_file_path = 'output/bursar-converted-%s-%s.dat' % ('DEBITS', time.strftime("%d%m%Y"))
community_output_file_path = 'output/bursar-community-%s.csv' % (
time.strftime("%d%m%Y"))
# Namespace map for XML processing
nsm = {"xb": "http://com/exlibris/urm/rep/externalsysfinesfees/xmlbeans"}
# This dictionary allows us to switch out the library fee types for the
# corresponding PeopleSoft item_type.
item_type_map = {'LIBRARYCARDREPLACEMENT': '300000005921',
'LOSTITEMPROCESSFEE': '300000006918',
'OVERDUEFINE': '300000006919',
'CREDIT': '820000206129',
'LOSTITEMREPLACEMENTFEE': '300000006920'}
class User:
"""
The User class is a structure used to store basic
information about the users in the XML file.
There will be a User generated for each user_id in the XML.
Users each contain a list of Fees accessible through get_fees()
"""
def __init__(self, username, name, user_type = 'campus'):
self.username = username
self.fees = []
self.user_type = user_type
self.name = name
def add_fee(self, fee):
self.fees.append(fee)
def get_fees(self):
return self.fees
def set_type(self, user_type):
self.user_type = user_type
class Fee:
def __init__(self, term, lib_code, amount, date):
self.term = term
self.lib_code = lib_code
self.amount = amount
self.date = date
def find_file():
"""
Looks for the .XML file in the current woeking directory.
NOTE: There can only be one .XML file present.
"""
input_file_name = ''
xml_count = 0
for file in listdir('.'):
if file.endswith(".xml"):
input_file_name = file
xml_count += 1
if xml_count > 1:
error = "There are %s '.xml' files in this directory. There can only be one." % (xml_count)
error += "\nPlease remove any .xml files that are not the target file and try again."
raise Exception(error)
elif xml_count == 0:
error = "No '.xml' file found. The file transfer from Alma may have failed."
raise Exception(error)
else:
return input_file_name
def parse_data(input_file):
"""
Takes in an input file, loads the XML into a tree
traverses the file and grabs out fees and associates them
with a user.
Returns list of User objects.
"""
print "Converting Data..."
# Load XML into tree / find fees.
users = []
tree = etree.parse(open(input_file))
root = tree.getroot()
userExportedFineFees_list = root.findall(
'xb:userExportedFineFees', namespaces=nsm)
userExportedFineFees = userExportedFineFees_list[1]
userExportedList = userExportedFineFees.find(
'xb:userExportedList', namespaces=nsm)
userExportedFineFeesList = userExportedList.findall(
'xb:userExportedFineFeesList', namespaces=nsm)
# Create instances of User and add their fees.
for user_fees in userExportedFineFeesList:
user = user_fees.find('xb:user', namespaces=nsm)
username = user.find('xb:value', namespaces=nsm).text
name = user_fees.find('xb:patronName', namespaces=nsm).text
new_user = User(username, name)
fee_list_elem = user_fees.find('xb:finefeeList', namespaces=nsm)
for item in fee_list_elem:
new_fee = None
fee_type = item.find('xb:fineFeeType', namespaces=nsm).text
txn_dt = item.find('xb:lastTransactionDate', namespaces=nsm).text
amount = item.find('xb:compositeSum', namespaces=nsm).find(
'xb:sum', namespaces=nsm).text
date = item.find('xb:lastTransactionDate', namespaces=nsm).text.split(' ')[0]
if float(amount) < 0:
new_fee = Fee(1234, 'CREDIT', str(float(amount) * -1), date)
elif float(amount) > 0:
new_fee = Fee(1234, fee_type, amount, date)
new_user.add_fee(new_fee)
if is_campus_user(new_user.username):
users.append(new_user)
else:
new_user.set_type('community')
users.append(new_user)
return users
def write_data(user_list, student_file_name, community_file_name):
"""
Takes in a list of Users, student file name, and community file name.
Seperates the users by type (campus vs commnuity), and writes the users into their correspinding
files.
"""
student_output_file = open(student_file_name, 'w')
output_credits = open(student_file_name.replace('DEBITS','CREDITS'), 'w')
community_output_file = open(community_file_name, 'w')
community_output_file.write('UserID' + ',' + 'Name' + ',' + 'Item Type' + ',' +'Amount' + ',' + 'Date\n')
for user in user_list:
if user.user_type == 'campus':
for fee in user.get_fees():
if item_type_map[str(fee.lib_code)] == '820000206129':
output_credits.write(str(user.username) + ',' + item_type_map[str(fee.lib_code)] +
',' + str('{:07.2f}'.format(float(fee.amount))) + ',' + fee.date + '\n')
else:
student_output_file.write(str(user.username) + ',' + item_type_map[str(fee.lib_code)] +
',' + str('{:07.2f}'.format(float(fee.amount))) + ',' + fee.date + '\n')
else:
for fee in user.get_fees():
community_output_file.write(str(user.username) + ','
+ '"' + str(user.name) + '"' + ','
+ str(fee.lib_code) + ','
+ str('{:07.2f}'.format(float(fee.amount))) + ','
+ fee.date + '\n')
def archive_file( input_file_name, path):
"""
This function simply moves one file to another location.
Its purpose is to move the xml file after it has undergone conversion
to the archival path.
"""
mv(input_file_name, path + input_file_name)
def gather_stats(all_users):
"""
Gethers basic statistics about what was read from the XML.
Best used for for basic validation of totals between files.
"""
campus_count = 0
campus_debits = 0
campus_debit_count = 0
campus_credits = 0
campus_credit_count =0
community_count = 0
community_debits = 0
community_credits = 0
output_string = ''
for user in all_users:
if user.user_type == 'campus':
campus_count += 1
for fee in user.get_fees():
amount = float(fee.amount)
fee_type = fee.lib_code
if fee_type != 'CREDIT':
campus_debits += amount
campus_debit_count += 1
else:
campus_credits += amount
campus_credit_count += 1
else:
community_count += 1
for fee in user.get_fees():
amount = float(fee.amount)
fee_type = fee.lib_code
if fee_type != 'CREDIT':
community_debits += amount
else:
community_credits += amount
output_string += "\nCampus users: " + str(campus_count)
output_string += "\nCampus debits: $" + str(campus_debits) + ' | Transactions: ' + str(campus_debit_count)
output_string += "\nCampus credits: $" + str(campus_credits) + ' | Transactions: ' + str(campus_credit_count)
output_string += "\n--"
output_string += "\nCommunity users: " + str(community_count)
output_string += "\nCommunity debits: $" + str(community_debits)
output_string += "\nCommunity credits: $" + str(community_credits)
output_string += "\n----"
output_string += "\nTotal debits: $" + str(campus_debits + community_debits)
output_string += "\nTotal credits: $" + str(campus_credits + community_credits)
output_string += "\nGross total: $" + str((campus_debits + community_debits) - (campus_credits + community_credits))
output_string += '\n'
print output_string
return output_string
def is_campus_user(username):
"""
This function is used to identify if a user ID is a valid
university ID.
"""
if len(username) == 9 and username[0] in ['0','9']:
return True
else:
return False
def clear_output_dir():
"""
Moves previous output files into an archival directory.
"""
output_path = 'output/'
input_file_name = ''
for file in listdir(output_path):
if file.endswith(".dat") or file.endswith(".csv") :
input_file_name = file
shutil.move(output_path + input_file_name, output_path + 'old/' + input_file_name)
def send_email(from_addr,to_addr,message):
"""
This function sends an email.
Params: from_addr - address where email wil be sent from.
to_addr - address where email will ve sent.
message - string containing the content of the email.
"""
try:
smtpObj = smtplib.SMTP('smtp.humboldt.edu')
smtpObj.sendmail(from_addr, to_addr,message)
print "Email Sent!"
except Exception, e:
print "Email sending failed:", e
def send_success_email(stats):
"""
Sends an email notfying of script success and
delivers basic stats of the run using gather_stats()
"""
message = "Subject: Bursar Data Conversion - SUCCESS"
message += "\nTo: Library Billing Support <[email protected]>"
message += "\nFrom: HSU Student Finance Jobs <[email protected]>"
message += "\nThe Alma Bursar conversion script executed successfully!"
message += "\nBelow are the metrics:\n"
message += stats
send_email('[email protected]','[email protected]',message)
def send_failure_email(error):
"""
sends an email notifying users of script failure to generate files.
The error is incluede in the email message.
"""
message = "Subject: Bursar Data Conversion - ERROR"
message += "\nTo: Library Billing Support <[email protected]>"
message += "\nFrom: HSU Student Finance Jobs <[email protected]>"
message += "\nThe Alma Bursar conversion script did not execute successfully."
message += "\nSee error below:\n\n"
message += str(error)
send_email('[email protected]','[email protected]',message)
def main():
chdir('/home/alma/bursar')
print "----", time.ctime(), "----"
input_file_name = find_file()
all_users = parse_data(input_file_name)
clear_output_dir()
write_data(all_users, student_output_file_path, community_output_file_path)
send_success_email(gather_stats(all_users))
archive_file( input_file_name, archival_path)
if __name__ == '__main__':
try:
main()
except Exception, e:
send_failure_email(e)
print 'Error:', e