示例#1
0
class ResourceIterator(object):
    def __init__(self, infile, spec, orig_spec, validate=False, debug=False):
        self.spec = spec
        self.table_schema = SchemaModel(orig_spec['schema'])
        self.validate = validate
        self.infile = infile
        self.debug = debug
        self.stopped = False

    def __iter__(self):
        return self

    def __next__(self):
        if self.stopped:
            raise StopIteration()
        if self.debug:
            logging.error('WAITING')
        line = self.infile.readline().strip()
        if self.debug:
            logging.error('INGESTING: %r', line)
        if line == '':
            self.stopped = True
            raise StopIteration()
        line = json.loads(line)
        if self.validate:
            for k, v in line.items():
                try:
                    self.table_schema.cast(k, v)
                except (InvalidCastError, TypeError):
                    field = self.table_schema.get_field(k)
                    if field is None:
                        raise ValueError('Validation failed: No such field %s',
                                         k)
                    else:
                        raise ValueError(
                            'Validation failed: Bad value %r '
                            'for field %s with type %s', v, k,
                            field.get('type'))

        return line

    def next(self):
        return self.__next__()