source: main/waeup.sirp/trunk/tools/fix_import_file.py @ 7561

Last change on this file since 7561 was 7550, checked in by Henrik Bettermann, 13 years ago

Let's use the script for course tickets too.

  • Property svn:keywords set to Id
File size: 5.8 KB
Line 
1## $Id: fix_import_file.py 7550 2012-02-01 11:26:14Z 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"""
19Fix exports from old SRP portal to make them importable by current portal.
20
21Usage:
22
23Change into this directory, set the options below (files are assumed
24to be in the same directory) and then run
25
26  python fix_import_file.py <filename>
27
28Errors/warnings will be displayed on the shell, the output will be put
29into the specified output file.
30"""
31
32import sys
33
34
35if len(sys.argv) != 2:
36    print 'Usage: python fix_import_file.py <filename>'
37    sys.exit(1)
38
39##
40## CONFIGURATION SECTION
41##
42# file with input data
43INPUT_FILE = '%s' % sys.argv[1]
44
45# file written with modified output
46OUTPUT_FILE = '%s_edited.csv' % sys.argv[1].split('.')[0]
47
48# keys are fieldnames in input file, values are methods of class
49# Converter (see below)
50OPTIONS = {
51    'sex': 'gender',
52    'birthday': 'date',
53    'request_date': 'datetime',
54    'marit_stat': 'marit_stat',
55    'session': 'session',
56    'entry_session': 'session',
57    'current_session': 'session',
58    'reg_state': 'reg_state',
59    'password': 'password',
60    }
61
62# Mapping input file colnames --> output file colnames
63COLNAME_MAPPING = {
64    # base date
65    'jamb_reg_no': 'reg_number',
66    'birthday': 'date_of_birth',
67    'clr_ac_pin': 'clr_code',
68    # study course
69    'study_course': 'certificate',
70    # study level
71    'session': 'level_session',
72    'verdict': 'level_verdict',
73    # course ticket
74    'level_id': 'level',
75    }
76
77# Mapping input regh_state --> output reg_state
78REGSTATE_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
90##
91## END OF CONFIG
92##
93
94import csv
95import datetime
96import sys
97
98def convert_fieldnames(fieldnames):
99    """Replace input fieldnames by fieldnames of COLNAME_MAPPING.
100    """
101    header = dict([(name, name) for name in fieldnames])
102    for in_name, out_name in COLNAME_MAPPING.items():
103        if in_name not in header:
104            continue
105        header[in_name] = out_name
106    return header
107
108class Converters():
109    """Converters to turn old-style values into new ones.
110    """
111    @classmethod
112    def reg_state(self, value):
113        """ 'courses_validated' --> 'courses validated'
114        """
115        return REGSTATE_MAPPING.get(value,value)
116
117    @classmethod
118    def session(self, value):
119        """ '08' --> '2008'
120        """
121        try:
122            number = int(value)
123        except ValueError:
124            return 9999
125        if number < 14:
126            return number + 2000
127        elif number in range(2000,2015):
128            return number
129        else:
130            return 9999
131
132    @classmethod
133    def marit_stat(self, value):
134        """ 'True'/'False' --> 'married'/'unmarried'
135        """
136        if value == 'True':
137            value = 'married'
138        elif value == 'False':
139            value = 'unmarried'
140        else:
141            value = ''
142        return value
143
144    @classmethod
145    def gender(self, value):
146        """ 'True'/'False' --> 'f'/'m'
147        """
148        if value == 'True':
149            value = 'f'
150        elif value == 'False':
151            value = 'm'
152        else:
153            value = ''
154        return value
155
156    @classmethod
157    def date(self, value):
158        """ 'yyyy/mm/dd' --> 'yyyy-mm-dd'
159        """
160        if value == "None":
161            value = ""
162        elif value == "":
163            value = ""
164        else:
165            value = value.replace('/', '-')
166            # We add the hash symbol to avoid automatic date transformation
167            # in Excel and Calc for further processing
168            value += '#'
169        return value
170
171    @classmethod
172    def datetime(self, value):
173        """ 'yyyy/mm/dd' --> 'yyyy-mm-dd'
174        """
175        #print  "IN: ", value
176        if value == "None":
177            value = ""
178        elif value == "":
179            value = ""
180        else:
181            #value = datetime.datetime.strptime(value, '%Y/%m/%d')
182            #value = datetime.datetime.strftime(value, '%Y-%m-%d')
183            pass
184        #print "OUT: ", value
185        return value
186
187    @classmethod
188    def password(self, value):
189        if value == "not set":
190            return ""
191        return value
192
193
194reader = csv.DictReader(open(INPUT_FILE, 'rb'))
195writer = None
196
197for num, row in enumerate(reader):
198    if num == 0:
199        writer = csv.DictWriter(open(OUTPUT_FILE, 'wb'), reader.fieldnames)
200        print "FIELDS: "
201        for x, y in enumerate(reader.fieldnames):
202            print x, y
203        header = convert_fieldnames(reader.fieldnames)
204        writer.writerow(header)
205    for key, value in row.items():
206        if not key in OPTIONS.keys():
207            continue
208        conv_name = OPTIONS[key]
209        converter = getattr(Converters, conv_name, None)
210        if converter is None:
211            print "WARNING: cannot find converter %s" % conv_name
212            continue
213        row[key] = converter(row[key])
214    writer.writerow(row)
215
216print "Output written to %s" % OUTPUT_FILE
Note: See TracBrowser for help on using the repository browser.