Coder Social home page Coder Social logo

proto-schema-parser's Introduction

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 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.

proto-schema-parser's People

Contributors

criccomini avatar raffber avatar anneyang720 avatar jpihl avatar usefulalgorithm avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.