def check_uploaded_files(project, contractor): """Iterate over all files, get parsers for them, try the parsers on the files. Returned is a list of errors, in the form of 3-tuples (current-file-path, original-filename, error-message).""" files = current_files(all_measurements(project, contractor)) specifics = project.specifics() errors = [] for path in files: with MovedFile(path) as moved_file: filename = os.path.basename(moved_file) parsers = specifics.parsers(filename) for parser in parsers: parse_object = parser_factory(parser, project, contractor, moved_file) result = parse_object.parse(check_only=True) if result.success: # Skip other parsers break elif not result.error: # Unsuccessful but no errors, parser not suited continue else: errors.append((path, filename, result.error)) break else: errors.append((path, filename, "No suitable parser found.")) return errors
def add_to_zipfile(project, contractor, measurement_type): """Iterate over all files, get parsers for them, try the parsers on the files. Returned is a list of errors, in the form of 3-tuples (current-file-path, original-filename, error-message).""" if measurement_type: mtypes = (measurement_type,) else: mtypes = tuple(models.MeasurementType.objects.filter(project=project)) zipped = zipfile.ZipFile("files.zip", "w") for mtype in mtypes: files = current_files(all_measurements(project, contractor). filter(scheduled__measurement_type=mtype)) for path in files: with MovedFile(path) as moved_file: archive_filename = os.path.join(mtype.mtype.slug, os.path.basename(moved_file)) print archive_filename zipped.write(path, archive_filename) zipped.close()