Skip to content

Commit

Permalink
✨ [#38] dd setting to make runtime proto generation optional
Browse files Browse the repository at this point in the history
  • Loading branch information
GuangTianLi committed Mar 20, 2021
1 parent 1efb507 commit 46ae53a
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 45 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ History
--------------------

* Support gRPC with xDS
* Add `PROTO_AUTO_GENERATED` setting to make runtime proto generation optional

0.6.*(2020-10-27)
--------------------
Expand Down
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@
# the built documents.
#
# The short X.Y version.
version = "0.7.0"
version = "0.7.2"
# The full version, including alpha/beta/rc tags.
release = "0.7.0"
release = "0.7.2"

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down
2 changes: 1 addition & 1 deletion grpcalchemy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

__author__ = """GuangTian Li"""
__email__ = "[email protected]"
__version__ = "0.7.0"
__version__ = "0.7.2"

__all__ = ["Blueprint", "Context", "grpcmethod", "DefaultConfig", "Server"]

Expand Down
6 changes: 6 additions & 0 deletions grpcalchemy/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class DefaultConfig(BaseConfig):
#: generated by the protocol buffer compiler
PROTO_TEMPLATE_ROOT = ""
PROTO_TEMPLATE_PATH = "protos"
#: If set to false, proto and pb file will not be generated on runtime automatically.
PROTO_AUTO_GENERATED = True

#: Max workers in service thread pool
GRPC_SERVER_MAX_WORKERS = 8
Expand Down Expand Up @@ -49,3 +51,7 @@ class DefaultConfig(BaseConfig):
#: If set to true, retrieves server configuration via xDS. This is an
#: EXPERIMENTAL option. Only for grpcio >= 1.36.0
GRPC_XDS_SUPPORT = False

#: If set to true, retrieves server configuration via xDS. This is an
#: EXPERIMENTAL option. Only for grpcio >= 1.36.0
GRPC_XDS_SUPPORT = False
83 changes: 44 additions & 39 deletions grpcalchemy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,48 +68,53 @@ def make_packages(name, mode=0o777, exist_ok=False):
open(init_file, "a").close()


def generate_proto_file(template_path_root: str = "", template_path: str = "protos"):
def generate_proto_file(
template_path_root: str = "",
template_path: str = "protos",
auto_generate: bool = True,
):
abs_template_path = join(template_path_root, template_path)

env = Environment(
loader=FileSystemLoader(
searchpath=abspath(join(dirname(__file__), "templates"))
),
autoescape=False,
trim_blocks=True,
lstrip_blocks=True,
)

if not exists(abs_template_path):
make_packages(abs_template_path)
env.get_template("README.md.tmpl").stream().dump(
join(abs_template_path, "README.md")
if auto_generate:
env = Environment(
loader=FileSystemLoader(
searchpath=abspath(join(dirname(__file__), "templates"))
),
autoescape=False,
trim_blocks=True,
lstrip_blocks=True,
)
template = env.get_template("rpc.proto.tmpl")
for filename, meta in __meta__.items():
template.stream(
file_path=template_path,
import_files=sorted(meta.import_files),
messages=meta.messages,
services=meta.services,
).dump(join(abs_template_path, f"{filename}.proto"))

# copy from grpc_tools
protoc_file = pkg_resources.resource_filename("grpc_tools", "protoc.py")
proto_include = pkg_resources.resource_filename("grpc_tools", "_proto")
for _, dirs, files in walk(f"{template_path}"):
for file in files:
if file[-5:] == "proto":
grpc_tools.protoc.main(
[
protoc_file,
"-I.",
"--python_out=.",
"--grpc_python_out=.",
f".{FILE_SEPARATOR}{template_path}{FILE_SEPARATOR}{file}",
]
+ ["-I{}".format(proto_include)]
)

if not exists(abs_template_path):
make_packages(abs_template_path)
env.get_template("README.md.tmpl").stream().dump(
join(abs_template_path, "README.md")
)
template = env.get_template("rpc.proto.tmpl")
for filename, meta in __meta__.items():
template.stream(
file_path=template_path,
import_files=sorted(meta.import_files),
messages=meta.messages,
services=meta.services,
).dump(join(abs_template_path, f"{filename}.proto"))

# copy from grpc_tools
protoc_file = pkg_resources.resource_filename("grpc_tools", "protoc.py")
proto_include = pkg_resources.resource_filename("grpc_tools", "_proto")
for _, dirs, files in walk(f"{template_path}"):
for file in files:
if file[-5:] == "proto":
grpc_tools.protoc.main(
[
protoc_file,
"-I.",
"--python_out=.",
"--grpc_python_out=.",
f".{FILE_SEPARATOR}{template_path}{FILE_SEPARATOR}{file}",
]
+ ["-I{}".format(proto_include)]
)
for meta in __meta__.values():
reload(import_module(abs_template_path.split(FILE_SEPARATOR, 1)[0]))
for messageCls in meta.messages:
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.7.0
current_version = 0.7.2
commit = True
tag = True
message = :bookmark: Bump version: {current_version} → {new_version}
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@
tests_require=test_requirements,
url="https://github.com/GuangTianLi/grpcalchemy",
python_requires=">=3.6.0",
version="0.7.0",
version="0.7.2",
zip_safe=False,
)
2 changes: 1 addition & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def test_socket_bind_test_UNIX(self):
if os.path.exists(server_address):
os.remove(server_address)

def test_generate_proto_file(self):
def test_generate_proto_file_automatically(self):
from grpcalchemy.orm import Message

class TestNestedPackage(Message):
Expand Down

0 comments on commit 46ae53a

Please sign in to comment.