|
| 1 | +import pathlib |
| 2 | +import sys |
| 3 | + |
| 4 | +import blackboxprotobuf |
| 5 | +import ccl_segb.ccl_segb1 as ccl_segb1 |
| 6 | +import ccl_segb.ccl_segb2 as ccl_segb2 |
| 7 | +from rich import inspect, pretty, print_json |
| 8 | +from rich.console import Console |
| 9 | +from rich.traceback import install |
| 10 | + |
| 11 | +install(show_locals=True) |
| 12 | +pretty.install() |
| 13 | + |
| 14 | +console = Console() |
| 15 | + |
| 16 | + |
| 17 | +def insp(arg): |
| 18 | + return inspect(arg, all=True, help=True) |
| 19 | + |
| 20 | + |
| 21 | +def print_d(input_dict): |
| 22 | + return print_json(data=input_dict) |
| 23 | + |
| 24 | + |
| 25 | +if __name__ == "__main__": |
| 26 | + if len(sys.argv) < 2: |
| 27 | + print(f"USAGE: {pathlib.Path(sys.argv[0]).name} <SEGB file>") |
| 28 | + print() |
| 29 | + sys.exit(1) |
| 30 | + |
| 31 | + input_path = sys.argv[1] |
| 32 | + result = [] |
| 33 | + if ccl_segb1.file_matches_segbv1_signature(input_path): |
| 34 | + for record in ccl_segb1.read_segb1_file(input_path): |
| 35 | + offset = record.data_start_offset |
| 36 | + state = record.state |
| 37 | + data = record.data |
| 38 | + ts1 = record.timestamp1 |
| 39 | + ts2 = record.timestamp2 |
| 40 | + |
| 41 | + if not any(data): # null-padded record |
| 42 | + continue |
| 43 | + |
| 44 | + message, typedef = blackboxprotobuf.decode_message(data) |
| 45 | + |
| 46 | + result.append( |
| 47 | + { |
| 48 | + "offset": offset, |
| 49 | + "state": state, |
| 50 | + "ts1": ts1.strftime("%Y-%m-%d %H:%M:%S %Z"), |
| 51 | + "ts2": ts2.strftime("%Y-%m-%d %H:%M:%S %Z"), |
| 52 | + "message": message, |
| 53 | + } |
| 54 | + ) |
| 55 | + # print_d(result) |
| 56 | + console.print(result) |
| 57 | + elif ccl_segb2.file_matches_segbv2_signature(input_path): |
| 58 | + for record in ccl_segb2.read_segb2_file(input_path): |
| 59 | + offset = record.data_start_offset |
| 60 | + metadata_offset = record.metadata.metadata_offset |
| 61 | + state = record.metadata.state.name |
| 62 | + ts = record.metadata.creation |
| 63 | + data = record.data |
| 64 | + |
| 65 | + if not any(data): # null-padded record |
| 66 | + continue |
| 67 | + message, typedef = blackboxprotobuf.decode_message(data) |
| 68 | + result.append( |
| 69 | + { |
| 70 | + "offset": offset, |
| 71 | + "metadata_offset": metadata_offset, |
| 72 | + "state": state, |
| 73 | + "ts": ts.strftime("%Y-%m-%d %H:%M:%S %Z"), |
| 74 | + "message": message, |
| 75 | + } |
| 76 | + ) |
| 77 | + # print_d(result) |
| 78 | + console.print(result) |
| 79 | + |
| 80 | + else: |
| 81 | + print("File is not a SEGB File") |
| 82 | + sys.exit(1) |
| 83 | + print() |
0 commit comments