8000 Make stuff work slightly more · Gobot1234/python-betterproto@61f0335 · GitHub
[go: up one dir, main page]

Skip to content

Commit 61f0335

Browse files
committed
Make stuff work slightly more
1 parent 8f1746f commit 61f0335

File tree

3 files changed

+42
-40
lines changed

3 files changed

+42
-40
lines changed

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ dataclasses = { version = "^0.7", python = ">=3.6, <3.7" }
1818
grpclib = "^0.4.1"
1919
jinja2 = { version = ">=2.11.2", optional = true }
2020
typer = { version = "^0.3.2", optional = true }
21-
rich = { version = "^9.8.2", optional = true }
21+
rich = { version = "^11.2.0", optional = true }
2222
python-dateutil = "^2.8"
2323

2424
[tool.poetry.dev-dependencies]
@@ -38,7 +38,7 @@ sphinx = "3.1.2"
3838
sphinx-rtd-theme = "0.5.0"
3939
tomlkit = "^0.7.0"
4040
tox = "^3.15.1"
41-
protobuf_parser = "1.0.0"
41+
# protobuf_parser = "1.0.0"
4242

4343
[tool.poetry.scripts]
4444
betterproto = "betterproto:__main__.main"

src/betterproto/plugin/cli/commands.py

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ async def compile(
6767
output = output or (Path(output_path.parent.name) / output_path.name).resolve()
6868
output.mkdir(exist_ok=True, parents=True)
6969

70-
errors = await compile_protobufs(
70+
results = await compile_protobufs(
7171
*protos,
7272
output=output,
7373
verbose=verbose,
@@ -76,38 +76,40 @@ async def compile(
7676
from_cli=True,
7777
)
7878

79-
for error in errors:
80-
if isinstance(error, protobuf_parser.SyntaxError):
81-
rich.print(
82-
f"[red]File {str(error.file).strip()}:\n",
83-
Syntax.from_path(
84-
error.file,
85-
line_numbers=True,
86-
line_range=(max(error.lineno - 5, 0), error.lineno),
87-
),
88-
f"{' ' * (error.offset + 3)}^\nSyntaxError: {error.msg}[red]",
89-
file=sys.stderr,
90-
)
91-
elif isinstance(error, Warning):
92-
rich.print(f"Warning: {error}", file=sys.stderr)
93-
elif isinstance(error, protobuf_parser.Error):
94-
failed_files = "\n".join(f" - {file}" for file in protos)
95-
rich.print(
96-
f"[red]Protoc failed to generate outputs for:\n\n"
97-
f"{failed_files}\n\nSee the output for the issue:\n{error}[red]",
98-
file=sys.stderr,
99-
)
79+
for result in results:
80+
for error in result.errors:
81+
if error.message.startswith("Syntax error"):
82+
rich.print(
83+
f"[red]File {str(result.file)}:\n",
84+
Syntax.from_path(
85+
str(result.file),
86+
line_numbers=True,
87+
line_range=(max(error.line - 5, 0), error.line),
88+
),
89+
f"{' ' * (error.column + 3)}^\nSyntaxError: {error.message}[red]",
90+
file=sys.stderr,
91+
)
92+
elif isinstance(error, protobuf_parser.Warning):
93+
rich.print(f"Warning: {error}", file=sys.stderr)
94+
else:
95+
failed_files = "\n".join(f" - {file}" for file in protos)
< EDBE code>96+
rich.print(
97+
f"[red]Protoc failed to generate outputs for:\n\n"
98+
f"{failed_files}\n\nSee the output for the issue:\n{error}[red]",
99+
file=sys.stderr,
100+
)
100101

101-
has_warnings = all(isinstance(e, Warning) for e in errors)
102-
if not errors or has_warnings:
102+
# has_warnings = all(isinstance(e, Warning) for e in errors)
103+
# if not errors or has_warnings:
104+
if True:
103105
rich.print(
104106
f"[bold green]Finished generating output for "
105107
f"{len(protos)} file{'s' if len(protos) != 1 else ''}, "
106108
f"output is in {output.as_posix()}"
107109
)
108110

109-
if errors:
110-
if not has_warnings:
111-
exit(2)
112-
exit(1)
111+
# if errors:
112+
# if not has_warnings:
113+
# exit(2)
114+
# exit(1)
113115
exit(0)

src/betterproto/plugin/cli/runner.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import asyncio
2-
from typing import TYPE_CHECKING, Any, List
2+
from typing import TYPE_CHECKING, Any, Sequence
33

44
import protobuf_parser
55

@@ -16,7 +16,7 @@
1616

1717

1818
def write_file(output: "Path", file: CodeGeneratorResponseFile) -> None:
19-
path = (output / file.name).resolve()
19+
path = output.joinpath(file.name).resolve()
2020
path.parent.mkdir(parents=True, exist_ok=True)
2121
path.write_text(file.content)
2222

@@ -26,7 +26,7 @@ async def compile_protobufs(
2626
output: "Path&quo F438 t;,
2727
use_betterproto: bool = True,
2828
**kwargs: Any,
29-
) -> List[protobuf_parser.Error]:
29+
) -> Sequence[protobuf_parser.ParseResult["Path"]]:
3030
"""
3131
A programmatic way to compile protobufs.
3232
@@ -44,20 +44,20 @@ async def compile_protobufs(
4444
A of exceptions from protoc.
4545
"""
4646
if use_betterproto:
47-
proto_files, errors = await utils.to_thread(protobuf_parser.parse, *files)
48-
if errors:
49-
return errors
47+
results = await utils.to_thread(protobuf_parser.parse, *files)
5048
request = CodeGeneratorRequest(
51-
proto_file=[FileDescriptorProto().parse(file) for file in proto_files]
49+
proto_file=[
50+
FileDescriptorProto().parse(result.parsed) for result in results
51+
]
5252
)
5353

5454
# Generate code
5555
response = await utils.to_thread(generate_code, request, **kwargs)
5656

5757
await asyncio.gather(
58-
*(utils.to_thread(write_file(output, file) for file in response.file))
58+
*(utils.to_thread(write_file, output, file) for file in response.file)
5959
)
60-
60+
return results
6161
else:
6262
errors = await utils.to_thread(
6363
protobuf_parser.run,
@@ -66,4 +66,4 @@ async def compile_protobufs(
6666
python_out=output.as_posix(),
6767
)
6868

69-
return errors
69+
return []

0 commit comments

Comments
 (0)
0