Load protobuf and grpc definition file dynamiclly
Project description
GRaPeC
Pronunciation (IPA): /ɡreɪ.peɪk/ (similar to "gray-pay-k").
A lightweight utility for dynamically loading .proto files at runtime and exposing the two Python gRPC module views.
Usage
import grapec
pb2, pb2_grpc = grapec.load("path/to/your.proto")
pb2: message types andDESCRIPTOR(equivalent to*_pb2.py)pb2_grpc:Stub/Servicer/add_*_to_serverhelpers (equivalent to*_pb2_grpc.py)- Supports both relative and absolute proto paths.
If the proto has no
service,loadstill returns(pb2, pb2_grpc), butpb2_grpcwill not containStub/Servicersymbols.
Import Hook
import grapec
grapec.install_import_hook()
import hello_pb2
import hello_pb2_grpc
- After
install_import_hook(), Grapec searchescwd + sys.pathfor a matching.protofile, for examplehello_pb2->hello.proto. - Call
grapec.uninstall_import_hook()to remove the hook when you no longer need it.
Quick Example
The demo lives in examples/hello/.
examples/hello/hello.proto:
syntax = "proto3";
package examples.hello.v1;
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
examples/hello/server.py:
from pathlib import Path
import grpc
import logging
import grapec
from concurrent import futures
PROTO_PATH = Path(__file__).resolve().with_name("hello.proto")
hello_pb2, hello_pb2_grpc = grapec.load(str(PROTO_PATH))
class GreeterServicer(hello_pb2_grpc.GreeterServicer):
def SayHello(self, request, context):
if not request.name.strip():
context.abort(grpc.StatusCode.INVALID_ARGUMENT, "name is required")
return hello_pb2.HelloReply(message=f"Hello, {request.name}")
def serve():
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
hello_pb2_grpc.add_GreeterServicer_to_server(
GreeterServicer(), server
)
server.add_insecure_port("localhost:50051")
server.start()
server.wait_for_termination()
if __name__ == "__main__":
logging.basicConfig()
serve()
examples/hello/client.py:
import grpc
import grapec
grapec.install_import_hook()
import hello_pb2
import hello_pb2_grpc
def run():
with grpc.insecure_channel("localhost:50051") as channel:
stub = hello_pb2_grpc.GreeterStub(channel)
req = hello_pb2.HelloRequest(name='Grapec')
resp = stub.SayHello(req)
print(resp.message)
if __name__ == "__main__":
run()
- Start the server:
uv run --with grapec server.py
- Run the client in another terminal:
uv run --with grapec client.py
Expected output:
Hello, Grapec
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file grapec-0.2.0.tar.gz.
File metadata
- Download URL: grapec-0.2.0.tar.gz
- Upload date:
- Size: 26.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9fa2a5dd14f628d6e697a415484fe5874af34d3c79ff72f23888a2a3efc1df5f
|
|
| MD5 |
7526b5fe74364480596b1f4398abec7c
|
|
| BLAKE2b-256 |
ad3c6bfc46dabbd43607e1b4a44cd0a8674b5b5f9b82ac182b0b064132356680
|
File details
Details for the file grapec-0.2.0-py3-none-any.whl.
File metadata
- Download URL: grapec-0.2.0-py3-none-any.whl
- Upload date:
- Size: 3.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f00995cfa8b0c03efb391650ea1c61aa6ed7ffe274eed076ed72f2b8d24de0e1
|
|
| MD5 |
414fe7f4cd3bc94a75322f81003b8f43
|
|
| BLAKE2b-256 |
cbbcc64384d69fef8702e6d473241cede83e165256bc05fc531bbdb12f21e5ca
|