python 3类型暗示的protobuf绑定生成器
protobuf-gen的Python项目详细描述
动机
这是一个库,用于生成类型提示的协议缓冲区绑定。这个工具解决了原始的两个主要问题 google cproto编译器python输出:
- cproto不支持导出具有不同于所提供的包根的代码 由原始的.proto文件作者编写,因此期望协议用户通过代码sed(参见问题 881和1491)。
- 它在运行时构建接口,因此不允许使用静态类型检查程序并禁用 在你最喜欢的IDE中自动完成。
示例
此示例可用于在python中构建功能齐全的etcd3客户机。否则请参见tests。
让我们从克隆包含要编译的必要.proto文件的存储库开始。ETCD3引用文件 来自几个protobuf库,所以我们需要所有这些。
mkdir pb-includes
# etcd references it's own .proto files against the root of their own repository, so the doubling here is intended
git clone https://github.com/coreos/etcd.git ./pb-includes/etcd/etcd
git clone https://github.com/grpc-ecosystem/grpc-gateway.git ./pb-includes/grpc-gateway
git clone https://github.com/gogo/protobuf.git ./pb-includes/protobuf
现在,让我们实际生成文件。
fromprotobuf_genimportremap,wrap# all of the _pb2 modules will now be importable through `etcd3py.pb_mods.*`# for example a module "google/api/http.proto" will be available as "etcd3py.pb_mods.google.api.http_pb2"remap(# the working directory is given as the parent directory of the package folder (etcd3py in this case).'.','etcd3py.pb_mods,# .proto include directories['./pb-includes/grpc-gateway/third_party/googleapis','./pb-includes/etcd','./pb-includes/protobuf',],# .proto files to be included in the distribution['etcd/etcdserver/etcdserverpb/rpc.proto','etcd/mvcc/mvccpb/kv.proto','etcd/auth/authpb/auth.proto','google/api/annotations.proto','gogoproto/gogo.proto','google/api/http.proto',])