def write_file(interface, filename): first_timestamp = None with open(filename, "r") as output_file: corrupt_entries = 0 message_count = 0 start_time = time.time() for line in output_file: try: parsed_message = JsonFormatter.deserialize( line.encode("utf-8")) if not isinstance(parsed_message, dict): raise ValueError() except ValueError: corrupt_entries += 1 else: # TODO at the moment it's taking longer to write all of # individual CAN messages than the time that actually # elapsed in receiving the trace - need to implement # batching to speed this up. right now this will never sleep # because it's always behind. timestamp = parsed_message.get('timestamp', None) # TODO this duplicates some code from sources/trace.py if timestamp is not None: first_timestamp = first_timestamp or timestamp target_time = start_time + (timestamp - first_timestamp) time.sleep(max(.0002, target_time - time.time())) message_count += 1 interface.write(**parsed_message) print(("%d lines sent" % message_count)) if corrupt_entries > 0: print(("%d invalid lines in the data file were not sent" % corrupt_entries))
def write_file(interface, filename): first_timestamp = None with open(filename, "r") as output_file: corrupt_entries = 0 message_count = 0 start_time = time.time() for line in output_file: try: parsed_message = JsonFormatter.deserialize(line.encode("utf-8")) if not isinstance(parsed_message, dict): raise ValueError() except ValueError: corrupt_entries += 1 else: # TODO at the moment it's taking longer to write all of # individual CAN messages than the time that actually # elapsed in receiving the trace - need to implement # batching to speed this up. right now this will never sleep # because it's always behind. timestamp = parsed_message.get('timestamp', None) # TODO this duplicates some code from sources/trace.py if timestamp is not None: first_timestamp = first_timestamp or timestamp target_time = start_time + (timestamp - first_timestamp) time.sleep(max(.0002, target_time - time.time())) message_count += 1 interface.write(**parsed_message) print("%d lines sent" % message_count) if corrupt_entries > 0: print("%d invalid lines in the data file were not sent" % corrupt_entries)
def write_file(controller, filename, raw=False): """Write File Routine @param controller the controller object instance. @param filename The specified filename to write the contents to. @param raw the raw object instance setting. True or False.""" with open(filename, "r") as output_file: corrupt_entries = 0 message_count = 0 start_time = time.time() for line in output_file: try: parsed_message = JsonFormatter.deserialize(line.encode("utf-8")) if not isinstance(parsed_message, dict): raise ValueError() except ValueError: corrupt_entries += 1 else: # TODO at the moment it's taking longer to write all of # individual CAN messages than the time that actually # elapsed in receiving the trace - need to implement # batching to speed this up. right now this will never sleep # because it's always behind. if 'timestamp' in parsed_message: time.sleep(max(.0002, ( parsed_message['timestamp'] + start_time) - time.time())) message_count += 1 controller.write(raw=raw, **parsed_message) print("%d lines sent" % message_count) if corrupt_entries > 0: print("%d invalid lines in the data file were not sent" % corrupt_entries)
def write_file(controller, filename, raw=False): with open(filename, "r") as output_file: corrupt_entries = 0 message_count = 0 start_time = time.time() for line in output_file: try: parsed_message = JsonFormatter.deserialize( line.encode("utf-8")) if not isinstance(parsed_message, dict): raise ValueError() except ValueError: corrupt_entries += 1 else: # TODO at the moment it's taking longer to write all of # individual CAN messages than the time that actually # elapsed in receiving the trace - need to implement # batching to speed this up. right now this will never sleep # because it's always behind. if 'timestamp' in parsed_message: time.sleep( max(.0002, (parsed_message['timestamp'] + start_time) - time.time())) message_count += 1 controller.write(raw=raw, **parsed_message) print("%d lines sent" % message_count) if corrupt_entries > 0: print("%d invalid lines in the data file were not sent" % corrupt_entries)
def _parse_json_message(self, message_buffer): parsed_message = None remainder = message_buffer message = "" if b"\x00" in message_buffer: message, _, remainder = message_buffer.partition(b"\x00") try: parsed_message = JsonFormatter.deserialize(message) if not isinstance(parsed_message, dict): raise ValueError() except ValueError: pass return parsed_message, remainder, len(message)
def read(self): """Read a line of data from the input source at a time.""" line = self.trace_file.readline() if line == '': if self.loop: self._reopen_file() else: self.trace_file.close() self.trace_file = None raise DataSourceError() message = JsonFormatter.deserialize(line) timestamp = message.get('timestamp', None) if self.realtime and timestamp is not None: self._store_timestamp(timestamp) self._wait(self.starting_time, self.first_timestamp, timestamp) return line + "\x00"
def read(self): """Read a line of data from the input source at a time.""" line = self.trace_file.readline() if line == "": if self.loop: self._reopen_file() else: self.trace_file.close() self.trace_file = None raise DataSourceError() message = JsonFormatter.deserialize(line) timestamp = message.get("timestamp", None) if self.realtime and timestamp is not None: self._store_timestamp(timestamp) self._wait(self.starting_time, self.first_timestamp, timestamp) return line + "\x00"
def _parse_message(self, message_buffer): """If a message can be parsed from the given buffer, return it and remove it. Returns the message if one could be parsed, otherwise None, and the remainder of the buffer. """ if not isinstance(message_buffer, bytes): message_buffer = message_buffer.encode("utf-8") parsed_message = None remainder = message_buffer message = "" if b"\n" in message_buffer: message, _, remainder = message_buffer.partition(b"\n") try: parsed_message = JsonFormatter.deserialize(message) if not isinstance(parsed_message, dict): raise ValueError() except ValueError: pass return parsed_message, remainder, len(message)