Protocol buffer code generation (protobuf)¶
Description¶
The protobuf module supports code generation for protocol buffers.
To use the module in inline code generation import it by:
cogeno.import_module('protobuf')
In case you want to use the protobuf module in another Python project import it by:
import cogeno.modules.protobuf
Code generation functions¶
-
cogeno.modules.protobuf.proto_gen(generator='nanopb', force_generate=False)¶ Get protocol buffer code prepared for cogeno use.
- Return
Protocol buffer code database
- Parameters
generator: The protocol buffer generator to use to generate the protocol buffer code. One of ‘nanopb’, ‘pbtools’, ‘protobluff’, ‘cpp’, ‘python’. Default is ‘nanopb’.force_generate: Force generation of the protocol buffer code, even if already generated. Default is False.
Generate code with protobuf¶
Protocol buffers are a language-neutral, platform-neutral extensible mechanism for serializing structured data. There exist several implementations for a lot of programming languages.
The protobuf module supports the following code generators:
Language |
Generator |
|---|---|
C |
|
C |
|
C |
|
C++ |
|
Python |
The protocol buffer code that is generated by a generator is added to the protocol buffer code database.
-
cogeno.modules.protobuf.ProtoCodeDb : public MutableMapping Protocol buffers code database.
All generated code is cached in dictionary.
Usage:
cogeno.module_import('protobuf') db = protobuf.protodb("nanopb") db2 = protobuf.protodb("py") cogeno.out(db['my_message.pb.h']) cogeno.out(db('pb_encode.c'])
Public Functions
-
__init__(self, generator=None, sources=None, include_dirs=None, dst_dir=None)¶ Initialize protocol buffer code database.
- Parameters
generator: Any of ‘nonopb’, …sources: *.proto sourcesinclude_dirs: Directories to search for *.proto importdst_dir: Destination directory for generated code
-
__getitem__(self, key)¶
-
__setitem__(self, key, value)¶
-
__delitem__(self, key)¶
-
__iter__(self)¶
-
__len__(self)¶
-
add_sources(self, sources)¶ Add sources to the list of sources for generation.
- Return
True in case new sources were added, False otherwise.
- Parameters
sources: List of source file pathes
-
add_include_dirs(self, include_dirs)¶ Add include directories to the list for generation.
- Return
True in case new include dirs were added, False otherwise.
- Parameters
include_dirs: List of include dir pathes
-
load(self)¶ Load generated code into database.
-
generate(self)¶ Generate protocol buffer code.
- Note
This routine will use grpcio-provided protoc if it exists, and is using the system-installed protoc as a fallback.
- Return
protoc invocation return value
-
Generate protobuf code using protobluff¶
There is no uptodate PyPI package available for protobluff. The protobluff plugin for protoc has to be build and installed from source as described in the protobluff documentation. The source code is available on GitHub.
On Ubuntu you might use this script (setup_protobluff.sh):
#! /usr/bin/env sh
# Install protobluff to user local
# Ubuntu
sudo apt install git gcc automake libtool protobuf-compiler libprotoc-dev libprotobuf-dev check
# get the sources
git clone https://github.com/squidfunk/protobluff protobluff.git
cd protobluff.git
# configure
./autogen.sh
./configure --prefix ${HOME}/.local
# build & test & install to user dir
make
make test
make install
# remove the git repository
cd ..
rm -rf protobluff.git
The protobuf module searches the pathes defined in the environment for protoc-gen-protobluff.