|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import importlib |
| 5 | + |
| 6 | +# Command-line argument parsing |
| 7 | +parser = argparse.ArgumentParser(description="Displays LCM message's data structure") |
| 8 | +parser.add_argument("--module", type=str, default="mbot_lcm_msgs", help="Module to use for decoding messages") |
| 9 | + |
| 10 | +subparsers = parser.add_subparsers(dest='command', help='sub-command help') |
| 11 | + |
| 12 | +# Sub-parser for the "show" command |
| 13 | +parser_show = subparsers.add_parser('show', help='Output the message definition') |
| 14 | +parser_show.add_argument('msgs', type=str, help='Comma-separated list of message names you want to check') |
| 15 | + |
| 16 | +# Sub-parser for the "list" command |
| 17 | +parser_list = subparsers.add_parser('list', help='List all available LCM messages') |
| 18 | + |
| 19 | +args = parser.parse_args() |
| 20 | + |
| 21 | +# Load the module for decoding messages if provided |
| 22 | +decode_module = None |
| 23 | +if args.module: |
| 24 | + try: |
| 25 | + decode_module = importlib.import_module(args.module) |
| 26 | + except ImportError: |
| 27 | + print(f"Error: Could not import module {args.module}") |
| 28 | + decode_module = None |
| 29 | + |
| 30 | +def read_all_lcm_type(): |
| 31 | + lcm_type_dict = dict() |
| 32 | + if decode_module: |
| 33 | + try: |
| 34 | + # Attempt to decode the message to find its type |
| 35 | + for attr in dir(decode_module): |
| 36 | + lcm_type_class = getattr(decode_module, attr) |
| 37 | + if isinstance(lcm_type_class, type) and hasattr(lcm_type_class, 'decode'): |
| 38 | + try: |
| 39 | + # lcm_type is the name of the message type |
| 40 | + lcm_type = lcm_type_class.__name__ |
| 41 | + # lcm_type_dict[lcm_type] is a list of tuple (data type, variable name) |
| 42 | + lcm_type_dict[lcm_type] = list(zip(lcm_type_class.__typenames__, lcm_type_class.__slots__)) |
| 43 | + except Exception: |
| 44 | + continue |
| 45 | + except Exception as e: |
| 46 | + lcm_type_dict['Error'] = str(e) |
| 47 | + |
| 48 | + return lcm_type_dict |
| 49 | + |
| 50 | +def inspect_class_attributes(cls): |
| 51 | + attributes = dir(cls) |
| 52 | + for attr in attributes: |
| 53 | + try: |
| 54 | + value = getattr(cls, attr) |
| 55 | + print(f"{attr}: {value}") |
| 56 | + except AttributeError: |
| 57 | + print(f"{attr}: <unavailable>") |
| 58 | + |
| 59 | +def show_msg_struct(msgs): |
| 60 | + msgs_to_print = msgs.split(',') |
| 61 | + lcm_type_dict = read_all_lcm_type() |
| 62 | + for msg in msgs_to_print: |
| 63 | + if msg not in lcm_type_dict.keys(): |
| 64 | + print(f"Error: {msg} is not a valid lcm message type!") |
| 65 | + continue |
| 66 | + print(f"{msg}\n") |
| 67 | + for dtype, vname in lcm_type_dict[msg]: |
| 68 | + print(f"{' ' * 4}{dtype:<10} {vname:<10}") |
| 69 | + print() |
| 70 | + |
| 71 | + |
| 72 | +def main(): |
| 73 | + if args.command == 'list': |
| 74 | + lcm_type_dict = read_all_lcm_type() |
| 75 | + for key in lcm_type_dict.keys(): |
| 76 | + print(f"{key}") |
| 77 | + elif args.command == 'show': |
| 78 | + show_msg_struct(args.msgs) |
| 79 | + |
| 80 | +if __name__ == "__main__": |
| 81 | + try: |
| 82 | + main() |
| 83 | + except KeyboardInterrupt: |
| 84 | + print("\nExiting gracefully...") |
0 commit comments