本文整理汇总了Python中google.protobuf.descriptor_pb2.EnumDescriptorProto方法的典型用法代码示例。如果您正苦于以下问题:Python descriptor_pb2.EnumDescriptorProto方法的具体用法?Python descriptor_pb2.EnumDescriptorProto怎么用?Python descriptor_pb2.EnumDescriptorProto使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类google.protobuf.descriptor_pb2
的用法示例。
在下文中一共展示了descriptor_pb2.EnumDescriptorProto方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testCopyToProto_ForeignEnum
# 需要导入模块: from google.protobuf import descriptor_pb2 [as 别名]
# 或者: from google.protobuf.descriptor_pb2 import EnumDescriptorProto [as 别名]
def testCopyToProto_ForeignEnum(self):
TEST_FOREIGN_ENUM_ASCII = """
name: 'ForeignEnum'
value: <
name: 'FOREIGN_FOO'
number: 4
>
value: <
name: 'FOREIGN_BAR'
number: 5
>
value: <
name: 'FOREIGN_BAZ'
number: 6
>
"""
self._InternalTestCopyToProto(
unittest_pb2.ForeignEnum.DESCRIPTOR,
descriptor_pb2.EnumDescriptorProto,
TEST_FOREIGN_ENUM_ASCII)
示例2: testCopyToProto_ForeignEnum
# 需要导入模块: from google.protobuf import descriptor_pb2 [as 别名]
# 或者: from google.protobuf.descriptor_pb2 import EnumDescriptorProto [as 别名]
def testCopyToProto_ForeignEnum(self):
TEST_FOREIGN_ENUM_ASCII = """
name: 'ForeignEnum'
value: <
name: 'FOREIGN_FOO'
number: 4
>
value: <
name: 'FOREIGN_BAR'
number: 5
>
value: <
name: 'FOREIGN_BAZ'
number: 6
>
"""
self._InternalTestCopyToProto(
unittest_pb2._FOREIGNENUM,
descriptor_pb2.EnumDescriptorProto,
TEST_FOREIGN_ENUM_ASCII)
示例3: make_enum
# 需要导入模块: from google.protobuf import descriptor_pb2 [as 别名]
# 或者: from google.protobuf.descriptor_pb2 import EnumDescriptorProto [as 别名]
def make_enum(
name: str,
package: str = 'foo.bar.v1',
module: str = 'baz',
values: typing.Tuple[str, int] = (),
meta: metadata.Metadata = None,
) -> wrappers.EnumType:
enum_value_pbs = [
desc.EnumValueDescriptorProto(name=i[0], number=i[1])
for i in values
]
enum_pb = desc.EnumDescriptorProto(
name=name,
value=enum_value_pbs,
)
return wrappers.EnumType(
enum_pb=enum_pb,
values=[wrappers.EnumValueType(enum_value_pb=evpb)
for evpb in enum_value_pbs],
meta=meta or metadata.Metadata(address=metadata.Address(
name=name,
package=tuple(package.split('.')),
module=module,
)),
)
示例4: test_enums
# 需要导入模块: from google.protobuf import descriptor_pb2 [as 别名]
# 或者: from google.protobuf.descriptor_pb2 import EnumDescriptorProto [as 别名]
def test_enums():
L = descriptor_pb2.SourceCodeInfo.Location
enum_pb = descriptor_pb2.EnumDescriptorProto(name='Silly', value=(
descriptor_pb2.EnumValueDescriptorProto(name='ZERO', number=0),
descriptor_pb2.EnumValueDescriptorProto(name='ONE', number=1),
descriptor_pb2.EnumValueDescriptorProto(name='THREE', number=3),
))
fdp = make_file_pb2(package='google.enum.v1', enums=(enum_pb,), locations=(
L(path=(5, 0), leading_comments='This is the Silly enum.'),
L(path=(5, 0, 2, 0), leading_comments='This is the zero value.'),
L(path=(5, 0, 2, 1), leading_comments='This is the one value.'),
))
proto = api.Proto.build(fdp, file_to_generate=True, naming=make_naming())
assert len(proto.enums) == 1
enum = proto.enums['google.enum.v1.Silly']
assert enum.meta.doc == 'This is the Silly enum.'
assert isinstance(enum, wrappers.EnumType)
assert len(enum.values) == 3
assert all([isinstance(i, wrappers.EnumValueType) for i in enum.values])
assert enum.values[0].name == 'ZERO'
assert enum.values[0].meta.doc == 'This is the zero value.'
assert enum.values[1].name == 'ONE'
assert enum.values[1].meta.doc == 'This is the one value.'
assert enum.values[2].name == 'THREE'
assert enum.values[2].meta.doc == ''
示例5: test_mock_value_enum
# 需要导入模块: from google.protobuf import descriptor_pb2 [as 别名]
# 或者: from google.protobuf.descriptor_pb2 import EnumDescriptorProto [as 别名]
def test_mock_value_enum():
values = [
descriptor_pb2.EnumValueDescriptorProto(name='UNSPECIFIED', number=0),
descriptor_pb2.EnumValueDescriptorProto(name='SPECIFIED', number=1),
]
enum = wrappers.EnumType(
values=[wrappers.EnumValueType(enum_value_pb=i) for i in values],
enum_pb=descriptor_pb2.EnumDescriptorProto(value=values),
meta=metadata.Metadata(address=metadata.Address(
module='bogus',
name='Enumerable',
)),
)
field = make_field(
type='TYPE_ENUM',
type_name='bogus.Enumerable',
enum=enum,
)
assert field.mock_value == 'bogus.Enumerable.SPECIFIED'
示例6: generate_enum_doc
# 需要导入模块: from google.protobuf import descriptor_pb2 [as 别名]
# 或者: from google.protobuf.descriptor_pb2 import EnumDescriptorProto [as 别名]
def generate_enum_doc(enum_descriptor, locations, path, name_prefix=''):
"""Generate doc for an enum.
Args:
enum_descriptor: descriptor_pb2.EnumDescriptorProto instance for enum
to generate docs for.
locations: Dictionary of location paths tuples to
descriptor_pb2.SourceCodeInfo.Location instances.
path: Path tuple to the enum definition.
name_prefix: Optional prefix for this enum's name.
"""
print(make_subsection(name_prefix + enum_descriptor.name))
location = locations[path]
if location.HasField('leading_comments'):
print(textwrap.dedent(location.leading_comments))
row_tuples = []
for value_index, value in enumerate(enum_descriptor.value):
field_location = locations[path + (2, value_index)]
row_tuples.append((
make_code(value.name),
value.number,
textwrap.fill(get_comment_from_location(field_location), INFINITY),
))
print_table(('Name', 'Number', 'Description'), row_tuples)
示例7: _ConvertEnumDescriptor
# 需要导入模块: from google.protobuf import descriptor_pb2 [as 别名]
# 或者: from google.protobuf.descriptor_pb2 import EnumDescriptorProto [as 别名]
def _ConvertEnumDescriptor(self, enum_proto, package=None, file_desc=None,
containing_type=None, scope=None):
"""Make a protobuf EnumDescriptor given an EnumDescriptorProto protobuf.
Args:
enum_proto: The descriptor_pb2.EnumDescriptorProto protobuf message.
package: Optional package name for the new message EnumDescriptor.
file_desc: The file containing the enum descriptor.
containing_type: The type containing this enum.
scope: Scope containing available types.
Returns:
The added descriptor
"""
if package:
enum_name = '.'.join((package, enum_proto.name))
else:
enum_name = enum_proto.name
if file_desc is None:
file_name = None
else:
file_name = file_desc.name
values = [self._MakeEnumValueDescriptor(value, index)
for index, value in enumerate(enum_proto.value)]
desc = descriptor.EnumDescriptor(name=enum_proto.name,
full_name=enum_name,
filename=file_name,
file=file_desc,
values=values,
containing_type=containing_type,
options=_OptionsOrNone(enum_proto))
scope['.%s' % enum_name] = desc
self._CheckConflictRegister(desc)
self._enum_descriptors[enum_name] = desc
return desc
示例8: _ConvertEnumDescriptor
# 需要导入模块: from google.protobuf import descriptor_pb2 [as 别名]
# 或者: from google.protobuf.descriptor_pb2 import EnumDescriptorProto [as 别名]
def _ConvertEnumDescriptor(self, enum_proto, package=None, file_desc=None,
containing_type=None, scope=None):
"""Make a protobuf EnumDescriptor given an EnumDescriptorProto protobuf.
Args:
enum_proto: The descriptor_pb2.EnumDescriptorProto protobuf message.
package: Optional package name for the new message EnumDescriptor.
file_desc: The file containing the enum descriptor.
containing_type: The type containing this enum.
scope: Scope containing available types.
Returns:
The added descriptor
"""
if package:
enum_name = '.'.join((package, enum_proto.name))
else:
enum_name = enum_proto.name
if file_desc is None:
file_name = None
else:
file_name = file_desc.name
values = [self._MakeEnumValueDescriptor(value, index)
for index, value in enumerate(enum_proto.value)]
desc = descriptor.EnumDescriptor(name=enum_proto.name,
full_name=enum_name,
filename=file_name,
file=file_desc,
values=values,
containing_type=containing_type,
options=_OptionsOrNone(enum_proto))
scope['.%s' % enum_name] = desc
self._enum_descriptors[enum_name] = desc
return desc
示例9: convert_protodef_to_editable
# 需要导入模块: from google.protobuf import descriptor_pb2 [as 别名]
# 或者: from google.protobuf.descriptor_pb2 import EnumDescriptorProto [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)
示例10: _ConvertEnumDescriptor
# 需要导入模块: from google.protobuf import descriptor_pb2 [as 别名]
# 或者: from google.protobuf.descriptor_pb2 import EnumDescriptorProto [as 别名]
def _ConvertEnumDescriptor(self, enum_proto, package=None, file_desc=None,
containing_type=None, scope=None):
"""Make a protobuf EnumDescriptor given an EnumDescriptorProto protobuf.
Args:
enum_proto: The descriptor_pb2.EnumDescriptorProto protobuf message.
package: Optional package name for the new message EnumDescriptor.
file_desc: The file containing the enum descriptor.
containing_type: The type containing this enum.
scope: Scope containing available types.
Returns:
The added descriptor
"""
if package:
enum_name = '.'.join((package, enum_proto.name))
else:
enum_name = enum_proto.name
if file_desc is None:
file_name = None
else:
file_name = file_desc.name
values = [self._MakeEnumValueDescriptor(value, index)
for index, value in enumerate(enum_proto.value)]
desc = descriptor.EnumDescriptor(name=enum_proto.name,
full_name=enum_name,
filename=file_name,
file=file_desc,
values=values,
containing_type=containing_type,
options=enum_proto.options)
scope['.%s' % enum_name] = desc
self._enum_descriptors[enum_name] = desc
return desc
示例11: write_enum_values
# 需要导入模块: from google.protobuf import descriptor_pb2 [as 别名]
# 或者: from google.protobuf.descriptor_pb2 import EnumDescriptorProto [as 别名]
def write_enum_values(self, enum, enum_full_name):
# type: (d.EnumDescriptorProto, Text) -> None
for val in enum.value:
if val.name in PYTHON_RESERVED:
continue
self._write_line(
"{} = {}({}, {})",
val.name,
self._import("typing", "cast"),
enum_full_name,
val.number,
)
示例12: write_enums
# 需要导入模块: from google.protobuf import descriptor_pb2 [as 别名]
# 或者: from google.protobuf.descriptor_pb2 import EnumDescriptorProto [as 别名]
def write_enums(self, enums, prefix=""):
# type: (Iterable[d.EnumDescriptorProto], Text) -> None
l = self._write_line
for enum in [e for e in enums if e.name not in PYTHON_RESERVED]:
enum_value_type = enum.name + "Value"
l(
"{} = {}('{}', {})",
enum_value_type,
self._import("typing", "NewType"),
enum_value_type,
self._builtin("int"),
)
l("{} = {}", _mangle_message(enum_value_type), enum_value_type)
enum_value_full_type = prefix + enum_value_type
l("{}: {}", enum.name, "_" + enum.name)
l(
"class {}({}[{}]):",
"_" + enum.name,
self._import(
"google.protobuf.internal.enum_type_wrapper", "_EnumTypeWrapper"
),
enum_value_full_type,
)
with self._indent():
l(
"DESCRIPTOR: {} = ...",
self._import("google.protobuf.descriptor", "EnumDescriptor"),
)
self.write_enum_values(enum, enum_value_full_type)
self.write_enum_values(enum, enum_value_full_type)
l("{} = {}", _mangle_message(enum.name), enum.name)
l("")
示例13: __init__
# 需要导入模块: from google.protobuf import descriptor_pb2 [as 别名]
# 或者: from google.protobuf.descriptor_pb2 import EnumDescriptorProto [as 别名]
def __init__(self, request):
# type: (plugin_pb2.CodeGeneratorRequest) -> None
files = {f.name: f for f in request.proto_file}
to_generate = {n: files[n] for n in request.file_to_generate}
self.files = files # type: Dict[Text, d.FileDescriptorProto]
self.to_generate = to_generate # type: Dict[Text, d.FileDescriptorProto]
self.messages = {} # type: Dict[Text, d.DescriptorProto]
self.message_to_fd = {} # type: Dict[Text, d.FileDescriptorProto]
def _add_enums(enums, prefix, _fd):
# type: (RepeatedCompositeFieldContainer[d.EnumDescriptorProto], Text, d.FileDescriptorProto) -> None
for enum in enums:
self.message_to_fd[prefix + enum.name] = _fd
self.message_to_fd[prefix + enum.name + "Value"] = _fd
def _add_messages(messages, prefix, _fd):
# type: (RepeatedCompositeFieldContainer[d.DescriptorProto], Text, d.FileDescriptorProto) -> None
for message in messages:
self.messages[prefix + message.name] = message
self.message_to_fd[prefix + message.name] = _fd
sub_prefix = prefix + message.name + "."
_add_messages(message.nested_type, sub_prefix, _fd)
_add_enums(message.enum_type, sub_prefix, _fd)
for fd in request.proto_file:
start_prefix = "." + fd.package + "." if fd.package else "."
_add_messages(fd.message_type, start_prefix, fd)
_add_enums(fd.enum_type, start_prefix, fd)
示例14: CopyToProto
# 需要导入模块: from google.protobuf import descriptor_pb2 [as 别名]
# 或者: from google.protobuf.descriptor_pb2 import EnumDescriptorProto [as 别名]
def CopyToProto(self, proto):
"""Copies this to a descriptor_pb2.EnumDescriptorProto.
Args:
proto (descriptor_pb2.EnumDescriptorProto): An empty descriptor proto.
"""
# This function is overridden to give a better doc comment.
super(EnumDescriptor, self).CopyToProto(proto)
示例15: get_enum
# 需要导入模块: from google.protobuf import descriptor_pb2 [as 别名]
# 或者: from google.protobuf.descriptor_pb2 import EnumDescriptorProto [as 别名]
def get_enum(dot_path: str) -> wrappers.EnumType:
pieces = dot_path.split('.')
pkg, module, name = pieces[:-2], pieces[-2], pieces[-1]
return wrappers.EnumType(
enum_pb=desc.EnumDescriptorProto(name=name),
meta=metadata.Metadata(address=metadata.Address(
name=name,
package=tuple(pkg),
module=module,
)),
values=[],
)