def stream(self): """ Yields Flow objects from the dump. """ # There is a weird mingw bug that breaks .tell() when reading from stdin. try: self.fo.tell() except IOError: # pragma: no cover can_tell = False else: can_tell = True off = 0 try: while True: data = tnetstring.load(self.fo) try: data = io_compat.migrate_flow(data) except ValueError as e: raise exceptions.FlowReadException(str(e)) if can_tell: off = self.fo.tell() if data["type"] not in models.FLOW_TYPES: raise exceptions.FlowReadException("Unknown flow type: {}".format(data["type"])) yield models.FLOW_TYPES[data["type"]].from_state(data) except ValueError: # Error is due to EOF if can_tell and self.fo.tell() == off and self.fo.read() == '': return raise exceptions.FlowReadException("Invalid data format.")
def stream(self): """ Yields Flow objects from the dump. """ # There is a weird mingw bug that breaks .tell() when reading from stdin. try: self.fo.tell() except IOError: # pragma: no cover can_tell = False else: can_tell = True off = 0 try: while True: data = tnetstring.load(self.fo) try: data = io_compat.migrate_flow(data) except ValueError as e: raise exceptions.FlowReadException(str(e)) if can_tell: off = self.fo.tell() if data["type"] not in models.FLOW_TYPES: raise exceptions.FlowReadException( "Unknown flow type: {}".format(data["type"])) yield models.FLOW_TYPES[data["type"]].from_state(data) except ValueError: # Error is due to EOF if can_tell and self.fo.tell() == off and self.fo.read() == '': return raise exceptions.FlowReadException("Invalid data format.")
def read_tnetstring(input): # tnetstring throw a ValueError on EOF, which is hard to catch # because they raise ValueErrors for a couple of other reasons. # Check for EOF to avoid this. if not input.read(1): return None else: input.seek(-1, 1) return tnetstring.load(input)