本文整理匯總了Python中google.protobuf.descriptor.FieldDescriptor.TYPE_MESSAGE屬性的典型用法代碼示例。如果您正苦於以下問題:Python FieldDescriptor.TYPE_MESSAGE屬性的具體用法?Python FieldDescriptor.TYPE_MESSAGE怎麽用?Python FieldDescriptor.TYPE_MESSAGE使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類google.protobuf.descriptor.FieldDescriptor
的用法示例。
在下文中一共展示了FieldDescriptor.TYPE_MESSAGE屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _get_field_value_adaptor
# 需要導入模塊: from google.protobuf.descriptor import FieldDescriptor [as 別名]
# 或者: from google.protobuf.descriptor.FieldDescriptor import TYPE_MESSAGE [as 別名]
def _get_field_value_adaptor(pb, field, use_enum_labels,
including_default_value_fields):
if field.type == FieldDescriptor.TYPE_MESSAGE:
# recursively encode protobuf sub-message
return lambda pb: msg2dict(
pb, use_enum_labels=use_enum_labels,
including_default_value_fields=including_default_value_fields)
if use_enum_labels and field.type == FieldDescriptor.TYPE_ENUM:
return lambda value: enum_label_name(field, value)
if field.type in TYPE_CALLABLE_MAP:
return TYPE_CALLABLE_MAP[field.type]
raise TypeError("Field %s.%s has unrecognised type id %d" % (
pb.__class__.__name__, field.name, field.type))
示例2: json2pb
# 需要導入模塊: from google.protobuf.descriptor import FieldDescriptor [as 別名]
# 或者: from google.protobuf.descriptor.FieldDescriptor import TYPE_MESSAGE [as 別名]
def json2pb(pb, js):
''' convert JSON string to google.protobuf.descriptor instance '''
for field in pb.DESCRIPTOR.fields:
if field.name not in js:
continue
if field.type == FD.TYPE_MESSAGE:
pass
elif field.type in _js2ftype:
ftype = _js2ftype[field.type]
else:
raise ParseError("Field %s.%s of type '%d' is not supported" % (pb.__class__.__name__, field.name, field.type, ))
value = js[field.name]
if field.label == FD.LABEL_REPEATED:
pb_value = getattr(pb, field.name, None)
for v in value:
if field.type == FD.TYPE_MESSAGE:
json2pb(pb_value.add(), v)
else:
pb_value.append(ftype(v))
else:
if field.type == FD.TYPE_MESSAGE:
json2pb(getattr(pb, field.name, None), value)
else:
setattr(pb, field.name, ftype(value))
return pb
示例3: pb2json
# 需要導入模塊: from google.protobuf.descriptor import FieldDescriptor [as 別名]
# 或者: from google.protobuf.descriptor.FieldDescriptor import TYPE_MESSAGE [as 別名]
def pb2json(pb):
''' convert google.protobuf.descriptor instance to JSON string '''
js = {}
# fields = pb.DESCRIPTOR.fields #all fields
fields = pb.ListFields() #only filled (including extensions)
for field,value in fields:
if field.type == FD.TYPE_MESSAGE:
ftype = pb2json
elif field.type in _ftype2js:
ftype = _ftype2js[field.type]
else:
raise ParseError("Field %s.%s of type '%d' is not supported" % (pb.__class__.__name__, field.name, field.type, ))
if field.label == FD.LABEL_REPEATED:
js_value = []
for v in value:
js_value.append(ftype(v))
else:
js_value = ftype(value)
js[field.name] = js_value
return js
示例4: pb2dict
# 需要導入模塊: from google.protobuf.descriptor import FieldDescriptor [as 別名]
# 或者: from google.protobuf.descriptor.FieldDescriptor import TYPE_MESSAGE [as 別名]
def pb2dict(obj):
"""
Takes a ProtoBuf Message obj and convertes it to a dict.
"""
adict = {}
if not obj.IsInitialized():
return None
for field in obj.DESCRIPTOR.fields:
if not getattr(obj, field.name):
continue
if not field.label == FD.LABEL_REPEATED:
if not field.type == FD.TYPE_MESSAGE:
adict[field.name] = getattr(obj, field.name)
else:
value = pb2dict(getattr(obj, field.name))
if value:
adict[field.name] = value
else:
if field.type == FD.TYPE_MESSAGE:
adict[field.name] = \
[pb2dict(v) for v in getattr(obj, field.name)]
else:
adict[field.name] = [v for v in getattr(obj, field.name)]
return adict
示例5: pb2json
# 需要導入模塊: from google.protobuf.descriptor import FieldDescriptor [as 別名]
# 或者: from google.protobuf.descriptor.FieldDescriptor import TYPE_MESSAGE [as 別名]
def pb2json(pb, useFieldNumber=False):
''' convert google.protobuf.descriptor instance to JSON string '''
js = {}
# fields = pb.DESCRIPTOR.fields #all fields
fields = pb.ListFields() #only filled (including extensions)
for field,value in fields:
if useFieldNumber:
key = field.number
else:
key = field.name
if field.type == FD.TYPE_MESSAGE:
ftype = partial(pb2json, useFieldNumber=useFieldNumber)
elif field.type in _ftype2js:
ftype = _ftype2js[field.type]
else:
raise ParseError("Field %s.%s of type '%d' is not supported" % (pb.__class__.__name__, field.name, field.type, ))
if field.label == FD.LABEL_REPEATED:
js_value = []
for v in value:
js_value.append(ftype(v))
else:
js_value = ftype(value)
js[key] = js_value
return js
示例6: protobuf_to_dict
# 需要導入模塊: from google.protobuf.descriptor import FieldDescriptor [as 別名]
# 或者: from google.protobuf.descriptor.FieldDescriptor import TYPE_MESSAGE [as 別名]
def protobuf_to_dict(pb, containers=CONTAINER_MAP, converters=TYPE_CALLABLE_MAP):
result = message_to_container(pb, containers)
# for field, value in pb.ListFields(): # only non-empty fields
for field in pb.DESCRIPTOR.fields: # empty fields too
value = getattr(pb, field.name)
if (field.message_type and field.message_type.has_options and
field.message_type.GetOptions().map_entry):
converter = dict
elif field.type == FieldDescriptor.TYPE_MESSAGE:
# recursively encode protobuf sub-message
converter = partial(protobuf_to_dict, containers=containers,
converters=converters)
elif field.type == FieldDescriptor.TYPE_ENUM:
converter = partial(enum_to_label, field)
else:
converter = converters[field.type]
if field.label == FieldDescriptor.LABEL_REPEATED:
converter = partial(map, converter)
result[field.name] = converter(value)
return result
示例7: _decode_repeated_field
# 需要導入模塊: from google.protobuf.descriptor import FieldDescriptor [as 別名]
# 或者: from google.protobuf.descriptor.FieldDescriptor import TYPE_MESSAGE [as 別名]
def _decode_repeated_field(message, field, value_list):
"""Decode repeated field."""
if field.type == FieldDescriptor.TYPE_MESSAGE:
for value in value_list:
decode(getattr(message, field.name).add(), value)
else:
try:
for value in value_list:
if field.type == FieldDescriptor.TYPE_BYTES:
value = base64.b64decode(value)
getattr(message, field.name).append(value)
except (ValueError, TypeError) as e:
# ValueError: invalid enum value, negative unsigned int value, or
# invalid base64
# TypeError: mismatched type
logger.warning('Message %r ignoring repeated field %s: %s',
message.__class__.__name__, field.name, e)
# Ignore any values already decoded by clearing list
message.ClearField(field.name)
示例8: traverse_field_options
# 需要導入模塊: from google.protobuf.descriptor import FieldDescriptor [as 別名]
# 或者: from google.protobuf.descriptor.FieldDescriptor import TYPE_MESSAGE [as 別名]
def traverse_field_options(fields, prefix):
field_options = []
for field in fields:
assert isinstance(field, FieldDescriptorProto)
full_name = prefix + '-' + field.name
option = None
if field.type == FieldDescriptor.TYPE_MESSAGE and field.label != \
FieldDescriptor.LABEL_REPEATED:
if field.options:
for fd, val in field.options.ListFields():
if fd.full_name == 'voltha.yang_inline_node':
field_options.append(
{'name': full_name,
'option': fd.full_name,
'proto_name': val.id,
'proto_type': val.type
}
)
return field_options
示例9: json2pb
# 需要導入模塊: from google.protobuf.descriptor import FieldDescriptor [as 別名]
# 或者: from google.protobuf.descriptor.FieldDescriptor import TYPE_MESSAGE [as 別名]
def json2pb(pb, js):
""" convert JSON string to google.protobuf.descriptor instance
:param pb: protobuf class to fill
:param js: json input data
"""
for field in pb.DESCRIPTOR.fields:
if field.name not in js:
continue
if field.type == FD.TYPE_MESSAGE:
pass
elif field.type in _js2ftype:
ftype = _js2ftype[field.type]
else:
raise ParseError(
"Field %s.%s of type '%d' is not supported" % (pb.__class__.__name__, field.name, field.type,))
value = js[field.name]
if field.label == FD.LABEL_REPEATED:
pb_value = getattr(pb, field.name, None)
for v in value:
if field.type == FD.TYPE_MESSAGE:
json2pb(pb_value.add(), v)
else:
pb_value.append(ftype(v))
else:
if field.type == FD.TYPE_MESSAGE:
json2pb(getattr(pb, field.name, None), value)
elif field.type == FD.TYPE_ENUM:
real_value = field.enum_type.values_by_name[value].number
setattr(pb, field.name, real_value)
else:
setattr(pb, field.name, ftype(value))
return pb
示例10: pb2json
# 需要導入模塊: from google.protobuf.descriptor import FieldDescriptor [as 別名]
# 或者: from google.protobuf.descriptor.FieldDescriptor import TYPE_MESSAGE [as 別名]
def pb2json(pb):
""" convert google.protobuf.descriptor instance to JSON string
:param pb: protobuf class to be converted in json
"""
js = {}
# fields = pb.DESCRIPTOR.fields #all fields
fields = pb.ListFields() # only filled (including extensions)
for field, value in fields:
if field.type == FD.TYPE_MESSAGE:
ftype = pb2json
elif field.type == FD.TYPE_ENUM:
ftype = str
value = field.enum_type.values_by_number[value].name
elif field.type in _ftype2js:
ftype = _ftype2js[field.type]
else:
raise ParseError(
"Field %s.%s of type '%d' is not supported" % (pb.__class__.__name__, field.name, field.type,))
if field.label == FD.LABEL_REPEATED:
js_value = []
for v in value:
js_value.append(ftype(v))
else:
js_value = ftype(value)
js[field.name] = js_value
return js
示例11: _is_map_entry
# 需要導入模塊: from google.protobuf.descriptor import FieldDescriptor [as 別名]
# 或者: from google.protobuf.descriptor.FieldDescriptor import TYPE_MESSAGE [as 別名]
def _is_map_entry(field):
return (field.type == FieldDescriptor.TYPE_MESSAGE and
field.message_type.has_options and
field.message_type.GetOptions().map_entry)
示例12: _IsValidPath
# 需要導入模塊: from google.protobuf.descriptor import FieldDescriptor [as 別名]
# 或者: from google.protobuf.descriptor.FieldDescriptor import TYPE_MESSAGE [as 別名]
def _IsValidPath(message_descriptor, path):
"""Checks whether the path is valid for Message Descriptor."""
parts = path.split('.')
last = parts.pop()
for name in parts:
field = message_descriptor.fields_by_name.get(name)
if (field is None or
field.label == FieldDescriptor.LABEL_REPEATED or
field.type != FieldDescriptor.TYPE_MESSAGE):
return False
message_descriptor = field.message_type
return last in message_descriptor.fields_by_name
示例13: _IsValidPath
# 需要導入模塊: from google.protobuf.descriptor import FieldDescriptor [as 別名]
# 或者: from google.protobuf.descriptor.FieldDescriptor import TYPE_MESSAGE [as 別名]
def _IsValidPath(message_descriptor, path):
"""Checks whether the path is valid for Message Descriptor."""
parts = path.split('.')
last = parts.pop()
for name in parts:
field = message_descriptor.fields_by_name[name]
if (field is None or
field.label == FieldDescriptor.LABEL_REPEATED or
field.type != FieldDescriptor.TYPE_MESSAGE):
return False
message_descriptor = field.message_type
return last in message_descriptor.fields_by_name
示例14: _get_field_value_adaptor
# 需要導入模塊: from google.protobuf.descriptor import FieldDescriptor [as 別名]
# 或者: from google.protobuf.descriptor.FieldDescriptor import TYPE_MESSAGE [as 別名]
def _get_field_value_adaptor(pb, field, type_callable_map=TYPE_CALLABLE_MAP, use_enum_labels=False):
if field.type == FieldDescriptor.TYPE_MESSAGE:
# recursively encode protobuf sub-message
return lambda pb: protobuf_to_dict(pb, type_callable_map, use_enum_labels)
if use_enum_labels and field.type == FieldDescriptor.TYPE_ENUM:
return lambda value: enum_label_name(field, value)
if field.type in type_callable_map:
return type_callable_map[field.type]
raise TypeError("Field %s.%s has unrecognised type id %d" % (
pb.__class__.__name__, field.name, field.type))
示例15: _dict_to_protobuf
# 需要導入模塊: from google.protobuf.descriptor import FieldDescriptor [as 別名]
# 或者: from google.protobuf.descriptor.FieldDescriptor import TYPE_MESSAGE [as 別名]
def _dict_to_protobuf(pb, value, type_callable_map, strict):
""" dict to protobuf
Args:
pb: data in dict format
Returns:
pb: data in proto format
"""
fields = _get_field_mapping(pb, value, strict)
for field, input_value, pb_value in fields:
if field.label == FieldDescriptor.LABEL_REPEATED:
for item in input_value:
if field.type == FieldDescriptor.TYPE_MESSAGE:
m = pb_value.add()
_dict_to_protobuf(m, item, type_callable_map, strict)
elif field.type == FieldDescriptor.TYPE_ENUM and isinstance(item, basestring):
pb_value.append(_string_to_enum(field, item))
else:
pb_value.append(item)
continue
if field.type == FieldDescriptor.TYPE_MESSAGE:
_dict_to_protobuf(pb_value, input_value, type_callable_map, strict)
continue
if field.type in type_callable_map:
input_value = type_callable_map[field.type](input_value)
if field.is_extension:
pb.Extensions[field] = input_value
continue
if field.type == FieldDescriptor.TYPE_ENUM and isinstance(input_value, basestring):
input_value = _string_to_enum(field, input_value)
setattr(pb, field.name, input_value)
return pb