(path: str, messages: List[Any], recv: bool, progress_bar: Optional[ProgressBar])
| 93 | |
| 94 | |
| 95 | def process_file(path: str, messages: List[Any], recv: bool, progress_bar: Optional[ProgressBar]) -> None: |
| 96 | with open(path, 'rb') as f_in: |
| 97 | if progress_bar: |
| 98 | bytes_read = 0 |
| 99 | |
| 100 | while True: |
| 101 | if progress_bar: |
| 102 | # Update progress bar |
| 103 | diff = f_in.tell() - bytes_read - 1 |
| 104 | progress_bar.update(diff) |
| 105 | bytes_read = f_in.tell() - 1 |
| 106 | |
| 107 | # Read the Header |
| 108 | tmp_header_raw = f_in.read(TIME_SIZE + LENGTH_SIZE + MSGTYPE_SIZE) |
| 109 | if not tmp_header_raw: |
| 110 | break |
| 111 | tmp_header = BytesIO(tmp_header_raw) |
| 112 | time = int.from_bytes(tmp_header.read(TIME_SIZE), "little") # type: int |
| 113 | msgtype = tmp_header.read(MSGTYPE_SIZE).split(b'\x00', 1)[0] # type: bytes |
| 114 | length = int.from_bytes(tmp_header.read(LENGTH_SIZE), "little") # type: int |
| 115 | |
| 116 | # Start converting the message to a dictionary |
| 117 | msg_dict = {} |
| 118 | msg_dict["direction"] = "recv" if recv else "sent" |
| 119 | msg_dict["time"] = time |
| 120 | msg_dict["size"] = length # "size" is less readable here, but more readable in the output |
| 121 | |
| 122 | msg_ser = BytesIO(f_in.read(length)) |
| 123 | |
| 124 | # Determine message type |
| 125 | if msgtype not in MESSAGEMAP: |
| 126 | # Unrecognized message type |
| 127 | try: |
| 128 | msgtype_tmp = msgtype.decode() |
| 129 | if not msgtype_tmp.isprintable(): |
| 130 | raise UnicodeDecodeError |
| 131 | msg_dict["msgtype"] = msgtype_tmp |
| 132 | except UnicodeDecodeError: |
| 133 | msg_dict["msgtype"] = "UNREADABLE" |
| 134 | msg_dict["body"] = msg_ser.read().hex() |
| 135 | msg_dict["error"] = "Unrecognized message type." |
| 136 | messages.append(msg_dict) |
| 137 | print(f"WARNING - Unrecognized message type {msgtype} in {path}", file=sys.stderr) |
| 138 | continue |
| 139 | |
| 140 | # Deserialize the message |
| 141 | msg = MESSAGEMAP[msgtype]() |
| 142 | msg_dict["msgtype"] = msgtype.decode() |
| 143 | |
| 144 | try: |
| 145 | msg.deserialize(msg_ser) |
| 146 | except KeyboardInterrupt: |
| 147 | raise |
| 148 | except Exception: |
| 149 | # Unable to deserialize message body |
| 150 | msg_ser.seek(0, os.SEEK_SET) |
| 151 | msg_dict["body"] = msg_ser.read().hex() |
| 152 | msg_dict["error"] = "Unable to deserialize message." |
no test coverage detected