Compile proto files based on package structure
This commit is contained in:
parent
f7c2fd1194
commit
fdf3b2e764
@ -3,11 +3,9 @@
|
|||||||
import itertools
|
import itertools
|
||||||
import os.path
|
import os.path
|
||||||
import re
|
import re
|
||||||
import stringcase
|
|
||||||
import sys
|
import sys
|
||||||
import textwrap
|
import textwrap
|
||||||
from typing import List
|
from typing import List
|
||||||
from betterproto.casing import safe_snake_case
|
|
||||||
from betterproto.compile.importing import get_ref_type
|
from betterproto.compile.importing import get_ref_type
|
||||||
import betterproto
|
import betterproto
|
||||||
from betterproto.compile.naming import (
|
from betterproto.compile.naming import (
|
||||||
@ -131,17 +129,21 @@ def generate_code(request, response):
|
|||||||
|
|
||||||
output_map = {}
|
output_map = {}
|
||||||
for proto_file in request.proto_file:
|
for proto_file in request.proto_file:
|
||||||
out = proto_file.package
|
if (
|
||||||
|
proto_file.package == "google.protobuf"
|
||||||
if out == "google.protobuf" and "INCLUDE_GOOGLE" not in plugin_options:
|
and "INCLUDE_GOOGLE" not in plugin_options
|
||||||
|
):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if not out:
|
# if proto_file.package:
|
||||||
out = os.path.splitext(proto_file.name)[0].replace(os.path.sep, ".")
|
output_file = (
|
||||||
|
os.path.join(proto_file.package.replace(".", os.path.sep), "__init__")
|
||||||
|
+ ".py"
|
||||||
|
)
|
||||||
|
|
||||||
if out not in output_map:
|
if output_file not in output_map:
|
||||||
output_map[out] = {"package": proto_file.package, "files": []}
|
output_map[output_file] = {"package": proto_file.package, "files": []}
|
||||||
output_map[out]["files"].append(proto_file)
|
output_map[output_file]["files"].append(proto_file)
|
||||||
|
|
||||||
# TODO: Figure out how to handle gRPC request/response messages and add
|
# TODO: Figure out how to handle gRPC request/response messages and add
|
||||||
# processing below for Service.
|
# processing below for Service.
|
||||||
@ -349,8 +351,7 @@ def generate_code(request, response):
|
|||||||
|
|
||||||
# Fill response
|
# Fill response
|
||||||
f = response.file.add()
|
f = response.file.add()
|
||||||
# print(filename, file=sys.stderr)
|
f.name = filename
|
||||||
f.name = filename.replace(".", os.path.sep) + ".py"
|
|
||||||
|
|
||||||
# Render and then format the output file.
|
# Render and then format the output file.
|
||||||
f.content = black.format_str(
|
f.content = black.format_str(
|
||||||
@ -358,32 +359,24 @@ def generate_code(request, response):
|
|||||||
mode=black.FileMode(target_versions=set([black.TargetVersion.PY37])),
|
mode=black.FileMode(target_versions=set([black.TargetVersion.PY37])),
|
||||||
)
|
)
|
||||||
|
|
||||||
inits = set([""])
|
output_files = list(output_map.keys())
|
||||||
for f in response.file:
|
for output_file in output_files:
|
||||||
# Ensure output paths exist
|
path = os.path.dirname(output_file)
|
||||||
# print(f.name, file=sys.stderr)
|
|
||||||
dirnames = os.path.dirname(f.name)
|
|
||||||
if dirnames:
|
|
||||||
os.makedirs(dirnames, exist_ok=True)
|
|
||||||
base = ""
|
|
||||||
for part in dirnames.split(os.path.sep):
|
|
||||||
base = os.path.join(base, part)
|
|
||||||
inits.add(base)
|
|
||||||
|
|
||||||
for base in inits:
|
for sub_path in path.split(os.path.sep):
|
||||||
name = os.path.join(base, "__init__.py")
|
init_file = os.path.join(sub_path, "__init__.py")
|
||||||
|
|
||||||
if os.path.exists(name):
|
if init_file in output_files:
|
||||||
# Never overwrite inits as they may have custom stuff in them.
|
continue
|
||||||
continue
|
|
||||||
|
|
||||||
init = response.file.add()
|
output_files.append(init_file)
|
||||||
init.name = name
|
init = response.file.add()
|
||||||
init.content = b""
|
init.name = init_file
|
||||||
|
init.content = b""
|
||||||
|
|
||||||
filenames = sorted([f.name for f in response.file])
|
filenames = sorted([f.name for f in response.file])
|
||||||
for fname in filenames:
|
for fname in filenames:
|
||||||
print(f"Writing {fname}", file=sys.stderr)
|
print(f"Writing {repr(fname)}", file=sys.stderr)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
Loading…
x
Reference in New Issue
Block a user