def __iter__(self) -> Event: """ Iterates over events in the current events file :return: An Event object :except: NotImplementedError if the stream is in non-blocking mode. :except: EventReadingError on reading error. """ while True: header_size = struct.calcsize('Q') header = self._read_and_check(header_size) if header is None: break event_size = struct.unpack('Q', header)[0] event_raw = self._read_and_check(event_size) if event_raw is None: raise EventReadingError('Unexpected end of events file') event = Event() event.ParseFromString(event_raw) yield event
def event_reader(filepointer): while True: # So, first read a Q eightbytes = filepointer.read(8) if len(eightbytes) != 8: break header, = struct.unpack("Q", eightbytes) fourbytes = filepointer.read(4) purported_header_crc32, = struct.unpack("I", fourbytes) data = filepointer.read(header) fourbytes = filepointer.read(4) purported_data_crc32, = struct.unpack("I", fourbytes) empiric_header_crc32 = masked_crc32c(struct.pack("Q", header)) empiric_data_crc32 = masked_crc32c(data) assert empiric_header_crc32 == purported_header_crc32 assert empiric_data_crc32 == purported_data_crc32 evt = Event() evt.ParseFromString(data) yield evt