1 | ## $Id: fix_import_file.py 7645 2012-02-14 09:37:40Z henrik $ |
---|
2 | ## |
---|
3 | ## Copyright (C) 2012 Uli Fouquet & Henrik Bettermann |
---|
4 | ## This program is free software; you can redistribute it and/or modify |
---|
5 | ## it under the terms of the GNU General Public License as published by |
---|
6 | ## the Free Software Foundation; either version 2 of the License, or |
---|
7 | ## (at your option) any later version. |
---|
8 | ## |
---|
9 | ## This program is distributed in the hope that it will be useful, |
---|
10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | ## GNU General Public License for more details. |
---|
13 | ## |
---|
14 | ## You should have received a copy of the GNU General Public License |
---|
15 | ## along with this program; if not, write to the Free Software |
---|
16 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
17 | ## |
---|
18 | """ |
---|
19 | Fix exports from old SRP portal to make them importable by current portal. |
---|
20 | |
---|
21 | Usage: |
---|
22 | |
---|
23 | Change into this directory, set the options below (files are assumed |
---|
24 | to be in the same directory) and then run |
---|
25 | |
---|
26 | python fix_import_file.py <filename> |
---|
27 | |
---|
28 | Errors/warnings will be displayed on the shell, the output will be put |
---|
29 | into the specified output file. |
---|
30 | """ |
---|
31 | import csv |
---|
32 | import datetime |
---|
33 | import re |
---|
34 | import sys |
---|
35 | |
---|
36 | ## |
---|
37 | ## CONFIGURATION SECTION |
---|
38 | ## |
---|
39 | # keys are fieldnames in input file, values are methods of class |
---|
40 | # Converter (see below) |
---|
41 | OPTIONS = { |
---|
42 | 'sex': 'gender', |
---|
43 | 'birthday': 'date', |
---|
44 | 'request_date': 'datetime', |
---|
45 | 'marit_stat': 'marit_stat', |
---|
46 | 'session': 'session', |
---|
47 | 'entry_session': 'session', |
---|
48 | 'current_session': 'session', |
---|
49 | 'session_id': 'session', |
---|
50 | 'entry_mode': 'mode', |
---|
51 | 'reg_state': 'reg_state', |
---|
52 | 'password': 'password', |
---|
53 | 'phone': 'phone', |
---|
54 | 'level': 'level', |
---|
55 | 'start_level': 'level', |
---|
56 | 'end_level': 'level', |
---|
57 | 'level_id': 'level', |
---|
58 | 'current_level': 'level', |
---|
59 | 'semester': 'semester', |
---|
60 | 'application_category': 'application_category', |
---|
61 | 'lga': 'lga', |
---|
62 | 'order_id': 'no_int', |
---|
63 | } |
---|
64 | |
---|
65 | # Mapping input file colnames --> output file colnames |
---|
66 | COLNAME_MAPPING = { |
---|
67 | # base date |
---|
68 | 'jamb_reg_no': 'reg_number', |
---|
69 | 'matric_no': 'matric_number', |
---|
70 | 'birthday': 'date_of_birth', |
---|
71 | 'clr_ac_pin': 'clr_code', |
---|
72 | # study course |
---|
73 | 'study_course': 'certificate', |
---|
74 | # study level |
---|
75 | 'session': 'level_session', |
---|
76 | 'verdict': 'level_verdict', |
---|
77 | # course ticket |
---|
78 | 'level_id': 'level', |
---|
79 | # payment ticket |
---|
80 | 'order_id': 'p_id', |
---|
81 | 'status': 'p_state', |
---|
82 | 'category': 'p_category', |
---|
83 | 'resp_pay_reference': 'r_pay_reference', |
---|
84 | 'resp_desc': 'r_desc', |
---|
85 | 'resp_approved_amount': 'r_amount_approved', |
---|
86 | 'item': 'p_item', |
---|
87 | 'amount': 'amount_auth', |
---|
88 | 'resp_card_num': 'r_card_num', |
---|
89 | 'resp_code': 'r_code', |
---|
90 | 'date': 'creation_date', |
---|
91 | 'surcharge': 'surcharge_1', |
---|
92 | 'session_id': 'p_session', |
---|
93 | } |
---|
94 | |
---|
95 | # Mapping input regh_state --> output reg_state |
---|
96 | REGSTATE_MAPPING = { |
---|
97 | 'student_created': 'created', |
---|
98 | 'admitted': 'admitted', |
---|
99 | 'objection_raised': 'clearance started', |
---|
100 | 'clearance_pin_entered': 'clearance started', |
---|
101 | 'clearance_requested': 'clearance requested', |
---|
102 | 'cleared_and_validated': 'cleared', |
---|
103 | 'school_fee_paid': 'school fee paid', |
---|
104 | 'returning': 'returning', |
---|
105 | 'courses_registered': 'courses registered', |
---|
106 | 'courses_validated': 'courses validated', |
---|
107 | } |
---|
108 | |
---|
109 | ## |
---|
110 | ## END OF CONFIG |
---|
111 | ## |
---|
112 | |
---|
113 | # Look for the first sequence of numbers |
---|
114 | RE_PHONE = re.compile('[^\d]*(\d*)[^\d]*') |
---|
115 | |
---|
116 | def convert_fieldnames(fieldnames): |
---|
117 | """Replace input fieldnames by fieldnames of COLNAME_MAPPING. |
---|
118 | """ |
---|
119 | header = dict([(name, name) for name in fieldnames]) |
---|
120 | for in_name, out_name in COLNAME_MAPPING.items(): |
---|
121 | if in_name not in header: |
---|
122 | continue |
---|
123 | header[in_name] = out_name |
---|
124 | return header |
---|
125 | |
---|
126 | class Converters(): |
---|
127 | """Converters to turn old-style values into new ones. |
---|
128 | """ |
---|
129 | @classmethod |
---|
130 | def reg_state(self, value): |
---|
131 | """ 'courses_validated' --> 'courses validated' |
---|
132 | """ |
---|
133 | return REGSTATE_MAPPING.get(value,value) |
---|
134 | |
---|
135 | @classmethod |
---|
136 | def level(self, value): |
---|
137 | """ '000' --> '10' |
---|
138 | """ |
---|
139 | try: |
---|
140 | number = int(value) |
---|
141 | except ValueError: |
---|
142 | return 9999 |
---|
143 | if number == 0: |
---|
144 | return 10 |
---|
145 | return number |
---|
146 | |
---|
147 | @classmethod |
---|
148 | def semester(self, value): |
---|
149 | """ '0' --> '9' |
---|
150 | """ |
---|
151 | try: |
---|
152 | number = int(value) |
---|
153 | except ValueError: |
---|
154 | return 9999 |
---|
155 | if number == 0: |
---|
156 | return 9 |
---|
157 | return number |
---|
158 | |
---|
159 | @classmethod |
---|
160 | def application_category(self, value): |
---|
161 | """ '' --> 'no' |
---|
162 | """ |
---|
163 | if value == '': |
---|
164 | return 'no' |
---|
165 | return value |
---|
166 | |
---|
167 | @classmethod |
---|
168 | def lga(self, value): |
---|
169 | """ Remove apostrophe |
---|
170 | """ |
---|
171 | if value == 'akwa_ibom_uru_offong_oruko': |
---|
172 | return 'akwa_ibom_urue-offong-oruko' |
---|
173 | try: |
---|
174 | value = value.replace("'","") |
---|
175 | value = value.lower() |
---|
176 | except: |
---|
177 | return '' |
---|
178 | return value |
---|
179 | |
---|
180 | |
---|
181 | @classmethod |
---|
182 | def session(self, value): |
---|
183 | """ '08' --> '2008' |
---|
184 | """ |
---|
185 | try: |
---|
186 | number = int(value) |
---|
187 | except ValueError: |
---|
188 | #import pdb; pdb.set_trace() |
---|
189 | return 9999 |
---|
190 | if number < 14: |
---|
191 | return number + 2000 |
---|
192 | elif number in range(2000,2015): |
---|
193 | return number |
---|
194 | else: |
---|
195 | return 9999 |
---|
196 | |
---|
197 | @classmethod |
---|
198 | def marit_stat(self, value): |
---|
199 | """ 'True'/'False' --> 'married'/'unmarried' |
---|
200 | """ |
---|
201 | if value in ('True','married'): |
---|
202 | value = 'married' |
---|
203 | elif value in ('False','unmarried'): |
---|
204 | value = 'unmarried' |
---|
205 | else: |
---|
206 | value = '' |
---|
207 | return value |
---|
208 | |
---|
209 | @classmethod |
---|
210 | def gender(self, value): |
---|
211 | """ 'True'/'False' --> 'f'/'m' |
---|
212 | """ |
---|
213 | if value in ('True','f'): |
---|
214 | value = 'f' |
---|
215 | elif value in ('False','m'): |
---|
216 | value = 'm' |
---|
217 | else: |
---|
218 | value = '' |
---|
219 | return value |
---|
220 | |
---|
221 | @classmethod |
---|
222 | def date(self, value): |
---|
223 | """ 'yyyy/mm/dd' --> 'yyyy-mm-dd' |
---|
224 | """ |
---|
225 | if value == "None": |
---|
226 | value = "" |
---|
227 | elif value == "": |
---|
228 | value = "" |
---|
229 | else: |
---|
230 | value = value.replace('/', '-') |
---|
231 | # We add the hash symbol to avoid automatic date transformation |
---|
232 | # in Excel and Calc for further processing |
---|
233 | value += '#' |
---|
234 | return value |
---|
235 | |
---|
236 | @classmethod |
---|
237 | def no_int(self, value): |
---|
238 | """ Add hash. |
---|
239 | """ |
---|
240 | # We add the hash symbol to avoid automatic number transformation |
---|
241 | # in Excel and Calc for further processing |
---|
242 | value += '#' |
---|
243 | return value |
---|
244 | |
---|
245 | |
---|
246 | @classmethod |
---|
247 | def datetime(self, value): |
---|
248 | """ 'yyyy/mm/dd' --> 'yyyy-mm-dd' |
---|
249 | """ |
---|
250 | #print "IN: ", value |
---|
251 | if value == "None": |
---|
252 | value = "" |
---|
253 | elif value == "": |
---|
254 | value = "" |
---|
255 | else: |
---|
256 | #value = datetime.datetime.strptime(value, '%Y/%m/%d') |
---|
257 | #value = datetime.datetime.strftime(value, '%Y-%m-%d') |
---|
258 | pass |
---|
259 | #print "OUT: ", value |
---|
260 | return value |
---|
261 | |
---|
262 | @classmethod |
---|
263 | def mode(self, value): |
---|
264 | if value == "transfer_fulltime": |
---|
265 | return "transfer_ft" |
---|
266 | return value |
---|
267 | |
---|
268 | @classmethod |
---|
269 | def password(self, value): |
---|
270 | if value == "not set": |
---|
271 | return "" |
---|
272 | return value |
---|
273 | |
---|
274 | @classmethod |
---|
275 | def phone(self, value): |
---|
276 | """ '<num-seq1>-<num-seq2> asd' -> '--<num-seq1><num-seq2>' |
---|
277 | |
---|
278 | Dashes and slashes are removed before looking for sequences |
---|
279 | of numbers. |
---|
280 | """ |
---|
281 | value = value.replace('-', '') |
---|
282 | value = value.replace('/', '') |
---|
283 | match = RE_PHONE.match(value) |
---|
284 | phone = match.groups()[0] |
---|
285 | value = '--%s' % phone |
---|
286 | return value |
---|
287 | |
---|
288 | |
---|
289 | def main(): |
---|
290 | input_file = '%s' % sys.argv[1] |
---|
291 | output_file = '%s_edited.csv' % sys.argv[1].split('.')[0] |
---|
292 | reader = csv.DictReader(open(input_file, 'rb')) |
---|
293 | writer = None |
---|
294 | |
---|
295 | for num, row in enumerate(reader): |
---|
296 | if num == 0: |
---|
297 | writer = csv.DictWriter(open(output_file, 'wb'), reader.fieldnames) |
---|
298 | print "FIELDS: " |
---|
299 | for x, y in enumerate(reader.fieldnames): |
---|
300 | print x, y |
---|
301 | header = convert_fieldnames(reader.fieldnames) |
---|
302 | writer.writerow(header) |
---|
303 | for key, value in row.items(): |
---|
304 | if not key in OPTIONS.keys(): |
---|
305 | continue |
---|
306 | conv_name = OPTIONS[key] |
---|
307 | converter = getattr(Converters, conv_name, None) |
---|
308 | if converter is None: |
---|
309 | print "WARNING: cannot find converter %s" % conv_name |
---|
310 | continue |
---|
311 | row[key] = converter(row[key]) |
---|
312 | try: |
---|
313 | writer.writerow(row) |
---|
314 | except: |
---|
315 | print row['student_id'] |
---|
316 | |
---|
317 | print "Output written to %s" % output_file |
---|
318 | |
---|
319 | |
---|
320 | if __name__ == '__main__': |
---|
321 | if len(sys.argv) != 2: |
---|
322 | print 'Usage: %s <filename>' % __file__ |
---|
323 | sys.exit(1) |
---|
324 | main() |
---|