Name | py2proto JSON |
Version |
2.2.0
JSON |
| download |
home_page | None |
Summary | An ORM-like Python library for generating and managing gRPC proto files and pb2 files |
upload_time | 2024-07-27 13:32:38 |
maintainer | None |
docs_url | None |
author | None |
requires_python | >=3.8 |
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. |
keywords |
grpc
protobuf
code generation
protobuf orm
auto create pb2 & proto file
py2proto
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# py2proto
py2proto is a powerful Python library that simplifies the process of creating gRPC services and Protocol Buffer definitions. It automatically generates .proto files, gRPC code, and Swagger UI documentation from Python class definitions.
## Features
- Automatic generation of .proto files from Python classes
- Generation of gRPC Python code
- Swagger UI generation for easy API testing and documentation
- Support for complex data types (lists, dictionaries)
- Custom output directory setting
- Built-in Swagger UI server
## Installation
Install py2proto using pip:
```bash
pip install py2proto
```
## Usage
1. Import necessary modules:
```python
from py2proto import ProtoGenerator, relation
from typing import List, Dict
```
2. Define your message classes:
```python
class MessageProto(ProtoGenerator):
class MessageRequest(ProtoGenerator):
message: str
number: int
class MessageResponse(ProtoGenerator):
message: str
# relation(`relation Name`, `request function`, `returnes fucntion`)
service = relation("SendMessage", "MessageRequest", "MessageResponse")
```
3. Generate files and run Swagger UI:
```python
if __name__ == "__main__":
# set_output_directory(`The destination directory for dumping json, pb2 and proto files`)
MessageProto.set_output_directory("outputs")
# generate_proto(`proto package name`, `proto file name`)
proto_file = MessageProto.generate_proto("messageservice", "message_service")
# generate_pb2(`proto file`)
MessageProto.generate_pb2(proto_file)
# generate_wsagger(`proto file`)
swagger_file = MessageProto.generate_swagger(proto_file
# run_swagger(`version`, `port`)
MessageProto.run_swagger(version="2.0.1", port=5937)
```
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:
## Detailed Function Explanations
### relation(method_name: str, request: str, response: str)
Defines a gRPC service method with its request and response types.
Example:
```python
service = relation("SendMessage", "MessageRequest", "MessageResponse")
```
### set_output_directory(directory: str)
Sets the output directory for generated files.
Example:
```python
MessageProto.set_output_directory("custom_output")
```
### generate_proto(package_name: str, file_name: str) -> str
Generates a .proto file based on the defined classes.
Example:
```python
proto_file = MessageProto.generate_proto("mypackage", "myservice")
```
### generate_pb2(proto_file: str)
Generates Python gRPC code from the .proto file.
Example:
```python
MessageProto.generate_pb2(proto_file)
```
### generate_swagger(proto_file: str) -> str
Generates a Swagger JSON file for API documentation.
Example:
```python
swagger_file = MessageProto.generate_swagger(proto_file)
```
### run_swagger()
Starts a Flask server to serve the Swagger UI.
Example:
```python
MessageProto.run_swagger()
```
## Advanced Usage
You can use complex data types in your message definitions:
```python
class ComplexProto(ProtoGenerator):
class ComplexRequest(ProtoGenerator):
list_field: List[str]
dict_field: Dict[str, int]
class ComplexResponse(ProtoGenerator):
result: List[Dict[str, str]]
service = relation("ComplexRequest", "ComplexResponse")
```
## Multiple Services
Define multiple services in a single Proto class:
```python
class MultiServiceProto(ProtoGenerator):
class Request1(ProtoGenerator):
field1: str
class Response1(ProtoGenerator):
result1: str
class Request2(ProtoGenerator):
field2: int
class Response2(ProtoGenerator):
result2: int
service1 = relation("Request1", "Response1")
service2 = relation("Request2", "Response2")
```
## Why Use py2proto?
1. Simplicity: Define your gRPC services using familiar Python syntax.
2. Automation: Automatically generate .proto files and gRPC code.
3. Documentation: Get Swagger UI documentation out of the box.
4. Flexibility: Support for complex data types and multiple services.
5. Time-saving: Reduce boilerplate code and manual proto file writing.
## Requirements
- Python 3.6+
- grpcio
- grpcio-tools
- Flask (for Swagger UI)
## 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)`
- List[Type] `(repeated)`
- Dict[KeyType, ValueType] `(map)`
## Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Raw data
{
"_id": null,
"home_page": null,
"name": "py2proto",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": null,
"keywords": "grpc, protobuf, code generation, protobuf ORM, auto create pb2 & proto file, py2proto",
"author": null,
"author_email": "Mahdi Ghasemi <mahdighasemiabari@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/a2/63/d393a950701157ee42507b8f9a52bf4cf42d5b1374c2a349cb7383d2c237/py2proto-2.2.0.tar.gz",
"platform": null,
"description": "# py2proto\n\npy2proto is a powerful Python library that simplifies the process of creating gRPC services and Protocol Buffer definitions. It automatically generates .proto files, gRPC code, and Swagger UI documentation from Python class definitions.\n\n## Features\n\n- Automatic generation of .proto files from Python classes\n- Generation of gRPC Python code\n- Swagger UI generation for easy API testing and documentation\n- Support for complex data types (lists, dictionaries)\n- Custom output directory setting\n- Built-in Swagger UI server\n\n## Installation\n\nInstall py2proto using pip:\n\n```bash\npip install py2proto\n```\n\n## Usage\n\n1. Import necessary modules:\n\n```python\nfrom py2proto import ProtoGenerator, relation\nfrom typing import List, Dict\n```\n2. Define your message classes:\n```python\nclass MessageProto(ProtoGenerator):\n class MessageRequest(ProtoGenerator):\n message: str\n number: int\n\n class MessageResponse(ProtoGenerator):\n message: str\n\n # relation(`relation Name`, `request function`, `returnes fucntion`)\n service = relation(\"SendMessage\", \"MessageRequest\", \"MessageResponse\")\n```\n3. Generate files and run Swagger UI:\n```python\nif __name__ == \"__main__\":\n # set_output_directory(`The destination directory for dumping json, pb2 and proto files`)\n MessageProto.set_output_directory(\"outputs\")\n # generate_proto(`proto package name`, `proto file name`)\n proto_file = MessageProto.generate_proto(\"messageservice\", \"message_service\")\n # generate_pb2(`proto file`)\n MessageProto.generate_pb2(proto_file)\n # generate_wsagger(`proto file`)\n swagger_file = MessageProto.generate_swagger(proto_file\n # run_swagger(`version`, `port`)\n MessageProto.run_swagger(version=\"2.0.1\", port=5937)\n```\n\nThis script will generate a .proto file in the 'protos/' directory and pb2 files in the 'outputs/' directory.\nFinally, the output of the generated proto file will be as follows:\n\n## Detailed Function Explanations\n\n### relation(method_name: str, request: str, response: str)\nDefines a gRPC service method with its request and response types.\n\nExample:\n```python\nservice = relation(\"SendMessage\", \"MessageRequest\", \"MessageResponse\")\n```\n\n### set_output_directory(directory: str)\nSets the output directory for generated files.\n\nExample:\n```python\nMessageProto.set_output_directory(\"custom_output\")\n```\n\n### generate_proto(package_name: str, file_name: str) -> str\nGenerates a .proto file based on the defined classes.\n\nExample:\n```python\nproto_file = MessageProto.generate_proto(\"mypackage\", \"myservice\")\n```\n\n### generate_pb2(proto_file: str)\nGenerates Python gRPC code from the .proto file.\n\nExample:\n```python\nMessageProto.generate_pb2(proto_file)\n```\n\n### generate_swagger(proto_file: str) -> str\nGenerates a Swagger JSON file for API documentation.\n\nExample:\n```python\nswagger_file = MessageProto.generate_swagger(proto_file)\n```\n\n### run_swagger()\nStarts a Flask server to serve the Swagger UI.\n\nExample:\n```python\nMessageProto.run_swagger()\n```\n\n## Advanced Usage\nYou can use complex data types in your message definitions:\n```python\nclass ComplexProto(ProtoGenerator):\n class ComplexRequest(ProtoGenerator):\n list_field: List[str]\n dict_field: Dict[str, int]\n\n class ComplexResponse(ProtoGenerator):\n result: List[Dict[str, str]]\n\n service = relation(\"ComplexRequest\", \"ComplexResponse\")\n```\n\n## Multiple Services\nDefine multiple services in a single Proto class:\n```python\nclass MultiServiceProto(ProtoGenerator):\n class Request1(ProtoGenerator):\n field1: str\n\n class Response1(ProtoGenerator):\n result1: str\n\n class Request2(ProtoGenerator):\n field2: int\n\n class Response2(ProtoGenerator):\n result2: int\n\n service1 = relation(\"Request1\", \"Response1\")\n service2 = relation(\"Request2\", \"Response2\")\n```\n\n## Why Use py2proto?\n1. Simplicity: Define your gRPC services using familiar Python syntax.\n2. Automation: Automatically generate .proto files and gRPC code.\n3. Documentation: Get Swagger UI documentation out of the box.\n4. Flexibility: Support for complex data types and multiple services.\n5. Time-saving: Reduce boilerplate code and manual proto file writing.\n\n## Requirements\n\n- Python 3.6+\n- grpcio\n- grpcio-tools\n- Flask (for Swagger UI)\n\n\n## Supported Data Types\n\npy2proto supports the following Protocol Buffer data types:\n\n- str `(string)`\n- int `(int32)`\n- float `(float)`\n- bool `(bool)`\n- bytes `(bytes)`\n- \"int64\" `(int64)`\n- \"uint32\" `(uint32)`\n- \"uint64\" `(uint64)`\n- \"sint32\" `(sint32)`\n- \"sint64\" `(sint64)`\n- \"fixed32\" `(fixed32)`\n- \"fixed64\" `(fixed64)`\n- \"sfixed32\" `(sfixed32)`\n- \"sfixed64\" `(sfixed64)`\n- \"double\" `(double)`\n- List[Type] `(repeated)`\n- Dict[KeyType, ValueType] `(map)`\n\n\n## Contributing\nContributions are welcome! Please feel free to submit a Pull Request.\n",
"bugtrack_url": null,
"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.",
"summary": "An ORM-like Python library for generating and managing gRPC proto files and pb2 files",
"version": "2.2.0",
"project_urls": {
"Bug Tracker": "https://github.com/prodbygodfather/py2proto/issues",
"Homepage": "https://github.com/prodbygodfather/py2proto"
},
"split_keywords": [
"grpc",
" protobuf",
" code generation",
" protobuf orm",
" auto create pb2 & proto file",
" py2proto"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "0661adacf340fb5f4380daff673ac47262c7e82ff3396937faa39e01b15a8c37",
"md5": "72ccc49764f3948e9a49f55b4de43520",
"sha256": "b84a996b75e61e16428ac052ef1c4b64929cfdb97ef52b11a8d9cedf99a14746"
},
"downloads": -1,
"filename": "py2proto-2.2.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "72ccc49764f3948e9a49f55b4de43520",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 8319,
"upload_time": "2024-07-27T13:32:36",
"upload_time_iso_8601": "2024-07-27T13:32:36.825897Z",
"url": "https://files.pythonhosted.org/packages/06/61/adacf340fb5f4380daff673ac47262c7e82ff3396937faa39e01b15a8c37/py2proto-2.2.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "a263d393a950701157ee42507b8f9a52bf4cf42d5b1374c2a349cb7383d2c237",
"md5": "112557e73dc08e010cdb360d8be721e7",
"sha256": "d02e258c4026a2937e100ea798d2ef2a58a4c27f229a3b326a786785ff8fb4f0"
},
"downloads": -1,
"filename": "py2proto-2.2.0.tar.gz",
"has_sig": false,
"md5_digest": "112557e73dc08e010cdb360d8be721e7",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 8767,
"upload_time": "2024-07-27T13:32:38",
"upload_time_iso_8601": "2024-07-27T13:32:38.664009Z",
"url": "https://files.pythonhosted.org/packages/a2/63/d393a950701157ee42507b8f9a52bf4cf42d5b1374c2a349cb7383d2c237/py2proto-2.2.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-07-27 13:32:38",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "prodbygodfather",
"github_project": "py2proto",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [],
"lcname": "py2proto"
}