## $Id: fix_import_file.py 7537 2012-01-30 07:46:08Z henrik $ ## ## Copyright (C) 2012 Uli Fouquet & Henrik Bettermann ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; if not, write to the Free Software ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ## """ Fix exports from old SRP portal to make them importable by current portal. Usage: Change into this directory, set the options below (files are assumed to be in the same directory) and then run python fix_import_file.py Errors/warnings will be displayed on the shell, the output will be put into the specified output file. """ import sys if len(sys.argv) != 2: print 'Usage: python fix_import_file.py ' sys.exit(1) ## ## CONFIGURATION SECTION ## # file with input data INPUT_FILE = '%s' % sys.argv[1] # file written with modified output OUTPUT_FILE = '%s_edited.csv' % sys.argv[1].split('.')[0] # keys are fieldnames in input file, values are methods of class # Converter (see below) OPTIONS = { 'sex': 'gender', 'birthday': 'date', 'request_date': 'datetime', 'marit_stat': 'marit_stat', 'session': 'session', 'entry_session': 'session', 'current_session': 'session', 'reg_state': 'reg_state', 'password': 'password', } # Mapping input file colnames --> output file colnames COLNAME_MAPPING = { 'jamb_reg_no': 'reg_number', 'birthday': 'date_of_birth', 'clr_ac_pin': 'clr_code', 'study_course': 'certificate', 'session': 'level_session', 'verdict': 'level_verdict', } # Mapping input regh_state --> output reg_state REGSTATE_MAPPING = { 'student_created': 'created', 'admitted': 'admitted', 'clearance_pin_entered': 'clearance started', 'clearance_requested': 'clearance requested', 'cleared_and_validated': 'cleared', 'school_fee_paid': 'school fee paid', 'returning': 'returning', 'courses_registered': 'courses registered', 'courses_validated': 'courses validated', } ## ## END OF CONFIG ## import csv import datetime import sys def convert_fieldnames(fieldnames): """Replace input fieldnames by fieldnames of COLNAME_MAPPING. """ header = dict([(name, name) for name in fieldnames]) for in_name, out_name in COLNAME_MAPPING.items(): if in_name not in header: continue header[in_name] = out_name return header class Converters(): """Converters to turn old-style values into new ones. """ @classmethod def reg_state(self, value): """ 'courses_validated' --> 'courses validated' """ return REGSTATE_MAPPING.get(value,value) @classmethod def session(self, value): """ '08' --> '2008' """ try: number = int(value) except ValueError: return 9999 if number < 14: return number + 2000 elif number in range(2000,2015): return number else: return 9999 @classmethod def marit_stat(self, value): """ 'True'/'False' --> 'married'/'unmarried' """ if value == 'True': value = 'married' elif value == 'False': value = 'unmarried' else: value = '' return value @classmethod def gender(self, value): """ 'True'/'False' --> 'f'/'m' """ if value == 'True': value = 'f' elif value == 'False': value = 'm' else: value = '' return value @classmethod def date(self, value): """ 'yyyy/mm/dd' --> 'yyyy-mm-dd' """ if value == "None": value = "" elif value == "": value = "" else: value = value.replace('/', '-') # We add the hash symbol to avoid automatic date transformation # in Excel and Calc for further processing value += '#' return value @classmethod def datetime(self, value): """ 'yyyy/mm/dd' --> 'yyyy-mm-dd' """ #print "IN: ", value if value == "None": value = "" elif value == "": value = "" else: #value = datetime.datetime.strptime(value, '%Y/%m/%d') #value = datetime.datetime.strftime(value, '%Y-%m-%d') pass #print "OUT: ", value return value @classmethod def password(self, value): if value == "not set": return "" return value reader = csv.DictReader(open(INPUT_FILE, 'rb')) writer = None for num, row in enumerate(reader): if num == 0: writer = csv.DictWriter(open(OUTPUT_FILE, 'wb'), reader.fieldnames) print "FIELDS: " for x, y in enumerate(reader.fieldnames): print x, y header = convert_fieldnames(reader.fieldnames) writer.writerow(header) for key, value in row.items(): if not key in OPTIONS.keys(): continue conv_name = OPTIONS[key] converter = getattr(Converters, conv_name, None) if converter is None: print "WARNING: cannot find converter %s" % conv_name continue row[key] = converter(row[key]) writer.writerow(row) print "Output written to %s" % OUTPUT_FILE