Skip to content

Commit e00d75f

Browse files
[feat] Expose containing module for servergen (#105)
Why === We have some `sed` commands that get run against generated code. Instead, surface config options so we don't need to do that. We also don't need to lint generated sources. What changed ============ Added `replit_river.codegen server --module ...` `# ruff: noqa` at the top of generated files Test plan ========= Manual testing resulted in the same file after replacing `sed` with `--module ...`
1 parent 6405ae8 commit e00d75f

File tree

3 files changed

+32
-16
lines changed

3 files changed

+32
-16
lines changed

replit_river/codegen/client.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@ def generate_river_client_module(
466466
chunks: List[str] = [
467467
dedent(
468468
"""\
469+
# ruff: noqa
469470
# Code generated by river.codegen. DO NOT EDIT.
470471
from collections.abc import AsyncIterable, AsyncIterator
471472
import datetime

replit_river/codegen/run.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ def main() -> None:
1313
server = subparsers.add_parser(
1414
"server", help="Codegen a River server from gRPC protos"
1515
)
16+
server.add_argument(
17+
"--module", dest="module_name", help="output module", default="."
18+
)
1619
server.add_argument("--output", help="output directory", required=True)
1720
server.add_argument("proto", help="proto file")
1821

@@ -39,7 +42,7 @@ def main() -> None:
3942
if args.command == "server":
4043
proto_path = os.path.abspath(args.proto)
4144
target_directory = os.path.abspath(args.output)
42-
proto_to_river_server_codegen(proto_path, target_directory)
45+
proto_to_river_server_codegen(args.module_name, proto_path, target_directory)
4346
elif args.command == "server-schema":
4447
proto_path = os.path.abspath(args.proto)
4548
target_directory = os.path.abspath(args.output)

replit_river/codegen/server.py

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import collections
22
import os.path
33
import tempfile
4+
from textwrap import dedent
45
from typing import DefaultDict, List, Sequence
56

67
import black
@@ -220,20 +221,27 @@ def message_encoder(
220221

221222
def generate_river_module(
222223
module_name: str,
224+
pb_module_name: str,
223225
fds: descriptor_pb2.FileDescriptorSet,
224226
) -> Sequence[str]:
225227
"""Generates the lines of a River module."""
226228
chunks: List[str] = [
227-
"# Code generated by river.codegen. DO NOT EDIT.",
228-
"import datetime",
229-
"from typing import Any, Dict, Mapping, Tuple",
230-
"",
231-
"from google.protobuf import timestamp_pb2",
232-
"from google.protobuf.wrappers_pb2 import BoolValue",
229+
dedent(
230+
f"""\
231+
# Code generated by river.codegen. DO NOT EDIT.
232+
import datetime
233+
from typing import Any, Dict, Mapping, Tuple
234+
235+
from google.protobuf import timestamp_pb2
236+
from google.protobuf.wrappers_pb2 import BoolValue
237+
238+
import replit_river as river
239+
240+
from {module_name} import {pb_module_name}_pb2, {pb_module_name}_pb2_grpc
241+
"""
242+
),
233243
"",
234-
"import replit_river as river",
235244
"",
236-
f"from . import {module_name}_pb2, {module_name}_pb2_grpc\n\n",
237245
]
238246
for pd in fds.file:
239247

@@ -242,15 +250,15 @@ def _remove_namespace(name: str) -> str:
242250

243251
# Generate the message encoders/decoders.
244252
for message in pd.message_type:
245-
chunks.extend(message_encoder(module_name, message))
246-
chunks.extend(message_decoder(module_name, message))
253+
chunks.extend(message_encoder(pb_module_name, message))
254+
chunks.extend(message_decoder(pb_module_name, message))
247255

248256
# Generate the service stubs.
249257
for service in pd.service:
250258
chunks.extend(
251259
[
252260
f"""def add_{service.name}Servicer_to_server(
253-
servicer: {module_name}_pb2_grpc.{service.name}Servicer,
261+
servicer: {pb_module_name}_pb2_grpc.{service.name}Servicer,
254262
server: river.Server,
255263
) -> None:""",
256264
(
@@ -301,7 +309,11 @@ def _remove_namespace(name: str) -> str:
301309
return chunks
302310

303311

304-
def proto_to_river_server_codegen(proto_path: str, target_directory: str) -> None:
312+
def proto_to_river_server_codegen(
313+
module_name: str,
314+
proto_path: str,
315+
target_directory: str,
316+
) -> None:
305317
fds = descriptor_pb2.FileDescriptorSet()
306318
with tempfile.TemporaryDirectory() as tempdir:
307319
descriptor_path = os.path.join(tempdir, "descriptor.pb")
@@ -317,12 +329,12 @@ def proto_to_river_server_codegen(proto_path: str, target_directory: str) -> Non
317329
)
318330
with open(descriptor_path, "rb") as f:
319331
fds.ParseFromString(f.read())
320-
module_name = os.path.splitext(os.path.basename(proto_path))[0]
332+
pb_module_name = os.path.splitext(os.path.basename(proto_path))[0]
321333
contents = black.format_str(
322-
"\n".join(generate_river_module(module_name, fds)),
334+
"\n".join(generate_river_module(module_name, pb_module_name, fds)),
323335
mode=black.FileMode(string_normalization=False),
324336
)
325337
os.makedirs(target_directory, exist_ok=True)
326-
output_path = f"{target_directory}/{module_name}_river.py"
338+
output_path = f"{target_directory}/{pb_module_name}_river.py"
327339
with open(output_path, "w") as f:
328340
f.write(contents)

0 commit comments

Comments
 (0)