Index: main/waeup.sirp/trunk/tools/fix_import_file.py
===================================================================
--- main/waeup.sirp/trunk/tools/fix_import_file.py	(revision 7512)
+++ main/waeup.sirp/trunk/tools/fix_import_file.py	(revision 7512)
@@ -0,0 +1,125 @@
+##
+## fix_import_file.py
+## Login : <uli@pu.smp.net>
+## Started on  Wed Jan 25 17:08:30 2012 Uli Fouquet
+## $Id$
+## 
+## Copyright (C) 2012 Uli Fouquet
+## 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 SIRP 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.
+"""
+
+##
+## CONFIGURATION SECTION
+##
+# file with input data
+INPUT_FILE = 'students_for_reimport.csv'
+
+# file written with modified output
+OUTPUT_FILE = 'out.csv'
+
+# keys are fieldnames in input file, values are methods of class
+# Converter (see below)
+OPTIONS = {
+    'sex': 'gender',
+    'birthday': 'date',
+    'request_date': 'datetime',
+    }
+##
+## END OF CONFIG
+##
+
+import csv
+import datetime
+import sys
+
+class Converters():
+    """Converters to turn old-style values into new ones.
+    """
+    @classmethod
+    def gender(self, value):
+        """ 'True'/'False' --> 'female'/'male'/''
+        """
+        if value == 'True':
+            value = 'female'
+        elif value == 'False':
+            value = 'male'
+        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('/', '-')
+        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
+
+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 = dict([(x, x) for x in 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 "Ouput written to %s" % OUTPUT_FILE
