Skip to content

Commit

Permalink
Merge pull request #44 from JoshYuJump/main
Browse files Browse the repository at this point in the history
Optimized *_pb2_grpc.py imports, `clients/intermediates`
  • Loading branch information
JoshYuJump authored Sep 14, 2022
2 parents 5f61ca8 + 0379fc5 commit 33e3316
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ indent_style = space
tab_width = 2

[*.py]
max_line_length = 99
max_line_length = 79
2 changes: 1 addition & 1 deletion .style.yapf
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[style]
based_on_style = facebook
COLUMN_LIMIT = 99
COLUMN_LIMIT = 79
35 changes: 34 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,40 @@
# This project adheres to [Semantic Versioning](http://semver.org/).
# includes Added / Changed / Fixed

## [1.2.0] UNRELEASED
## [2.4.0] UNRELEASED

## [2.3.0] 2021-09-14
### Changed
- Upgraded grpcio
- Optimized *_pb2_grpc.py imports, `clients/intermediates` no longer need to join the runtime environment

### Fixed
- Fixed import MAX_RECURSION_DEPTH and protobuf.__version__
- Fixed async client feature

## [2.2.0] 2021-08-22
### Added
Supported async gRPC client. [#40](https://github.com/bali-framework/bali-cli/issues/40)

## [2.1.8] 2021-07-19
### Added
- Adjusted the Jinja2 version

## [2.1.7] 2021-06-13
### Added
- Supported build command find protobuf files in /protos, further simplify project structure. [https://github.com/JoshYuJump/bali/tree/main/examples/todos](https://github.com/JoshYuJump/bali/tree/main/examples/todos)
- Adjust requirement Pydantic version.

## [2.1.6] 2021-05-12
### Added
- Supported multi gRPC address configurations

### Fixed
- Fixed import issues

## [2.0.0] 2021-05-12
### Added
- Added `refresh` argument to clear cache

## [1.1.2] 2021-04-14
### Fixed
Expand Down
2 changes: 1 addition & 1 deletion cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '2.3.0-rc.1'
__version__ = '2.3.0'
15 changes: 15 additions & 0 deletions cli/biz.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,24 @@ def prepare_current_repo(work_dir: Path) -> Path:


def compile_proto_file(output_dir: Path, proto_file_name: str) -> None:
"""Compile protobuf to pb2 & pb2_grpc files"""
options = [f"-I{output_dir}", f"--python_out={output_dir}", f"--grpc_python_out={output_dir}"]
os.system(f"python -m grpc_tools.protoc {' '.join(options)} {proto_file_name}")

service = proto_file_name.replace(".proto", "")

# Modified import in *_pb2_grpc.py files
pb2_grpc_file = f'{service}_pb2_grpc.py'
pb2_grpc_path: Path = output_dir / pb2_grpc_file
with pb2_grpc_path.open(mode="r+") as f:
content = f.read()
f.seek(0)
f.truncate()
f.write(content.replace(
f'import {service}_pb2 as',
f'from . import {service}_pb2 as',
))


def compile_client_file(proto_path: Path, service_name: str):
service_pattern = re.compile(r"^service\s+(.*?)\s+{$")
Expand Down
8 changes: 7 additions & 1 deletion cli/templates/utils.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ from grpc import insecure_channel
from google.protobuf import json_format, __version__ as protobuf__version__
from google.protobuf.empty_pb2 import Empty

from ._config import GRPC_ADDRESS, MAX_RECURSION_DEPTH
from ._config import GRPC_ADDRESS

# `GRPC_ADDRESS` is Generic gRPC channel target
# `GRPC_ADDRESS_EXTENDS` is gRPC channel target specified by service abbreviation
Expand All @@ -28,6 +28,12 @@ try:
except ImportError:
CHANNEL_OPTIONS = None

# ProtobufParser max_recursion_depth
try:
from ._config import MAX_RECURSION_DEPTH
except ImportError:
MAX_RECURSION_DEPTH = 99


# All RPC clients inherited from the `ClientMixin`
class ClientMixin:
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ grpcio-tools>=1.32.0,<=1.48
GitPython==3.1.11
jinja2>=2.11.2
MarkupSafe==2.0.1
protobuf>=3.15,<=3.20
protobuf>=3.15,<=3.20.2
protobuf2pydantic==2021.11.25
pydantic~=1.7
typer>=0.3.2

0 comments on commit 33e3316

Please sign in to comment.