首页 文章

协议缓冲区导入解析

提问于
浏览
1

在阅读这个相当长的问题之前,我提出了一个错误https://github.com/GoogleCloudPlatform/python-docs-samples/issues/1103 .

Proto Packages and Name Resolution的文档说明

您可以通过导入来使用其他.proto文件中的定义 . 要导入另一个.proto的定义,可以在文件顶部添加import语句 .

我的 example.proto 取决于annotations.prototranscode HTTP/JSON to gRPC . 这是一个简单的例子,但请注意我使用googleapis/google/api Git repo的导入路径(即 google/api/annotations.proto ):

syntax = "proto3";
import "google/api/annotations.proto";

message MyExample {
  // Message definition here.
}

注意,annotations.proto取决于http.proto - 他们是同一个包中的兄弟姐妹(googleapis/google/api

enter image description here

我的本地项目目录包含三个.proto文件:

  • example.proto

  • google/api/annotations.proto

  • google/api/http.proto

......或作为一棵树:

|____google
| |____api
| | |____annotations.proto
| | |____http.proto
|____example.proto

目标(或'out')目录也被添加,准备接收生成的python文件:

|____generated_pb2
| |____google
| | |____api

我的完整项目目录结构是:

  • example.proto

  • google/api/annotations.proto

  • google/api/http.proto

  • generated_pb2/google/api

......或作为一棵树:

|____example.proto
|____google
| |____api
| | |____annotations.proto
| | |____http.proto
|____generated_pb2
| |____google
| | |____api

有了这个,我可以编译我的protos(添加格式以提高可读性):

python -m grpc_tools.protoc
  --python_out=generated_pb2
  --grpc_python_out=generated_pb2
  -I ~/protoc/include/google/protobuf
  -I /google/api
  example.proto

打破这个:

  • generated_pb2 - 生成的python文件和gprc文件的目标 .

  • ~/protoc/include/google/protobuf - 自annotations.proto以来需要的protoc二进制文件附带的公共protos的位置取决于google/protobuf/descriptor.proto .

  • google/api - annotations.protohttp.proto 的位置

这编译 example.proto 给出:

  • generated_pb2/example_pb2.py

  • generated_pb2/example_pb2_gprc.py

但是 generated_pb2/example_pb2.py 的第一行导入 annotations.proto 生成的文件:

from google.api import annotations_pb2 as google_dot_api_dot_annotations__pb2

这个文件没有't exist. No problem, I' ll分别编译 annotations.proto

python -m grpc_tools.protoc
  --python_out=generated_pb2/google/api
  --grpc_python_out=generated_pb2/google/api
  -I ~/protoc/include/google/protobuf
  -I google/api annotations.proto

打破这个:

  • generated_pb2/google/api - 生成的python文件和gprc文件的目标 .

  • ~/protoc/include/google/protobuf - 自annotations.proto以来需要的protoc二进制文件附带的公共protos的位置取决于google/protobuf/descriptor.proto .

  • google/api - annotations.proto 所依据的 http.proto 的位置 .

不幸的是我在这一点上得到了一个错误:

google/api/http.proto: File not found.
annotations.proto: Import "google/api/http.proto" was not found or had errors.
annotations.proto:30:3: "HttpRule" is not defined.

我想这是因为 annotations.protogoogle/api 中寻找 http.proto

syntax = "proto3";
package google.api;

import "google/api/http.proto";
import "google/protobuf/descriptor.proto";

但是,目前还不清楚这种依赖关系是如何解决的 . protoc --help 记录 -I 标志:

-IPATH, --proto_path=PATH   Specify the directory in which to search for
                            imports.  May be specified multiple times;
                            directories will be searched in order.  If not
                            given, the current working directory is used.

annotations.proto 取决于 http.proto 如何解决?

1 回答

  • 0

    因为我正在尝试同样的事情,我想出了一个可能的解决方案,使用Makefile来创建适当的文件 . 因为我正在使用python进行测试,我安装了grpc python包并通过python使用protoc而不是直接使用它,但输入和结果应该是相同的 .

    每个 protoc 调用中使用的一般protobuf标志:

    GRPC_FLAGS := \
        -I. \
        -I/usr/local/include \
        -I$(GOPATH)/src \
        -I$(GOPATH)/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis
    

    源代

    源代码特定标志:

    CLIENT_FLAGS := \
        --proto_path=./protos \       <-- This is where my *.proto live
        --python_out=grpctest/client \
        --grpc_python_out=grpctest/client
    

    调用 protoc 从* .proto生成项目特定协议

    python3 -m grpc_tools.protoc $(CLIENT_FLAGS) $(GRPC_FLAGS) protos/*.proto
    

    注释生成

    注释特定标志:

    CLIENT_GW_FLAGS := \
        --python_out=grpctest/client \
        --grpc_python_out=grpctest/client
    

    调用 protoc 以生成注释特定文件:

    python3 -m grpc_tools.protoc $(CLIENT_GW_FLAGS) $(GRPC_FLAGS) google/api/annotations.proto
    python3 -m grpc_tools.protoc $(CLIENT_GW_FLAGS) $(GRPC_FLAGS) google/api/http.proto
    

    最终文件系统结构

    ├── client.py
    ├── config.yml
    ├── file
    ├── google
    │   └── api
    │       ├── __pycache__
    │       ├── annotations_pb2.py
    │       ├── annotations_pb2_grpc.py
    │       ├── http_pb2.py
    │       └── http_pb2_grpc.py
    ├── grpctest_pb2.py
    └── grpctest_pb2_grpc.py
    

相关问题