A Pure Python Protobuf .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
- ✅ line and block comment preservation
Installation
Install the package via pip:
pip install proto-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)
The proto
variable now contains the string:
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
File details
Details for the file proto_schema_parser-1.4.1.tar.gz
.
File metadata
- Download URL: proto_schema_parser-1.4.1.tar.gz
- Upload date:
- Size: 66.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: pdm/2.19.3 CPython/3.10.12 Linux/6.5.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c7e0d60a7c5cf0c3c8b73ca69033549c362233b7197e888f2255ba7240546c84 |
|
MD5 | f17abfe6f5d49b3e7bf6e38b596bad49 |
|
BLAKE2b-256 | c7892777991f8bfb5a216f6c9f66bdedfa83789d52d62df770ab59cc041f9d2e |
File details
Details for the file proto_schema_parser-1.4.1-py3-none-any.whl
.
File metadata
- Download URL: proto_schema_parser-1.4.1-py3-none-any.whl
- Upload date:
- Size: 63.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: pdm/2.19.3 CPython/3.10.12 Linux/6.5.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f7f60b2e4a275db194a45030c1c7e45a81060ce7f209db13e053f2b5c525edd7 |
|
MD5 | 16c09eb5db30cc71e1b61cf1f8d0553d |
|
BLAKE2b-256 | 442cf81812ae98a8cf25e47ea94062609eeb0af002002f25d0b61c5c8f97c089 |