[7512] | 1 | ## $Id: fix_import_file.py 7602 2012-02-08 07:24:41Z henrik $ |
---|
| 2 | ## |
---|
[7518] | 3 | ## Copyright (C) 2012 Uli Fouquet & Henrik Bettermann |
---|
[7512] | 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 | """ |
---|
[7518] | 19 | Fix exports from old SRP portal to make them importable by current portal. |
---|
[7512] | 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 | |
---|
[7518] | 26 | python fix_import_file.py <filename> |
---|
[7512] | 27 | |
---|
| 28 | Errors/warnings will be displayed on the shell, the output will be put |
---|
| 29 | into the specified output file. |
---|
| 30 | """ |
---|
[7573] | 31 | import csv |
---|
| 32 | import datetime |
---|
[7575] | 33 | import re |
---|
[7573] | 34 | import sys |
---|
[7512] | 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', |
---|
[7514] | 45 | 'marit_stat': 'marit_stat', |
---|
[7537] | 46 | 'session': 'session', |
---|
[7514] | 47 | 'entry_session': 'session', |
---|
| 48 | 'current_session': 'session', |
---|
[7602] | 49 | 'session_id': 'session', |
---|
[7526] | 50 | 'reg_state': 'reg_state', |
---|
| 51 | 'password': 'password', |
---|
[7575] | 52 | 'phone': 'phone', |
---|
[7602] | 53 | 'level': 'level', |
---|
| 54 | 'start_level': 'level', |
---|
| 55 | 'end_level': 'level', |
---|
| 56 | 'level_id': 'level', |
---|
| 57 | 'current_level': 'level', |
---|
| 58 | 'semester': 'semester', |
---|
| 59 | 'application_category': 'application_category', |
---|
[7512] | 60 | } |
---|
[7516] | 61 | |
---|
| 62 | # Mapping input file colnames --> output file colnames |
---|
| 63 | COLNAME_MAPPING = { |
---|
[7550] | 64 | # base date |
---|
[7518] | 65 | 'jamb_reg_no': 'reg_number', |
---|
[7516] | 66 | 'birthday': 'date_of_birth', |
---|
[7526] | 67 | 'clr_ac_pin': 'clr_code', |
---|
[7550] | 68 | # study course |
---|
[7530] | 69 | 'study_course': 'certificate', |
---|
[7550] | 70 | # study level |
---|
[7537] | 71 | 'session': 'level_session', |
---|
| 72 | 'verdict': 'level_verdict', |
---|
[7550] | 73 | # course ticket |
---|
| 74 | 'level_id': 'level', |
---|
[7516] | 75 | } |
---|
[7526] | 76 | |
---|
| 77 | # Mapping input regh_state --> output reg_state |
---|
| 78 | REGSTATE_MAPPING = { |
---|
| 79 | 'student_created': 'created', |
---|
| 80 | 'admitted': 'admitted', |
---|
| 81 | 'clearance_pin_entered': 'clearance started', |
---|
| 82 | 'clearance_requested': 'clearance requested', |
---|
| 83 | 'cleared_and_validated': 'cleared', |
---|
| 84 | 'school_fee_paid': 'school fee paid', |
---|
| 85 | 'returning': 'returning', |
---|
| 86 | 'courses_registered': 'courses registered', |
---|
| 87 | 'courses_validated': 'courses validated', |
---|
| 88 | } |
---|
| 89 | |
---|
[7512] | 90 | ## |
---|
| 91 | ## END OF CONFIG |
---|
| 92 | ## |
---|
| 93 | |
---|
[7575] | 94 | # Look for the first sequence of numbers |
---|
| 95 | RE_PHONE = re.compile('[^\d]*(\d*)[^\d]*') |
---|
| 96 | |
---|
[7516] | 97 | def convert_fieldnames(fieldnames): |
---|
| 98 | """Replace input fieldnames by fieldnames of COLNAME_MAPPING. |
---|
| 99 | """ |
---|
| 100 | header = dict([(name, name) for name in fieldnames]) |
---|
| 101 | for in_name, out_name in COLNAME_MAPPING.items(): |
---|
| 102 | if in_name not in header: |
---|
| 103 | continue |
---|
| 104 | header[in_name] = out_name |
---|
[7514] | 105 | return header |
---|
| 106 | |
---|
[7512] | 107 | class Converters(): |
---|
| 108 | """Converters to turn old-style values into new ones. |
---|
| 109 | """ |
---|
| 110 | @classmethod |
---|
[7526] | 111 | def reg_state(self, value): |
---|
| 112 | """ 'courses_validated' --> 'courses validated' |
---|
| 113 | """ |
---|
| 114 | return REGSTATE_MAPPING.get(value,value) |
---|
| 115 | |
---|
| 116 | @classmethod |
---|
[7602] | 117 | def level(self, value): |
---|
| 118 | """ '000' --> '10' |
---|
| 119 | """ |
---|
| 120 | try: |
---|
| 121 | number = int(value) |
---|
| 122 | except ValueError: |
---|
| 123 | return 9999 |
---|
| 124 | if number == 0: |
---|
| 125 | return 10 |
---|
| 126 | return number |
---|
| 127 | |
---|
| 128 | @classmethod |
---|
| 129 | def semester(self, value): |
---|
| 130 | """ '0' --> '9' |
---|
| 131 | """ |
---|
| 132 | try: |
---|
| 133 | number = int(value) |
---|
| 134 | except ValueError: |
---|
| 135 | return 9999 |
---|
| 136 | if number == 0: |
---|
| 137 | return 9 |
---|
| 138 | return number |
---|
| 139 | |
---|
| 140 | @classmethod |
---|
| 141 | def application_category(self, value): |
---|
| 142 | """ '' --> 'no' |
---|
| 143 | """ |
---|
| 144 | if value == '': |
---|
| 145 | return 'no' |
---|
| 146 | return value |
---|
| 147 | |
---|
| 148 | |
---|
| 149 | @classmethod |
---|
[7514] | 150 | def session(self, value): |
---|
| 151 | """ '08' --> '2008' |
---|
| 152 | """ |
---|
| 153 | try: |
---|
| 154 | number = int(value) |
---|
| 155 | except ValueError: |
---|
[7602] | 156 | #import pdb; pdb.set_trace() |
---|
[7514] | 157 | return 9999 |
---|
| 158 | if number < 14: |
---|
| 159 | return number + 2000 |
---|
| 160 | elif number in range(2000,2015): |
---|
| 161 | return number |
---|
| 162 | else: |
---|
| 163 | return 9999 |
---|
| 164 | |
---|
| 165 | @classmethod |
---|
| 166 | def marit_stat(self, value): |
---|
| 167 | """ 'True'/'False' --> 'married'/'unmarried' |
---|
| 168 | """ |
---|
| 169 | if value == 'True': |
---|
| 170 | value = 'married' |
---|
| 171 | elif value == 'False': |
---|
| 172 | value = 'unmarried' |
---|
| 173 | else: |
---|
| 174 | value = '' |
---|
| 175 | return value |
---|
| 176 | |
---|
| 177 | @classmethod |
---|
| 178 | def gender(self, value): |
---|
[7526] | 179 | """ 'True'/'False' --> 'f'/'m' |
---|
[7514] | 180 | """ |
---|
| 181 | if value == 'True': |
---|
[7526] | 182 | value = 'f' |
---|
[7514] | 183 | elif value == 'False': |
---|
[7526] | 184 | value = 'm' |
---|
[7514] | 185 | else: |
---|
| 186 | value = '' |
---|
| 187 | return value |
---|
| 188 | |
---|
| 189 | @classmethod |
---|
[7512] | 190 | def date(self, value): |
---|
| 191 | """ 'yyyy/mm/dd' --> 'yyyy-mm-dd' |
---|
| 192 | """ |
---|
| 193 | if value == "None": |
---|
| 194 | value = "" |
---|
| 195 | elif value == "": |
---|
| 196 | value = "" |
---|
| 197 | else: |
---|
| 198 | value = value.replace('/', '-') |
---|
[7514] | 199 | # We add the hash symbol to avoid automatic date transformation |
---|
| 200 | # in Excel and Calc for further processing |
---|
| 201 | value += '#' |
---|
[7512] | 202 | return value |
---|
| 203 | |
---|
| 204 | @classmethod |
---|
| 205 | def datetime(self, value): |
---|
| 206 | """ 'yyyy/mm/dd' --> 'yyyy-mm-dd' |
---|
| 207 | """ |
---|
| 208 | #print "IN: ", value |
---|
| 209 | if value == "None": |
---|
| 210 | value = "" |
---|
| 211 | elif value == "": |
---|
| 212 | value = "" |
---|
| 213 | else: |
---|
| 214 | #value = datetime.datetime.strptime(value, '%Y/%m/%d') |
---|
| 215 | #value = datetime.datetime.strftime(value, '%Y-%m-%d') |
---|
| 216 | pass |
---|
| 217 | #print "OUT: ", value |
---|
| 218 | return value |
---|
| 219 | |
---|
[7526] | 220 | @classmethod |
---|
| 221 | def password(self, value): |
---|
| 222 | if value == "not set": |
---|
| 223 | return "" |
---|
| 224 | return value |
---|
| 225 | |
---|
[7575] | 226 | @classmethod |
---|
| 227 | def phone(self, value): |
---|
| 228 | """ '<num-seq1>-<num-seq2> asd' -> '--<num-seq1><num-seq2>' |
---|
[7526] | 229 | |
---|
[7575] | 230 | Dashes and slashes are removed before looking for sequences |
---|
| 231 | of numbers. |
---|
| 232 | """ |
---|
| 233 | value = value.replace('-', '') |
---|
| 234 | value = value.replace('/', '') |
---|
| 235 | match = RE_PHONE.match(value) |
---|
| 236 | phone = match.groups()[0] |
---|
| 237 | value = '--%s' % phone |
---|
| 238 | return value |
---|
| 239 | |
---|
| 240 | |
---|
[7572] | 241 | def main(): |
---|
[7573] | 242 | input_file = '%s' % sys.argv[1] |
---|
| 243 | output_file = '%s_edited.csv' % sys.argv[1].split('.')[0] |
---|
| 244 | reader = csv.DictReader(open(input_file, 'rb')) |
---|
[7572] | 245 | writer = None |
---|
[7512] | 246 | |
---|
[7572] | 247 | for num, row in enumerate(reader): |
---|
| 248 | if num == 0: |
---|
[7573] | 249 | writer = csv.DictWriter(open(output_file, 'wb'), reader.fieldnames) |
---|
[7572] | 250 | print "FIELDS: " |
---|
| 251 | for x, y in enumerate(reader.fieldnames): |
---|
| 252 | print x, y |
---|
| 253 | header = convert_fieldnames(reader.fieldnames) |
---|
| 254 | writer.writerow(header) |
---|
| 255 | for key, value in row.items(): |
---|
| 256 | if not key in OPTIONS.keys(): |
---|
| 257 | continue |
---|
| 258 | conv_name = OPTIONS[key] |
---|
| 259 | converter = getattr(Converters, conv_name, None) |
---|
| 260 | if converter is None: |
---|
| 261 | print "WARNING: cannot find converter %s" % conv_name |
---|
| 262 | continue |
---|
| 263 | row[key] = converter(row[key]) |
---|
[7602] | 264 | try: |
---|
| 265 | writer.writerow(row) |
---|
| 266 | except: |
---|
| 267 | print row['student_id'] |
---|
[7512] | 268 | |
---|
[7573] | 269 | print "Output written to %s" % output_file |
---|
[7572] | 270 | |
---|
| 271 | |
---|
| 272 | if __name__ == '__main__': |
---|
| 273 | if len(sys.argv) != 2: |
---|
| 274 | print 'Usage: %s <filename>' % __file__ |
---|
| 275 | sys.exit(1) |
---|
| 276 | main() |
---|