Changeset 4832 for waeup


Ignore:
Timestamp:
17 Jan 2010, 14:06:56 (15 years ago)
Author:
uli
Message:

Add certificate-course processor.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • waeup/branches/ulif-importers/src/waeup/utils/batching.py

    r4831 r4832  
    1515from waeup.interfaces import (IBatchProcessor, IFacultyContainer, IFaculty,
    1616                              ICourse, IDepartment, ICertificate,
    17                               ISchemaTypeConverter, FatalCSVError)
     17                              ICertificateCourse, ISchemaTypeConverter,
     18                              FatalCSVError)
    1819
    1920class BatchProcessor(grok.GlobalUtility):
     
    108109        return result
    109110       
    110     def getMapping(self, path, headerfields):
     111    def getMapping(self, path, headerfields, mode):
    111112        """Get a mapping from CSV file headerfields to actually used
    112113           fieldnames.
    113114        """
     115        result = dict()
    114116        reader = csv.reader(open(path, 'rb'))
    115117        raw_header = reader.next()
    116         return dict([(x, headerfields[raw_header.index(x)])
    117                      for x in raw_header])
     118        for num, field in enumerate(headerfields):
     119            if field not in self.location_fields and mode == 'remove':
     120                # Ignore non-location fields when removing...
     121                field = '--IGNORE--'
     122            result[raw_header[num]] = field
     123        return result
    118124
    119125    def getFieldConverters(self, fieldnames):
     
    176182        for key, value in row.items():
    177183            setattr(obj, key, value)
    178         return
    179 
    180     def createLogfile(self, path, fail_path, num, warnings, mode, user):
     184        parent = self.getParent(row, site)
     185        parent[row['name']] = obj
     186        return
     187
     188    def createLogfile(self, path, fail_path, num, warnings, mode, user,
     189                      timedelta):
    181190        """Write a log file.
    182191        """
     
    188197        fd.write("User: %s\n" % user)
    189198        fd.write("Failed datasets: %s\n" % fail_path)
     199        fd.write("Processing time: %0.3f s (%0.4f s/item)\n" % (
     200                timedelta, timedelta/(num or 1)))
    190201        fd.write("Processed: %s lines (%s successful/ %s failed)\n" % (
    191202                num, num - len(warnings), len(warnings)
     
    202213        """Perform actual import.
    203214        """
     215        time_start = time.time()
    204216        self.checkHeaders(headerfields, mode)
    205         mapping = self.getMapping(path, headerfields)
     217        mapping = self.getMapping(path, headerfields, mode)
    206218        converters = self.getFieldConverters(headerfields)
    207219        reader = csv.DictReader(open(path, 'rb'))
     
    253265                self.updateEntry(obj, row, site)
    254266
     267        time_end = time.time()
     268        timedelta = time_end - time_start
     269       
    255270        if path.endswith('.pending'):
    256271            os.unlink(path)
    257272            os.rename(failed_path, path)
    258273            failed_path = path
    259         self.createLogfile(path, failed_path, num, warnings, mode, user)
     274        self.createLogfile(path, failed_path, num, warnings, mode, user,
     275                           timedelta)
    260276        if len(warnings) == 0:
    261277            del failed_writer
     
    441457        del parent[row['code']]
    442458        return
     459
     460class CertificateCourseProcessor(BatchProcessor):
     461    """A batch processor for ICertificateCourse objects.
     462    """
     463    grok.provides(IBatchProcessor)
     464    grok.context(Interface)
     465    util_name = 'certificatecourseimporter'
     466    grok.name(util_name)
     467
     468    name = u'CertificateCourse importer'
     469    iface = ICertificateCourse
     470
     471    location_fields = ['course', 'level', 'faculty_code', 'department_code',
     472                       'certificate_code',]
     473    factory_name = 'waeup.CertificateCourse'
     474
     475    def parentsExist(self, row, site):
     476        if not 'faculties' in site.keys():
     477            return False
     478        if not row['faculty_code'] in site['faculties'].keys():
     479            return False
     480        faculty = site['faculties'][row['faculty_code']]
     481        if not row['department_code'] in faculty.keys():
     482            return False
     483        dept = faculty[row['department_code']]
     484        return row['certificate_code'] in dept.certificates.keys()
     485
     486    def entryExists(self, row, site):
     487        if not self.parentsExist(row, site):
     488            return False
     489        parent = self.getParent(row, site)
     490        code = "%s_%s" % (row['course'].code, row['level'])
     491        return code in parent.keys()
     492
     493    def getParent(self, row, site):
     494        dept = site['faculties'][row['faculty_code']][row['department_code']]
     495        return dept.certificates[row['certificate_code']]
     496
     497    def getEntry(self, row, site):
     498        if not self.entryExists(row, site):
     499            return None
     500        parent = self.getParent(row, site)
     501        return parent.get(row['course'])
     502
     503    def addEntry(self, obj, row, site):
     504        parent = self.getParent(row, site)
     505        parent.addCourseRef(row['course'],
     506                            row['level'], row['core_or_elective'])
     507        return
     508
     509    def delEntry(self, row, site):
     510        parent = self.getParent(row, site)
     511        parent.delCourseRef(row['course'].code, row['level'])
     512        return
Note: See TracChangeset for help on using the changeset viewer.