A Pure Python Protobuf 3 .proto Parser
Project description
Protobuf Schema Parser
Protobuf Schema Parser is a pure-Python library that parses and writes Protobuf schemas to and from an abstract syntax tree (AST).
The library uses proto_schema_parser.parser.Parser
to parse the CST into an AST. The proto_schema_parser.generator.Generator
class converts the AST back into a CST (a Protobuf schema string).
The lexer and parser are autogenerated from Buf's ANTLR lexer and parser grammar files.
Features
- ✅ proto2 and proto3 support
- ✅ message, field, enum, optional, required, repeated
- ✅ import, package, oneof, map, and option
- ✅ group and extend (in proto2)
- ⬜ service, rpc, and stream
Installation
Install the package via pip:
pip install protobuf-schema-parser
Usage
To parse a protobuf schema, create a Parser
object and call the parse
method:
from proto_schema_parser.parser import Parser
text = """
syntax = "proto3";
message SearchRequest {
string query = 1;
int32 page_number = 2;
int32 result_per_page = 3;
}
"""
result = Parser().parse(text)
This will return an AST object (ast.File
) representing the parsed protobuf schema.
File(syntax='proto3',
file_elements=[Message(name='SearchRequest',
elements=[Field(name='query',
number=1,
type='string',
cardinality=None,
options=[]),
Field(name='page_number',
number=2,
type='int32',
cardinality=None,
options=[]),
Field(name='result_per_page',
number=3,
type='int32',
cardinality=None,
options=[])])])
To write the AST back to a protobuf schema, create a Generator
object and call the generate
method:
from proto_schema_parser.generator import Generator
proto = Generator().generate(result)
This will print:
syntax = "proto3";
message SearchRequest {
string query = 1;
int32 page_number = 2;
int32 result_per_page = 3;
}
Contributing
I welcome contributions!
- Submit a PR and I'll review it as soon as I can.
- Open an issue if you find a bug or have a feature request.
License
Protobuf Schema Parser is licensed under the MIT license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for proto_schema_parser-0.4.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3e3524ebb7c3c739e2306f02cf1f2fb6bb119578004fb7cc5adb4b3d8bf50440 |
|
MD5 | c1739b9876dbae4cae39abb424326a59 |
|
BLAKE2b-256 | 47e07a56a918337d13cc0e1b730cccf06b9ecd6e9cb73fcc2258676e194ad974 |
Hashes for proto_schema_parser-0.4.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7e7c625d94f6291c59835ca1f02dfc3ba39b4e68354b79bfca59228700c332d8 |
|
MD5 | 58b19a2a41e092f1b3728a97d481b677 |
|
BLAKE2b-256 | f67ac1b4d7171b19c9376aa5110e4d6410a62db5abc37e52b26cdd8067b77fa7 |