Skip to main content

Python protocol buffers for the rest of us

Project description

Protocol Buffers for the Rest of Us

CI

Motivation

Are you annoyed by having to fix the absolute imports generated by protoc?

If so, then protoletariat is the tool for you.

protoletariat has one goal: fixing the broken imports for the Python code generated by protoc.

See https://github.com/protocolbuffers/protobuf/issues/1491 for the discussion that inspired this tool.

Installation

Artifact Status Installation Command
PyPI Package PyPI pip install protoletariat
Conda Package Conda Version conda install protoletariat -c conda-forge
Docker Image docker pull ghcr.io/cpcloud/protoletariat:latest

Note: the conda-forge package version may lag behind the other artifacts by a few hours.

Usage

protoletariat is designed to be run as a post-processing step after running protoc. It operates directly on the generated code.

Example

Here's an example of how to use the tool, called protol:

  1. Create a few protobuf files
// thing1.proto
syntax = "proto3";

import "thing2.proto";

package things;

message Thing1 {
  Thing2 thing2 = 1;
}
// thing2.proto
syntax = "proto3";

package things;

message Thing2 {
  string data = 1;
}
  1. Run protoc on those files
$ mkdir out
$ protoc \
  --python_out=out \
  --proto_path=directory/containing/protos thing1.proto thing2.proto
  1. Run protol on the generated code
$ protol \
  --create-package \
  --in-place \
  --python-out out \
  protoc --proto-path=directory/containing/protos thing1.proto thing2.proto

The out/thing1_pb2.py module should show a diff containing at least these lines:

-import thing2_pb2 as thing2__pb2
-
+from . import thing2_pb2 as thing2__pb2

How it works

At a high level protoletariat converts absolute imports to relative imports.

However, it doesn't convert just any absolute import to a relative import.

The protol tool will only convert imports that were generated from .proto files. It does this by inspecting FileDescriptorProtos generated from the proto files.

The core rewrite mechanism is implemented using a simplified form of pattern matching that looks at the Python AST, and invokes rewrite rules for matched import patterns.

Subcommands

protoletariat has a subcommand for each tool that you might like to use to generate FileDescriptorSet bytes:

Subcommand Description
protoc Uses protoc to generate FileDescriptorSet bytes
buf Uses buf to generate FileDescriptorSet bytes
raw You provide the FileDescriptorSet bytes as a file or directly from stdin

Help

$ protol
Usage: protol [OPTIONS] COMMAND [ARGS]...

  Rewrite protoc or buf-generated imports for use by the protoletariat.

Options:
  -o, --python-out DIRECTORY      Directory containing protoc or buf-generated Python code  [required]
  --in-place / --not-in-place     Overwrite all relevant files under `--python-out` with adjusted imports  [default: not-in-place]
  --create-package / --dont-create-package
                                  Recursively create __init__.py files under `--python-out`  [default: dont-create-package]
  -s, --module-suffixes TEXT      Suffixes of Python/mypy modules to process  [default: _pb2.py, _pb2.pyi, _pb2_grpc.py, _pb2_grpc.pyi]
  --exclude-google-imports / --dont-exclude-google-imports
                                  Exclude rewriting imports prefixed with google/protobuf
  -e, --exclude-imports-glob TEXT
                                  Exclude imports matching a glob pattern from being rewritten. Multiple values are allowed
  --help                          Show this message and exit.

Commands:
  buf     Use buf to generate the FileDescriptorSet blob
  protoc  Use protoc to generate the FileDescriptorSet blob
  raw     Rewrite imports using FileDescriptorSet bytes from a file or stdin

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

protoletariat-3.2.10.tar.gz (19.7 kB view details)

Uploaded Source

Built Distribution

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

protoletariat-3.2.10-py3-none-any.whl (20.4 kB view details)

Uploaded Python 3

File details

Details for the file protoletariat-3.2.10.tar.gz.

File metadata

  • Download URL: protoletariat-3.2.10.tar.gz
  • Upload date:
  • Size: 19.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.10.11 Linux/5.15.0-1039-azure

File hashes

Hashes for protoletariat-3.2.10.tar.gz
Algorithm Hash digest
SHA256 11a50e55777853bf3a43ce92c6809663f0a186b2d41846db40893303da0e48e0
MD5 2335f0b89ddae6779ceeb6d0bf17d9fe
BLAKE2b-256 a292bc546d4e4c47469da78833dd3714cac6dc5d262f45f84fa1527e6a69f034

See more details on using hashes here.

File details

Details for the file protoletariat-3.2.10-py3-none-any.whl.

File metadata

  • Download URL: protoletariat-3.2.10-py3-none-any.whl
  • Upload date:
  • Size: 20.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.10.11 Linux/5.15.0-1039-azure

File hashes

Hashes for protoletariat-3.2.10-py3-none-any.whl
Algorithm Hash digest
SHA256 89ccddc5366135f7a9b4b4b097875eaf9611d6ebe4d2bd74363a25438a1c2214
MD5 15526ec52435415017f4a429f86ea12d
BLAKE2b-256 8c0c4a083ae10834e154b56033b08f71c9779877771c3e4f8df512747c9c47ba

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