- Timestamp:
- 17 Jan 2010, 14:06:56 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
waeup/branches/ulif-importers/src/waeup/utils/batching.py
r4831 r4832 15 15 from waeup.interfaces import (IBatchProcessor, IFacultyContainer, IFaculty, 16 16 ICourse, IDepartment, ICertificate, 17 ISchemaTypeConverter, FatalCSVError) 17 ICertificateCourse, ISchemaTypeConverter, 18 FatalCSVError) 18 19 19 20 class BatchProcessor(grok.GlobalUtility): … … 108 109 return result 109 110 110 def getMapping(self, path, headerfields ):111 def getMapping(self, path, headerfields, mode): 111 112 """Get a mapping from CSV file headerfields to actually used 112 113 fieldnames. 113 114 """ 115 result = dict() 114 116 reader = csv.reader(open(path, 'rb')) 115 117 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 118 124 119 125 def getFieldConverters(self, fieldnames): … … 176 182 for key, value in row.items(): 177 183 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): 181 190 """Write a log file. 182 191 """ … … 188 197 fd.write("User: %s\n" % user) 189 198 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))) 190 201 fd.write("Processed: %s lines (%s successful/ %s failed)\n" % ( 191 202 num, num - len(warnings), len(warnings) … … 202 213 """Perform actual import. 203 214 """ 215 time_start = time.time() 204 216 self.checkHeaders(headerfields, mode) 205 mapping = self.getMapping(path, headerfields )217 mapping = self.getMapping(path, headerfields, mode) 206 218 converters = self.getFieldConverters(headerfields) 207 219 reader = csv.DictReader(open(path, 'rb')) … … 253 265 self.updateEntry(obj, row, site) 254 266 267 time_end = time.time() 268 timedelta = time_end - time_start 269 255 270 if path.endswith('.pending'): 256 271 os.unlink(path) 257 272 os.rename(failed_path, path) 258 273 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) 260 276 if len(warnings) == 0: 261 277 del failed_writer … … 441 457 del parent[row['code']] 442 458 return 459 460 class 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.