protobuf 6.33.0__cp39-abi3-manylinux2014_s390x.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.
Potentially problematic release.
This version of protobuf might be problematic. Click here for more details.
- google/_upb/_message.abi3.so +0 -0
- google/protobuf/__init__.py +10 -0
- google/protobuf/any.py +53 -0
- google/protobuf/any_pb2.py +37 -0
- google/protobuf/api_pb2.py +47 -0
- google/protobuf/compiler/__init__.py +0 -0
- google/protobuf/compiler/plugin_pb2.py +46 -0
- google/protobuf/descriptor.py +1676 -0
- google/protobuf/descriptor_database.py +172 -0
- google/protobuf/descriptor_pb2.py +3363 -0
- google/protobuf/descriptor_pool.py +1370 -0
- google/protobuf/duration.py +100 -0
- google/protobuf/duration_pb2.py +37 -0
- google/protobuf/empty_pb2.py +37 -0
- google/protobuf/field_mask_pb2.py +37 -0
- google/protobuf/internal/__init__.py +7 -0
- google/protobuf/internal/api_implementation.py +136 -0
- google/protobuf/internal/builder.py +118 -0
- google/protobuf/internal/containers.py +690 -0
- google/protobuf/internal/decoder.py +1066 -0
- google/protobuf/internal/encoder.py +806 -0
- google/protobuf/internal/enum_type_wrapper.py +112 -0
- google/protobuf/internal/extension_dict.py +194 -0
- google/protobuf/internal/field_mask.py +312 -0
- google/protobuf/internal/message_listener.py +55 -0
- google/protobuf/internal/python_edition_defaults.py +5 -0
- google/protobuf/internal/python_message.py +1599 -0
- google/protobuf/internal/testing_refleaks.py +128 -0
- google/protobuf/internal/type_checkers.py +455 -0
- google/protobuf/internal/well_known_types.py +695 -0
- google/protobuf/internal/wire_format.py +245 -0
- google/protobuf/json_format.py +1111 -0
- google/protobuf/message.py +448 -0
- google/protobuf/message_factory.py +190 -0
- google/protobuf/proto.py +153 -0
- google/protobuf/proto_builder.py +111 -0
- google/protobuf/proto_json.py +83 -0
- google/protobuf/proto_text.py +129 -0
- google/protobuf/pyext/__init__.py +0 -0
- google/protobuf/pyext/cpp_message.py +49 -0
- google/protobuf/reflection.py +36 -0
- google/protobuf/runtime_version.py +104 -0
- google/protobuf/service_reflection.py +272 -0
- google/protobuf/source_context_pb2.py +37 -0
- google/protobuf/struct_pb2.py +47 -0
- google/protobuf/symbol_database.py +179 -0
- google/protobuf/testdata/__init__.py +0 -0
- google/protobuf/text_encoding.py +106 -0
- google/protobuf/text_format.py +1884 -0
- google/protobuf/timestamp.py +112 -0
- google/protobuf/timestamp_pb2.py +37 -0
- google/protobuf/type_pb2.py +53 -0
- google/protobuf/unknown_fields.py +96 -0
- google/protobuf/util/__init__.py +0 -0
- google/protobuf/wrappers_pb2.py +53 -0
- protobuf-6.33.0.dist-info/LICENSE +32 -0
- protobuf-6.33.0.dist-info/METADATA +17 -0
- protobuf-6.33.0.dist-info/RECORD +59 -0
- protobuf-6.33.0.dist-info/WHEEL +4 -0
google/protobuf/proto.py
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
# Protocol Buffers - Google's data interchange format
|
|
2
|
+
# Copyright 2008 Google Inc. All rights reserved.
|
|
3
|
+
#
|
|
4
|
+
# Use of this source code is governed by a BSD-style
|
|
5
|
+
# license that can be found in the LICENSE file or at
|
|
6
|
+
# https://developers.google.com/open-source/licenses/bsd
|
|
7
|
+
|
|
8
|
+
"""Contains the Nextgen Pythonic protobuf APIs."""
|
|
9
|
+
|
|
10
|
+
import io
|
|
11
|
+
from typing import Text, Type, TypeVar
|
|
12
|
+
|
|
13
|
+
from google.protobuf.internal import decoder
|
|
14
|
+
from google.protobuf.internal import encoder
|
|
15
|
+
from google.protobuf.message import Message
|
|
16
|
+
|
|
17
|
+
_MESSAGE = TypeVar('_MESSAGE', bound='Message')
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def serialize(message: _MESSAGE, deterministic: bool = None) -> bytes:
|
|
21
|
+
"""Return the serialized proto.
|
|
22
|
+
|
|
23
|
+
Args:
|
|
24
|
+
message: The proto message to be serialized.
|
|
25
|
+
deterministic: If true, requests deterministic serialization
|
|
26
|
+
of the protobuf, with predictable ordering of map keys.
|
|
27
|
+
|
|
28
|
+
Returns:
|
|
29
|
+
A binary bytes representation of the message.
|
|
30
|
+
"""
|
|
31
|
+
return message.SerializeToString(deterministic=deterministic)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def parse(message_class: Type[_MESSAGE], payload: bytes) -> _MESSAGE:
|
|
35
|
+
"""Given a serialized data in binary form, deserialize it into a Message.
|
|
36
|
+
|
|
37
|
+
Args:
|
|
38
|
+
message_class: The message meta class.
|
|
39
|
+
payload: A serialized bytes in binary form.
|
|
40
|
+
|
|
41
|
+
Returns:
|
|
42
|
+
A new message deserialized from payload.
|
|
43
|
+
"""
|
|
44
|
+
new_message = message_class()
|
|
45
|
+
new_message.ParseFromString(payload)
|
|
46
|
+
return new_message
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def serialize_length_prefixed(message: _MESSAGE, output: io.BytesIO) -> None:
|
|
50
|
+
"""Writes the size of the message as a varint and the serialized message.
|
|
51
|
+
|
|
52
|
+
Writes the size of the message as a varint and then the serialized message.
|
|
53
|
+
This allows more data to be written to the output after the message. Use
|
|
54
|
+
parse_length_prefixed to parse messages written by this method.
|
|
55
|
+
|
|
56
|
+
The output stream must be buffered, e.g. using
|
|
57
|
+
https://docs.python.org/3/library/io.html#buffered-streams.
|
|
58
|
+
|
|
59
|
+
Example usage:
|
|
60
|
+
out = io.BytesIO()
|
|
61
|
+
for msg in message_list:
|
|
62
|
+
proto.serialize_length_prefixed(msg, out)
|
|
63
|
+
|
|
64
|
+
Args:
|
|
65
|
+
message: The protocol buffer message that should be serialized.
|
|
66
|
+
output: BytesIO or custom buffered IO that data should be written to.
|
|
67
|
+
"""
|
|
68
|
+
size = message.ByteSize()
|
|
69
|
+
encoder._VarintEncoder()(output.write, size)
|
|
70
|
+
out_size = output.write(serialize(message))
|
|
71
|
+
|
|
72
|
+
if out_size != size:
|
|
73
|
+
raise TypeError(
|
|
74
|
+
'Failed to write complete message (wrote: %d, expected: %d)'
|
|
75
|
+
'. Ensure output is using buffered IO.' % (out_size, size)
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def parse_length_prefixed(
|
|
80
|
+
message_class: Type[_MESSAGE], input_bytes: io.BytesIO
|
|
81
|
+
) -> _MESSAGE:
|
|
82
|
+
"""Parse a message from input_bytes.
|
|
83
|
+
|
|
84
|
+
Args:
|
|
85
|
+
message_class: The protocol buffer message class that parser should parse.
|
|
86
|
+
input_bytes: A buffered input.
|
|
87
|
+
|
|
88
|
+
Example usage:
|
|
89
|
+
while True:
|
|
90
|
+
msg = proto.parse_length_prefixed(message_class, input_bytes)
|
|
91
|
+
if msg is None:
|
|
92
|
+
break
|
|
93
|
+
...
|
|
94
|
+
|
|
95
|
+
Returns:
|
|
96
|
+
A parsed message if successful. None if input_bytes is at EOF.
|
|
97
|
+
"""
|
|
98
|
+
size = decoder._DecodeVarint(input_bytes)
|
|
99
|
+
if size is None:
|
|
100
|
+
# It is the end of buffered input. See example usage in the
|
|
101
|
+
# API description.
|
|
102
|
+
return None
|
|
103
|
+
|
|
104
|
+
message = message_class()
|
|
105
|
+
|
|
106
|
+
if size == 0:
|
|
107
|
+
return message
|
|
108
|
+
|
|
109
|
+
parsed_size = message.ParseFromString(input_bytes.read(size))
|
|
110
|
+
if parsed_size != size:
|
|
111
|
+
raise ValueError(
|
|
112
|
+
'Truncated message or non-buffered input_bytes: '
|
|
113
|
+
'Expected {0} bytes but only {1} bytes parsed for '
|
|
114
|
+
'{2}.'.format(size, parsed_size, message.DESCRIPTOR.name)
|
|
115
|
+
)
|
|
116
|
+
return message
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def byte_size(message: Message) -> int:
|
|
120
|
+
"""Returns the serialized size of this message.
|
|
121
|
+
|
|
122
|
+
Args:
|
|
123
|
+
message: A proto message.
|
|
124
|
+
|
|
125
|
+
Returns:
|
|
126
|
+
int: The number of bytes required to serialize this message.
|
|
127
|
+
"""
|
|
128
|
+
return message.ByteSize()
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def clear_message(message: Message) -> None:
|
|
132
|
+
"""Clears all data that was set in the message.
|
|
133
|
+
|
|
134
|
+
Args:
|
|
135
|
+
message: The proto message to be cleared.
|
|
136
|
+
"""
|
|
137
|
+
message.Clear()
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def clear_field(message: Message, field_name: Text) -> None:
|
|
141
|
+
"""Clears the contents of a given field.
|
|
142
|
+
|
|
143
|
+
Inside a oneof group, clears the field set. If the name neither refers to a
|
|
144
|
+
defined field or oneof group, :exc:`ValueError` is raised.
|
|
145
|
+
|
|
146
|
+
Args:
|
|
147
|
+
message: The proto message.
|
|
148
|
+
field_name (str): The name of the field to be cleared.
|
|
149
|
+
|
|
150
|
+
Raises:
|
|
151
|
+
ValueError: if the `field_name` is not a member of this message.
|
|
152
|
+
"""
|
|
153
|
+
message.ClearField(field_name)
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# Protocol Buffers - Google's data interchange format
|
|
2
|
+
# Copyright 2008 Google Inc. All rights reserved.
|
|
3
|
+
#
|
|
4
|
+
# Use of this source code is governed by a BSD-style
|
|
5
|
+
# license that can be found in the LICENSE file or at
|
|
6
|
+
# https://developers.google.com/open-source/licenses/bsd
|
|
7
|
+
|
|
8
|
+
"""Dynamic Protobuf class creator."""
|
|
9
|
+
|
|
10
|
+
from collections import OrderedDict
|
|
11
|
+
import hashlib
|
|
12
|
+
import os
|
|
13
|
+
|
|
14
|
+
from google.protobuf import descriptor_pb2
|
|
15
|
+
from google.protobuf import descriptor
|
|
16
|
+
from google.protobuf import descriptor_pool
|
|
17
|
+
from google.protobuf import message_factory
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _GetMessageFromFactory(pool, full_name):
|
|
21
|
+
"""Get a proto class from the MessageFactory by name.
|
|
22
|
+
|
|
23
|
+
Args:
|
|
24
|
+
pool: a descriptor pool.
|
|
25
|
+
full_name: str, the fully qualified name of the proto type.
|
|
26
|
+
Returns:
|
|
27
|
+
A class, for the type identified by full_name.
|
|
28
|
+
Raises:
|
|
29
|
+
KeyError, if the proto is not found in the factory's descriptor pool.
|
|
30
|
+
"""
|
|
31
|
+
proto_descriptor = pool.FindMessageTypeByName(full_name)
|
|
32
|
+
proto_cls = message_factory.GetMessageClass(proto_descriptor)
|
|
33
|
+
return proto_cls
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def MakeSimpleProtoClass(fields, full_name=None, pool=None):
|
|
37
|
+
"""Create a Protobuf class whose fields are basic types.
|
|
38
|
+
|
|
39
|
+
Note: this doesn't validate field names!
|
|
40
|
+
|
|
41
|
+
Args:
|
|
42
|
+
fields: dict of {name: field_type} mappings for each field in the proto. If
|
|
43
|
+
this is an OrderedDict the order will be maintained, otherwise the
|
|
44
|
+
fields will be sorted by name.
|
|
45
|
+
full_name: optional str, the fully-qualified name of the proto type.
|
|
46
|
+
pool: optional DescriptorPool instance.
|
|
47
|
+
Returns:
|
|
48
|
+
a class, the new protobuf class with a FileDescriptor.
|
|
49
|
+
"""
|
|
50
|
+
pool_instance = pool or descriptor_pool.DescriptorPool()
|
|
51
|
+
if full_name is not None:
|
|
52
|
+
try:
|
|
53
|
+
proto_cls = _GetMessageFromFactory(pool_instance, full_name)
|
|
54
|
+
return proto_cls
|
|
55
|
+
except KeyError:
|
|
56
|
+
# The factory's DescriptorPool doesn't know about this class yet.
|
|
57
|
+
pass
|
|
58
|
+
|
|
59
|
+
# Get a list of (name, field_type) tuples from the fields dict. If fields was
|
|
60
|
+
# an OrderedDict we keep the order, but otherwise we sort the field to ensure
|
|
61
|
+
# consistent ordering.
|
|
62
|
+
field_items = fields.items()
|
|
63
|
+
if not isinstance(fields, OrderedDict):
|
|
64
|
+
field_items = sorted(field_items)
|
|
65
|
+
|
|
66
|
+
# Use a consistent file name that is unlikely to conflict with any imported
|
|
67
|
+
# proto files.
|
|
68
|
+
fields_hash = hashlib.sha1()
|
|
69
|
+
for f_name, f_type in field_items:
|
|
70
|
+
fields_hash.update(f_name.encode('utf-8'))
|
|
71
|
+
fields_hash.update(str(f_type).encode('utf-8'))
|
|
72
|
+
proto_file_name = fields_hash.hexdigest() + '.proto'
|
|
73
|
+
|
|
74
|
+
# If the proto is anonymous, use the same hash to name it.
|
|
75
|
+
if full_name is None:
|
|
76
|
+
full_name = ('net.proto2.python.public.proto_builder.AnonymousProto_' +
|
|
77
|
+
fields_hash.hexdigest())
|
|
78
|
+
try:
|
|
79
|
+
proto_cls = _GetMessageFromFactory(pool_instance, full_name)
|
|
80
|
+
return proto_cls
|
|
81
|
+
except KeyError:
|
|
82
|
+
# The factory's DescriptorPool doesn't know about this class yet.
|
|
83
|
+
pass
|
|
84
|
+
|
|
85
|
+
# This is the first time we see this proto: add a new descriptor to the pool.
|
|
86
|
+
pool_instance.Add(
|
|
87
|
+
_MakeFileDescriptorProto(proto_file_name, full_name, field_items))
|
|
88
|
+
return _GetMessageFromFactory(pool_instance, full_name)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def _MakeFileDescriptorProto(proto_file_name, full_name, field_items):
|
|
92
|
+
"""Populate FileDescriptorProto for MessageFactory's DescriptorPool."""
|
|
93
|
+
package, name = full_name.rsplit('.', 1)
|
|
94
|
+
file_proto = descriptor_pb2.FileDescriptorProto()
|
|
95
|
+
file_proto.name = os.path.join(package.replace('.', '/'), proto_file_name)
|
|
96
|
+
file_proto.package = package
|
|
97
|
+
desc_proto = file_proto.message_type.add()
|
|
98
|
+
desc_proto.name = name
|
|
99
|
+
for f_number, (f_name, f_type) in enumerate(field_items, 1):
|
|
100
|
+
field_proto = desc_proto.field.add()
|
|
101
|
+
field_proto.name = f_name
|
|
102
|
+
# # If the number falls in the reserved range, reassign it to the correct
|
|
103
|
+
# # number after the range.
|
|
104
|
+
if f_number >= descriptor.FieldDescriptor.FIRST_RESERVED_FIELD_NUMBER:
|
|
105
|
+
f_number += (
|
|
106
|
+
descriptor.FieldDescriptor.LAST_RESERVED_FIELD_NUMBER -
|
|
107
|
+
descriptor.FieldDescriptor.FIRST_RESERVED_FIELD_NUMBER + 1)
|
|
108
|
+
field_proto.number = f_number
|
|
109
|
+
field_proto.label = descriptor_pb2.FieldDescriptorProto.LABEL_OPTIONAL
|
|
110
|
+
field_proto.type = f_type
|
|
111
|
+
return file_proto
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# Protocol Buffers - Google's data interchange format
|
|
2
|
+
# Copyright 2008 Google Inc. All rights reserved.
|
|
3
|
+
#
|
|
4
|
+
# Use of this source code is governed by a BSD-style
|
|
5
|
+
# license that can be found in the LICENSE file or at
|
|
6
|
+
# https://developers.google.com/open-source/licenses/bsd
|
|
7
|
+
|
|
8
|
+
"""Contains the Nextgen Pythonic Protobuf JSON APIs."""
|
|
9
|
+
|
|
10
|
+
from typing import Optional, Type
|
|
11
|
+
|
|
12
|
+
from google.protobuf.message import Message
|
|
13
|
+
from google.protobuf.descriptor_pool import DescriptorPool
|
|
14
|
+
from google.protobuf import json_format
|
|
15
|
+
|
|
16
|
+
def serialize(
|
|
17
|
+
message: Message,
|
|
18
|
+
always_print_fields_with_no_presence: bool=False,
|
|
19
|
+
preserving_proto_field_name: bool=False,
|
|
20
|
+
use_integers_for_enums: bool=False,
|
|
21
|
+
descriptor_pool: Optional[DescriptorPool]=None,
|
|
22
|
+
float_precision: int=None,
|
|
23
|
+
) -> dict:
|
|
24
|
+
"""Converts protobuf message to a dictionary.
|
|
25
|
+
|
|
26
|
+
When the dictionary is encoded to JSON, it conforms to proto3 JSON spec.
|
|
27
|
+
|
|
28
|
+
Args:
|
|
29
|
+
message: The protocol buffers message instance to serialize.
|
|
30
|
+
always_print_fields_with_no_presence: If True, fields without
|
|
31
|
+
presence (implicit presence scalars, repeated fields, and map fields) will
|
|
32
|
+
always be serialized. Any field that supports presence is not affected by
|
|
33
|
+
this option (including singular message fields and oneof fields).
|
|
34
|
+
preserving_proto_field_name: If True, use the original proto field names as
|
|
35
|
+
defined in the .proto file. If False, convert the field names to
|
|
36
|
+
lowerCamelCase.
|
|
37
|
+
use_integers_for_enums: If true, print integers instead of enum names.
|
|
38
|
+
descriptor_pool: A Descriptor Pool for resolving types. If None use the
|
|
39
|
+
default.
|
|
40
|
+
float_precision: If set, use this to specify float field valid digits.
|
|
41
|
+
|
|
42
|
+
Returns:
|
|
43
|
+
A dict representation of the protocol buffer message.
|
|
44
|
+
"""
|
|
45
|
+
return json_format.MessageToDict(
|
|
46
|
+
message,
|
|
47
|
+
always_print_fields_with_no_presence=always_print_fields_with_no_presence,
|
|
48
|
+
preserving_proto_field_name=preserving_proto_field_name,
|
|
49
|
+
use_integers_for_enums=use_integers_for_enums,
|
|
50
|
+
float_precision=float_precision,
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
def parse(
|
|
54
|
+
message_class: Type[Message],
|
|
55
|
+
js_dict: dict,
|
|
56
|
+
ignore_unknown_fields: bool=False,
|
|
57
|
+
descriptor_pool: Optional[DescriptorPool]=None,
|
|
58
|
+
max_recursion_depth: int=100
|
|
59
|
+
) -> Message:
|
|
60
|
+
"""Parses a JSON dictionary representation into a message.
|
|
61
|
+
|
|
62
|
+
Args:
|
|
63
|
+
message_class: The message meta class.
|
|
64
|
+
js_dict: Dict representation of a JSON message.
|
|
65
|
+
ignore_unknown_fields: If True, do not raise errors for unknown fields.
|
|
66
|
+
descriptor_pool: A Descriptor Pool for resolving types. If None use the
|
|
67
|
+
default.
|
|
68
|
+
max_recursion_depth: max recursion depth of JSON message to be deserialized.
|
|
69
|
+
JSON messages over this depth will fail to be deserialized. Default value
|
|
70
|
+
is 100.
|
|
71
|
+
|
|
72
|
+
Returns:
|
|
73
|
+
A new message passed from json_dict.
|
|
74
|
+
"""
|
|
75
|
+
new_message = message_class()
|
|
76
|
+
json_format.ParseDict(
|
|
77
|
+
js_dict=js_dict,
|
|
78
|
+
message=new_message,
|
|
79
|
+
ignore_unknown_fields=ignore_unknown_fields,
|
|
80
|
+
descriptor_pool=descriptor_pool,
|
|
81
|
+
max_recursion_depth=max_recursion_depth,
|
|
82
|
+
)
|
|
83
|
+
return new_message
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
# Protocol Buffers - Google's data interchange format
|
|
2
|
+
# Copyright 2025 Google Inc. All rights reserved.
|
|
3
|
+
#
|
|
4
|
+
# Use of this source code is governed by a BSD-style
|
|
5
|
+
# license that can be found in the LICENSE file or at
|
|
6
|
+
# https://developers.google.com/open-source/licenses/bsd
|
|
7
|
+
|
|
8
|
+
"""Contains the Nextgen Pythonic Protobuf Text Format APIs."""
|
|
9
|
+
from typing import AnyStr, Callable, Optional, Text, Type, Union
|
|
10
|
+
|
|
11
|
+
from google.protobuf import text_format
|
|
12
|
+
from google.protobuf.descriptor_pool import DescriptorPool
|
|
13
|
+
from google.protobuf.message import Message
|
|
14
|
+
|
|
15
|
+
_MsgFormatter = Callable[[Message, Union[int, bool], bool], Optional[Text]]
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def serialize(
|
|
19
|
+
message: Message,
|
|
20
|
+
as_utf8: bool = True,
|
|
21
|
+
as_one_line: bool = False,
|
|
22
|
+
use_short_repeated_primitives: bool = False,
|
|
23
|
+
pointy_brackets: bool = False,
|
|
24
|
+
use_index_order: bool = False,
|
|
25
|
+
float_format: Optional[str] = None,
|
|
26
|
+
double_format: Optional[str] = None,
|
|
27
|
+
use_field_number: bool = False,
|
|
28
|
+
descriptor_pool: Optional[DescriptorPool] = None,
|
|
29
|
+
indent: int = 0,
|
|
30
|
+
message_formatter: Optional[_MsgFormatter] = None,
|
|
31
|
+
print_unknown_fields: bool = False,
|
|
32
|
+
force_colon: bool = False,
|
|
33
|
+
) -> str:
|
|
34
|
+
"""Convert protobuf message to text format.
|
|
35
|
+
|
|
36
|
+
Double values can be formatted compactly with 15 digits of
|
|
37
|
+
precision (which is the most that IEEE 754 "double" can guarantee)
|
|
38
|
+
using double_format='.15g'. To ensure that converting to text and back to a
|
|
39
|
+
proto will result in an identical value, double_format='.17g' should be used.
|
|
40
|
+
|
|
41
|
+
Args:
|
|
42
|
+
message: The protocol buffers message.
|
|
43
|
+
as_utf8: Return unescaped Unicode for non-ASCII characters.
|
|
44
|
+
as_one_line: Don't introduce newlines between fields.
|
|
45
|
+
use_short_repeated_primitives: Use short repeated format for primitives.
|
|
46
|
+
pointy_brackets: If True, use angle brackets instead of curly braces for
|
|
47
|
+
nesting.
|
|
48
|
+
use_index_order: If True, fields of a proto message will be printed using
|
|
49
|
+
the order defined in source code instead of the field number, extensions
|
|
50
|
+
will be printed at the end of the message and their relative order is
|
|
51
|
+
determined by the extension number. By default, use the field number
|
|
52
|
+
order.
|
|
53
|
+
float_format (str): If set, use this to specify float field formatting (per
|
|
54
|
+
the "Format Specification Mini-Language"); otherwise, shortest float that
|
|
55
|
+
has same value in wire will be printed. Also affect double field if
|
|
56
|
+
double_format is not set but float_format is set.
|
|
57
|
+
double_format (str): If set, use this to specify double field formatting
|
|
58
|
+
(per the "Format Specification Mini-Language"); if it is not set but
|
|
59
|
+
float_format is set, use float_format. Otherwise, use ``str()``
|
|
60
|
+
use_field_number: If True, print field numbers instead of names.
|
|
61
|
+
descriptor_pool (DescriptorPool): Descriptor pool used to resolve Any types.
|
|
62
|
+
indent (int): The initial indent level, in terms of spaces, for pretty
|
|
63
|
+
print.
|
|
64
|
+
message_formatter (function(message, indent, as_one_line) -> unicode|None):
|
|
65
|
+
Custom formatter for selected sub-messages (usually based on message
|
|
66
|
+
type). Use to pretty print parts of the protobuf for easier diffing.
|
|
67
|
+
print_unknown_fields: If True, unknown fields will be printed.
|
|
68
|
+
force_colon: If set, a colon will be added after the field name even if the
|
|
69
|
+
field is a proto message.
|
|
70
|
+
|
|
71
|
+
Returns:
|
|
72
|
+
str: A string of the text formatted protocol buffer message.
|
|
73
|
+
"""
|
|
74
|
+
return text_format.MessageToString(
|
|
75
|
+
message=message,
|
|
76
|
+
as_utf8=as_utf8,
|
|
77
|
+
as_one_line=as_one_line,
|
|
78
|
+
use_short_repeated_primitives=use_short_repeated_primitives,
|
|
79
|
+
pointy_brackets=pointy_brackets,
|
|
80
|
+
use_index_order=use_index_order,
|
|
81
|
+
float_format=float_format,
|
|
82
|
+
double_format=double_format,
|
|
83
|
+
use_field_number=use_field_number,
|
|
84
|
+
descriptor_pool=descriptor_pool,
|
|
85
|
+
indent=indent,
|
|
86
|
+
message_formatter=message_formatter,
|
|
87
|
+
print_unknown_fields=print_unknown_fields,
|
|
88
|
+
force_colon=force_colon,
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def parse(
|
|
93
|
+
message_class: Type[Message],
|
|
94
|
+
text: AnyStr,
|
|
95
|
+
allow_unknown_extension: bool = False,
|
|
96
|
+
allow_field_number: bool = False,
|
|
97
|
+
descriptor_pool: Optional[DescriptorPool] = None,
|
|
98
|
+
allow_unknown_field: bool = False,
|
|
99
|
+
) -> Message:
|
|
100
|
+
"""Parses a text representation of a protocol message into a message.
|
|
101
|
+
|
|
102
|
+
Args:
|
|
103
|
+
message_class: The message meta class.
|
|
104
|
+
text (str): Message text representation.
|
|
105
|
+
message (Message): A protocol buffer message to merge into.
|
|
106
|
+
allow_unknown_extension: if True, skip over missing extensions and keep
|
|
107
|
+
parsing
|
|
108
|
+
allow_field_number: if True, both field number and field name are allowed.
|
|
109
|
+
descriptor_pool (DescriptorPool): Descriptor pool used to resolve Any types.
|
|
110
|
+
allow_unknown_field: if True, skip over unknown field and keep parsing.
|
|
111
|
+
Avoid to use this option if possible. It may hide some errors (e.g.
|
|
112
|
+
spelling error on field name)
|
|
113
|
+
|
|
114
|
+
Returns:
|
|
115
|
+
Message: A new message passed from text.
|
|
116
|
+
|
|
117
|
+
Raises:
|
|
118
|
+
ParseError: On text parsing problems.
|
|
119
|
+
"""
|
|
120
|
+
new_message = message_class()
|
|
121
|
+
text_format.Parse(
|
|
122
|
+
text=text,
|
|
123
|
+
message=new_message,
|
|
124
|
+
allow_unknown_extension=allow_unknown_extension,
|
|
125
|
+
allow_field_number=allow_field_number,
|
|
126
|
+
descriptor_pool=descriptor_pool,
|
|
127
|
+
allow_unknown_field=allow_unknown_field,
|
|
128
|
+
)
|
|
129
|
+
return new_message
|
|
File without changes
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# Protocol Buffers - Google's data interchange format
|
|
2
|
+
# Copyright 2008 Google Inc. All rights reserved.
|
|
3
|
+
#
|
|
4
|
+
# Use of this source code is governed by a BSD-style
|
|
5
|
+
# license that can be found in the LICENSE file or at
|
|
6
|
+
# https://developers.google.com/open-source/licenses/bsd
|
|
7
|
+
|
|
8
|
+
"""Protocol message implementation hooks for C++ implementation.
|
|
9
|
+
|
|
10
|
+
Contains helper functions used to create protocol message classes from
|
|
11
|
+
Descriptor objects at runtime backed by the protocol buffer C++ API.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
__author__ = 'tibell@google.com (Johan Tibell)'
|
|
15
|
+
|
|
16
|
+
from google.protobuf.internal import api_implementation
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
# pylint: disable=protected-access
|
|
20
|
+
_message = api_implementation._c_module
|
|
21
|
+
# TODO: Remove this import after fix api_implementation
|
|
22
|
+
if _message is None:
|
|
23
|
+
from google.protobuf.pyext import _message
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class GeneratedProtocolMessageType(_message.MessageMeta):
|
|
27
|
+
|
|
28
|
+
"""Metaclass for protocol message classes created at runtime from Descriptors.
|
|
29
|
+
|
|
30
|
+
The protocol compiler currently uses this metaclass to create protocol
|
|
31
|
+
message classes at runtime. Clients can also manually create their own
|
|
32
|
+
classes at runtime, as in this example:
|
|
33
|
+
|
|
34
|
+
mydescriptor = Descriptor(.....)
|
|
35
|
+
factory = symbol_database.Default()
|
|
36
|
+
factory.pool.AddDescriptor(mydescriptor)
|
|
37
|
+
MyProtoClass = message_factory.GetMessageClass(mydescriptor)
|
|
38
|
+
myproto_instance = MyProtoClass()
|
|
39
|
+
myproto.foo_field = 23
|
|
40
|
+
...
|
|
41
|
+
|
|
42
|
+
The above example will not work for nested types. If you wish to include them,
|
|
43
|
+
use reflection.MakeClass() instead of manually instantiating the class in
|
|
44
|
+
order to create the appropriate class structure.
|
|
45
|
+
"""
|
|
46
|
+
|
|
47
|
+
# Must be consistent with the protocol-compiler code in
|
|
48
|
+
# proto2/compiler/internal/generator.*.
|
|
49
|
+
_DESCRIPTOR_KEY = 'DESCRIPTOR'
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# Protocol Buffers - Google's data interchange format
|
|
2
|
+
# Copyright 2008 Google Inc. All rights reserved.
|
|
3
|
+
#
|
|
4
|
+
# Use of this source code is governed by a BSD-style
|
|
5
|
+
# license that can be found in the LICENSE file or at
|
|
6
|
+
# https://developers.google.com/open-source/licenses/bsd
|
|
7
|
+
|
|
8
|
+
# This code is meant to work on Python 2.4 and above only.
|
|
9
|
+
|
|
10
|
+
"""Contains a metaclass and helper functions used to create
|
|
11
|
+
protocol message classes from Descriptor objects at runtime.
|
|
12
|
+
|
|
13
|
+
Recall that a metaclass is the "type" of a class.
|
|
14
|
+
(A class is to a metaclass what an instance is to a class.)
|
|
15
|
+
|
|
16
|
+
In this case, we use the GeneratedProtocolMessageType metaclass
|
|
17
|
+
to inject all the useful functionality into the classes
|
|
18
|
+
output by the protocol compiler at compile-time.
|
|
19
|
+
|
|
20
|
+
The upshot of all this is that the real implementation
|
|
21
|
+
details for ALL pure-Python protocol buffers are *here in
|
|
22
|
+
this file*.
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
__author__ = 'robinson@google.com (Will Robinson)'
|
|
26
|
+
|
|
27
|
+
import warnings
|
|
28
|
+
|
|
29
|
+
from google.protobuf import message_factory
|
|
30
|
+
from google.protobuf import symbol_database
|
|
31
|
+
|
|
32
|
+
# The type of all Message classes.
|
|
33
|
+
# Part of the public interface, but normally only used by message factories.
|
|
34
|
+
GeneratedProtocolMessageType = message_factory._GENERATED_PROTOCOL_MESSAGE_TYPE
|
|
35
|
+
|
|
36
|
+
MESSAGE_CLASS_CACHE = {}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# Protocol Buffers - Google's data interchange format
|
|
2
|
+
# Copyright 2008 Google Inc. All rights reserved.
|
|
3
|
+
#
|
|
4
|
+
# Use of this source code is governed by a BSD-style
|
|
5
|
+
# license that can be found in the LICENSE file or at
|
|
6
|
+
# https://developers.google.com/open-source/licenses/bsd
|
|
7
|
+
|
|
8
|
+
"""Protobuf Runtime versions and validators.
|
|
9
|
+
|
|
10
|
+
It should only be accessed by Protobuf gencodes and tests. DO NOT USE it
|
|
11
|
+
elsewhere.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
__author__ = 'shaod@google.com (Dennis Shao)'
|
|
15
|
+
|
|
16
|
+
from enum import Enum
|
|
17
|
+
import os
|
|
18
|
+
import warnings
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class Domain(Enum):
|
|
22
|
+
GOOGLE_INTERNAL = 1
|
|
23
|
+
PUBLIC = 2
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
# The versions of this Python Protobuf runtime to be changed automatically by
|
|
27
|
+
# the Protobuf release process. Do not edit them manually.
|
|
28
|
+
# These OSS versions are not stripped to avoid merging conflicts.
|
|
29
|
+
OSS_DOMAIN = Domain.PUBLIC
|
|
30
|
+
OSS_MAJOR = 6
|
|
31
|
+
OSS_MINOR = 33
|
|
32
|
+
OSS_PATCH = 0
|
|
33
|
+
OSS_SUFFIX = ''
|
|
34
|
+
|
|
35
|
+
DOMAIN = OSS_DOMAIN
|
|
36
|
+
MAJOR = OSS_MAJOR
|
|
37
|
+
MINOR = OSS_MINOR
|
|
38
|
+
PATCH = OSS_PATCH
|
|
39
|
+
SUFFIX = OSS_SUFFIX
|
|
40
|
+
|
|
41
|
+
# Avoid flooding of warnings.
|
|
42
|
+
_MAX_WARNING_COUNT = 20
|
|
43
|
+
_warning_count = 0
|
|
44
|
+
|
|
45
|
+
class VersionError(Exception):
|
|
46
|
+
"""Exception class for version violation."""
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _ReportVersionError(msg):
|
|
50
|
+
raise VersionError(msg)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def ValidateProtobufRuntimeVersion(
|
|
54
|
+
gen_domain, gen_major, gen_minor, gen_patch, gen_suffix, location
|
|
55
|
+
):
|
|
56
|
+
"""Function to validate versions.
|
|
57
|
+
|
|
58
|
+
Args:
|
|
59
|
+
gen_domain: The domain where the code was generated from.
|
|
60
|
+
gen_major: The major version number of the gencode.
|
|
61
|
+
gen_minor: The minor version number of the gencode.
|
|
62
|
+
gen_patch: The patch version number of the gencode.
|
|
63
|
+
gen_suffix: The version suffix e.g. '-dev', '-rc1' of the gencode.
|
|
64
|
+
location: The proto location that causes the version violation.
|
|
65
|
+
|
|
66
|
+
Raises:
|
|
67
|
+
VersionError: if gencode version is invalid or incompatible with the
|
|
68
|
+
runtime.
|
|
69
|
+
"""
|
|
70
|
+
|
|
71
|
+
disable_flag = os.getenv('TEMPORARILY_DISABLE_PROTOBUF_VERSION_CHECK')
|
|
72
|
+
if disable_flag is not None and disable_flag.lower() == 'true':
|
|
73
|
+
return
|
|
74
|
+
|
|
75
|
+
global _warning_count
|
|
76
|
+
|
|
77
|
+
version = f'{MAJOR}.{MINOR}.{PATCH}{SUFFIX}'
|
|
78
|
+
gen_version = f'{gen_major}.{gen_minor}.{gen_patch}{gen_suffix}'
|
|
79
|
+
|
|
80
|
+
if gen_major < 0 or gen_minor < 0 or gen_patch < 0:
|
|
81
|
+
raise VersionError(f'Invalid gencode version: {gen_version}')
|
|
82
|
+
|
|
83
|
+
error_prompt = (
|
|
84
|
+
'See Protobuf version guarantees at'
|
|
85
|
+
' https://protobuf.dev/support/cross-version-runtime-guarantee.'
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
if gen_domain != DOMAIN:
|
|
89
|
+
_ReportVersionError(
|
|
90
|
+
'Detected mismatched Protobuf Gencode/Runtime domains when loading'
|
|
91
|
+
f' {location}: gencode {gen_domain.name} runtime {DOMAIN.name}.'
|
|
92
|
+
' Cross-domain usage of Protobuf is not supported.'
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
if (
|
|
96
|
+
MAJOR < gen_major
|
|
97
|
+
or (MAJOR == gen_major and MINOR < gen_minor)
|
|
98
|
+
or (MAJOR == gen_major and MINOR == gen_minor and PATCH < gen_patch)
|
|
99
|
+
):
|
|
100
|
+
_ReportVersionError(
|
|
101
|
+
'Detected incompatible Protobuf Gencode/Runtime versions when loading'
|
|
102
|
+
f' {location}: gencode {gen_version} runtime {version}. Runtime version'
|
|
103
|
+
f' cannot be older than the linked gencode version. {error_prompt}'
|
|
104
|
+
)
|