|
1 | 1 | """grpc_generator entry points.""" |
2 | 2 |
|
3 | | -import logging |
| 3 | +import pathlib |
4 | 4 |
|
5 | 5 | import click |
6 | 6 |
|
7 | 7 | from . import generator |
8 | 8 |
|
9 | 9 |
|
10 | | -_logger = logging.getLogger(__name__) |
11 | | -_logger.addHandler(logging.NullHandler()) |
| 10 | +REPO_ROOT = next( |
| 11 | + (p for p in pathlib.Path(__file__).parents if (p / "third_party").exists()), pathlib.Path(".") |
| 12 | +) |
12 | 13 |
|
13 | 14 |
|
14 | | -@click.command() |
| 15 | +@click.command(epilog=generator.USAGE_EXAMPLE) |
| 16 | +@click.option( |
| 17 | + "--output-basepath", |
| 18 | + metavar="PATH", |
| 19 | + type=click.Path(file_okay=False, writable=True, path_type=pathlib.Path), |
| 20 | + required=True, |
| 21 | + help="Emit the generated gRPC files to PATH", |
| 22 | +) |
| 23 | +@click.option( |
| 24 | + "--output-format", |
| 25 | + type=click.Choice(choices=[entry.value for entry in generator.OutputFormat]), |
| 26 | + required=True, |
| 27 | + help="Generate a Python submodule or subpackage", |
| 28 | +) |
| 29 | +@click.option( |
| 30 | + "--proto-basepath", |
| 31 | + metavar="PATH", |
| 32 | + type=click.Path(file_okay=False, path_type=pathlib.Path), |
| 33 | + default=REPO_ROOT.joinpath("third_party/ni-apis"), |
| 34 | + show_default=True, |
| 35 | + help="Use PATH as the base for --proto-subpath", |
| 36 | +) |
| 37 | +@click.option( |
| 38 | + "--proto-include-path", |
| 39 | + metavar="PATH", |
| 40 | + multiple=True, |
| 41 | + type=click.Path(file_okay=False, path_type=pathlib.Path), |
| 42 | + default=[REPO_ROOT.joinpath("third_party/ni-apis")], |
| 43 | + show_default=True, |
| 44 | + help="Add PATH to the import search list, can be used more than once", |
| 45 | +) |
| 46 | +@click.option( |
| 47 | + "--proto-subpath", |
| 48 | + metavar="PATH", |
| 49 | + type=click.Path(file_okay=False, path_type=pathlib.Path), |
| 50 | + required=True, |
| 51 | + help="Use the proto files under PATH as input", |
| 52 | +) |
15 | 53 | @click.help_option() |
16 | | -def cli() -> None: |
17 | | - """Generate gRPC Python stubs from proto files.""" |
18 | | - generator.handle_cli() |
| 54 | +@click.version_option() |
| 55 | +def cli( |
| 56 | + proto_basepath: pathlib.Path, |
| 57 | + proto_subpath: pathlib.Path, |
| 58 | + proto_include_path: list[pathlib.Path], |
| 59 | + output_basepath: pathlib.Path, |
| 60 | + output_format: str, |
| 61 | +) -> None: |
| 62 | + """Generate gRPC Python stubs from proto files. |
| 63 | +
|
| 64 | + Specifying input and output locations |
| 65 | +
|
| 66 | + This script uses the protobuf files from the folder specified by |
| 67 | + --proto-basepath and --proto-subpath and emits Python files into the |
| 68 | + folder specified by --output-basepath: |
| 69 | +
|
| 70 | + \b |
| 71 | + {proto-basepath}/{proto-subpath} --> {output-basepath}/{proto-subpath} |
| 72 | +
|
| 73 | + The script resolves gRPC imports from --proto-basepath by default. Include |
| 74 | + additional paths by using --proto-include-path for each required folder. |
| 75 | +
|
| 76 | + Specifying output format |
| 77 | +
|
| 78 | + The script supports generating gRPC packages as either subpackages |
| 79 | + or submodules with --output-format. |
| 80 | +
|
| 81 | + When generating submodules, the script creates Python files with names |
| 82 | + that match the source protobuf files: |
| 83 | +
|
| 84 | + \b |
| 85 | + waveform.proto --> waveform_pb2.py |
| 86 | +
|
| 87 | + When generating subpackages, the script creates folders with names |
| 88 | + that match the source protobuf files: |
| 89 | +
|
| 90 | + \b |
| 91 | + waveform.proto --> waveform_pb2/__init__.py |
| 92 | +
|
| 93 | + Clients use the same "import waveform_pb2" syntax. |
| 94 | +
|
| 95 | + """ # noqa: D301 - Use r""" if any backslashes in a docstring |
| 96 | + generator.handle_cli( |
| 97 | + proto_basepath=proto_basepath, |
| 98 | + proto_subpath=proto_subpath, |
| 99 | + proto_include_paths=proto_include_path, |
| 100 | + output_basepath=output_basepath, |
| 101 | + output_format=generator.OutputFormat(output_format), |
| 102 | + ) |
19 | 103 |
|
20 | 104 |
|
21 | 105 | if __name__ == "__main__": |
|
0 commit comments