Programmatically work with .proto file

647 views
Skip to first unread message

Timo L.

unread,
May 26, 2021, 9:35:37 AM5/26/21
to prot...@googlegroups.com
Hey,

I have to write a proxy that translates message flows from a custom message format to protobuf.
There is quite a lot messages to be converted and hence I want to create a proto file generator, preferably in Python (Others possible as well).

With generator I do not mean the protoc compiler, but a library or framework that allows me to read a .proto file, modify it's structure and write it back.

When doing Internet research I always end up with how to generate python code from protos, which is not my question.

Has someone ever seen such framework?

I have also read about Descriptors, but I guess they are rather about reading meta information, than creating or modifying files.

Of course I can also read them as plain file and parse the structure myself, but hope there is something already available.


Thanks!

Adam Cozzette

unread,
May 26, 2021, 9:58:36 AM5/26/21
to Timo L., Protocol Buffers
You can parse the .proto file into a serialized FileDescriptorSet like this: protoc --include_source_info --descriptor_set_out=descriptors.pb <proto files...>

The --include_source_info flag will cause the descriptors to include more information about the original .proto file, including the locations of declarations in the .proto file. I haven't tried this before but in principle you could use this information to make targeted edits to the file. In any case parsing the proto file is the easy part because you can rely on protoc to do that; writing out the modified file is the harder part.

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/protobuf/CALgCaPteNxt6pOUeCTr8B_uzGCVHh2n%2B2C3U%3DgUFyeO5LFtENQ%40mail.gmail.com.

TL

unread,
May 29, 2021, 1:01:38 PM5/29/21
to Protocol Buffers
Thanks for that hint!
I had a deeper look.
There is

std::string Descriptor::DebugString() const
Write the contents of this descriptor in a human-readable form.
Output will be suitable for re-parsing.

available in the C++ API, that would do the formatting back to a .proto.
But I cannot find something similar in Python3.

Any hint someone?

Reply all
Reply to author
Forward
0 new messages