Skip to main content

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, including_default_value_fields=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 SomeMessage # import the schematics message class

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

Or use a message loaded in python by Protobuf:

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

# ... get your protobuf message as the pb class representation
schematics_root_message = SomeMessage.import_from_protobuf_message(protobuf_message)

Example

This proto file:

syntax = "proto3";

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

message ProtocolAndPorts {
    repeated uint32 ports = 3;
}

message FlowFilter {
    enum SomeEnum {
        VALUE = 0;
    };
    string id = 1 [deprecated = true];
    SomeEnum 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(ProtobufMessageModel):
    ports = ListType(IntType())


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

    id = StringType()
    consumer_filter_id = EnumType(SomeEnum)
    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

TODO

  • Add more meaningful unittests.

  • Improve Protobuf support and integration.

  • add support for parsing the Protobuf generated files as it is more comprehensive than the current Pyrobuf support.

Read for Release

0.4.0 (2019-01-16)

  • Added more meaningful unittests.

  • Export Types and Models out of the templates.

  • Export heavy definition logic (such as field definition) from the jinja template to pythonic filters.

  • Improve integration with Protobuf.

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.

Metadata

Release files for protobuf-schematics 0.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for protobuf-schematics 0.4.1
File Size Uploaded
protobuf_schematics-0.4.1.tar.gz 16.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for protobuf-schematics 0.4.1
File Interpreter ABI Platform
protobuf_schematics-0.4.1-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 25.1 kB

Release files / protobuf_schematics-0.4.1.tar.gz

Download URL protobuf_schematics-0.4.1.tar.gz
Size 16.2 kB
Tags Source
SHA-256 checksum
How to use checksums
b401ac3e657737b9c51b8f5aad93e14726d5a926ecefbdb73f04cc6d26c53d97
BLAKE2b-256 checksum
How to use checksums
64ce3cd78f17239db2a41924844beab2ade1d4a6364f4ab588f6ee7ed9dd19ed
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.7.2 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/3.6.3

Release files / protobuf_schematics-0.4.1-py2.py3-none-any.whl

Download URL protobuf_schematics-0.4.1-py2.py3-none-any.whl
Size 8.9 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
3b203893d61e27e303982719f85609706048ebed3c4bc8068d308ea237b64c55
BLAKE2b-256 checksum
How to use checksums
8eaabeb58a77b5f7b777e0ca664035069e86ca48dd8725ff6359e29b7efd07b8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.7.2 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/3.6.3

Release history Release notifications | RSS feed

This release

0.4.1 This release

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page