本文整理汇总了Python中google.protobuf.descriptor_pb2.FieldDescriptorProto方法的典型用法代码示例。如果您正苦于以下问题:Python descriptor_pb2.FieldDescriptorProto方法的具体用法?Python descriptor_pb2.FieldDescriptorProto怎么用?Python descriptor_pb2.FieldDescriptorProto使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类google.protobuf.descriptor_pb2
的用法示例。
在下文中一共展示了descriptor_pb2.FieldDescriptorProto方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_service_python_modules_signature
# 需要导入模块: from google.protobuf import descriptor_pb2 [as 别名]
# 或者: from google.protobuf.descriptor_pb2 import FieldDescriptorProto [as 别名]
def test_service_python_modules_signature():
service = make_service_with_method_options(
in_fields=(
# type=5 is int, so nothing is added.
descriptor_pb2.FieldDescriptorProto(name='secs', type=5),
descriptor_pb2.FieldDescriptorProto(
name='d',
type=14, # enum
type_name='a.b.c.v2.D',
),
),
method_signature='secs,d',
)
# Ensure that the service will have the expected imports.
method = service.methods['DoBigThing']
imports = {i.ident.python_import for i in method.ref_types}
assert imports == {
imp.Import(package=('a', 'b', 'c'), module='v2'),
imp.Import(package=('foo',), module='bar'),
imp.Import(package=('foo',), module='baz'),
imp.Import(package=('foo',), module='qux'),
imp.Import(package=('google', 'api_core'), module='operation'),
imp.Import(package=('google', 'api_core'), module='operation_async'),
}
示例2: convert_protodef_to_editable
# 需要导入模块: from google.protobuf import descriptor_pb2 [as 别名]
# 或者: from google.protobuf.descriptor_pb2 import FieldDescriptorProto [as 别名]
def convert_protodef_to_editable(proto):
"""
Protobuf objects can't have arbitrary fields addedd and we need to later on
add comments to them, so we instead make "Editable" objects that can do so
"""
class Editable(object):
def __init__(self, prot):
self.kind = type(prot)
self.name = prot.name
self.comment = ""
self.options = dict([(key.name, value) for (key, value) in prot.options.ListFields()])
if isinstance(prot, EnumDescriptorProto):
self.value = [convert_protodef_to_editable(x) for x in prot.value]
elif isinstance(prot, DescriptorProto):
self.field = [convert_protodef_to_editable(x) for x in prot.field]
self.enum_type = [convert_protodef_to_editable(x) for x in prot.enum_type]
self.nested_type = prot.nested_type
self.oneof_decl = prot.oneof_decl
elif isinstance(prot, EnumValueDescriptorProto):
self.number = prot.number
elif isinstance(prot, FieldDescriptorProto):
if prot.type in [11, 14]:
self.ref_type = prot.type_name[1:]
self.type = prot.type
self.label = prot.label
elif isinstance(prot, ServiceDescriptorProto):
self.method = [convert_protodef_to_editable(x) for x in prot.method]
elif isinstance(prot, MethodDescriptorProto):
self.input_type = prot.input_type
self.output_type = prot.output_type
else:
raise Exception, type(prot)
return Editable(proto)
示例3: write_extensions
# 需要导入模块: from google.protobuf import descriptor_pb2 [as 别名]
# 或者: from google.protobuf.descriptor_pb2 import FieldDescriptorProto [as 别名]
def write_extensions(self, extensions):
# type: (Sequence[d.FieldDescriptorProto]) -> None
if not extensions:
return
l = self._write_line
field_descriptor_class = self._import(
"google.protobuf.descriptor", "FieldDescriptor"
)
for extension in extensions:
l("{}: {} = ...", extension.name, field_descriptor_class)
l("")
示例4: python_type
# 需要导入模块: from google.protobuf import descriptor_pb2 [as 别名]
# 或者: from google.protobuf.descriptor_pb2 import FieldDescriptorProto [as 别名]
def python_type(self, field):
# type: (d.FieldDescriptorProto) -> Text
b_float = self._builtin("float")
b_int = self._builtin("int")
b_bool = self._builtin("bool")
b_bytes = self._builtin("bytes")
mapping = {
d.FieldDescriptorProto.TYPE_DOUBLE: lambda: b_float,
d.FieldDescriptorProto.TYPE_FLOAT: lambda: b_float,
d.FieldDescriptorProto.TYPE_INT64: lambda: b_int,
d.FieldDescriptorProto.TYPE_UINT64: lambda: b_int,
d.FieldDescriptorProto.TYPE_FIXED64: lambda: b_int,
d.FieldDescriptorProto.TYPE_SFIXED64: lambda: b_int,
d.FieldDescriptorProto.TYPE_SINT64: lambda: b_int,
d.FieldDescriptorProto.TYPE_INT32: lambda: b_int,
d.FieldDescriptorProto.TYPE_UINT32: lambda: b_int,
d.FieldDescriptorProto.TYPE_FIXED32: lambda: b_int,
d.FieldDescriptorProto.TYPE_SFIXED32: lambda: b_int,
d.FieldDescriptorProto.TYPE_SINT32: lambda: b_int,
d.FieldDescriptorProto.TYPE_BOOL: lambda: b_bool,
d.FieldDescriptorProto.TYPE_STRING: lambda: self._import("typing", "Text"),
d.FieldDescriptorProto.TYPE_BYTES: lambda: b_bytes,
d.FieldDescriptorProto.TYPE_ENUM: lambda: self._import_message(
field.type_name + "Value"
),
d.FieldDescriptorProto.TYPE_MESSAGE: lambda: self._import_message(
field.type_name
),
d.FieldDescriptorProto.TYPE_GROUP: lambda: self._import_message(
field.type_name
),
} # type: Dict[d.FieldDescriptorProto.Type, Callable[[], Text]]
assert field.type in mapping, "Unrecognized type: " + repr(field.type)
return mapping[field.type]()
示例5: is_scalar
# 需要导入模块: from google.protobuf import descriptor_pb2 [as 别名]
# 或者: from google.protobuf.descriptor_pb2 import FieldDescriptorProto [as 别名]
def is_scalar(fd):
# type: (d.FieldDescriptorProto) -> bool
return not (
fd.type == d.FieldDescriptorProto.TYPE_MESSAGE
or fd.type == d.FieldDescriptorProto.TYPE_GROUP
)
示例6: names_from_type_name
# 需要导入模块: from google.protobuf import descriptor_pb2 [as 别名]
# 或者: from google.protobuf.descriptor_pb2 import FieldDescriptorProto [as 别名]
def names_from_type_name(type_name):
'''Parse Names() from FieldDescriptorProto type_name'''
if type_name[0] != '.':
raise NotImplementedError("Lookup of non-absolute type names is not supported")
return Names(type_name[1:].split('.'))
示例7: iterate_extensions
# 需要导入模块: from google.protobuf import descriptor_pb2 [as 别名]
# 或者: from google.protobuf.descriptor_pb2 import FieldDescriptorProto [as 别名]
def iterate_extensions(desc, flatten = False, names = Names()):
'''Recursively find all extensions.
For each, yield name, FieldDescriptorProto.
'''
for extension in desc.extension:
yield names, extension
for subname, subdesc in iterate_messages(desc, flatten, names):
for extension in subdesc.extension:
yield subname, extension
示例8: make_service_with_method_options
# 需要导入模块: from google.protobuf import descriptor_pb2 [as 别名]
# 或者: from google.protobuf.descriptor_pb2 import FieldDescriptorProto [as 别名]
def make_service_with_method_options(
*,
http_rule: http_pb2.HttpRule = None,
method_signature: str = '',
in_fields: typing.Tuple[desc.FieldDescriptorProto] = ()
) -> wrappers.Service:
# Declare a method with options enabled for long-running operations and
# field headers.
method = get_method(
'DoBigThing',
'foo.bar.ThingRequest',
'google.longrunning.operations_pb2.Operation',
lro_response_type='foo.baz.ThingResponse',
lro_metadata_type='foo.qux.ThingMetadata',
in_fields=in_fields,
http_rule=http_rule,
method_signature=method_signature,
)
# Define a service descriptor.
service_pb = desc.ServiceDescriptorProto(name='ThingDoer')
# Return a service object to test.
return wrappers.Service(
service_pb=service_pb,
methods={method.name: method},
)
示例9: get_method
# 需要导入模块: from google.protobuf import descriptor_pb2 [as 别名]
# 或者: from google.protobuf.descriptor_pb2 import FieldDescriptorProto [as 别名]
def get_method(name: str,
in_type: str,
out_type: str,
lro_response_type: str = '',
lro_metadata_type: str = '', *,
in_fields: typing.Tuple[desc.FieldDescriptorProto] = (),
http_rule: http_pb2.HttpRule = None,
method_signature: str = '',
) -> wrappers.Method:
input_ = get_message(in_type, fields=in_fields)
output = get_message(out_type)
lro = None
# Define a method descriptor. Set the field headers if appropriate.
method_pb = desc.MethodDescriptorProto(
name=name,
input_type=input_.ident.proto,
output_type=output.ident.proto,
)
if lro_response_type:
lro = wrappers.OperationInfo(
response_type=get_message(lro_response_type),
metadata_type=get_message(lro_metadata_type),
)
if http_rule:
ext_key = annotations_pb2.http
method_pb.options.Extensions[ext_key].MergeFrom(http_rule)
if method_signature:
ext_key = client_pb2.method_signature
method_pb.options.Extensions[ext_key].append(method_signature)
return wrappers.Method(
method_pb=method_pb,
input=input_,
output=output,
lro=lro,
meta=input_.meta,
)
示例10: get_message
# 需要导入模块: from google.protobuf import descriptor_pb2 [as 别名]
# 或者: from google.protobuf.descriptor_pb2 import FieldDescriptorProto [as 别名]
def get_message(dot_path: str, *,
fields: typing.Tuple[desc.FieldDescriptorProto] = (),
) -> wrappers.MessageType:
# Pass explicit None through (for lro_metadata).
if dot_path is None:
return None
# Note: The `dot_path` here is distinct from the canonical proto path
# because it includes the module, which the proto path does not.
#
# So, if trying to test the DescriptorProto message here, the path
# would be google.protobuf.descriptor.DescriptorProto (whereas the proto
# path is just google.protobuf.DescriptorProto).
pieces = dot_path.split('.')
pkg, module, name = pieces[:-2], pieces[-2], pieces[-1]
return wrappers.MessageType(
fields={i.name: wrappers.Field(
field_pb=i,
enum=get_enum(i.type_name) if i.type_name else None,
) for i in fields},
nested_messages={},
nested_enums={},
message_pb=desc.DescriptorProto(name=name, field=fields),
meta=metadata.Metadata(address=metadata.Address(
name=name,
package=tuple(pkg),
module=module,
)),
)
示例11: make_field
# 需要导入模块: from google.protobuf import descriptor_pb2 [as 别名]
# 或者: from google.protobuf.descriptor_pb2 import FieldDescriptorProto [as 别名]
def make_field(
name: str = 'my_field',
number: int = 1,
repeated: bool = False,
message: wrappers.MessageType = None,
enum: wrappers.EnumType = None,
meta: metadata.Metadata = None,
**kwargs
) -> wrappers.Field:
T = desc.FieldDescriptorProto.Type
if message:
kwargs.setdefault('type_name', str(message.meta.address))
kwargs['type'] = 'TYPE_MESSAGE'
elif enum:
kwargs.setdefault('type_name', str(enum.meta.address))
kwargs['type'] = 'TYPE_ENUM'
else:
kwargs.setdefault('type', T.Value('TYPE_BOOL'))
if isinstance(kwargs['type'], str):
kwargs['type'] = T.Value(kwargs['type'])
label = kwargs.pop('label', 3 if repeated else 1)
field_pb = desc.FieldDescriptorProto(
name=name,
label=label,
number=number,
**kwargs
)
return wrappers.Field(
field_pb=field_pb,
enum=enum,
message=message,
meta=meta or metadata.Metadata(),
)
示例12: make_field_pb2
# 需要导入模块: from google.protobuf import descriptor_pb2 [as 别名]
# 或者: from google.protobuf.descriptor_pb2 import FieldDescriptorProto [as 别名]
def make_field_pb2(name: str, number: int,
type: int = 11, # 11 == message
type_name: str = None,
) -> desc.FieldDescriptorProto:
return desc.FieldDescriptorProto(
name=name,
number=number,
type=type,
type_name=type_name,
)
示例13: make_field
# 需要导入模块: from google.protobuf import descriptor_pb2 [as 别名]
# 或者: from google.protobuf.descriptor_pb2 import FieldDescriptorProto [as 别名]
def make_field(*, message=None, enum=None, **kwargs) -> wrappers.Field:
T = descriptor_pb2.FieldDescriptorProto.Type
kwargs.setdefault('name', 'my_field')
kwargs.setdefault('number', 1)
kwargs.setdefault('type', T.Value('TYPE_BOOL'))
if isinstance(kwargs['type'], str):
kwargs['type'] = T.Value(kwargs['type'])
field_pb = descriptor_pb2.FieldDescriptorProto(**kwargs)
return wrappers.Field(field_pb=field_pb, message=message, enum=enum)
示例14: testDescriptorProtoSupport
# 需要导入模块: from google.protobuf import descriptor_pb2 [as 别名]
# 或者: from google.protobuf.descriptor_pb2 import FieldDescriptorProto [as 别名]
def testDescriptorProtoSupport(self):
# Hand written descriptors/reflection are only supported by the pure-Python
# implementation of the API.
if api_implementation.Type() != 'python':
return
def AddDescriptorField(proto, field_name, field_type):
AddDescriptorField.field_index += 1
new_field = proto.field.add()
new_field.name = field_name
new_field.type = field_type
new_field.number = AddDescriptorField.field_index
new_field.label = descriptor_pb2.FieldDescriptorProto.LABEL_OPTIONAL
AddDescriptorField.field_index = 0
desc_proto = descriptor_pb2.DescriptorProto()
desc_proto.name = 'Car'
fdp = descriptor_pb2.FieldDescriptorProto
AddDescriptorField(desc_proto, 'name', fdp.TYPE_STRING)
AddDescriptorField(desc_proto, 'year', fdp.TYPE_INT64)
AddDescriptorField(desc_proto, 'automatic', fdp.TYPE_BOOL)
AddDescriptorField(desc_proto, 'price', fdp.TYPE_DOUBLE)
# Add a repeated field
AddDescriptorField.field_index += 1
new_field = desc_proto.field.add()
new_field.name = 'owners'
new_field.type = fdp.TYPE_STRING
new_field.number = AddDescriptorField.field_index
new_field.label = descriptor_pb2.FieldDescriptorProto.LABEL_REPEATED
desc = descriptor.MakeDescriptor(desc_proto)
self.assertTrue('name' in desc.fields_by_name)
self.assertTrue('year' in desc.fields_by_name)
self.assertTrue('automatic' in desc.fields_by_name)
self.assertTrue('price' in desc.fields_by_name)
self.assertTrue('owners' in desc.fields_by_name)
class CarMessage(six.with_metaclass(reflection.GeneratedProtocolMessageType, message.Message)):
DESCRIPTOR = desc
prius = CarMessage()
prius.name = 'prius'
prius.year = 2010
prius.automatic = True
prius.price = 25134.75
prius.owners.extend(['bob', 'susan'])
serialized_prius = prius.SerializeToString()
new_prius = reflection.ParseMessage(desc, serialized_prius)
self.assertTrue(new_prius is not prius)
self.assertEqual(prius, new_prius)
# these are unnecessary assuming message equality works as advertised but
# explicitly check to be safe since we're mucking about in metaclass foo
self.assertEqual(prius.name, new_prius.name)
self.assertEqual(prius.year, new_prius.year)
self.assertEqual(prius.automatic, new_prius.automatic)
self.assertEqual(prius.price, new_prius.price)
self.assertEqual(prius.owners, new_prius.owners)
示例15: type_to_string
# 需要导入模块: from google.protobuf import descriptor_pb2 [as 别名]
# 或者: from google.protobuf.descriptor_pb2 import FieldDescriptorProto [as 别名]
def type_to_string(f, map_types):
"""
Convert type info to pretty names, based on numbers from from FieldDescriptorProto
https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.descriptor.pb
"""
if f.type in [1]:
return "double"
elif f.type in [2]:
return "float"
elif f.type in [3]:
return "long"
elif f.type in [4]:
return "uint64"
elif f.type in [5]:
return "integer"
elif f.type in [6]:
return "fixed64"
elif f.type in [7]:
return "fixed32"
elif f.type in [8]:
return "boolean"
elif f.type in [9]:
return "string"
# missing type 10 - Group
elif f.type in [11, 14]:
ref_name = f.ref_type
if ref_name in map_types:
ref_fields = map_types[ref_name]
return {
"type": "map",
"key": " %s "% type_to_string(ref_fields["key"], map_types),
"value": " %s "% type_to_string(ref_fields["value"], map_types)
}
else:
kind = ":protobuf:message:`%s`" % simplify_name(f.ref_type)
if f.label == 3: # LABEL_REPEATED
return "list of " + kind
else:
return kind
elif f.type in [12]:
return "bytes"
elif f.type in [13]:
return "uint32"
elif f.type in [15]:
return "sfixed32"
elif f.type in [16]:
return "sfixed64"
elif f.type in [17]:
return "sint32"
elif f.type in [18]:
return "sint64"
else:
raise Exception, f.type