项目作者: manuelzander

项目描述 :
A custom Python protoc plugin
高级语言: Python
项目地址: git://github.com/manuelzander/python-protoc-plugin.git
创建时间: 2020-12-03T18:50:43Z
项目社区:https://github.com/manuelzander/python-protoc-plugin

开源协议:MIT License

下载


python-protoc-example

last-commit issues-pr-raw license

A basic skeleton for a custom Python plugin for protoc, Google’s official compiler for protobuf.

A plugin is just a program which reads a CodeGeneratorRequest protocol buffer from standard input and then writes a CodeGeneratorResponse protocol buffer to standard output. These message types are defined in plugin.proto. We recommend that all third-party code generators be written as plugins, as this allows all generators to provide a consistent interface and share a single parser implementation.

I’ve also written two small blog posts about this example, see here and here.

Prerequisites

python

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.

I ran and tested the code with Python 3.9 on macOS 10.15.7.

Environment

Clone the repo to your local machine.

Create a virtual environment for Python 3 with:

  1. python3 -m pip install virtualenv
  2. python3 -m virtualenv env

Activate the virtual environment with:

  1. source env/bin/activate

Install the required Python packages with:

  1. pip3 install -r requirements.txt

To install protoc (on Mac):

  1. brew install protobuf

Validate your installation with:

  1. protoc --version

The output should be libprotoc 3.14.0 or similar.

On Linux you can use:

  1. apt install -y protobuf-compiler

Run the example

Make sure the virtual environment is activated.

Simply run make, or the below to start the code generation:

  1. protoc example.proto --plugin=protoc-gen-custom-plugin=./plugin.py --custom-plugin_out=.

Note that custom-plugin is both the last portion of the plugin name and the first part of the out argument.

Authors

  • Manuel Zander