betterproto2-compiler 0.1.0__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.
@@ -5,8 +5,9 @@ from typing import (
5
5
  TYPE_CHECKING,
6
6
  )
7
7
 
8
+ from betterproto2.lib.google import protobuf as google_protobuf
9
+
8
10
  from ..casing import safe_snake_case
9
- from ..lib.google import protobuf as google_protobuf
10
11
  from .naming import pythonize_class_name
11
12
 
12
13
  if TYPE_CHECKING:
@@ -1 +0,0 @@
1
- from betterproto2_compiler.lib.std.google.protobuf import *
@@ -1 +1,229 @@
1
- from betterproto2_compiler.lib.std.google.protobuf.compiler import *
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
@@ -41,13 +41,7 @@ from typing import (
41
41
  )
42
42
 
43
43
  import betterproto2
44
-
45
- from betterproto2_compiler.compile.naming import (
46
- pythonize_class_name,
47
- pythonize_field_name,
48
- pythonize_method_name,
49
- )
50
- from betterproto2_compiler.lib.google.protobuf import (
44
+ from betterproto2.lib.google.protobuf import (
51
45
  DescriptorProto,
52
46
  EnumDescriptorProto,
53
47
  FieldDescriptorProto,
@@ -56,6 +50,13 @@ from betterproto2_compiler.lib.google.protobuf import (
56
50
  FieldDescriptorProtoType as FieldType,
57
51
  FileDescriptorProto,
58
52
  MethodDescriptorProto,
53
+ OneofDescriptorProto,
54
+ )
55
+
56
+ from betterproto2_compiler.compile.naming import (
57
+ pythonize_class_name,
58
+ pythonize_field_name,
59
+ pythonize_method_name,
59
60
  )
60
61
  from betterproto2_compiler.lib.google.protobuf.compiler import CodeGeneratorRequest
61
62
 
@@ -117,28 +118,6 @@ PROTO_PACKED_TYPES = (
117
118
  )
118
119
 
119
120
 
120
- # TODO patch again to make field optional
121
- # def monkey_patch_oneof_index():
122
- # """
123
- # The compiler message types are written for proto2, but we read them as proto3.
124
- # For this to work in the case of the oneof_index fields, which depend on being able
125
- # to tell whether they were set, we have to treat them as oneof fields. This method
126
- # monkey patches the generated classes after the fact to force this behaviour.
127
- # """
128
- # object.__setattr__(
129
- # FieldDescriptorProto.__dataclass_fields__["oneof_index"].metadata[
130
- # "betterproto"
131
- # ],
132
- # "group",
133
- # "oneof_index",
134
- # )
135
- # object.__setattr__(
136
- # Field.__dataclass_fields__["oneof_index"].metadata["betterproto"],
137
- # "group",
138
- # "oneof_index",
139
- # )
140
-
141
-
142
121
  def get_comment(
143
122
  proto_file: "FileDescriptorProto",
144
123
  path: list[int],
@@ -287,6 +266,7 @@ class MessageCompiler(ProtoContentBase):
287
266
  proto_obj: DescriptorProto = PLACEHOLDER
288
267
  path: list[int] = PLACEHOLDER
289
268
  fields: list[Union["FieldCompiler", "MessageCompiler"]] = field(default_factory=list)
269
+ oneofs: list["OneofCompiler"] = field(default_factory=list)
290
270
  builtins_types: set[str] = field(default_factory=set)
291
271
 
292
272
  def __post_init__(self) -> None:
@@ -566,6 +546,26 @@ class MapEntryCompiler(FieldCompiler):
566
546
  return False # maps cannot be repeated
567
547
 
568
548
 
549
+ @dataclass
550
+ class OneofCompiler(ProtoContentBase):
551
+ source_file: FileDescriptorProto
552
+ typing_compiler: TypingCompiler
553
+ path: list[int] = PLACEHOLDER
554
+
555
+ parent: MessageCompiler = PLACEHOLDER
556
+ proto_obj: OneofDescriptorProto = PLACEHOLDER
557
+
558
+ def __post_init__(self) -> None:
559
+ # Add oneof to message
560
+ if isinstance(self.parent, MessageCompiler): # TODO why?
561
+ self.parent.oneofs.append(self)
562
+ super().__post_init__()
563
+
564
+ @property
565
+ def name(self) -> str:
566
+ return self.proto_obj.name
567
+
568
+
569
569
  @dataclass
570
570
  class EnumDefinitionCompiler(MessageCompiler):
571
571
  """Representation of a proto Enum definition."""
@@ -2,13 +2,13 @@ import pathlib
2
2
  import sys
3
3
  from collections.abc import Generator
4
4
 
5
- from betterproto2_compiler.lib.google.protobuf import (
5
+ from betterproto2.lib.google.protobuf import (
6
6
  DescriptorProto,
7
7
  EnumDescriptorProto,
8
- FieldDescriptorProto,
9
8
  FileDescriptorProto,
10
9
  ServiceDescriptorProto,
11
10
  )
11
+
12
12
  from betterproto2_compiler.lib.google.protobuf.compiler import (
13
13
  CodeGeneratorRequest,
14
14
  CodeGeneratorResponse,
@@ -22,6 +22,7 @@ from .models import (
22
22
  FieldCompiler,
23
23
  MapEntryCompiler,
24
24
  MessageCompiler,
25
+ OneofCompiler,
25
26
  OneOfFieldCompiler,
26
27
  OutputTemplate,
27
28
  PluginRequestCompiler,
@@ -173,22 +174,6 @@ def generate_code(request: CodeGeneratorRequest) -> CodeGeneratorResponse:
173
174
  return response
174
175
 
175
176
 
176
- def _make_one_of_field_compiler(
177
- output_package: OutputTemplate,
178
- source_file: "FileDescriptorProto",
179
- parent: MessageCompiler,
180
- proto_obj: "FieldDescriptorProto",
181
- path: list[int],
182
- ) -> FieldCompiler:
183
- return OneOfFieldCompiler(
184
- source_file=source_file,
185
- parent=parent,
186
- proto_obj=proto_obj,
187
- path=path,
188
- typing_compiler=output_package.typing_compiler,
189
- )
190
-
191
-
192
177
  def read_protobuf_type(
193
178
  item: DescriptorProto,
194
179
  path: list[int],
@@ -217,7 +202,13 @@ def read_protobuf_type(
217
202
  typing_compiler=output_package.typing_compiler,
218
203
  )
219
204
  elif is_oneof(field):
220
- _make_one_of_field_compiler(output_package, source_file, message_data, field, path + [2, index])
205
+ OneOfFieldCompiler(
206
+ source_file=source_file,
207
+ parent=message_data,
208
+ proto_obj=field,
209
+ path=path + [2, index],
210
+ typing_compiler=output_package.typing_compiler,
211
+ )
221
212
  else:
222
213
  FieldCompiler(
223
214
  source_file=source_file,
@@ -226,6 +217,16 @@ def read_protobuf_type(
226
217
  path=path + [2, index],
227
218
  typing_compiler=output_package.typing_compiler,
228
219
  )
220
+
221
+ for index, oneof in enumerate(item.oneof_decl):
222
+ OneofCompiler(
223
+ source_file=source_file,
224
+ typing_compiler=output_package.typing_compiler,
225
+ path=path + [8, index],
226
+ parent=message_data,
227
+ proto_obj=oneof,
228
+ )
229
+
229
230
  elif isinstance(item, EnumDescriptorProto):
230
231
  # Enum
231
232
  EnumDefinitionCompiler(
@@ -32,9 +32,16 @@ class {{ enum.py_name }}(betterproto2.Enum):
32
32
  @dataclass(eq=False, repr=False)
33
33
  {% endif %}
34
34
  class {{ message.py_name }}(betterproto2.Message):
35
- {% if message.comment %}
35
+ {% if message.comment or message.oneofs %}
36
36
  """
37
37
  {{ message.comment | indent(4) }}
38
+
39
+ {% if message.oneofs %}
40
+ Oneofs:
41
+ {% for oneof in message.oneofs %}
42
+ - {{ oneof.name }}: {{ oneof.comment | indent(12) }}
43
+ {% endfor %}
44
+ {% endif %}
38
45
  """
39
46
  {% endif %}
40
47
 
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.3
2
2
  Name: betterproto2_compiler
3
- Version: 0.1.0
3
+ Version: 0.1.1
4
4
  Summary: Compiler for betterproto2
5
5
  Home-page: https://github.com/betterproto/python-betterproto2-compiler
6
6
  License: MIT
@@ -14,7 +14,7 @@ Classifier: Programming Language :: Python :: 3.10
14
14
  Classifier: Programming Language :: Python :: 3.11
15
15
  Classifier: Programming Language :: Python :: 3.12
16
16
  Classifier: Programming Language :: Python :: 3.13
17
- Requires-Dist: betterproto2 (==0.0.2)
17
+ Requires-Dist: betterproto2 (>=0.1.1,<0.2.0)
18
18
  Requires-Dist: grpclib (>=0.4.1,<0.5.0)
19
19
  Requires-Dist: jinja2 (>=3.0.3)
20
20
  Requires-Dist: ruff (>=0.7.4,<0.8.0)
@@ -0,0 +1,26 @@
1
+ betterproto2_compiler/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ betterproto2_compiler/casing.py,sha256=bMdI4W0hfYh6kV-DQIqFEjSfGYEqUtPciAzP64z5HLQ,3587
3
+ betterproto2_compiler/compile/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
+ betterproto2_compiler/compile/importing.py,sha256=JgvVFlWiA8zeb8L_pcAbxH61tDGNNNx18kVXRrfmIlo,7400
5
+ betterproto2_compiler/compile/naming.py,sha256=zf0VOmNojzyv33upOGelGxjZTEDE8JULEEED5_3inHg,562
6
+ betterproto2_compiler/lib/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
7
+ betterproto2_compiler/lib/google/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
8
+ betterproto2_compiler/lib/google/protobuf/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
9
+ betterproto2_compiler/lib/google/protobuf/compiler/__init__.py,sha256=CX9UOXUUZD8GpeS7CWb1JTJoPIKi2e6lH7BCw6RJ8uI,9285
10
+ betterproto2_compiler/plugin/__init__.py,sha256=L3pW0b4CvkM5x53x_sYt1kYiSFPO0_vaeH6EQPq9FAM,43
11
+ betterproto2_compiler/plugin/__main__.py,sha256=vBQ82334kX06ImDbFlPFgiBRiLIinwNk3z8Khs6hd74,31
12
+ betterproto2_compiler/plugin/compiler.py,sha256=jICLI4-5rAOkWQI1v5j7JqIvoao-ZM9szMuq0OBRteA,2138
13
+ betterproto2_compiler/plugin/main.py,sha256=Q9PmcJqXuYYFe51l7AqHVzJrHqi2LWCUu80CZSQOOwk,1469
14
+ betterproto2_compiler/plugin/models.py,sha256=RfHRt8Iw_v9q-Rpi58VNHkcDfED5TwHWur2O4wr1Z8Q,24482
15
+ betterproto2_compiler/plugin/module_validation.py,sha256=RdPFwdmkbD6NKADaHC5eaPix_pz-yGxHvYJj8Ev48fA,4822
16
+ betterproto2_compiler/plugin/parser.py,sha256=AZihuRp7XCTKG04YNJcthR9fmarllfkPSfLS0l9exLI,9668
17
+ betterproto2_compiler/plugin/plugin.bat,sha256=lfLT1WguAXqyerLLsRL6BfHA0RqUE6QG79v-1BYVSpI,48
18
+ betterproto2_compiler/plugin/typing_compiler.py,sha256=IK6m4ggHXK7HL98Ed_WjvQ_yeWfIpf_fIBZ9SA8UcyM,4873
19
+ betterproto2_compiler/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
20
+ betterproto2_compiler/templates/header.py.j2,sha256=nxqsengMcM_IRqQYNVntPQ0gUFUPCy_1P1mcoLvbDos,1464
21
+ betterproto2_compiler/templates/template.py.j2,sha256=R_9a5IvX2wU6yKzcdjj-uMb09askExrShu4UX2ayQO8,8911
22
+ betterproto2_compiler-0.1.1.dist-info/LICENSE.md,sha256=Pgl2pReU-2yw2miGeQ55UFlyzqAZ_EpYVyZ2nWjwRv4,1121
23
+ betterproto2_compiler-0.1.1.dist-info/METADATA,sha256=G9d9cnAsNG8x00x3wWxDDcWZdXOaZyaitkblivnE8lw,1170
24
+ betterproto2_compiler-0.1.1.dist-info/WHEEL,sha256=RaoafKOydTQ7I_I3JTrPCg6kUmTgtm4BornzOqyEfJ8,88
25
+ betterproto2_compiler-0.1.1.dist-info/entry_points.txt,sha256=re3Qg8lLljbVobeeKH2f1FVQZ114wfZkGv3zCZTD8Ok,84
26
+ betterproto2_compiler-0.1.1.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: poetry-core 1.9.1
2
+ Generator: poetry-core 2.0.0
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
File without changes
File without changes