Skip to main content

Convert ProtoBuf proto file to cute Schematics classes file

Project description

ProtoBuf Schematics

https://img.shields.io/pypi/v/protobuf_schematics.svg https://img.shields.io/travis/AlmogCohen/protobuf_schematics.svg Documentation Status

Convert ProtoBuf proto file to cute Schematics classes file.

Google Protobuf is great when it comes to high performance schema aware APIs, but when Google designed Protobuf, it didn’t tried to make the generated code idiomatic in Python, which brings a problem when exporting messages outside interface modules or having nice IDE auto-completions. Schematics is a cute and Pythonic schema library that goes well with most applications. Why not join both?

Currently this package does not support the Protobuf binary format and will work with a any other textual representation which is easily generated with the original Protobuf API for any language. Ease of use was prioritized while writing this package rather than mere performance.

Usage

  1. Convert the proto file to python Schematics classes:

    protobuf_schematics <path-to-file.proto> generated_schematics_proto.py # or any output filename
  2. Convert your ProtoBuf message to Json:

In Java:

import com.google.protobuf.util.JsonFormat;

FileWriter file = new FileWriter("protoBufMessage.json")
JsonFormat.Printer printer = JsonFormat.printer().preservingProtoFieldNames();
String message = printer.print(someProtoBufMessage);
file.write(message)

or from Python:

import json
from google.protobuf.json_format import MessageToJson

json = MessageToJson(org, preserving_proto_field_name=True)
with open("protoBufMessage.json", 'w') as output:
    json.dump(json, output)
  1. In your project, load the message in python as Schematics object:

import json
from generated_schematics_proto import SomeClass # import the schematics message class

schematics_root_message = SomeClass(json.load(open('protoBufMessage.json')))

Example

This proto file:

syntax = "proto3";

enum IPAddressFamily {
    INVALID = 0;
    IPv4 = 1;
    IPv6 = 2;
};

message ProtocolAndPorts {
    repeated uint32 ports = 3;
}

message FlowFilter {
    enum InnerEnum {
        VALUE = 0;
    };
    string id = 1 [deprecated = true];
    InnerEnum consumer_filter_id = 2;
    map<string, ProtocolAndPorts> ports = 3;
    repeated ProtocolAndPorts protocol_and_ports = 4;
}

Will be converted to:

class IPAddressFamily(Enum):
    INVALID = 0
    IPv4 = 1
    IPv6 = 2


class ProtocolAndPorts(Model):
    ports = ListType(IntType())


class FlowFilter(Model):
    class InnerEnum(Enum):
        VALUE = 0

    id = StringType()
    consumer_filter_id = EnumType(InnerEnum)
    ports = DictType(ModelType(ProtocolAndPorts), str)
    protocol_and_ports = ListType(ModelType(ProtocolAndPorts))

Features

  • Support both Protobuf syntax 2 and 3.

  • Support builtin types such as StringType, IntType.

  • Support proto map fields as Schematics DictType.

  • Support repeated modifier as convert to ListType.

  • Support Enum class generation and custom Schematics EnumType.

  • Support custom schematics ByteArrayType base64 encoded byte arrays converted from Java.

Development

First, install the Pipfile and create the proper virtual environment:

pipenv install --dev

To check linting with flake8, run:

make lint

To run the unittests against your working python version:

py.test

To see coverage report:

make coverage

To run tests against all supported python versions:

tox

To make the docs (which will be automatically published to readthedocs on commits to the master branch):

make docs

Credits

The parsing work of .proto files is provided thanks to the awesome guys at PyroBuf.

This package was created with Cookiecutter and the elgertam/cookiecutter-pipenv project template, based on audreyr/cookiecutter-pypackage.

History

In Progress

  • Add more meaningful unittests.

0.2.0 (2019-01-14)

  • Improve README - Add usage examples.

  • Update the setup.py to point to official PyroBuf 0.8.5 PyPi release.

0.1.0 (2019-01-13)

  • First release on PyPI.

  • CLI interface for compiling a given proto file to schematics definition file.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

protobuf_schematics-0.2.0.tar.gz (10.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

protobuf_schematics-0.2.0-py2.py3-none-any.whl (5.8 kB view details)

Uploaded Python 2Python 3

File details

Details for the file protobuf_schematics-0.2.0.tar.gz.

File metadata

  • Download URL: protobuf_schematics-0.2.0.tar.gz
  • Upload date:
  • Size: 10.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.6.3

File hashes

Hashes for protobuf_schematics-0.2.0.tar.gz
Algorithm Hash digest
SHA256 84cc60814a2078758adebe8a56158f282e64b6d29c765cc937de40d4baf84f95
MD5 b1dd87744a3c804cf4a22e5059a6d178
BLAKE2b-256 80bce92ba9fb7feb02a5715ac9ac40145e9c3036a3591efda7aaf7ca50c21cce

See more details on using hashes here.

File details

Details for the file protobuf_schematics-0.2.0-py2.py3-none-any.whl.

File metadata

  • Download URL: protobuf_schematics-0.2.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.6.3

File hashes

Hashes for protobuf_schematics-0.2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 d7bd7073e6e0c996bde04f45187da7bbb2da8a406c6935525d7589540d499567
MD5 fd5f0c6cd815486cb20c25aa1ac70ebc
BLAKE2b-256 5bb31887b991bb19e8a0eb00b87a16b1546e9d08c4609424cf34a8b2eb636f8d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page