1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- import json
- import os
- import sys
- import argparse
- import importlib.util
- import logging
- from pathlib import Path
- from google.protobuf import json_format
- from google.protobuf.json_format import MessageToJson
- logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
- def load_protobuf_module(source, includes):
- """Dynamically load a protobuf module given its file name."""
- for include in includes:
- sys.path.append(include)
- module_name = Path(source).stem
- module_file = module_name + '_pb2.py'
- module_location = Path(source).parent / module_file
- logging.info(f'Loading module {module_file} from {module_location} with includes [{", ".join(includes)}]')
- spec = importlib.util.spec_from_file_location(name=module_name, location=str(module_location))
- if spec is not None:
- module = importlib.util.module_from_spec(spec)
- spec.loader.exec_module(module)
- logging.info(f'Loaded protobuf module: {module_name}')
- return module
- else:
- logging.error(f'Failed to load module {module_file} from {module_location}')
- return None
- def protobuf_to_dict(message):
- """Convert a protobuf message to a dictionary."""
-
- json_str = MessageToJson(message, including_default_value_fields=True)
-
- return json.loads(json_str)
- def main():
- parser = argparse.ArgumentParser(description='Process protobuf and JSON files.')
- parser.add_argument('--proto_file', help='Name of the protobuf file (without extension)')
- parser.add_argument('--main_class', help='Main message class to process')
- parser.add_argument('--source', help='Source file to parse')
- parser.add_argument('--include', help='Directory where message python files can be found', default=None,action = 'append' )
- parser.add_argument('--json', help='Source JSON file(s)',action = 'append' )
- args = parser.parse_args()
-
- logging.info(f'Loading modules')
- proto_module = load_protobuf_module(args.proto_file, args.include)
-
- main_message_class = getattr(proto_module, args.main_class)
- message = main_message_class()
- with open(args.source, 'rb') as bin_file:
- data =bin_file.read()
- message.ParseFromString(data)
- logging.info(f'Parsed: {MessageToJson(message)}')
-
-
-
-
-
-
-
-
-
-
-
-
- if __name__ == '__main__':
- main()
|