本文整理匯總了Python中google.protobuf.descriptor_pb2.FileDescriptorProto方法的典型用法代碼示例。如果您正苦於以下問題:Python descriptor_pb2.FileDescriptorProto方法的具體用法?Python descriptor_pb2.FileDescriptorProto怎麽用?Python descriptor_pb2.FileDescriptorProto使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類google.protobuf.descriptor_pb2
的用法示例。
在下文中一共展示了descriptor_pb2.FileDescriptorProto方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: testFileDescriptorOptionsWithCustomDescriptorPool
# 需要導入模塊: from google.protobuf import descriptor_pb2 [as 別名]
# 或者: from google.protobuf.descriptor_pb2 import FileDescriptorProto [as 別名]
def testFileDescriptorOptionsWithCustomDescriptorPool(self):
# Create a descriptor pool, and add a new FileDescriptorProto to it.
pool = descriptor_pool.DescriptorPool()
file_name = 'file_descriptor_options_with_custom_descriptor_pool.proto'
file_descriptor_proto = descriptor_pb2.FileDescriptorProto(name=file_name)
extension_id = file_options_test_pb2.foo_options
file_descriptor_proto.options.Extensions[extension_id].foo_name = 'foo'
pool.Add(file_descriptor_proto)
# The options set on the FileDescriptorProto should be available in the
# descriptor even if they contain extensions that cannot be deserialized
# using the pool.
file_descriptor = pool.FindFileByName(file_name)
options = file_descriptor.GetOptions()
self.assertEqual('foo', options.Extensions[extension_id].foo_name)
# The object returned by GetOptions() is cached.
self.assertIs(options, file_descriptor.GetOptions())
示例2: testParsingFlatClassWithExplicitClassDeclaration
# 需要導入模塊: from google.protobuf import descriptor_pb2 [as 別名]
# 或者: from google.protobuf.descriptor_pb2 import FileDescriptorProto [as 別名]
def testParsingFlatClassWithExplicitClassDeclaration(self):
"""Test that the generated class can parse a flat message."""
# TODO(xiaofeng): This test fails with cpp implemetnation in the call
# of six.with_metaclass(). The other two callsites of with_metaclass
# in this file are both excluded from cpp test, so it might be expected
# to fail. Need someone more familiar with the python code to take a
# look at this.
if api_implementation.Type() != 'python':
return
file_descriptor = descriptor_pb2.FileDescriptorProto()
file_descriptor.ParseFromString(self._GetSerializedFileDescriptor('A'))
msg_descriptor = descriptor.MakeDescriptor(
file_descriptor.message_type[0])
class MessageClass(six.with_metaclass(reflection.GeneratedProtocolMessageType, message.Message)):
DESCRIPTOR = msg_descriptor
msg = MessageClass()
msg_str = (
'flat: 0 '
'flat: 1 '
'flat: 2 ')
text_format.Merge(msg_str, msg)
self.assertEqual(msg.flat, [0, 1, 2])
示例3: testParsingNestedClass
# 需要導入模塊: from google.protobuf import descriptor_pb2 [as 別名]
# 或者: from google.protobuf.descriptor_pb2 import FileDescriptorProto [as 別名]
def testParsingNestedClass(self):
"""Test that the generated class can parse a nested message."""
file_descriptor = descriptor_pb2.FileDescriptorProto()
file_descriptor.ParseFromString(self._GetSerializedFileDescriptor('C'))
msg_descriptor = descriptor.MakeDescriptor(
file_descriptor.message_type[0])
msg_class = reflection.MakeClass(msg_descriptor)
msg = msg_class()
msg_str = (
'bar {'
' baz {'
' deep: 4'
' }'
'}')
text_format.Merge(msg_str, msg)
self.assertEqual(msg.bar.baz.deep, 4)
示例4: testMakeDescriptorWithUnsignedIntField
# 需要導入模塊: from google.protobuf import descriptor_pb2 [as 別名]
# 或者: from google.protobuf.descriptor_pb2 import FileDescriptorProto [as 別名]
def testMakeDescriptorWithUnsignedIntField(self):
file_descriptor_proto = descriptor_pb2.FileDescriptorProto()
file_descriptor_proto.name = 'Foo'
message_type = file_descriptor_proto.message_type.add()
message_type.name = file_descriptor_proto.name
enum_type = message_type.enum_type.add()
enum_type.name = 'FOO'
enum_type_val = enum_type.value.add()
enum_type_val.name = 'BAR'
enum_type_val.number = 3
field = message_type.field.add()
field.number = 1
field.name = 'uint64_field'
field.label = descriptor.FieldDescriptor.LABEL_REQUIRED
field.type = descriptor.FieldDescriptor.TYPE_UINT64
enum_field = message_type.field.add()
enum_field.number = 2
enum_field.name = 'bar_field'
enum_field.label = descriptor.FieldDescriptor.LABEL_REQUIRED
enum_field.type = descriptor.FieldDescriptor.TYPE_ENUM
enum_field.type_name = 'Foo.FOO'
result = descriptor.MakeDescriptor(message_type)
self.assertEqual(result.fields[0].cpp_type,
descriptor.FieldDescriptor.CPPTYPE_UINT64)
示例5: testEnumDefaultValue
# 需要導入模塊: from google.protobuf import descriptor_pb2 [as 別名]
# 或者: from google.protobuf.descriptor_pb2 import FileDescriptorProto [as 別名]
def testEnumDefaultValue(self):
"""Test the default value of enums which don't start at zero."""
def _CheckDefaultValue(file_descriptor):
default_value = (file_descriptor
.message_types_by_name['DescriptorPoolTest1']
.fields_by_name['nested_enum']
.default_value)
self.assertEqual(default_value,
descriptor_pool_test1_pb2.DescriptorPoolTest1.BETA)
# First check what the generated descriptor contains.
_CheckDefaultValue(descriptor_pool_test1_pb2.DESCRIPTOR)
# Then check the generated pool. Normally this is the same descriptor.
file_descriptor = symbol_database.Default().pool.FindFileByName(
'google/protobuf/internal/descriptor_pool_test1.proto')
self.assertIs(file_descriptor, descriptor_pool_test1_pb2.DESCRIPTOR)
_CheckDefaultValue(file_descriptor)
# Then check the dynamic pool and its internal DescriptorDatabase.
descriptor_proto = descriptor_pb2.FileDescriptorProto.FromString(
descriptor_pool_test1_pb2.DESCRIPTOR.serialized_pb)
self.pool.Add(descriptor_proto)
# And do the same check as above
file_descriptor = self.pool.FindFileByName(
'google/protobuf/internal/descriptor_pool_test1.proto')
_CheckDefaultValue(file_descriptor)
示例6: _type_names
# 需要導入模塊: from google.protobuf import descriptor_pb2 [as 別名]
# 或者: from google.protobuf.descriptor_pb2 import FileDescriptorProto [as 別名]
def _type_names(
proto_file: FileDescriptorProto,
message_type: DescriptorProto,
parents: Optional[Deque[str]] = None,
) -> Iterator[Tuple[str, str]]:
if parents is None:
parents = deque()
proto_name_parts = ['']
if proto_file.package:
proto_name_parts.append(proto_file.package)
proto_name_parts.extend(parents)
proto_name_parts.append(message_type.name)
py_name_parts = [_proto2pb2_module_name(proto_file.name)]
py_name_parts.extend(parents)
py_name_parts.append(message_type.name)
yield '.'.join(proto_name_parts), '.'.join(py_name_parts)
parents.append(message_type.name)
for nested in message_type.nested_type:
yield from _type_names(proto_file, nested, parents=parents)
parents.pop()
示例7: test_file_by_filename_response
# 需要導入模塊: from google.protobuf import descriptor_pb2 [as 別名]
# 或者: from google.protobuf.descriptor_pb2 import FileDescriptorProto [as 別名]
def test_file_by_filename_response(channel):
r1, r2 = await ServerReflectionStub(channel).ServerReflectionInfo([
ServerReflectionRequest(
file_by_filename=DESCRIPTOR.name,
),
ServerReflectionRequest(
file_by_filename='my/missing.proto',
),
])
proto_bytes, = r1.file_descriptor_response.file_descriptor_proto
dummy_proto = FileDescriptorProto()
dummy_proto.ParseFromString(proto_bytes)
assert dummy_proto.name == DESCRIPTOR.name
assert dummy_proto.package == DESCRIPTOR.package
assert r2 == ServerReflectionResponse(
error_response=ErrorResponse(
error_code=5,
error_message='not found',
),
)
示例8: testParsingFlatClassWithExplicitClassDeclaration
# 需要導入模塊: from google.protobuf import descriptor_pb2 [as 別名]
# 或者: from google.protobuf.descriptor_pb2 import FileDescriptorProto [as 別名]
def testParsingFlatClassWithExplicitClassDeclaration(self):
"""Test that the generated class can parse a flat message."""
file_descriptor = descriptor_pb2.FileDescriptorProto()
file_descriptor.ParseFromString(self._GetSerializedFileDescriptor('A'))
msg_descriptor = descriptor.MakeDescriptor(
file_descriptor.message_type[0])
class MessageClass(message.Message, metaclass=reflection.GeneratedProtocolMessageType):
DESCRIPTOR = msg_descriptor
msg = MessageClass()
msg_str = (
'flat: 0 '
'flat: 1 '
'flat: 2 ')
text_format.Merge(msg_str, msg)
self.assertEqual(msg.flat, [0, 1, 2])
示例9: setUp
# 需要導入模塊: from google.protobuf import descriptor_pb2 [as 別名]
# 或者: from google.protobuf.descriptor_pb2 import FileDescriptorProto [as 別名]
def setUp(self):
# TODO(jieluo): Should make the pool which is created by
# serialized_pb same with generated pool.
# TODO(jieluo): More test coverage for the generated pool.
self.pool = descriptor_pool.DescriptorPool()
self.factory_test1_fd = descriptor_pb2.FileDescriptorProto.FromString(
factory_test1_pb2.DESCRIPTOR.serialized_pb)
self.factory_test2_fd = descriptor_pb2.FileDescriptorProto.FromString(
factory_test2_pb2.DESCRIPTOR.serialized_pb)
self.pool.Add(self.factory_test1_fd)
self.pool.Add(self.factory_test2_fd)
self.pool.Add(descriptor_pb2.FileDescriptorProto.FromString(
unittest_import_public_pb2.DESCRIPTOR.serialized_pb))
self.pool.Add(descriptor_pb2.FileDescriptorProto.FromString(
unittest_import_pb2.DESCRIPTOR.serialized_pb))
self.pool.Add(descriptor_pb2.FileDescriptorProto.FromString(
unittest_pb2.DESCRIPTOR.serialized_pb))
示例10: _MakeFileDescriptorProto
# 需要導入模塊: from google.protobuf import descriptor_pb2 [as 別名]
# 或者: from google.protobuf.descriptor_pb2 import FileDescriptorProto [as 別名]
def _MakeFileDescriptorProto(proto_file_name, full_name, field_items):
"""Populate FileDescriptorProto for MessageFactory's DescriptorPool."""
package, name = full_name.rsplit('.', 1)
file_proto = descriptor_pb2.FileDescriptorProto()
file_proto.name = os.path.join(package.replace('.', '/'), proto_file_name)
file_proto.package = package
desc_proto = file_proto.message_type.add()
desc_proto.name = name
for f_number, (f_name, f_type) in enumerate(field_items, 1):
field_proto = desc_proto.field.add()
field_proto.name = f_name
field_proto.number = f_number
field_proto.label = descriptor_pb2.FieldDescriptorProto.LABEL_OPTIONAL
field_proto.type = f_type
return file_proto
示例11: CopyToProto
# 需要導入模塊: from google.protobuf import descriptor_pb2 [as 別名]
# 或者: from google.protobuf.descriptor_pb2 import FileDescriptorProto [as 別名]
def CopyToProto(self, proto):
"""Copies this to a descriptor_pb2.FileDescriptorProto.
Args:
proto: An empty descriptor_pb2.FileDescriptorProto.
"""
proto.ParseFromString(self.serialized_pb)
示例12: testEnumDefaultValue
# 需要導入模塊: from google.protobuf import descriptor_pb2 [as 別名]
# 或者: from google.protobuf.descriptor_pb2 import FileDescriptorProto [as 別名]
def testEnumDefaultValue(self):
"""Test the default value of enums which don't start at zero."""
def _CheckDefaultValue(file_descriptor):
default_value = (file_descriptor
.message_types_by_name['DescriptorPoolTest1']
.fields_by_name['nested_enum']
.default_value)
self.assertEqual(default_value,
descriptor_pool_test1_pb2.DescriptorPoolTest1.BETA)
# First check what the generated descriptor contains.
_CheckDefaultValue(descriptor_pool_test1_pb2.DESCRIPTOR)
# Then check the generated pool. Normally this is the same descriptor.
file_descriptor = symbol_database.Default().pool.FindFileByName(
'google/protobuf/internal/descriptor_pool_test1.proto')
self.assertIs(file_descriptor, descriptor_pool_test1_pb2.DESCRIPTOR)
_CheckDefaultValue(file_descriptor)
if isinstance(self, SecondaryDescriptorFromDescriptorDB):
if api_implementation.Type() == 'cpp':
# Cpp extension cannot call Add on a DescriptorPool
# that uses a DescriptorDatabase.
# TODO(jieluo): Fix python and cpp extension diff.
return
# Then check the dynamic pool and its internal DescriptorDatabase.
descriptor_proto = descriptor_pb2.FileDescriptorProto.FromString(
descriptor_pool_test1_pb2.DESCRIPTOR.serialized_pb)
self.pool.Add(descriptor_proto)
# And do the same check as above
file_descriptor = self.pool.FindFileByName(
'google/protobuf/internal/descriptor_pool_test1.proto')
_CheckDefaultValue(file_descriptor)
示例13: testDefaultValueForCustomMessages
# 需要導入模塊: from google.protobuf import descriptor_pb2 [as 別名]
# 或者: from google.protobuf.descriptor_pb2 import FileDescriptorProto [as 別名]
def testDefaultValueForCustomMessages(self):
"""Check the value returned by non-existent fields."""
def _CheckValueAndType(value, expected_value, expected_type):
self.assertEqual(value, expected_value)
self.assertIsInstance(value, expected_type)
def _CheckDefaultValues(msg):
try:
int64 = int
except NameError: # Python3
int64 = int
try:
unicode_type = str
except NameError: # Python3
unicode_type = str
_CheckValueAndType(msg.optional_int32, 0, int)
_CheckValueAndType(msg.optional_uint64, 0, (int64, int))
_CheckValueAndType(msg.optional_float, 0, (float, int))
_CheckValueAndType(msg.optional_double, 0, (float, int))
_CheckValueAndType(msg.optional_bool, False, bool)
_CheckValueAndType(msg.optional_string, '', unicode_type)
_CheckValueAndType(msg.optional_bytes, b'', bytes)
_CheckValueAndType(msg.optional_nested_enum, msg.FOO, int)
# First for the generated message
_CheckDefaultValues(unittest_pb2.TestAllTypes())
# Then for a message built with from the DescriptorPool.
pool = descriptor_pool.DescriptorPool()
pool.Add(descriptor_pb2.FileDescriptorProto.FromString(
unittest_import_public_pb2.DESCRIPTOR.serialized_pb))
pool.Add(descriptor_pb2.FileDescriptorProto.FromString(
unittest_import_pb2.DESCRIPTOR.serialized_pb))
pool.Add(descriptor_pb2.FileDescriptorProto.FromString(
unittest_pb2.DESCRIPTOR.serialized_pb))
message_class = message_factory.MessageFactory(pool).GetPrototype(
pool.FindMessageTypeByName(
unittest_pb2.TestAllTypes.DESCRIPTOR.full_name))
_CheckDefaultValues(message_class())
示例14: testAddFileDescriptor
# 需要導入模塊: from google.protobuf import descriptor_pb2 [as 別名]
# 或者: from google.protobuf.descriptor_pb2 import FileDescriptorProto [as 別名]
def testAddFileDescriptor(self):
if isinstance(self, SecondaryDescriptorFromDescriptorDB):
if api_implementation.Type() == 'cpp':
# Cpp extension cannot call Add on a DescriptorPool
# that uses a DescriptorDatabase.
# TODO(jieluo): Fix python and cpp extension diff.
return
file_desc = descriptor_pb2.FileDescriptorProto(name='some/file.proto')
self.pool.Add(file_desc)
self.pool.AddSerializedFile(file_desc.SerializeToString())
示例15: setUp
# 需要導入模塊: from google.protobuf import descriptor_pb2 [as 別名]
# 或者: from google.protobuf.descriptor_pb2 import FileDescriptorProto [as 別名]
def setUp(self):
self.pool = descriptor_pool.Default()
self.factory_test1_fd = descriptor_pb2.FileDescriptorProto.FromString(
factory_test1_pb2.DESCRIPTOR.serialized_pb)
self.factory_test2_fd = descriptor_pb2.FileDescriptorProto.FromString(
factory_test2_pb2.DESCRIPTOR.serialized_pb)