Package: protoletariat
Protocol Buffers for the Rest of UsMotivationAre you annoyed by having to fix the absolute imports generated byprotoc?If so, thenprotoletariatis the tool for you.protoletariathas one goal: fixing the broken imports for the Python code
generated byprotoc.Seehttps://github.com/protocolbuffers/protobuf/issues/1491for the discussion that inspired this tool.InstallationArtifactStatusInstallation CommandPyPI Packagepip install protoletariatConda Packageconda install protoletariat -c conda-forgeDocker Image∅docker pull ghcr.io/cpcloud/protoletariat:latestNote: theconda-forgepackage version may lag behind the other artifacts by a few hours.Usageprotoletariatis designed to be run as a post-processing stepafterrunningprotoc. It operates directly on the generated code.ExampleHere's an example of how to use the tool, calledprotol:Create a few protobuf files// thing1.protosyntax="proto3";import"thing2.proto";packagethings;messageThing1{Thing2thing2=1;}// thing2.protosyntax="proto3";packagethings;messageThing2{stringdata=1;}Runprotocon those files$mkdirout
$protoc\--python_out=out\--proto_path=directory/containing/protosthing1.protothing2.protoRunprotolon the generated code$protol\--create-package\--in-place\--python-outout\protoc--proto-path=directory/containing/protosthing1.protothing2.protoTheout/thing1_pb2.pymodule should show a diff containing at least these lines:-import thing2_pb2 as thing2__pb2
-
+from . import thing2_pb2 as thing2__pb2How it worksAt a high levelprotoletariatconverts absolute imports to relative imports.However, it doesn't convert just any absolute import to a relative import.Theprotoltool will only convert imports that were generated from.protofiles. It does this by inspectingFileDescriptorProtosgenerated 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.Subcommandsprotoletariathas a subcommand for each tool that you might like to use to
generateFileDescriptorSetbytes:SubcommandDescriptionprotocUsesprotocto generateFileDescriptorSetbytesbufUsesbufto generateFileDescriptorSetbytesrawYou provide theFileDescriptorSetbytes as a file or directly from stdinHelp$ 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