A Hatch build plugin to generate Python files from Protocol Buffers .proto files
Project description
Hatch Protocol Buffers Generator
This Hatch plugin uses grpcio to generate Python files from Protocol Buffers .proto
files.
Usage
You will need to add hatch-protobuf
to your project's pyproject.toml
:
[tool.hatch.build.hooks.protobuf]
dependencies = ["hatch-protobuf"]
There are a few options that can be set in the [tool.hatch.build.hooks.protobuf]
section:
Key | Default | Description |
---|---|---|
generate_grpc |
true |
Whether to generate gRPC output files. |
generate_pyi |
true |
Whether to generate .pyi output files. Note that these are not generated for the gRPC output. You may want to use mypy-protobuf instead. |
proto_paths |
["."] or ["src"] |
An array of paths to search for .proto files. Also passed as --proto_path arguments to protoc . |
output_path |
"." or "src" |
The default output directory. This can be overridden on a per-generator basis for custom generators. |
Hatch-protobuf will guess whether to use "src" as the default input/output directory in
a similar way to the wheel builder. If
src/<NAME>/__init__.py
exists, but <NAME>/__init__.py
does not, it will use "src" as
the default for proto_paths
and output_path
. Otherwise it will use "." as the
default for both.
Custom generators
If you want to use custom generators (not just the python, gRPC and pyi ones built in to
the version of protoc that ships with grpcio-tools), you can add them in
[[tool.hatch.build.hooks.protobuf.generators]]
sections. You will also need to add the
plugin to the list of dependencies. See the "Mypy output" section below for an example.
Options that can be set in generator sections:
Key | Default | Description |
---|---|---|
name |
required | The name of the plugin. The argument passed to protoc will be --<name>_out . |
outputs |
required | A list of paths (relative to output_path ). See below for more information. |
output_path |
same as output_path from the main protobuf config section |
Where to write generated files to. This is the value passed to the --<name>_out argument. |
Each entry in the outputs
field is a template that depends on the .proto
file being
processed. The string {proto_name}
will be replaced with the base filename of each input .proto
file, and {proto_path}
will be replaced with the path (relative to the proto_paths) of
the input proto files. For example, if proto_paths
is set to ["src"]
, for the
input file src/foo/bar/test.proto
"{proto_name}" will expand to "test" and
"{proto_path}" will expand to "foo/bar".
Mypy output
The mypy-protobuf package provides mypy stub
files with comments copied from the input .proto
files. Here is an example of how to
use it in your pyproject.toml
:
[tool.hatch.build.hooks.protobuf]
dependencies = [
"hatch-protobuf",
"mypy-protobuf~=3.0",
]
generate_pyi = false # we'll let mypy-protobuf do this
[[tool.hatch.build.hooks.protobuf.generators]]
name = "mypy"
outputs = ["{proto_path}/{proto_name}_pb2.pyi"]
[[tool.hatch.build.hooks.protobuf.generators]]
name = "mypy_grpc"
outputs = ["{proto_path}/{proto_name}_pb2_grpc.pyi"]
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file hatch_protobuf-0.2.0.post1.tar.gz
.
File metadata
- Download URL: hatch_protobuf-0.2.0.post1.tar.gz
- Upload date:
- Size: 12.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4aa2b4cdfe116885c9bb1b7f6a37d86548fbacebcfca1866733029030b977642 |
|
MD5 | 6e472bf227e507ce74aaeb3615a96936 |
|
BLAKE2b-256 | 07e888f799e38dcbf4899dd1c3f86e6a37480ba2576586778c14428f0867b84b |
File details
Details for the file hatch_protobuf-0.2.0.post1-py3-none-any.whl
.
File metadata
- Download URL: hatch_protobuf-0.2.0.post1-py3-none-any.whl
- Upload date:
- Size: 10.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7152401c459823876776d87f40e6aac2eec76f7800f2410369ccca3edf50cc4c |
|
MD5 | bbd2a4543d31497f563208e035f613d0 |
|
BLAKE2b-256 | 40cffe64e0db330ed8b1e8287403c7ec41bcfd0cbe9d25956e0107af53d94f87 |