Metadata-Version: 2.1
Name: py2proto
Version: 1.3.4
Summary: An ORM-like Python library for generating and managing gRPC proto files and pb2 files
Author-email: Mahdi Ghasemi <mahdighasemiabari@gmail.com>
License: Copyright (c) 2024 The Python Packaging Authority
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
Project-URL: Homepage, https://github.com/prodbygodfather/py2proto
Project-URL: Bug Tracker, https://github.com/prodbygodfather/py2proto/issues
Keywords: grpc,protobuf,code generation,protobuf ORM,auto create pb2 & proto file,py2proto
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: grpcio-tools >=1.30.0
Requires-Dist: grpcio >=1.30.0

# py2proto

py2proto is a Python library that provides an ORM-like interface for generating gRPC proto files and pb2 files. It simplifies the process of creating and managing Protocol Buffer definitions for gRPC services.

## Features

- ORM-like syntax for defining Protocol Buffer messages and services
- Automatic generation of .proto files
- Automatic generation of pb2 and pb2_grpc files
- Support for various Protocol Buffer data types
- Easy-to-use API for defining services and message relationships

## Installation

You can install py2proto using pip:

```bash
pip install py2proto
```

## Usage

Here's a basic example of how to use py2proto:

```python
from py2proto import ProtoGenerator, relation

class MessageProto(ProtoGenerator):
    class MessageRequest(ProtoGenerator):
        message: str
        number: int
        big_number: "int64"
        unsigned_number: "uint32"
        repeated_field: str
        map_field: str

    class MessageResponse(ProtoGenerator):
        message: str
        status: str  # Enum handling can be added later
        nested: str  # Message handling can be added later

    service = relation("MessageRequest", "MessageResponse")

if __name__ == "__main__":
    # generate_proto(Package, ProtoFileName, Output Directory)
    file_name = MessageProto.generate_proto("messageservice", "message_service",'protos/')
    # generate_pb2(ProtoFile, Output Directory)
    MessageProto.generate_pb2(file_name, "outputs/")
```

This script will generate a .proto file in the 'protos/' directory and pb2 files in the 'outputs/' directory.
Finally, the output of the generated proto file will be as follows:

```proto
syntax = "proto3";

package messageservice;

message MessageRequest {
  string message = 1;
  int32 number = 2;
  int64 big_number = 3;
  uint32 unsigned_number = 4;
  string repeated_field = 5;
  string map_field = 6;
}

message MessageResponse {
  string message = 1;
  string status = 2;
  string nested = 3;
}

service MessageService {
  rpc SendMessage (MessageRequest) returns (MessageResponse) {}
}
```

## Supported Data Types

py2proto supports the following Protocol Buffer data types:

- str `(string)`
- int `(int32)`
- float `(float)`
- bool `(bool)`
- bytes `(bytes)`
- "int64" `(int64)`
- "uint32" `(uint32)`
- "uint64" `(uint64)`
- "sint32" `(sint32)`
- "sint64" `(sint64)`
- "fixed32" `(fixed32)`
- "fixed64" `(fixed64)`
- "sfixed32" `(sfixed32)`
- "sfixed64" `(sfixed64)`
- "double" `(double)`

## API Reference
### ProtoGenerator
The base class for defining Protocol Buffer messages and services.
### relation(request: str, response: str)
A function to define the relationship between request and response messages in a service.
### generate_proto(package_name: str, file_name: str, output_dir: str = None)
Generates a `.proto` file with the specified package name and file name.
- package_name: The name of the package for the Protocol Buffer definitions.
- file_name: The name of the output .proto file (without extension).
- output_dir: (Optional) The directory where the .proto file will be saved. If not specified, it will use the current directory.
### generate_pb2(proto_file: str, output_dir: str = None)
Generates pb2 and pb2_grpc files from the specified `.proto` file.
- proto_file: The path to the .proto file.
- output_dir: **(Optional)** The directory where the pb2 files will be saved. If not specified, it will use the current directory.

## Contributing
Contributions are welcome! Please feel free to submit a Pull Request.

[Email](milto:prodbygodfather@gmail.com)
