Skip to main content

Google Protocol Buffers tools.

Project description

buildstatus coverage codecov

About

Google Protocol Buffers tools in Python 3.6+.

  • C source code generator.

  • Only supports proto3.

Known limitations:

  • Oneof, imports and recursive messages are not yet supported.

  • Options, services (gRPC) and maps are ignored.

Project homepage: https://github.com/eerimoq/pbtools

Documentation: http://pbtools.readthedocs.org/en/latest

Installation

pip install pbtools

Example usage

C source code

In this example we use the simple proto-file hello_world.proto.

syntax = "proto3";

package hello_world;

message Foo {
    int32 bar = 1;
}

Generate C source code from the proto-file.

$ pbtools generate_c_source examples/hello_world/hello_world.proto
Successfully created pbtools.[hc] and hello_world.[hc].

See hello_world.h and hello_world.c for the contents of the generated files.

We’ll use the generated types and functions below.

struct hello_world_foo_t {
   struct pbtools_message_base_t base;
   int32_t bar;
};

struct hello_world_foo_t *hello_world_foo_new(
    void *workspace_p,
    size_t size);

int hello_world_foo_encode(
    struct hello_world_foo_t *self_p,
    void *encoded_p,
    size_t size);

int hello_world_foo_decode(
    struct hello_world_foo_t *self_p,
    const uint8_t *encoded_p,
    size_t size);

Encode and decode the Foo-message.

#include <stdio.h>
#include "hello_world.h"

int main(int argc, const char *argv[])
{
    int size;
    uint8_t workspace[64];
    uint8_t encoded[16];
    struct hello_world_foo_t *foo_p;

    /* Encode. */
    foo_p = hello_world_foo_new(&workspace[0], sizeof(workspace));

    if (foo_p == NULL) {
        return (1);
    }

    foo_p->bar = 78;
    size = hello_world_foo_encode(foo_p, &encoded[0], sizeof(encoded));

    if (size < 0) {
        return (2);
    }

    printf("Successfully encoded Foo into %d bytes.\n", size);

    /* Decode. */
    foo_p = hello_world_foo_new(&workspace[0], sizeof(workspace));

    if (foo_p == NULL) {
        return (3);
    }

    size = hello_world_foo_decode(foo_p, &encoded[0], size);

    if (size < 0) {
        return (4);
    }

    printf("Successfully decoded %d bytes into Foo.\n", size);
    printf("Foo.bar: %d\n", foo_p->bar);

    return (0);
}

See hello_world for all files used in this example.

Command line tool

The generate C source subcommand

Below is an example of how to generate C source code from a proto-file.

$ pbtools generate_c_source examples/address_book/address_book.proto
Successfully created pbtools.[hc] and address_book.[hc].

See address_book.h and address_book.c for the contents of the generated files.

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

pbtools-0.5.0.tar.gz (75.1 kB view details)

Uploaded Source

Built Distribution

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

pbtools-0.5.0-py2.py3-none-any.whl (22.7 kB view details)

Uploaded Python 2Python 3

File details

Details for the file pbtools-0.5.0.tar.gz.

File metadata

  • Download URL: pbtools-0.5.0.tar.gz
  • Upload date:
  • Size: 75.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for pbtools-0.5.0.tar.gz
Algorithm Hash digest
SHA256 78fe45bddbfe11b99c77138e69e0bbb1f58908da84797898f5f861316094f805
MD5 77468f619c7c8fdb1572c4611246d3a7
BLAKE2b-256 4d3bdcf8867c2fcf7257931f2fad270a4b23b17f3949cf23dc896663247ddfaf

See more details on using hashes here.

File details

Details for the file pbtools-0.5.0-py2.py3-none-any.whl.

File metadata

  • Download URL: pbtools-0.5.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 22.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for pbtools-0.5.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 3b00dde20da8b720f273c1f58726e37117272ce03cb2e62914d4fb1e8e37b4a7
MD5 e60a4ddd5b27042e8a54b575dbf3eec2
BLAKE2b-256 1318af76b9f2afc407192121320c4ab76e426a384f5377c3bbff32636dbd5bb3

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