betterproto2-compiler 0.0.3__py3-none-any.whl → 0.1.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- betterproto2_compiler/compile/importing.py +11 -15
- betterproto2_compiler/lib/google/protobuf/__init__.py +0 -1
- betterproto2_compiler/lib/google/protobuf/compiler/__init__.py +229 -1
- betterproto2_compiler/plugin/compiler.py +5 -2
- betterproto2_compiler/plugin/models.py +83 -81
- betterproto2_compiler/plugin/module_validation.py +2 -7
- betterproto2_compiler/plugin/parser.py +30 -34
- betterproto2_compiler/plugin/typing_compiler.py +8 -12
- betterproto2_compiler/templates/header.py.j2 +2 -0
- betterproto2_compiler/templates/template.py.j2 +8 -1
- {betterproto2_compiler-0.0.3.dist-info → betterproto2_compiler-0.1.1.dist-info}/METADATA +3 -3
- betterproto2_compiler-0.1.1.dist-info/RECORD +26 -0
- {betterproto2_compiler-0.0.3.dist-info → betterproto2_compiler-0.1.1.dist-info}/WHEEL +1 -1
- betterproto2_compiler-0.1.1.dist-info/entry_points.txt +3 -0
- betterproto2_compiler/_types.py +0 -13
- betterproto2_compiler/enum.py +0 -180
- betterproto2_compiler/grpc/__init__.py +0 -0
- betterproto2_compiler/grpc/grpclib_client.py +0 -172
- betterproto2_compiler/grpc/grpclib_server.py +0 -32
- betterproto2_compiler/grpc/util/__init__.py +0 -0
- betterproto2_compiler/grpc/util/async_channel.py +0 -190
- betterproto2_compiler/lib/pydantic/__init__.py +0 -0
- betterproto2_compiler/lib/pydantic/google/__init__.py +0 -0
- betterproto2_compiler/lib/pydantic/google/protobuf/__init__.py +0 -2690
- betterproto2_compiler/lib/pydantic/google/protobuf/compiler/__init__.py +0 -209
- betterproto2_compiler/lib/std/__init__.py +0 -0
- betterproto2_compiler/lib/std/google/__init__.py +0 -0
- betterproto2_compiler/lib/std/google/protobuf/__init__.py +0 -2521
- betterproto2_compiler/lib/std/google/protobuf/compiler/__init__.py +0 -197
- betterproto2_compiler-0.0.3.dist-info/RECORD +0 -41
- betterproto2_compiler-0.0.3.dist-info/entry_points.txt +0 -3
- {betterproto2_compiler-0.0.3.dist-info → betterproto2_compiler-0.1.1.dist-info}/LICENSE.md +0 -0
@@ -3,22 +3,18 @@ from __future__ import annotations
|
|
3
3
|
import os
|
4
4
|
from typing import (
|
5
5
|
TYPE_CHECKING,
|
6
|
-
Dict,
|
7
|
-
List,
|
8
|
-
Set,
|
9
|
-
Tuple,
|
10
|
-
Type,
|
11
6
|
)
|
12
7
|
|
8
|
+
from betterproto2.lib.google import protobuf as google_protobuf
|
9
|
+
|
13
10
|
from ..casing import safe_snake_case
|
14
|
-
from ..lib.google import protobuf as google_protobuf
|
15
11
|
from .naming import pythonize_class_name
|
16
12
|
|
17
13
|
if TYPE_CHECKING:
|
18
14
|
from ..plugin.models import PluginRequestCompiler
|
19
15
|
from ..plugin.typing_compiler import TypingCompiler
|
20
16
|
|
21
|
-
WRAPPER_TYPES:
|
17
|
+
WRAPPER_TYPES: dict[str, type] = {
|
22
18
|
".google.protobuf.DoubleValue": google_protobuf.DoubleValue,
|
23
19
|
".google.protobuf.FloatValue": google_protobuf.FloatValue,
|
24
20
|
".google.protobuf.Int32Value": google_protobuf.Int32Value,
|
@@ -31,7 +27,7 @@ WRAPPER_TYPES: Dict[str, Type] = {
|
|
31
27
|
}
|
32
28
|
|
33
29
|
|
34
|
-
def parse_source_type_name(field_type_name: str, request:
|
30
|
+
def parse_source_type_name(field_type_name: str, request: PluginRequestCompiler) -> tuple[str, str]:
|
35
31
|
"""
|
36
32
|
Split full source type name into package and type name.
|
37
33
|
E.g. 'root.package.Message' -> ('root.package', 'Message')
|
@@ -77,7 +73,7 @@ def get_type_reference(
|
|
77
73
|
imports: set,
|
78
74
|
source_type: str,
|
79
75
|
typing_compiler: TypingCompiler,
|
80
|
-
request:
|
76
|
+
request: PluginRequestCompiler,
|
81
77
|
unwrap: bool = True,
|
82
78
|
pydantic: bool = False,
|
83
79
|
) -> str:
|
@@ -98,8 +94,8 @@ def get_type_reference(
|
|
98
94
|
|
99
95
|
source_package, source_type = parse_source_type_name(source_type, request)
|
100
96
|
|
101
|
-
current_package:
|
102
|
-
py_package:
|
97
|
+
current_package: list[str] = package.split(".") if package else []
|
98
|
+
py_package: list[str] = source_package.split(".") if source_package else []
|
103
99
|
py_type: str = pythonize_class_name(source_type)
|
104
100
|
|
105
101
|
compiling_google_protobuf = current_package == ["google", "protobuf"]
|
@@ -122,7 +118,7 @@ def get_type_reference(
|
|
122
118
|
return reference_cousin(current_package, imports, py_package, py_type)
|
123
119
|
|
124
120
|
|
125
|
-
def reference_absolute(imports:
|
121
|
+
def reference_absolute(imports: set[str], py_package: list[str], py_type: str) -> str:
|
126
122
|
"""
|
127
123
|
Returns a reference to a python type located in the root, i.e. sys.path.
|
128
124
|
"""
|
@@ -139,7 +135,7 @@ def reference_sibling(py_type: str) -> str:
|
|
139
135
|
return f"{py_type}"
|
140
136
|
|
141
137
|
|
142
|
-
def reference_descendent(current_package:
|
138
|
+
def reference_descendent(current_package: list[str], imports: set[str], py_package: list[str], py_type: str) -> str:
|
143
139
|
"""
|
144
140
|
Returns a reference to a python type in a package that is a descendent of the
|
145
141
|
current package, and adds the required import that is aliased to avoid name
|
@@ -157,7 +153,7 @@ def reference_descendent(current_package: List[str], imports: Set[str], py_packa
|
|
157
153
|
return f"{string_import}.{py_type}"
|
158
154
|
|
159
155
|
|
160
|
-
def reference_ancestor(current_package:
|
156
|
+
def reference_ancestor(current_package: list[str], imports: set[str], py_package: list[str], py_type: str) -> str:
|
161
157
|
"""
|
162
158
|
Returns a reference to a python type in a package which is an ancestor to the
|
163
159
|
current package, and adds the required import that is aliased (if possible) to avoid
|
@@ -178,7 +174,7 @@ def reference_ancestor(current_package: List[str], imports: Set[str], py_package
|
|
178
174
|
return string_alias
|
179
175
|
|
180
176
|
|
181
|
-
def reference_cousin(current_package:
|
177
|
+
def reference_cousin(current_package: list[str], imports: set[str], py_package: list[str], py_type: str) -> str:
|
182
178
|
"""
|
183
179
|
Returns a reference to a python type in a package that is not descendent, ancestor
|
184
180
|
or sibling, and adds the required import that is aliased to avoid name conflicts.
|
@@ -1 +0,0 @@
|
|
1
|
-
from betterproto2_compiler.lib.std.google.protobuf import *
|
@@ -1 +1,229 @@
|
|
1
|
-
|
1
|
+
# Generated by the protocol buffer compiler. DO NOT EDIT!
|
2
|
+
# sources: plugin.proto
|
3
|
+
# plugin: python-betterproto2
|
4
|
+
# This file has been @generated
|
5
|
+
|
6
|
+
__all__ = (
|
7
|
+
"CodeGeneratorResponseFeature",
|
8
|
+
"CodeGeneratorRequest",
|
9
|
+
"CodeGeneratorResponse",
|
10
|
+
"CodeGeneratorResponseFile",
|
11
|
+
"Version",
|
12
|
+
)
|
13
|
+
|
14
|
+
|
15
|
+
from dataclasses import dataclass
|
16
|
+
from typing import (
|
17
|
+
TYPE_CHECKING,
|
18
|
+
List,
|
19
|
+
Optional,
|
20
|
+
)
|
21
|
+
|
22
|
+
import betterproto2
|
23
|
+
|
24
|
+
if TYPE_CHECKING:
|
25
|
+
pass
|
26
|
+
|
27
|
+
betterproto2.check_compiler_version("0.1.0")
|
28
|
+
|
29
|
+
|
30
|
+
class CodeGeneratorResponseFeature(betterproto2.Enum):
|
31
|
+
"""
|
32
|
+
Sync with code_generator.h.
|
33
|
+
"""
|
34
|
+
|
35
|
+
FEATURE_NONE = 0
|
36
|
+
|
37
|
+
FEATURE_PROTO3_OPTIONAL = 1
|
38
|
+
|
39
|
+
FEATURE_SUPPORTS_EDITIONS = 2
|
40
|
+
|
41
|
+
|
42
|
+
@dataclass(eq=False, repr=False)
|
43
|
+
class CodeGeneratorRequest(betterproto2.Message):
|
44
|
+
"""
|
45
|
+
An encoded CodeGeneratorRequest is written to the plugin's stdin.
|
46
|
+
|
47
|
+
"""
|
48
|
+
|
49
|
+
file_to_generate: "List[str]" = betterproto2.field(1, betterproto2.TYPE_STRING, repeated=True)
|
50
|
+
"""
|
51
|
+
The .proto files that were explicitly listed on the command-line. The
|
52
|
+
code generator should generate code only for these files. Each file's
|
53
|
+
descriptor will be included in proto_file, below.
|
54
|
+
"""
|
55
|
+
parameter: "str" = betterproto2.field(2, betterproto2.TYPE_STRING)
|
56
|
+
"""
|
57
|
+
The generator parameter passed on the command-line.
|
58
|
+
"""
|
59
|
+
proto_file: "List[betterproto2_lib_google_protobuf.FileDescriptorProto]" = betterproto2.field(
|
60
|
+
15, betterproto2.TYPE_MESSAGE, repeated=True
|
61
|
+
)
|
62
|
+
"""
|
63
|
+
FileDescriptorProtos for all files in files_to_generate and everything
|
64
|
+
they import. The files will appear in topological order, so each file
|
65
|
+
appears before any file that imports it.
|
66
|
+
|
67
|
+
Note: the files listed in files_to_generate will include runtime-retention
|
68
|
+
options only, but all other files will include source-retention options.
|
69
|
+
The source_file_descriptors field below is available in case you need
|
70
|
+
source-retention options for files_to_generate.
|
71
|
+
|
72
|
+
protoc guarantees that all proto_files will be written after
|
73
|
+
the fields above, even though this is not technically guaranteed by the
|
74
|
+
protobuf wire format. This theoretically could allow a plugin to stream
|
75
|
+
in the FileDescriptorProtos and handle them one by one rather than read
|
76
|
+
the entire set into memory at once. However, as of this writing, this
|
77
|
+
is not similarly optimized on protoc's end -- it will store all fields in
|
78
|
+
memory at once before sending them to the plugin.
|
79
|
+
|
80
|
+
Type names of fields and extensions in the FileDescriptorProto are always
|
81
|
+
fully qualified.
|
82
|
+
"""
|
83
|
+
source_file_descriptors: "List[betterproto2_lib_google_protobuf.FileDescriptorProto]" = betterproto2.field(
|
84
|
+
17, betterproto2.TYPE_MESSAGE, repeated=True
|
85
|
+
)
|
86
|
+
"""
|
87
|
+
File descriptors with all options, including source-retention options.
|
88
|
+
These descriptors are only provided for the files listed in
|
89
|
+
files_to_generate.
|
90
|
+
"""
|
91
|
+
compiler_version: "Optional[Version]" = betterproto2.field(3, betterproto2.TYPE_MESSAGE, optional=True)
|
92
|
+
"""
|
93
|
+
The version number of protocol compiler.
|
94
|
+
"""
|
95
|
+
|
96
|
+
|
97
|
+
@dataclass(eq=False, repr=False)
|
98
|
+
class CodeGeneratorResponse(betterproto2.Message):
|
99
|
+
"""
|
100
|
+
The plugin writes an encoded CodeGeneratorResponse to stdout.
|
101
|
+
|
102
|
+
"""
|
103
|
+
|
104
|
+
error: "str" = betterproto2.field(1, betterproto2.TYPE_STRING)
|
105
|
+
"""
|
106
|
+
Error message. If non-empty, code generation failed. The plugin process
|
107
|
+
should exit with status code zero even if it reports an error in this way.
|
108
|
+
|
109
|
+
This should be used to indicate errors in .proto files which prevent the
|
110
|
+
code generator from generating correct code. Errors which indicate a
|
111
|
+
problem in protoc itself -- such as the input CodeGeneratorRequest being
|
112
|
+
unparseable -- should be reported by writing a message to stderr and
|
113
|
+
exiting with a non-zero status code.
|
114
|
+
"""
|
115
|
+
supported_features: "int" = betterproto2.field(2, betterproto2.TYPE_UINT64)
|
116
|
+
"""
|
117
|
+
A bitmask of supported features that the code generator supports.
|
118
|
+
This is a bitwise "or" of values from the Feature enum.
|
119
|
+
"""
|
120
|
+
minimum_edition: "int" = betterproto2.field(3, betterproto2.TYPE_INT32)
|
121
|
+
"""
|
122
|
+
The minimum edition this plugin supports. This will be treated as an
|
123
|
+
Edition enum, but we want to allow unknown values. It should be specified
|
124
|
+
according the edition enum value, *not* the edition number. Only takes
|
125
|
+
effect for plugins that have FEATURE_SUPPORTS_EDITIONS set.
|
126
|
+
"""
|
127
|
+
maximum_edition: "int" = betterproto2.field(4, betterproto2.TYPE_INT32)
|
128
|
+
"""
|
129
|
+
The maximum edition this plugin supports. This will be treated as an
|
130
|
+
Edition enum, but we want to allow unknown values. It should be specified
|
131
|
+
according the edition enum value, *not* the edition number. Only takes
|
132
|
+
effect for plugins that have FEATURE_SUPPORTS_EDITIONS set.
|
133
|
+
"""
|
134
|
+
file: "List[CodeGeneratorResponseFile]" = betterproto2.field(15, betterproto2.TYPE_MESSAGE, repeated=True)
|
135
|
+
|
136
|
+
|
137
|
+
@dataclass(eq=False, repr=False)
|
138
|
+
class CodeGeneratorResponseFile(betterproto2.Message):
|
139
|
+
"""
|
140
|
+
Represents a single generated file.
|
141
|
+
|
142
|
+
"""
|
143
|
+
|
144
|
+
name: "str" = betterproto2.field(1, betterproto2.TYPE_STRING)
|
145
|
+
"""
|
146
|
+
The file name, relative to the output directory. The name must not
|
147
|
+
contain "." or ".." components and must be relative, not be absolute (so,
|
148
|
+
the file cannot lie outside the output directory). "/" must be used as
|
149
|
+
the path separator, not "\".
|
150
|
+
|
151
|
+
If the name is omitted, the content will be appended to the previous
|
152
|
+
file. This allows the generator to break large files into small chunks,
|
153
|
+
and allows the generated text to be streamed back to protoc so that large
|
154
|
+
files need not reside completely in memory at one time. Note that as of
|
155
|
+
this writing protoc does not optimize for this -- it will read the entire
|
156
|
+
CodeGeneratorResponse before writing files to disk.
|
157
|
+
"""
|
158
|
+
insertion_point: "str" = betterproto2.field(2, betterproto2.TYPE_STRING)
|
159
|
+
"""
|
160
|
+
If non-empty, indicates that the named file should already exist, and the
|
161
|
+
content here is to be inserted into that file at a defined insertion
|
162
|
+
point. This feature allows a code generator to extend the output
|
163
|
+
produced by another code generator. The original generator may provide
|
164
|
+
insertion points by placing special annotations in the file that look
|
165
|
+
like:
|
166
|
+
@@protoc_insertion_point(NAME)
|
167
|
+
The annotation can have arbitrary text before and after it on the line,
|
168
|
+
which allows it to be placed in a comment. NAME should be replaced with
|
169
|
+
an identifier naming the point -- this is what other generators will use
|
170
|
+
as the insertion_point. Code inserted at this point will be placed
|
171
|
+
immediately above the line containing the insertion point (thus multiple
|
172
|
+
insertions to the same point will come out in the order they were added).
|
173
|
+
The double-@ is intended to make it unlikely that the generated code
|
174
|
+
could contain things that look like insertion points by accident.
|
175
|
+
|
176
|
+
For example, the C++ code generator places the following line in the
|
177
|
+
.pb.h files that it generates:
|
178
|
+
// @@protoc_insertion_point(namespace_scope)
|
179
|
+
This line appears within the scope of the file's package namespace, but
|
180
|
+
outside of any particular class. Another plugin can then specify the
|
181
|
+
insertion_point "namespace_scope" to generate additional classes or
|
182
|
+
other declarations that should be placed in this scope.
|
183
|
+
|
184
|
+
Note that if the line containing the insertion point begins with
|
185
|
+
whitespace, the same whitespace will be added to every line of the
|
186
|
+
inserted text. This is useful for languages like Python, where
|
187
|
+
indentation matters. In these languages, the insertion point comment
|
188
|
+
should be indented the same amount as any inserted code will need to be
|
189
|
+
in order to work correctly in that context.
|
190
|
+
|
191
|
+
The code generator that generates the initial file and the one which
|
192
|
+
inserts into it must both run as part of a single invocation of protoc.
|
193
|
+
Code generators are executed in the order in which they appear on the
|
194
|
+
command line.
|
195
|
+
|
196
|
+
If |insertion_point| is present, |name| must also be present.
|
197
|
+
"""
|
198
|
+
content: "str" = betterproto2.field(15, betterproto2.TYPE_STRING)
|
199
|
+
"""
|
200
|
+
The file contents.
|
201
|
+
"""
|
202
|
+
generated_code_info: "Optional[betterproto2_lib_google_protobuf.GeneratedCodeInfo]" = betterproto2.field(
|
203
|
+
16, betterproto2.TYPE_MESSAGE, optional=True
|
204
|
+
)
|
205
|
+
"""
|
206
|
+
Information describing the file content being inserted. If an insertion
|
207
|
+
point is used, this information will be appropriately offset and inserted
|
208
|
+
into the code generation metadata for the generated files.
|
209
|
+
"""
|
210
|
+
|
211
|
+
|
212
|
+
@dataclass(eq=False, repr=False)
|
213
|
+
class Version(betterproto2.Message):
|
214
|
+
"""
|
215
|
+
The version number of protocol compiler.
|
216
|
+
|
217
|
+
"""
|
218
|
+
|
219
|
+
major: "int" = betterproto2.field(1, betterproto2.TYPE_INT32)
|
220
|
+
minor: "int" = betterproto2.field(2, betterproto2.TYPE_INT32)
|
221
|
+
patch: "int" = betterproto2.field(3, betterproto2.TYPE_INT32)
|
222
|
+
suffix: "str" = betterproto2.field(4, betterproto2.TYPE_STRING)
|
223
|
+
"""
|
224
|
+
A suffix for alpha, beta or rc release, e.g., "alpha-1", "rc2". It should
|
225
|
+
be empty for mainline stable releases.
|
226
|
+
"""
|
227
|
+
|
228
|
+
|
229
|
+
import betterproto2.lib.google.protobuf as betterproto2_lib_google_protobuf
|
@@ -1,6 +1,7 @@
|
|
1
1
|
import os.path
|
2
2
|
import subprocess
|
3
3
|
import sys
|
4
|
+
from importlib import metadata
|
4
5
|
|
5
6
|
from .module_validation import ModuleValidator
|
6
7
|
|
@@ -14,7 +15,7 @@ except ImportError as err:
|
|
14
15
|
"Please ensure that you've installed betterproto as "
|
15
16
|
'`pip install "betterproto[compiler]"` so that compiler dependencies '
|
16
17
|
"are included."
|
17
|
-
"\033[0m"
|
18
|
+
"\033[0m",
|
18
19
|
)
|
19
20
|
raise SystemExit(1)
|
20
21
|
|
@@ -24,6 +25,8 @@ from .models import OutputTemplate
|
|
24
25
|
def outputfile_compiler(output_file: OutputTemplate) -> str:
|
25
26
|
templates_folder = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "templates"))
|
26
27
|
|
28
|
+
version = metadata.version("betterproto2_compiler")
|
29
|
+
|
27
30
|
env = jinja2.Environment(
|
28
31
|
trim_blocks=True,
|
29
32
|
lstrip_blocks=True,
|
@@ -35,7 +38,7 @@ def outputfile_compiler(output_file: OutputTemplate) -> str:
|
|
35
38
|
header_template = env.get_template("header.py.j2")
|
36
39
|
|
37
40
|
code = body_template.render(output_file=output_file)
|
38
|
-
code = header_template.render(output_file=output_file) + "\n" + code
|
41
|
+
code = header_template.render(output_file=output_file, version=version) + "\n" + code
|
39
42
|
|
40
43
|
# Sort imports, delete unused ones
|
41
44
|
code = subprocess.check_output(
|