2
0

protoc-gen-dump.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/opt/esp/python_env/idf4.4_py3.8_env/bin/python
  2. import argparse
  3. import sys
  4. import os
  5. from google.protobuf.compiler import plugin_pb2 as plugin
  6. from google.protobuf.compiler.plugin_pb2 import CodeGeneratorResponse
  7. from urllib import parse
  8. import logging
  9. logger = logging.getLogger(__name__)
  10. logging.basicConfig(
  11. level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
  12. )
  13. # def main():
  14. # data = sys.stdin.buffer.read()
  15. # with open("C:/Users/sle11/Documents/VSCode/squeezelite-esp32/protobuf/generated/src/code_generator_request.bin", "wb") as file:
  16. # file.write(data)
  17. # if __name__ == "__main__":
  18. # main()
  19. def process(
  20. request: plugin.CodeGeneratorRequest, response: CodeGeneratorResponse, data
  21. ) -> None:
  22. logger.debug(f'Received parameter(s): {request.parameter}')
  23. params = request.parameter.split(',')
  24. param_dict = {p.split('=')[0]: parse.unquote(p.split('=')[1]) for p in params if '=' in p}
  25. param_dict['path'] = param_dict['path'].split('?')
  26. basename = "code_generator_request.bin"
  27. binpath = os.path.join(param_dict.get('binpath', './'),basename)
  28. file:CodeGeneratorResponse.File = response.file.add()
  29. file.name = f"{basename}.txt"
  30. file.content = f'Generated binary file {binpath}'
  31. logger.info(f"Dumping CodeGeneratorRequest object to : {binpath}")
  32. with open(binpath, "wb") as file:
  33. file.write(data)
  34. def GetData():
  35. parser = argparse.ArgumentParser(description='Process protobuf and JSON files.')
  36. parser.add_argument('--source', help='Python source file', default=None)
  37. args = parser.parse_args()
  38. if args.source:
  39. logger.info(f'Loading request data from {args.source}')
  40. with open(args.source, 'rb') as file:
  41. data = file.read()
  42. else:
  43. data = sys.stdin.buffer.read()
  44. return data
  45. def main():
  46. data = GetData()
  47. request = plugin.CodeGeneratorRequest.FromString(data)
  48. response = CodeGeneratorResponse()
  49. process(request, response,data)
  50. sys.stdout.buffer.write(response.SerializeToString())
  51. logger.info('Done dumping request')
  52. if __name__ == '__main__':
  53. main()