我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用google.protobuf.message.Message()。
def beta_create_AdminService_server(servicer, pool=None, pool_size=None, default_timeout=None, maximum_timeout=None): """The Beta API is deprecated for 0.15.0 and later. It is recommended to use the GA API (classes and functions in this file not marked beta) for all further purposes. This function was generated only to ease transition from grpcio<0.15.0 to grpcio>=0.15.0""" request_deserializers = { ('AdminService', 'Request'): Message.FromString, } response_serializers = { ('AdminService', 'Request'): Message.SerializeToString, } method_implementations = { ('AdminService', 'Request'): face_utilities.unary_unary_inline(servicer.Request), } server_options = beta_implementations.server_options(request_deserializers=request_deserializers, response_serializers=response_serializers, thread_pool=pool, thread_pool_size=pool_size, default_timeout=default_timeout, maximum_timeout=maximum_timeout) return beta_implementations.server(method_implementations, options=server_options)
def dict_to_protobuf(pb_klass_or_instance, values, type_callable_map=REVERSE_TYPE_CALLABLE_MAP, strict=True): """Populates a protobuf model from a dictionary. :param pb_klass_or_instance: a protobuf message class, or an protobuf instance :type pb_klass_or_instance: a type or instance of a subclass of google.protobuf.message.Message :param dict values: a dictionary of values. Repeated and nested values are fully supported. :param dict type_callable_map: a mapping of protobuf types to callables for setting values on the target instance. :param bool strict: complain if keys in the map are not fields on the message. """ if isinstance(pb_klass_or_instance, Message): instance = pb_klass_or_instance else: instance = pb_klass_or_instance() return _dict_to_protobuf(instance, values, type_callable_map, strict)
def _AddMessageMethods(message_descriptor, cls): """Adds implementations of all Message methods to cls.""" _AddListFieldsMethod(message_descriptor, cls) _AddHasFieldMethod(message_descriptor, cls) _AddClearFieldMethod(message_descriptor, cls) if message_descriptor.is_extendable: _AddClearExtensionMethod(cls) _AddHasExtensionMethod(cls) _AddEqualsMethod(message_descriptor, cls) _AddStrMethod(message_descriptor, cls) _AddReprMethod(message_descriptor, cls) _AddUnicodeMethod(message_descriptor, cls) _AddByteSizeMethod(message_descriptor, cls) _AddSerializeToStringMethod(message_descriptor, cls) _AddSerializePartialToStringMethod(message_descriptor, cls) _AddMergeFromStringMethod(message_descriptor, cls) _AddIsInitializedMethod(message_descriptor, cls) _AddMergeFromMethod(cls) _AddWhichOneofMethod(message_descriptor, cls) _AddReduceMethod(cls) # Adds methods which do not depend on cls. cls.Clear = _Clear cls.DiscardUnknownFields = _DiscardUnknownFields cls._SetListener = _SetListener
def setdefault(pb_or_dict, key, value): """Set the key on the object to the value if the current value is falsy. Because protobuf Messages do not distinguish between unset values and falsy ones particularly well, this method treats any falsy value (e.g. 0, empty list) as a target to be overwritten, on both Messages and dictionaries. Args: pb_or_dict (Union[~google.protobuf.message.Message, Mapping]): the object. key (str): The key on the object in question. value (Any): The value to set. Raises: TypeError: If pb_or_dict is not a Message or Mapping. """ if not get(pb_or_dict, key, default=None): set(pb_or_dict, key, value)
def get_messages(module): """Return a dictionary of message names and objects. Args: module (module): A Python module; dir() will be run against this module to find Message subclasses. Returns: dict[str, Message]: A dictionary with the Message class names as keys, and the Message subclasses themselves as values. """ answer = collections.OrderedDict() for name in dir(module): candidate = getattr(module, name) if inspect.isclass(candidate) and issubclass(candidate, Message): answer[name] = candidate return answer
def __init__(self, channel): """Constructor. Args: channel: A grpc.Channel. """ self.ExchangeNode = channel.unary_unary( '/Discovery/ExchangeNode', request_serializer=Node.SerializeToString, response_deserializer=Node.FromString, ) self.Hello = channel.unary_unary( '/Discovery/Hello', request_serializer=Message.SerializeToString, response_deserializer=Message.FromString, )
def add_DiscoveryServicer_to_server(servicer, server): rpc_method_handlers = { 'ExchangeNode': grpc.unary_unary_rpc_method_handler( servicer.ExchangeNode, request_deserializer=Node.FromString, response_serializer=Node.SerializeToString, ), 'Hello': grpc.unary_unary_rpc_method_handler( servicer.Hello, request_deserializer=Message.FromString, response_serializer=Message.SerializeToString, ), } generic_handler = grpc.method_handlers_generic_handler( 'Discovery', rpc_method_handlers) server.add_generic_rpc_handlers((generic_handler,))
def beta_create_Discovery_server(servicer, pool=None, pool_size=None, default_timeout=None, maximum_timeout=None): """The Beta API is deprecated for 0.15.0 and later. It is recommended to use the GA API (classes and functions in this file not marked beta) for all further purposes. This function was generated only to ease transition from grpcio<0.15.0 to grpcio>=0.15.0""" request_deserializers = { ('Discovery', 'ExchangeNode'): Node.FromString, ('Discovery', 'Hello'): Message.FromString, } response_serializers = { ('Discovery', 'ExchangeNode'): Node.SerializeToString, ('Discovery', 'Hello'): Message.SerializeToString, } method_implementations = { ('Discovery', 'ExchangeNode'): face_utilities.unary_unary_inline(servicer.ExchangeNode), ('Discovery', 'Hello'): face_utilities.unary_unary_inline(servicer.Hello), } server_options = beta_implementations.server_options(request_deserializers=request_deserializers, response_serializers=response_serializers, thread_pool=pool, thread_pool_size=pool_size, default_timeout=default_timeout, maximum_timeout=maximum_timeout) return beta_implementations.server(method_implementations, options=server_options)
def beta_create_AdminService_stub(channel, host=None, metadata_transformer=None, pool=None, pool_size=None): """The Beta API is deprecated for 0.15.0 and later. It is recommended to use the GA API (classes and functions in this file not marked beta) for all further purposes. This function was generated only to ease transition from grpcio<0.15.0 to grpcio>=0.15.0""" request_serializers = { ('AdminService', 'Request'): Message.SerializeToString, } response_deserializers = { ('AdminService', 'Request'): Message.FromString, } cardinalities = { 'Request': cardinality.Cardinality.UNARY_UNARY, } stub_options = beta_implementations.stub_options(host=host, metadata_transformer=metadata_transformer, request_serializers=request_serializers, response_deserializers=response_deserializers, thread_pool=pool, thread_pool_size=pool_size) return beta_implementations.dynamic_stub(channel, 'AdminService', cardinalities, options=stub_options)
def beta_create_Container_server(servicer, pool=None, pool_size=None, default_timeout=None, maximum_timeout=None): """The Beta API is deprecated for 0.15.0 and later. It is recommended to use the GA API (classes and functions in this file not marked beta) for all further purposes. This function was generated only to ease transition from grpcio<0.15.0 to grpcio>=0.15.0""" request_deserializers = { ('Container', 'Request'): Message.FromString, } response_serializers = { ('Container', 'Request'): Message.SerializeToString, } method_implementations = { ('Container', 'Request'): face_utilities.unary_unary_inline(servicer.Request), } server_options = beta_implementations.server_options(request_deserializers=request_deserializers, response_serializers=response_serializers, thread_pool=pool, thread_pool_size=pool_size, default_timeout=default_timeout, maximum_timeout=maximum_timeout) return beta_implementations.server(method_implementations, options=server_options)
def beta_create_Container_stub(channel, host=None, metadata_transformer=None, pool=None, pool_size=None): """The Beta API is deprecated for 0.15.0 and later. It is recommended to use the GA API (classes and functions in this file not marked beta) for all further purposes. This function was generated only to ease transition from grpcio<0.15.0 to grpcio>=0.15.0""" request_serializers = { ('Container', 'Request'): Message.SerializeToString, } response_deserializers = { ('Container', 'Request'): Message.FromString, } cardinalities = { 'Request': cardinality.Cardinality.UNARY_UNARY, } stub_options = beta_implementations.stub_options(host=host, metadata_transformer=metadata_transformer, request_serializers=request_serializers, response_deserializers=response_deserializers, thread_pool=pool, thread_pool_size=pool_size) return beta_implementations.dynamic_stub(channel, 'Container', cardinalities, options=stub_options)
def _AddMessageMethods(message_descriptor, cls): """Adds implementations of all Message methods to cls.""" _AddListFieldsMethod(message_descriptor, cls) _AddHasFieldMethod(message_descriptor, cls) _AddClearFieldMethod(message_descriptor, cls) if message_descriptor.is_extendable: _AddClearExtensionMethod(cls) _AddHasExtensionMethod(cls) _AddClearMethod(message_descriptor, cls) _AddEqualsMethod(message_descriptor, cls) _AddStrMethod(message_descriptor, cls) _AddReprMethod(message_descriptor, cls) _AddUnicodeMethod(message_descriptor, cls) _AddSetListenerMethod(cls) _AddByteSizeMethod(message_descriptor, cls) _AddSerializeToStringMethod(message_descriptor, cls) _AddSerializePartialToStringMethod(message_descriptor, cls) _AddMergeFromStringMethod(message_descriptor, cls) _AddIsInitializedMethod(message_descriptor, cls) _AddMergeFromMethod(cls) _AddWhichOneofMethod(message_descriptor, cls)
def toDict(self, protoObj): """Converts the (protobuf) result from an API call into a dict, for easier introspection.""" iterable = False if isinstance(protoObj, RepeatedCompositeFieldContainer): iterable = True else: protoObj = [protoObj] retlist = [] for po in protoObj: msg = dict() for fielddesc, value in po.ListFields(): #print value, type(value), getattr(value, "__iter__", False) if fielddesc.type == descriptor.FieldDescriptor.TYPE_GROUP or isinstance(value, RepeatedCompositeFieldContainer) or isinstance(value, Message): msg[fielddesc.name] = self.toDict(value) else: msg[fielddesc.name] = value retlist.append(msg) if not iterable: if len(retlist) > 0: return retlist[0] else: return None return retlist
def _AddEqualsMethod(message_descriptor, cls): """Helper for _AddMessageMethods().""" def __eq__(self, other): if (not isinstance(other, message_mod.Message) or other.DESCRIPTOR != self.DESCRIPTOR): return False if self is other: return True if not self.ListFields() == other.ListFields(): return False # Sort unknown fields because their order shouldn't affect equality test. unknown_fields = list(self._unknown_fields) unknown_fields.sort() other_unknown_fields = list(other._unknown_fields) other_unknown_fields.sort() return unknown_fields == other_unknown_fields cls.__eq__ = __eq__
def __init__(cls, name, bases, dictionary): """Here we perform the majority of our work on the class. We add enum getters, an __init__ method, implementations of all Message methods, and properties for all fields in the protocol type. Args: name: Name of the class (ignored, but required by the metaclass protocol). bases: Base classes of the class we're constructing. (Should be message.Message). We ignore this field, but it's required by the metaclass protocol dictionary: The class dictionary of the class we're constructing. dictionary[_DESCRIPTOR_KEY] must contain a Descriptor object describing this protocol message type. """ descriptor = dictionary[GeneratedProtocolMessageType._DESCRIPTOR_KEY] _InitMessage(descriptor, cls) superclass = super(GeneratedProtocolMessageType, cls) superclass.__init__(name, bases, dictionary)
def _AddMessageMethods(message_descriptor, cls): """Adds implementations of all Message methods to cls.""" _AddListFieldsMethod(message_descriptor, cls) _AddHasFieldMethod(message_descriptor, cls) _AddClearFieldMethod(message_descriptor, cls) if message_descriptor.is_extendable: _AddClearExtensionMethod(cls) _AddHasExtensionMethod(cls) _AddClearMethod(message_descriptor, cls) _AddEqualsMethod(message_descriptor, cls) _AddStrMethod(message_descriptor, cls) _AddUnicodeMethod(message_descriptor, cls) _AddSetListenerMethod(cls) _AddByteSizeMethod(message_descriptor, cls) _AddSerializeToStringMethod(message_descriptor, cls) _AddSerializePartialToStringMethod(message_descriptor, cls) _AddMergeFromStringMethod(message_descriptor, cls) _AddIsInitializedMethod(message_descriptor, cls) _AddMergeFromMethod(cls) _AddWhichOneofMethod(message_descriptor, cls)
def _AddMessageMethods(message_descriptor, cls): """Adds implementations of all Message methods to cls.""" _AddListFieldsMethod(message_descriptor, cls) _AddHasFieldMethod(message_descriptor, cls) _AddClearFieldMethod(message_descriptor, cls) if message_descriptor.is_extendable: _AddClearExtensionMethod(cls) _AddHasExtensionMethod(cls) _AddClearMethod(message_descriptor, cls) _AddEqualsMethod(message_descriptor, cls) _AddStrMethod(message_descriptor, cls) _AddUnicodeMethod(message_descriptor, cls) _AddSetListenerMethod(cls) _AddByteSizeMethod(message_descriptor, cls) _AddSerializeToStringMethod(message_descriptor, cls) _AddSerializePartialToStringMethod(message_descriptor, cls) _AddMergeFromStringMethod(message_descriptor, cls) _AddIsInitializedMethod(message_descriptor, cls) _AddMergeFromMethod(cls)
def parse_message(self, m, type_tag_name=None): assert isinstance(m, Message) d = OrderedDict() for field, value in m.ListFields(): assert isinstance(field, FieldDescriptor) if field.label in (1, 2): d[field.name] = self.parse(value, type_tag_name) elif field.label == 3: d[field.name] = [self.parse(x, type_tag_name) for x in value] else: raise InvalidDescriptorError() if type_tag_name is not None: d[type_tag_name] = m.DESCRIPTOR.full_name.strip('.') return d
def advertise(self, type, data, hash=None): if type in IGNORED_CALLBACKS: log.info('Ignoring event {} with data {}'.format(type, data)) return if type is CallbackType.POST_ADD: kind = ConfigEventType.add elif type is CallbackType.POST_REMOVE: kind = ConfigEventType.remove else: kind = ConfigEventType.update if isinstance(data, Message): msg = dumps(MessageToDict(data, True, True)) else: msg = data event = ConfigEvent( type=kind, hash=hash, data=msg ) self._event_bus_client.publish(self._topic, event)
def _AddMessageMethods(message_descriptor, cls): """Adds implementations of all Message methods to cls.""" _AddListFieldsMethod(message_descriptor, cls) _AddHasFieldMethod(message_descriptor, cls) _AddClearFieldMethod(message_descriptor, cls) if message_descriptor.is_extendable: _AddClearExtensionMethod(cls) _AddHasExtensionMethod(cls) _AddEqualsMethod(message_descriptor, cls) _AddStrMethod(message_descriptor, cls) _AddReprMethod(message_descriptor, cls) _AddUnicodeMethod(message_descriptor, cls) _AddByteSizeMethod(message_descriptor, cls) _AddSerializeToStringMethod(message_descriptor, cls) _AddSerializePartialToStringMethod(message_descriptor, cls) _AddMergeFromStringMethod(message_descriptor, cls) _AddIsInitializedMethod(message_descriptor, cls) _AddMergeFromMethod(cls) _AddWhichOneofMethod(message_descriptor, cls) # Adds methods which do not depend on cls. cls.Clear = _Clear cls.DiscardUnknownFields = _DiscardUnknownFields cls._SetListener = _SetListener
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])
def add_local_symbol(self, field, value): if len(field.component) == 1: self.stack[-1][field.component[0].name] = value else: components = list(reversed([component.name for component in field.component])) atom = self.lookup_local_key(components.pop()) parent, base, p = self.traverse_atom(atom, components) if isinstance(p, Message): result = parse_proto(value.literal.proto.value, value.literal.proto.field.component[0].name) getattr(parent, field.component[-1].name).CopyFrom(result) self.stack[-1][field.component[0].name].literal.proto.value = text_format.MessageToString(base) else: setattr(parent, field.component[-1].name, value) self.stack[-1][field.component[0].name].literal.proto.value = text_format.MessageToString(base)
def value(literal): if isinstance(literal, protocall_pb2.Expression) and literal.HasField("atom"): result = value(literal.atom.literal) elif isinstance(literal, protocall_pb2.Expression) and literal.HasField("arithmetic_operator"): result = literal.arithmetic_operator elif isinstance(literal, protocall_pb2.Expression) and literal.HasField("comparison_operator"): result = literal.comparison_operator elif isinstance(literal, protocall_pb2.Expression) and literal.HasField("expression"): result = literal.expression elif isinstance(literal, protocall_pb2.Literal) and literal.HasField("integer"): result = literal.integer.value elif isinstance(literal, protocall_pb2.Literal) and literal.HasField("string"): result = literal.string.value elif isinstance(literal, protocall_pb2.Literal) and literal.HasField("array"): result = '[ ' + ", ".join([str(value(element)) for element in literal.array.element]) + ' ]' elif isinstance(literal, protocall_pb2.Literal) and literal.HasField("proto"): print "XXX" print "literal:", literal result = literal.proto elif isinstance(literal, protocall_pb2.Atom): result = value(literal.literal) elif isinstance(literal, int): result = literal elif isinstance(literal, str): result = literal elif isinstance(literal, unicode): result = literal elif isinstance(literal, float): result = literal elif isinstance(literal, message.Message): print "YYY" result = literal elif isinstance(literal, None): print "Warning: None value." result = None else: print literal.__class__ import pdb; pdb.set_trace() raise RuntimeError return result
def iterate_proto_msg(module, base): for name, cls in getmembers(module): if isclass(cls) and issubclass(cls, Message): yield base + name, cls yield from iterate_proto_msg(cls, base + name + '.') # Routine for saving data returned by an extractor
def _assign_to_field(obj, name, val): 'Helper to assign an arbitrary value to a protobuf field' target = getattr(obj, name) if isinstance(target, containers.RepeatedScalarFieldContainer): target.append(val) elif isinstance(target, containers.RepeatedCompositeFieldContainer): target = target.add() target.CopyFrom(val) elif isinstance(target, (int, float, bool, str, bytes)): setattr(obj, name, val) elif isinstance(target, message.Message): target.CopyFrom(val) else: raise RuntimeError("Unsupported type: {}".format(type(target)))
def from_protobuf_class(protobuf_class): ''' Return a generator for an already-loaded Protobuf class. Args: protobuf_class(Message) -- A class object created from Protobuf- generated code. Returns: A ProtobufGenerator instance that can be used to create inter-field dependencies or to generate messages. ''' return ProtobufGenerator(protobuf_class.DESCRIPTOR)
def GetPrototype(self, descriptor): """Builds a proto2 message class based on the passed in descriptor. Passing a descriptor with a fully qualified name matching a previous invocation will cause the same class to be returned. Args: descriptor: The descriptor to build from. Returns: A class describing the passed in descriptor. """ if descriptor.full_name not in self._classes: descriptor_name = descriptor.name if str is bytes: # PY2 descriptor_name = descriptor.name.encode('ascii', 'ignore') result_class = reflection.GeneratedProtocolMessageType( descriptor_name, (message.Message,), {'DESCRIPTOR': descriptor, '__module__': None}) # If module not set, it wrongly points to the reflection.py module. self._classes[descriptor.full_name] = result_class for field in descriptor.fields: if field.message_type: self.GetPrototype(field.message_type) for extension in result_class.DESCRIPTOR.extensions: if extension.containing_type.full_name not in self._classes: self.GetPrototype(extension.containing_type) extended_class = self._classes[extension.containing_type.full_name] extended_class.RegisterExtension(extension) return self._classes[descriptor.full_name]
def __new__(cls, name, bases, dictionary): """Custom allocation for runtime-generated class types. We override __new__ because this is apparently the only place where we can meaningfully set __slots__ on the class we're creating(?). (The interplay between metaclasses and slots is not very well-documented). Args: name: Name of the class (ignored, but required by the metaclass protocol). bases: Base classes of the class we're constructing. (Should be message.Message). We ignore this field, but it's required by the metaclass protocol dictionary: The class dictionary of the class we're constructing. dictionary[_DESCRIPTOR_KEY] must contain a Descriptor object describing this protocol message type. Returns: Newly-allocated class. """ descriptor = dictionary[GeneratedProtocolMessageType._DESCRIPTOR_KEY] if descriptor.full_name in well_known_types.WKTBASES: bases += (well_known_types.WKTBASES[descriptor.full_name],) _AddClassAttributesForNestedExtensions(descriptor, dictionary) _AddSlots(descriptor, dictionary) superclass = super(GeneratedProtocolMessageType, cls) new_class = superclass.__new__(cls, name, bases, dictionary) return new_class
def _AddEqualsMethod(message_descriptor, cls): """Helper for _AddMessageMethods().""" def __eq__(self, other): if (not isinstance(other, message_mod.Message) or other.DESCRIPTOR != self.DESCRIPTOR): return False if self is other: return True if self.DESCRIPTOR.full_name == _AnyFullTypeName: any_a = _InternalUnpackAny(self) any_b = _InternalUnpackAny(other) if any_a and any_b: return any_a == any_b if not self.ListFields() == other.ListFields(): return False # Sort unknown fields because their order shouldn't affect equality test. unknown_fields = list(self._unknown_fields) unknown_fields.sort() other_unknown_fields = list(other._unknown_fields) other_unknown_fields.sort() return unknown_fields == other_unknown_fields cls.__eq__ = __eq__
def __init__(self, extended_message): """extended_message: Message instance for which we are the Extensions dict. """ self._extended_message = extended_message
def ParseMessage(descriptor, byte_str): """Generate a new Message instance from this Descriptor and a byte string. Args: descriptor: Protobuf Descriptor object byte_str: Serialized protocol buffer byte string Returns: Newly created protobuf Message object. """ result_class = MakeClass(descriptor) new_msg = result_class() new_msg.ParseFromString(byte_str) return new_msg
def MakeClass(descriptor): """Construct a class object for a protobuf described by descriptor. Composite descriptors are handled by defining the new class as a member of the parent class, recursing as deep as necessary. This is the dynamic equivalent to: class Parent(message.Message): __metaclass__ = GeneratedProtocolMessageType DESCRIPTOR = descriptor class Child(message.Message): __metaclass__ = GeneratedProtocolMessageType DESCRIPTOR = descriptor.nested_types[0] Sample usage: file_descriptor = descriptor_pb2.FileDescriptorProto() file_descriptor.ParseFromString(proto2_string) msg_descriptor = descriptor.MakeDescriptor(file_descriptor.message_type[0]) msg_class = reflection.MakeClass(msg_descriptor) msg = msg_class() Args: descriptor: A descriptor.Descriptor object describing the protobuf. Returns: The Message class object described by the descriptor. """ if descriptor in MESSAGE_CLASS_CACHE: return MESSAGE_CLASS_CACHE[descriptor] attributes = {} for name, nested_type in descriptor.nested_types_by_name.items(): attributes[name] = MakeClass(nested_type) attributes[GeneratedProtocolMessageType._DESCRIPTOR_KEY] = descriptor result = GeneratedProtocolMessageType( str(descriptor.name), (message.Message,), attributes) MESSAGE_CLASS_CACHE[descriptor] = result return result
def test_get_messages(self): answer = messages.get_messages(date_pb2) # Ensure that Date was exported properly. assert answer['Date'] is date_pb2.Date # Ensure that no non-Message objects were exported. for value in answer.values(): assert issubclass(value, Message)