Skip to main content

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 and DESCRIPTOR (equivalent to *_pb2.py)
  • pb2_grpc: Stub / Servicer / add_*_to_server helpers (equivalent to *_pb2_grpc.py)
  • Supports both relative and absolute proto paths.

If the proto has no service, load still returns (pb2, pb2_grpc), but pb2_grpc will not contain Stub/Servicer symbols.

Import Hook

import grapec

grapec.install_import_hook()

import hello_pb2
import hello_pb2_grpc
  • After install_import_hook(), Grapec searches cwd + sys.path for a matching .proto file, for example hello_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()
  1. Start the server:
uv run --with grapec server.py
  1. Run the client in another terminal:
uv run --with grapec client.py

Expected output:

Hello, Grapec

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

grapec-0.2.0.tar.gz (26.4 kB view details)

Uploaded Source

Built Distribution

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

grapec-0.2.0-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

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

Hashes for grapec-0.2.0.tar.gz
Algorithm Hash digest
SHA256 9fa2a5dd14f628d6e697a415484fe5874af34d3c79ff72f23888a2a3efc1df5f
MD5 7526b5fe74364480596b1f4398abec7c
BLAKE2b-256 ad3c6bfc46dabbd43607e1b4a44cd0a8674b5b5f9b82ac182b0b064132356680

See more details on using hashes here.

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

Hashes for grapec-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f00995cfa8b0c03efb391650ea1c61aa6ed7ffe274eed076ed72f2b8d24de0e1
MD5 414fe7f4cd3bc94a75322f81003b8f43
BLAKE2b-256 cbbcc64384d69fef8702e6d473241cede83e165256bc05fc531bbdb12f21e5ca

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