diff --git a/.gitignore b/.gitignore
index fd7c7e8..49922bb 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,4 +2,5 @@ README
build
dist
*.egg-info
-log_*
\ No newline at end of file
+log_*
+.eggs
diff --git a/README.md b/README.md
index be927f2..cec5399 100644
--- a/README.md
+++ b/README.md
@@ -100,6 +100,10 @@ This indicates an OS permission problem for access by your user to the USB seria
sudo usermod -a -G dialout
```
-## Required device software version
+## A note to developers of this lib
-This API and tool both require that the device is running Meshtastic 0.6.0 or later.
+If you need to build a new release you'll need:
+```
+apt install pandoc
+sudo pip3 install markdown pandoc webencodings pyparsing
+```
\ No newline at end of file
diff --git a/docs/meshtastic/apponly_pb2.html b/docs/meshtastic/apponly_pb2.html
new file mode 100644
index 0000000..5fd09c6
--- /dev/null
+++ b/docs/meshtastic/apponly_pb2.html
@@ -0,0 +1,194 @@
+
+
+
+
+
+
+meshtastic.apponly_pb2 API documentation
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -1054,6 +1053,8 @@ noProto – If True, don't try to run our protocol on the link - just be a d
self.nodes = None # FIXME
self.isConnected = threading.Event()
self.noProto = noProto
+ random.seed() # FIXME, we should not clobber the random seedval here, instead tell user they must call it
+ self.currentPacketId = random.randint(0, 0xffffffff)
self._startConfig()
def __enter__(self):
@@ -1284,7 +1285,6 @@ noProto – If True, don't try to run our protocol on the link - just be a d
self.nodes = {} # nodes keyed by ID
self.nodesByNum = {} # nodes keyed by nodenum
self.radioConfig = None
- self.currentPacketId = None
startConfig = mesh_pb2.ToRadio()
startConfig.want_config_id = MY_CONFIG_ID # we don't use this value
@@ -1316,8 +1316,6 @@ noProto – If True, don't try to run our protocol on the link - just be a d
raise Exception(
"This device needs a newer python client, please \"pip install --upgrade meshtastic\"")
# start assigning our packet IDs from the opposite side of where our local device is assigning them
- self.currentPacketId = (
- self.myInfo.current_packet_id + 0x80000000) & 0xffffffff
elif fromRadio.HasField("radio"):
self.radioConfig = fromRadio.radio
elif fromRadio.HasField("node_info"):
@@ -2221,7 +2219,9 @@ hostname {string} – Hostname/IP address of the device to connect to
class ChannelSettings
-(**kwargs)
+(*args, **kwargs)
-
Abstract base class for protocol messages.
-
Protocol message classes are almost always generated by the protocol
-compiler.
-These generated types subclass Message and implement the methods
-shown below.
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field ChannelSettings.bandwidth
var channel_num
-
Getter for channel_num.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field ChannelSettings.channel_num
var coding_rate
-
Getter for coding_rate.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field ChannelSettings.coding_rate
+
+
var downlink_enabled
+
+
Field ChannelSettings.downlink_enabled
+
+
var id
+
+
Field ChannelSettings.id
var modem_config
-
Getter for modem_config.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field ChannelSettings.modem_config
var name
-
Getter for name.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field ChannelSettings.name
var psk
-
Getter for psk.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field ChannelSettings.psk
var spread_factor
-
Getter for spread_factor.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field ChannelSettings.spread_factor
var tx_power
-
Getter for tx_power.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field ChannelSettings.tx_power
-
-
Methods
-
-
-def ByteSize(self)
-
+
var uplink_enabled
-
-
-
-Expand source code
-
-
def ByteSize(self):
- if not self._cached_byte_size_dirty:
- return self._cached_byte_size
-
- size = 0
- descriptor = self.DESCRIPTOR
- if descriptor.GetOptions().map_entry:
- # Fields of map entry should always be serialized.
- size = descriptor.fields_by_name['key']._sizer(self.key)
- size += descriptor.fields_by_name['value']._sizer(self.value)
- else:
- for field_descriptor, field_value in self.ListFields():
- size += field_descriptor._sizer(field_value)
- for tag_bytes, value_bytes in self._unknown_fields:
- size += len(tag_bytes) + len(value_bytes)
-
- self._cached_byte_size = size
- self._cached_byte_size_dirty = False
- self._listener_for_children.dirty = False
- return size
def ClearField(self, field_name):
- try:
- field = message_descriptor.fields_by_name[field_name]
- except KeyError:
- try:
- field = message_descriptor.oneofs_by_name[field_name]
- if field in self._oneofs:
- field = self._oneofs[field]
- else:
- return
- except KeyError:
- raise ValueError('Protocol message %s has no "%s" field.' %
- (message_descriptor.name, field_name))
-
- if field in self._fields:
- # To match the C++ implementation, we need to invalidate iterators
- # for map fields when ClearField() happens.
- if hasattr(self._fields[field], 'InvalidateIterators'):
- self._fields[field].InvalidateIterators()
-
- # Note: If the field is a sub-message, its listener will still point
- # at us. That's fine, because the worst than can happen is that it
- # will call _Modified() and invalidate our byte size. Big deal.
- del self._fields[field]
-
- if self._oneofs.get(field.containing_oneof, None) is field:
- del self._oneofs[field.containing_oneof]
-
- # Always call _Modified() -- even if nothing was changed, this is
- # a mutating method, and thus calling it should cause the field to become
- # present in the parent message.
- self._Modified()
-
-
-
-def DiscardUnknownFields(self)
-
-
-
-
-
-Expand source code
-
-
def _DiscardUnknownFields(self):
- self._unknown_fields = []
- self._unknown_field_set = None # pylint: disable=protected-access
- for field, value in self.ListFields():
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- if _IsMapField(field):
- if _IsMessageMapField(field):
- for key in value:
- value[key].DiscardUnknownFields()
- elif field.label == _FieldDescriptor.LABEL_REPEATED:
- for sub_message in value:
- sub_message.DiscardUnknownFields()
- else:
- value.DiscardUnknownFields()
-
-
-
-def FindInitializationErrors(self)
-
-
-
Finds required fields which are not initialized.
-
Returns
-
A list of strings.
-Each string is a path to an uninitialized field from
-the top-level message, e.g. "foo.bar[5].baz".
-
-
-Expand source code
-
-
def FindInitializationErrors(self):
- """Finds required fields which are not initialized.
-
- Returns:
- A list of strings. Each string is a path to an uninitialized field from
- the top-level message, e.g. "foo.bar[5].baz".
- """
-
- errors = [] # simplify things
-
- for field in required_fields:
- if not self.HasField(field.name):
- errors.append(field.name)
-
- for field, value in self.ListFields():
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- if field.is_extension:
- name = '(%s)' % field.full_name
- else:
- name = field.name
-
- if _IsMapField(field):
- if _IsMessageMapField(field):
- for key in value:
- element = value[key]
- prefix = '%s[%s].' % (name, key)
- sub_errors = element.FindInitializationErrors()
- errors += [prefix + error for error in sub_errors]
- else:
- # ScalarMaps can't have any initialization errors.
- pass
- elif field.label == _FieldDescriptor.LABEL_REPEATED:
- for i in range(len(value)):
- element = value[i]
- prefix = '%s[%d].' % (name, i)
- sub_errors = element.FindInitializationErrors()
- errors += [prefix + error for error in sub_errors]
- else:
- prefix = name + '.'
- sub_errors = value.FindInitializationErrors()
- errors += [prefix + error for error in sub_errors]
-
- return errors
-
-
-
-def HasField(self, field_name)
-
-
-
-
-
-Expand source code
-
-
def HasField(self, field_name):
- try:
- field = hassable_fields[field_name]
- except KeyError:
- raise ValueError(error_msg % (message_descriptor.full_name, field_name))
-
- if isinstance(field, descriptor_mod.OneofDescriptor):
- try:
- return HasField(self, self._oneofs[field].name)
- except KeyError:
- return False
- else:
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- value = self._fields.get(field)
- return value is not None and value._is_present_in_parent
- else:
- return field in self._fields
-
-
-
-def IsInitialized(self, errors=None)
-
-
-
Checks if all required fields of a message are set.
-
Args
-
-
errors
-
A list which, if provided, will be populated with the field
-paths of all missing required fields.
-
-
Returns
-
True iff the specified message has all required fields set.
-
-
-Expand source code
-
-
def IsInitialized(self, errors=None):
- """Checks if all required fields of a message are set.
-
- Args:
- errors: A list which, if provided, will be populated with the field
- paths of all missing required fields.
-
- Returns:
- True iff the specified message has all required fields set.
- """
-
- # Performance is critical so we avoid HasField() and ListFields().
-
- for field in required_fields:
- if (field not in self._fields or
- (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and
- not self._fields[field]._is_present_in_parent)):
- if errors is not None:
- errors.extend(self.FindInitializationErrors())
- return False
-
- for field, value in list(self._fields.items()): # dict can change size!
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- if field.label == _FieldDescriptor.LABEL_REPEATED:
- if (field.message_type.has_options and
- field.message_type.GetOptions().map_entry):
- continue
- for element in value:
- if not element.IsInitialized():
- if errors is not None:
- errors.extend(self.FindInitializationErrors())
- return False
- elif value._is_present_in_parent and not value.IsInitialized():
- if errors is not None:
- errors.extend(self.FindInitializationErrors())
- return False
-
- return True
-
-
-
-def ListFields(self)
-
-
-
-
-
-Expand source code
-
-
def ListFields(self):
- all_fields = [item for item in self._fields.items() if _IsPresent(item)]
- all_fields.sort(key = lambda item: item[0].number)
- return all_fields
-
-
-
-def MergeFrom(self, msg)
-
-
-
-
-
-Expand source code
-
-
def MergeFrom(self, msg):
- if not isinstance(msg, cls):
- raise TypeError(
- 'Parameter to MergeFrom() must be instance of same class: '
- 'expected %s got %s.' % (cls.__name__, msg.__class__.__name__))
-
- assert msg is not self
- self._Modified()
-
- fields = self._fields
-
- for field, value in msg._fields.items():
- if field.label == LABEL_REPEATED:
- field_value = fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
- fields[field] = field_value
- field_value.MergeFrom(value)
- elif field.cpp_type == CPPTYPE_MESSAGE:
- if value._is_present_in_parent:
- field_value = fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
- fields[field] = field_value
- field_value.MergeFrom(value)
- else:
- self._fields[field] = value
- if field.containing_oneof:
- self._UpdateOneofState(field)
-
- if msg._unknown_fields:
- if not self._unknown_fields:
- self._unknown_fields = []
- self._unknown_fields.extend(msg._unknown_fields)
- # pylint: disable=protected-access
- if self._unknown_field_set is None:
- self._unknown_field_set = containers.UnknownFieldSet()
- self._unknown_field_set._extend(msg._unknown_field_set)
-
-
-
-def MergeFromString(self, serialized)
-
-
-
-
-
-Expand source code
-
-
def MergeFromString(self, serialized):
- if isinstance(serialized, memoryview) and six.PY2:
- raise TypeError(
- 'memoryview not supported in Python 2 with the pure Python proto '
- 'implementation: this is to maintain compatibility with the C++ '
- 'implementation')
-
- serialized = memoryview(serialized)
- length = len(serialized)
- try:
- if self._InternalParse(serialized, 0, length) != length:
- # The only reason _InternalParse would return early is if it
- # encountered an end-group tag.
- raise message_mod.DecodeError('Unexpected end-group tag.')
- except (IndexError, TypeError):
- # Now ord(buf[p:p+1]) == ord('') gets TypeError.
- raise message_mod.DecodeError('Truncated message.')
- except struct.error as e:
- raise message_mod.DecodeError(e)
- return length # Return this for legacy reasons.
def SerializeToString(self, **kwargs):
- # Check if the message has all of its required fields set.
- if not self.IsInitialized():
- raise message_mod.EncodeError(
- 'Message %s is missing required fields: %s' % (
- self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors())))
- return self.SerializePartialToString(**kwargs)
-
-
-
-def SetInParent(self)
-
-
-
Sets the _cached_byte_size_dirty bit to true,
-and propagates this to our listener iff this was a state change.
-
-
-Expand source code
-
-
def Modified(self):
- """Sets the _cached_byte_size_dirty bit to true,
- and propagates this to our listener iff this was a state change.
- """
-
- # Note: Some callers check _cached_byte_size_dirty before calling
- # _Modified() as an extra optimization. So, if this method is ever
- # changed such that it does stuff even when _cached_byte_size_dirty is
- # already true, the callers need to be updated.
- if not self._cached_byte_size_dirty:
- self._cached_byte_size_dirty = True
- self._listener_for_children.dirty = True
- self._is_present_in_parent = True
- self._listener.Modified()
Returns the name of the currently set field inside a oneof, or None.
-
-
-Expand source code
-
-
def WhichOneof(self, oneof_name):
- """Returns the name of the currently set field inside a oneof, or None."""
- try:
- field = message_descriptor.oneofs_by_name[oneof_name]
- except KeyError:
- raise ValueError(
- 'Protocol message has no oneof "%s" field.' % oneof_name)
-
- nested_field = self._oneofs.get(field, None)
- if nested_field is not None and self.HasField(nested_field.name):
- return nested_field.name
- else:
- return None
-
+
Field ChannelSettings.uplink_enabled
class Data
-(**kwargs)
+(*args, **kwargs)
-
Abstract base class for protocol messages.
-
Protocol message classes are almost always generated by the protocol
-compiler.
-These generated types subclass Message and implement the methods
-shown below.
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field Data.payload
var portnum
-
Getter for portnum.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
-
-
-
Methods
-
-
-def ByteSize(self)
-
-
-
-
-
-Expand source code
-
-
def ByteSize(self):
- if not self._cached_byte_size_dirty:
- return self._cached_byte_size
-
- size = 0
- descriptor = self.DESCRIPTOR
- if descriptor.GetOptions().map_entry:
- # Fields of map entry should always be serialized.
- size = descriptor.fields_by_name['key']._sizer(self.key)
- size += descriptor.fields_by_name['value']._sizer(self.value)
- else:
- for field_descriptor, field_value in self.ListFields():
- size += field_descriptor._sizer(field_value)
- for tag_bytes, value_bytes in self._unknown_fields:
- size += len(tag_bytes) + len(value_bytes)
-
- self._cached_byte_size = size
- self._cached_byte_size_dirty = False
- self._listener_for_children.dirty = False
- return size
def ClearField(self, field_name):
- try:
- field = message_descriptor.fields_by_name[field_name]
- except KeyError:
- try:
- field = message_descriptor.oneofs_by_name[field_name]
- if field in self._oneofs:
- field = self._oneofs[field]
- else:
- return
- except KeyError:
- raise ValueError('Protocol message %s has no "%s" field.' %
- (message_descriptor.name, field_name))
-
- if field in self._fields:
- # To match the C++ implementation, we need to invalidate iterators
- # for map fields when ClearField() happens.
- if hasattr(self._fields[field], 'InvalidateIterators'):
- self._fields[field].InvalidateIterators()
-
- # Note: If the field is a sub-message, its listener will still point
- # at us. That's fine, because the worst than can happen is that it
- # will call _Modified() and invalidate our byte size. Big deal.
- del self._fields[field]
-
- if self._oneofs.get(field.containing_oneof, None) is field:
- del self._oneofs[field.containing_oneof]
-
- # Always call _Modified() -- even if nothing was changed, this is
- # a mutating method, and thus calling it should cause the field to become
- # present in the parent message.
- self._Modified()
-
-
-
-def DiscardUnknownFields(self)
-
-
-
-
-
-Expand source code
-
-
def _DiscardUnknownFields(self):
- self._unknown_fields = []
- self._unknown_field_set = None # pylint: disable=protected-access
- for field, value in self.ListFields():
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- if _IsMapField(field):
- if _IsMessageMapField(field):
- for key in value:
- value[key].DiscardUnknownFields()
- elif field.label == _FieldDescriptor.LABEL_REPEATED:
- for sub_message in value:
- sub_message.DiscardUnknownFields()
- else:
- value.DiscardUnknownFields()
-
-
-
-def FindInitializationErrors(self)
-
-
-
Finds required fields which are not initialized.
-
Returns
-
A list of strings.
-Each string is a path to an uninitialized field from
-the top-level message, e.g. "foo.bar[5].baz".
-
-
-Expand source code
-
-
def FindInitializationErrors(self):
- """Finds required fields which are not initialized.
-
- Returns:
- A list of strings. Each string is a path to an uninitialized field from
- the top-level message, e.g. "foo.bar[5].baz".
- """
-
- errors = [] # simplify things
-
- for field in required_fields:
- if not self.HasField(field.name):
- errors.append(field.name)
-
- for field, value in self.ListFields():
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- if field.is_extension:
- name = '(%s)' % field.full_name
- else:
- name = field.name
-
- if _IsMapField(field):
- if _IsMessageMapField(field):
- for key in value:
- element = value[key]
- prefix = '%s[%s].' % (name, key)
- sub_errors = element.FindInitializationErrors()
- errors += [prefix + error for error in sub_errors]
- else:
- # ScalarMaps can't have any initialization errors.
- pass
- elif field.label == _FieldDescriptor.LABEL_REPEATED:
- for i in range(len(value)):
- element = value[i]
- prefix = '%s[%d].' % (name, i)
- sub_errors = element.FindInitializationErrors()
- errors += [prefix + error for error in sub_errors]
- else:
- prefix = name + '.'
- sub_errors = value.FindInitializationErrors()
- errors += [prefix + error for error in sub_errors]
-
- return errors
-
-
-
-def HasField(self, field_name)
-
-
-
-
-
-Expand source code
-
-
def HasField(self, field_name):
- try:
- field = hassable_fields[field_name]
- except KeyError:
- raise ValueError(error_msg % (message_descriptor.full_name, field_name))
-
- if isinstance(field, descriptor_mod.OneofDescriptor):
- try:
- return HasField(self, self._oneofs[field].name)
- except KeyError:
- return False
- else:
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- value = self._fields.get(field)
- return value is not None and value._is_present_in_parent
- else:
- return field in self._fields
-
-
-
-def IsInitialized(self, errors=None)
-
-
-
Checks if all required fields of a message are set.
-
Args
-
-
errors
-
A list which, if provided, will be populated with the field
-paths of all missing required fields.
-
-
Returns
-
True iff the specified message has all required fields set.
-
-
-Expand source code
-
-
def IsInitialized(self, errors=None):
- """Checks if all required fields of a message are set.
-
- Args:
- errors: A list which, if provided, will be populated with the field
- paths of all missing required fields.
-
- Returns:
- True iff the specified message has all required fields set.
- """
-
- # Performance is critical so we avoid HasField() and ListFields().
-
- for field in required_fields:
- if (field not in self._fields or
- (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and
- not self._fields[field]._is_present_in_parent)):
- if errors is not None:
- errors.extend(self.FindInitializationErrors())
- return False
-
- for field, value in list(self._fields.items()): # dict can change size!
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- if field.label == _FieldDescriptor.LABEL_REPEATED:
- if (field.message_type.has_options and
- field.message_type.GetOptions().map_entry):
- continue
- for element in value:
- if not element.IsInitialized():
- if errors is not None:
- errors.extend(self.FindInitializationErrors())
- return False
- elif value._is_present_in_parent and not value.IsInitialized():
- if errors is not None:
- errors.extend(self.FindInitializationErrors())
- return False
-
- return True
-
-
-
-def ListFields(self)
-
-
-
-
-
-Expand source code
-
-
def ListFields(self):
- all_fields = [item for item in self._fields.items() if _IsPresent(item)]
- all_fields.sort(key = lambda item: item[0].number)
- return all_fields
-
-
-
-def MergeFrom(self, msg)
-
-
-
-
-
-Expand source code
-
-
def MergeFrom(self, msg):
- if not isinstance(msg, cls):
- raise TypeError(
- 'Parameter to MergeFrom() must be instance of same class: '
- 'expected %s got %s.' % (cls.__name__, msg.__class__.__name__))
-
- assert msg is not self
- self._Modified()
-
- fields = self._fields
-
- for field, value in msg._fields.items():
- if field.label == LABEL_REPEATED:
- field_value = fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
- fields[field] = field_value
- field_value.MergeFrom(value)
- elif field.cpp_type == CPPTYPE_MESSAGE:
- if value._is_present_in_parent:
- field_value = fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
- fields[field] = field_value
- field_value.MergeFrom(value)
- else:
- self._fields[field] = value
- if field.containing_oneof:
- self._UpdateOneofState(field)
-
- if msg._unknown_fields:
- if not self._unknown_fields:
- self._unknown_fields = []
- self._unknown_fields.extend(msg._unknown_fields)
- # pylint: disable=protected-access
- if self._unknown_field_set is None:
- self._unknown_field_set = containers.UnknownFieldSet()
- self._unknown_field_set._extend(msg._unknown_field_set)
-
-
-
-def MergeFromString(self, serialized)
-
-
-
-
-
-Expand source code
-
-
def MergeFromString(self, serialized):
- if isinstance(serialized, memoryview) and six.PY2:
- raise TypeError(
- 'memoryview not supported in Python 2 with the pure Python proto '
- 'implementation: this is to maintain compatibility with the C++ '
- 'implementation')
-
- serialized = memoryview(serialized)
- length = len(serialized)
- try:
- if self._InternalParse(serialized, 0, length) != length:
- # The only reason _InternalParse would return early is if it
- # encountered an end-group tag.
- raise message_mod.DecodeError('Unexpected end-group tag.')
- except (IndexError, TypeError):
- # Now ord(buf[p:p+1]) == ord('') gets TypeError.
- raise message_mod.DecodeError('Truncated message.')
- except struct.error as e:
- raise message_mod.DecodeError(e)
- return length # Return this for legacy reasons.
def SerializeToString(self, **kwargs):
- # Check if the message has all of its required fields set.
- if not self.IsInitialized():
- raise message_mod.EncodeError(
- 'Message %s is missing required fields: %s' % (
- self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors())))
- return self.SerializePartialToString(**kwargs)
-
-
-
-def SetInParent(self)
-
-
-
Sets the _cached_byte_size_dirty bit to true,
-and propagates this to our listener iff this was a state change.
-
-
-Expand source code
-
-
def Modified(self):
- """Sets the _cached_byte_size_dirty bit to true,
- and propagates this to our listener iff this was a state change.
- """
-
- # Note: Some callers check _cached_byte_size_dirty before calling
- # _Modified() as an extra optimization. So, if this method is ever
- # changed such that it does stuff even when _cached_byte_size_dirty is
- # already true, the callers need to be updated.
- if not self._cached_byte_size_dirty:
- self._cached_byte_size_dirty = True
- self._listener_for_children.dirty = True
- self._is_present_in_parent = True
- self._listener.Modified()
Returns the name of the currently set field inside a oneof, or None.
-
-
-Expand source code
-
-
def WhichOneof(self, oneof_name):
- """Returns the name of the currently set field inside a oneof, or None."""
- try:
- field = message_descriptor.oneofs_by_name[oneof_name]
- except KeyError:
- raise ValueError(
- 'Protocol message has no oneof "%s" field.' % oneof_name)
-
- nested_field = self._oneofs.get(field, None)
- if nested_field is not None and self.HasField(nested_field.name):
- return nested_field.name
- else:
- return None
-
-
-
-
-
-class DebugString
-(**kwargs)
-
-
-
Abstract base class for protocol messages.
-
Protocol message classes are almost always generated by the protocol
-compiler.
-These generated types subclass Message and implement the methods
-shown below.
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
-
-
-
Methods
-
-
-def ByteSize(self)
-
-
-
-
-
-Expand source code
-
-
def ByteSize(self):
- if not self._cached_byte_size_dirty:
- return self._cached_byte_size
-
- size = 0
- descriptor = self.DESCRIPTOR
- if descriptor.GetOptions().map_entry:
- # Fields of map entry should always be serialized.
- size = descriptor.fields_by_name['key']._sizer(self.key)
- size += descriptor.fields_by_name['value']._sizer(self.value)
- else:
- for field_descriptor, field_value in self.ListFields():
- size += field_descriptor._sizer(field_value)
- for tag_bytes, value_bytes in self._unknown_fields:
- size += len(tag_bytes) + len(value_bytes)
-
- self._cached_byte_size = size
- self._cached_byte_size_dirty = False
- self._listener_for_children.dirty = False
- return size
def ClearField(self, field_name):
- try:
- field = message_descriptor.fields_by_name[field_name]
- except KeyError:
- try:
- field = message_descriptor.oneofs_by_name[field_name]
- if field in self._oneofs:
- field = self._oneofs[field]
- else:
- return
- except KeyError:
- raise ValueError('Protocol message %s has no "%s" field.' %
- (message_descriptor.name, field_name))
-
- if field in self._fields:
- # To match the C++ implementation, we need to invalidate iterators
- # for map fields when ClearField() happens.
- if hasattr(self._fields[field], 'InvalidateIterators'):
- self._fields[field].InvalidateIterators()
-
- # Note: If the field is a sub-message, its listener will still point
- # at us. That's fine, because the worst than can happen is that it
- # will call _Modified() and invalidate our byte size. Big deal.
- del self._fields[field]
-
- if self._oneofs.get(field.containing_oneof, None) is field:
- del self._oneofs[field.containing_oneof]
-
- # Always call _Modified() -- even if nothing was changed, this is
- # a mutating method, and thus calling it should cause the field to become
- # present in the parent message.
- self._Modified()
-
-
-
-def DiscardUnknownFields(self)
-
-
-
-
-
-Expand source code
-
-
def _DiscardUnknownFields(self):
- self._unknown_fields = []
- self._unknown_field_set = None # pylint: disable=protected-access
- for field, value in self.ListFields():
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- if _IsMapField(field):
- if _IsMessageMapField(field):
- for key in value:
- value[key].DiscardUnknownFields()
- elif field.label == _FieldDescriptor.LABEL_REPEATED:
- for sub_message in value:
- sub_message.DiscardUnknownFields()
- else:
- value.DiscardUnknownFields()
-
-
-
-def FindInitializationErrors(self)
-
-
-
Finds required fields which are not initialized.
-
Returns
-
A list of strings.
-Each string is a path to an uninitialized field from
-the top-level message, e.g. "foo.bar[5].baz".
-
-
-Expand source code
-
-
def FindInitializationErrors(self):
- """Finds required fields which are not initialized.
-
- Returns:
- A list of strings. Each string is a path to an uninitialized field from
- the top-level message, e.g. "foo.bar[5].baz".
- """
-
- errors = [] # simplify things
-
- for field in required_fields:
- if not self.HasField(field.name):
- errors.append(field.name)
-
- for field, value in self.ListFields():
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- if field.is_extension:
- name = '(%s)' % field.full_name
- else:
- name = field.name
-
- if _IsMapField(field):
- if _IsMessageMapField(field):
- for key in value:
- element = value[key]
- prefix = '%s[%s].' % (name, key)
- sub_errors = element.FindInitializationErrors()
- errors += [prefix + error for error in sub_errors]
- else:
- # ScalarMaps can't have any initialization errors.
- pass
- elif field.label == _FieldDescriptor.LABEL_REPEATED:
- for i in range(len(value)):
- element = value[i]
- prefix = '%s[%d].' % (name, i)
- sub_errors = element.FindInitializationErrors()
- errors += [prefix + error for error in sub_errors]
- else:
- prefix = name + '.'
- sub_errors = value.FindInitializationErrors()
- errors += [prefix + error for error in sub_errors]
-
- return errors
-
-
-
-def HasField(self, field_name)
-
-
-
-
-
-Expand source code
-
-
def HasField(self, field_name):
- try:
- field = hassable_fields[field_name]
- except KeyError:
- raise ValueError(error_msg % (message_descriptor.full_name, field_name))
-
- if isinstance(field, descriptor_mod.OneofDescriptor):
- try:
- return HasField(self, self._oneofs[field].name)
- except KeyError:
- return False
- else:
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- value = self._fields.get(field)
- return value is not None and value._is_present_in_parent
- else:
- return field in self._fields
-
-
-
-def IsInitialized(self, errors=None)
-
-
-
Checks if all required fields of a message are set.
-
Args
-
-
errors
-
A list which, if provided, will be populated with the field
-paths of all missing required fields.
-
-
Returns
-
True iff the specified message has all required fields set.
-
-
-Expand source code
-
-
def IsInitialized(self, errors=None):
- """Checks if all required fields of a message are set.
-
- Args:
- errors: A list which, if provided, will be populated with the field
- paths of all missing required fields.
-
- Returns:
- True iff the specified message has all required fields set.
- """
-
- # Performance is critical so we avoid HasField() and ListFields().
-
- for field in required_fields:
- if (field not in self._fields or
- (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and
- not self._fields[field]._is_present_in_parent)):
- if errors is not None:
- errors.extend(self.FindInitializationErrors())
- return False
-
- for field, value in list(self._fields.items()): # dict can change size!
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- if field.label == _FieldDescriptor.LABEL_REPEATED:
- if (field.message_type.has_options and
- field.message_type.GetOptions().map_entry):
- continue
- for element in value:
- if not element.IsInitialized():
- if errors is not None:
- errors.extend(self.FindInitializationErrors())
- return False
- elif value._is_present_in_parent and not value.IsInitialized():
- if errors is not None:
- errors.extend(self.FindInitializationErrors())
- return False
-
- return True
-
-
-
-def ListFields(self)
-
-
-
-
-
-Expand source code
-
-
def ListFields(self):
- all_fields = [item for item in self._fields.items() if _IsPresent(item)]
- all_fields.sort(key = lambda item: item[0].number)
- return all_fields
-
-
-
-def MergeFrom(self, msg)
-
-
-
-
-
-Expand source code
-
-
def MergeFrom(self, msg):
- if not isinstance(msg, cls):
- raise TypeError(
- 'Parameter to MergeFrom() must be instance of same class: '
- 'expected %s got %s.' % (cls.__name__, msg.__class__.__name__))
-
- assert msg is not self
- self._Modified()
-
- fields = self._fields
-
- for field, value in msg._fields.items():
- if field.label == LABEL_REPEATED:
- field_value = fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
- fields[field] = field_value
- field_value.MergeFrom(value)
- elif field.cpp_type == CPPTYPE_MESSAGE:
- if value._is_present_in_parent:
- field_value = fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
- fields[field] = field_value
- field_value.MergeFrom(value)
- else:
- self._fields[field] = value
- if field.containing_oneof:
- self._UpdateOneofState(field)
-
- if msg._unknown_fields:
- if not self._unknown_fields:
- self._unknown_fields = []
- self._unknown_fields.extend(msg._unknown_fields)
- # pylint: disable=protected-access
- if self._unknown_field_set is None:
- self._unknown_field_set = containers.UnknownFieldSet()
- self._unknown_field_set._extend(msg._unknown_field_set)
-
-
-
-def MergeFromString(self, serialized)
-
-
-
-
-
-Expand source code
-
-
def MergeFromString(self, serialized):
- if isinstance(serialized, memoryview) and six.PY2:
- raise TypeError(
- 'memoryview not supported in Python 2 with the pure Python proto '
- 'implementation: this is to maintain compatibility with the C++ '
- 'implementation')
-
- serialized = memoryview(serialized)
- length = len(serialized)
- try:
- if self._InternalParse(serialized, 0, length) != length:
- # The only reason _InternalParse would return early is if it
- # encountered an end-group tag.
- raise message_mod.DecodeError('Unexpected end-group tag.')
- except (IndexError, TypeError):
- # Now ord(buf[p:p+1]) == ord('') gets TypeError.
- raise message_mod.DecodeError('Truncated message.')
- except struct.error as e:
- raise message_mod.DecodeError(e)
- return length # Return this for legacy reasons.
def SerializeToString(self, **kwargs):
- # Check if the message has all of its required fields set.
- if not self.IsInitialized():
- raise message_mod.EncodeError(
- 'Message %s is missing required fields: %s' % (
- self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors())))
- return self.SerializePartialToString(**kwargs)
-
-
-
-def SetInParent(self)
-
-
-
Sets the _cached_byte_size_dirty bit to true,
-and propagates this to our listener iff this was a state change.
-
-
-Expand source code
-
-
def Modified(self):
- """Sets the _cached_byte_size_dirty bit to true,
- and propagates this to our listener iff this was a state change.
- """
-
- # Note: Some callers check _cached_byte_size_dirty before calling
- # _Modified() as an extra optimization. So, if this method is ever
- # changed such that it does stuff even when _cached_byte_size_dirty is
- # already true, the callers need to be updated.
- if not self._cached_byte_size_dirty:
- self._cached_byte_size_dirty = True
- self._listener_for_children.dirty = True
- self._is_present_in_parent = True
- self._listener.Modified()
Returns the name of the currently set field inside a oneof, or None.
-
-
-Expand source code
-
-
def WhichOneof(self, oneof_name):
- """Returns the name of the currently set field inside a oneof, or None."""
- try:
- field = message_descriptor.oneofs_by_name[oneof_name]
- except KeyError:
- raise ValueError(
- 'Protocol message has no oneof "%s" field.' % oneof_name)
-
- nested_field = self._oneofs.get(field, None)
- if nested_field is not None and self.HasField(nested_field.name):
- return nested_field.name
- else:
- return None
-
-
-
-
-
-class DeviceState
-(**kwargs)
-
-
-
Abstract base class for protocol messages.
-
Protocol message classes are almost always generated by the protocol
-compiler.
-These generated types subclass Message and implement the methods
-shown below.
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
-
-
var my_node
-
-
Getter for my_node.
-
-
-Expand source code
-
-
def getter(self):
- field_value = self._fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
-
- # Atomically check if another thread has preempted us and, if not, swap
- # in the new object we just created. If someone has preempted us, we
- # take that object and discard ours.
- # WARNING: We are relying on setdefault() being atomic. This is true
- # in CPython but we haven't investigated others. This warning appears
- # in several other locations in this file.
- field_value = self._fields.setdefault(field, field_value)
- return field_value
-
-
-
var no_save
-
-
Getter for no_save.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
-
-
var node_db
-
-
Getter for node_db.
-
-
-Expand source code
-
-
def getter(self):
- field_value = self._fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
-
- # Atomically check if another thread has preempted us and, if not, swap
- # in the new object we just created. If someone has preempted us, we
- # take that object and discard ours.
- # WARNING: We are relying on setdefault() being atomic. This is true
- # in CPython but we haven't investigated others. This warning appears
- # in several other locations in this file.
- field_value = self._fields.setdefault(field, field_value)
- return field_value
-
-
-
var owner
-
-
Getter for owner.
-
-
-Expand source code
-
-
def getter(self):
- field_value = self._fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
-
- # Atomically check if another thread has preempted us and, if not, swap
- # in the new object we just created. If someone has preempted us, we
- # take that object and discard ours.
- # WARNING: We are relying on setdefault() being atomic. This is true
- # in CPython but we haven't investigated others. This warning appears
- # in several other locations in this file.
- field_value = self._fields.setdefault(field, field_value)
- return field_value
-
-
-
var radio
-
-
Getter for radio.
-
-
-Expand source code
-
-
def getter(self):
- field_value = self._fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
-
- # Atomically check if another thread has preempted us and, if not, swap
- # in the new object we just created. If someone has preempted us, we
- # take that object and discard ours.
- # WARNING: We are relying on setdefault() being atomic. This is true
- # in CPython but we haven't investigated others. This warning appears
- # in several other locations in this file.
- field_value = self._fields.setdefault(field, field_value)
- return field_value
-
-
-
var receive_queue
-
-
Getter for receive_queue.
-
-
-Expand source code
-
-
def getter(self):
- field_value = self._fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
-
- # Atomically check if another thread has preempted us and, if not, swap
- # in the new object we just created. If someone has preempted us, we
- # take that object and discard ours.
- # WARNING: We are relying on setdefault() being atomic. This is true
- # in CPython but we haven't investigated others. This warning appears
- # in several other locations in this file.
- field_value = self._fields.setdefault(field, field_value)
- return field_value
-
-
-
var rx_text_message
-
-
Getter for rx_text_message.
-
-
-Expand source code
-
-
def getter(self):
- field_value = self._fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
-
- # Atomically check if another thread has preempted us and, if not, swap
- # in the new object we just created. If someone has preempted us, we
- # take that object and discard ours.
- # WARNING: We are relying on setdefault() being atomic. This is true
- # in CPython but we haven't investigated others. This warning appears
- # in several other locations in this file.
- field_value = self._fields.setdefault(field, field_value)
- return field_value
-
-
-
var secondary_channels
-
-
Getter for secondary_channels.
-
-
-Expand source code
-
-
def getter(self):
- field_value = self._fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
-
- # Atomically check if another thread has preempted us and, if not, swap
- # in the new object we just created. If someone has preempted us, we
- # take that object and discard ours.
- # WARNING: We are relying on setdefault() being atomic. This is true
- # in CPython but we haven't investigated others. This warning appears
- # in several other locations in this file.
- field_value = self._fields.setdefault(field, field_value)
- return field_value
-
-
-
var version
-
-
Getter for version.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
-
-
-
Methods
-
-
-def ByteSize(self)
-
-
-
-
-
-Expand source code
-
-
def ByteSize(self):
- if not self._cached_byte_size_dirty:
- return self._cached_byte_size
-
- size = 0
- descriptor = self.DESCRIPTOR
- if descriptor.GetOptions().map_entry:
- # Fields of map entry should always be serialized.
- size = descriptor.fields_by_name['key']._sizer(self.key)
- size += descriptor.fields_by_name['value']._sizer(self.value)
- else:
- for field_descriptor, field_value in self.ListFields():
- size += field_descriptor._sizer(field_value)
- for tag_bytes, value_bytes in self._unknown_fields:
- size += len(tag_bytes) + len(value_bytes)
-
- self._cached_byte_size = size
- self._cached_byte_size_dirty = False
- self._listener_for_children.dirty = False
- return size
def ClearField(self, field_name):
- try:
- field = message_descriptor.fields_by_name[field_name]
- except KeyError:
- try:
- field = message_descriptor.oneofs_by_name[field_name]
- if field in self._oneofs:
- field = self._oneofs[field]
- else:
- return
- except KeyError:
- raise ValueError('Protocol message %s has no "%s" field.' %
- (message_descriptor.name, field_name))
-
- if field in self._fields:
- # To match the C++ implementation, we need to invalidate iterators
- # for map fields when ClearField() happens.
- if hasattr(self._fields[field], 'InvalidateIterators'):
- self._fields[field].InvalidateIterators()
-
- # Note: If the field is a sub-message, its listener will still point
- # at us. That's fine, because the worst than can happen is that it
- # will call _Modified() and invalidate our byte size. Big deal.
- del self._fields[field]
-
- if self._oneofs.get(field.containing_oneof, None) is field:
- del self._oneofs[field.containing_oneof]
-
- # Always call _Modified() -- even if nothing was changed, this is
- # a mutating method, and thus calling it should cause the field to become
- # present in the parent message.
- self._Modified()
-
-
-
-def DiscardUnknownFields(self)
-
-
-
-
-
-Expand source code
-
-
def _DiscardUnknownFields(self):
- self._unknown_fields = []
- self._unknown_field_set = None # pylint: disable=protected-access
- for field, value in self.ListFields():
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- if _IsMapField(field):
- if _IsMessageMapField(field):
- for key in value:
- value[key].DiscardUnknownFields()
- elif field.label == _FieldDescriptor.LABEL_REPEATED:
- for sub_message in value:
- sub_message.DiscardUnknownFields()
- else:
- value.DiscardUnknownFields()
-
-
-
-def FindInitializationErrors(self)
-
-
-
Finds required fields which are not initialized.
-
Returns
-
A list of strings.
-Each string is a path to an uninitialized field from
-the top-level message, e.g. "foo.bar[5].baz".
-
-
-Expand source code
-
-
def FindInitializationErrors(self):
- """Finds required fields which are not initialized.
-
- Returns:
- A list of strings. Each string is a path to an uninitialized field from
- the top-level message, e.g. "foo.bar[5].baz".
- """
-
- errors = [] # simplify things
-
- for field in required_fields:
- if not self.HasField(field.name):
- errors.append(field.name)
-
- for field, value in self.ListFields():
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- if field.is_extension:
- name = '(%s)' % field.full_name
- else:
- name = field.name
-
- if _IsMapField(field):
- if _IsMessageMapField(field):
- for key in value:
- element = value[key]
- prefix = '%s[%s].' % (name, key)
- sub_errors = element.FindInitializationErrors()
- errors += [prefix + error for error in sub_errors]
- else:
- # ScalarMaps can't have any initialization errors.
- pass
- elif field.label == _FieldDescriptor.LABEL_REPEATED:
- for i in range(len(value)):
- element = value[i]
- prefix = '%s[%d].' % (name, i)
- sub_errors = element.FindInitializationErrors()
- errors += [prefix + error for error in sub_errors]
- else:
- prefix = name + '.'
- sub_errors = value.FindInitializationErrors()
- errors += [prefix + error for error in sub_errors]
-
- return errors
-
-
-
-def HasField(self, field_name)
-
-
-
-
-
-Expand source code
-
-
def HasField(self, field_name):
- try:
- field = hassable_fields[field_name]
- except KeyError:
- raise ValueError(error_msg % (message_descriptor.full_name, field_name))
-
- if isinstance(field, descriptor_mod.OneofDescriptor):
- try:
- return HasField(self, self._oneofs[field].name)
- except KeyError:
- return False
- else:
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- value = self._fields.get(field)
- return value is not None and value._is_present_in_parent
- else:
- return field in self._fields
-
-
-
-def IsInitialized(self, errors=None)
-
-
-
Checks if all required fields of a message are set.
-
Args
-
-
errors
-
A list which, if provided, will be populated with the field
-paths of all missing required fields.
-
-
Returns
-
True iff the specified message has all required fields set.
-
-
-Expand source code
-
-
def IsInitialized(self, errors=None):
- """Checks if all required fields of a message are set.
-
- Args:
- errors: A list which, if provided, will be populated with the field
- paths of all missing required fields.
-
- Returns:
- True iff the specified message has all required fields set.
- """
-
- # Performance is critical so we avoid HasField() and ListFields().
-
- for field in required_fields:
- if (field not in self._fields or
- (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and
- not self._fields[field]._is_present_in_parent)):
- if errors is not None:
- errors.extend(self.FindInitializationErrors())
- return False
-
- for field, value in list(self._fields.items()): # dict can change size!
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- if field.label == _FieldDescriptor.LABEL_REPEATED:
- if (field.message_type.has_options and
- field.message_type.GetOptions().map_entry):
- continue
- for element in value:
- if not element.IsInitialized():
- if errors is not None:
- errors.extend(self.FindInitializationErrors())
- return False
- elif value._is_present_in_parent and not value.IsInitialized():
- if errors is not None:
- errors.extend(self.FindInitializationErrors())
- return False
-
- return True
-
-
-
-def ListFields(self)
-
-
-
-
-
-Expand source code
-
-
def ListFields(self):
- all_fields = [item for item in self._fields.items() if _IsPresent(item)]
- all_fields.sort(key = lambda item: item[0].number)
- return all_fields
-
-
-
-def MergeFrom(self, msg)
-
-
-
-
-
-Expand source code
-
-
def MergeFrom(self, msg):
- if not isinstance(msg, cls):
- raise TypeError(
- 'Parameter to MergeFrom() must be instance of same class: '
- 'expected %s got %s.' % (cls.__name__, msg.__class__.__name__))
-
- assert msg is not self
- self._Modified()
-
- fields = self._fields
-
- for field, value in msg._fields.items():
- if field.label == LABEL_REPEATED:
- field_value = fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
- fields[field] = field_value
- field_value.MergeFrom(value)
- elif field.cpp_type == CPPTYPE_MESSAGE:
- if value._is_present_in_parent:
- field_value = fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
- fields[field] = field_value
- field_value.MergeFrom(value)
- else:
- self._fields[field] = value
- if field.containing_oneof:
- self._UpdateOneofState(field)
-
- if msg._unknown_fields:
- if not self._unknown_fields:
- self._unknown_fields = []
- self._unknown_fields.extend(msg._unknown_fields)
- # pylint: disable=protected-access
- if self._unknown_field_set is None:
- self._unknown_field_set = containers.UnknownFieldSet()
- self._unknown_field_set._extend(msg._unknown_field_set)
-
-
-
-def MergeFromString(self, serialized)
-
-
-
-
-
-Expand source code
-
-
def MergeFromString(self, serialized):
- if isinstance(serialized, memoryview) and six.PY2:
- raise TypeError(
- 'memoryview not supported in Python 2 with the pure Python proto '
- 'implementation: this is to maintain compatibility with the C++ '
- 'implementation')
-
- serialized = memoryview(serialized)
- length = len(serialized)
- try:
- if self._InternalParse(serialized, 0, length) != length:
- # The only reason _InternalParse would return early is if it
- # encountered an end-group tag.
- raise message_mod.DecodeError('Unexpected end-group tag.')
- except (IndexError, TypeError):
- # Now ord(buf[p:p+1]) == ord('') gets TypeError.
- raise message_mod.DecodeError('Truncated message.')
- except struct.error as e:
- raise message_mod.DecodeError(e)
- return length # Return this for legacy reasons.
def SerializeToString(self, **kwargs):
- # Check if the message has all of its required fields set.
- if not self.IsInitialized():
- raise message_mod.EncodeError(
- 'Message %s is missing required fields: %s' % (
- self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors())))
- return self.SerializePartialToString(**kwargs)
-
-
-
-def SetInParent(self)
-
-
-
Sets the _cached_byte_size_dirty bit to true,
-and propagates this to our listener iff this was a state change.
-
-
-Expand source code
-
-
def Modified(self):
- """Sets the _cached_byte_size_dirty bit to true,
- and propagates this to our listener iff this was a state change.
- """
-
- # Note: Some callers check _cached_byte_size_dirty before calling
- # _Modified() as an extra optimization. So, if this method is ever
- # changed such that it does stuff even when _cached_byte_size_dirty is
- # already true, the callers need to be updated.
- if not self._cached_byte_size_dirty:
- self._cached_byte_size_dirty = True
- self._listener_for_children.dirty = True
- self._is_present_in_parent = True
- self._listener.Modified()
Returns the name of the currently set field inside a oneof, or None.
-
-
-Expand source code
-
-
def WhichOneof(self, oneof_name):
- """Returns the name of the currently set field inside a oneof, or None."""
- try:
- field = message_descriptor.oneofs_by_name[oneof_name]
- except KeyError:
- raise ValueError(
- 'Protocol message has no oneof "%s" field.' % oneof_name)
-
- nested_field = self._oneofs.get(field, None)
- if nested_field is not None and self.HasField(nested_field.name):
- return nested_field.name
- else:
- return None
-
+
Field Data.portnum
class FromRadio
-(**kwargs)
+(*args, **kwargs)
-
Abstract base class for protocol messages.
-
Protocol message classes are almost always generated by the protocol
-compiler.
-These generated types subclass Message and implement the methods
-shown below.
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field LogRecord.level
-
var debug_string
+
var message
-
Getter for debug_string.
-
-
-Expand source code
-
-
def getter(self):
- field_value = self._fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
-
- # Atomically check if another thread has preempted us and, if not, swap
- # in the new object we just created. If someone has preempted us, we
- # take that object and discard ours.
- # WARNING: We are relying on setdefault() being atomic. This is true
- # in CPython but we haven't investigated others. This warning appears
- # in several other locations in this file.
- field_value = self._fields.setdefault(field, field_value)
- return field_value
-
+
Field LogRecord.message
-
var my_info
+
var source
-
Getter for my_info.
-
-
-Expand source code
-
-
def getter(self):
- field_value = self._fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
-
- # Atomically check if another thread has preempted us and, if not, swap
- # in the new object we just created. If someone has preempted us, we
- # take that object and discard ours.
- # WARNING: We are relying on setdefault() being atomic. This is true
- # in CPython but we haven't investigated others. This warning appears
- # in several other locations in this file.
- field_value = self._fields.setdefault(field, field_value)
- return field_value
-
+
Field LogRecord.source
-
var node_info
+
var time
-
Getter for node_info.
-
-
-Expand source code
-
-
def getter(self):
- field_value = self._fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
-
- # Atomically check if another thread has preempted us and, if not, swap
- # in the new object we just created. If someone has preempted us, we
- # take that object and discard ours.
- # WARNING: We are relying on setdefault() being atomic. This is true
- # in CPython but we haven't investigated others. This warning appears
- # in several other locations in this file.
- field_value = self._fields.setdefault(field, field_value)
- return field_value
-
-
-
var num
-
-
Getter for num.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
-
-
var packet
-
-
Getter for packet.
-
-
-Expand source code
-
-
def getter(self):
- field_value = self._fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
-
- # Atomically check if another thread has preempted us and, if not, swap
- # in the new object we just created. If someone has preempted us, we
- # take that object and discard ours.
- # WARNING: We are relying on setdefault() being atomic. This is true
- # in CPython but we haven't investigated others. This warning appears
- # in several other locations in this file.
- field_value = self._fields.setdefault(field, field_value)
- return field_value
-
-
-
var radio
-
-
Getter for radio.
-
-
-Expand source code
-
-
def getter(self):
- field_value = self._fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
-
- # Atomically check if another thread has preempted us and, if not, swap
- # in the new object we just created. If someone has preempted us, we
- # take that object and discard ours.
- # WARNING: We are relying on setdefault() being atomic. This is true
- # in CPython but we haven't investigated others. This warning appears
- # in several other locations in this file.
- field_value = self._fields.setdefault(field, field_value)
- return field_value
-
-
-
var rebooted
-
-
Getter for rebooted.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
-
-
var secondary_channel
-
-
Getter for secondary_channel.
-
-
-Expand source code
-
-
def getter(self):
- field_value = self._fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
-
- # Atomically check if another thread has preempted us and, if not, swap
- # in the new object we just created. If someone has preempted us, we
- # take that object and discard ours.
- # WARNING: We are relying on setdefault() being atomic. This is true
- # in CPython but we haven't investigated others. This warning appears
- # in several other locations in this file.
- field_value = self._fields.setdefault(field, field_value)
- return field_value
-
-
-
-
Methods
-
-
-def ByteSize(self)
-
-
-
-
-
-Expand source code
-
-
def ByteSize(self):
- if not self._cached_byte_size_dirty:
- return self._cached_byte_size
-
- size = 0
- descriptor = self.DESCRIPTOR
- if descriptor.GetOptions().map_entry:
- # Fields of map entry should always be serialized.
- size = descriptor.fields_by_name['key']._sizer(self.key)
- size += descriptor.fields_by_name['value']._sizer(self.value)
- else:
- for field_descriptor, field_value in self.ListFields():
- size += field_descriptor._sizer(field_value)
- for tag_bytes, value_bytes in self._unknown_fields:
- size += len(tag_bytes) + len(value_bytes)
-
- self._cached_byte_size = size
- self._cached_byte_size_dirty = False
- self._listener_for_children.dirty = False
- return size
def ClearField(self, field_name):
- try:
- field = message_descriptor.fields_by_name[field_name]
- except KeyError:
- try:
- field = message_descriptor.oneofs_by_name[field_name]
- if field in self._oneofs:
- field = self._oneofs[field]
- else:
- return
- except KeyError:
- raise ValueError('Protocol message %s has no "%s" field.' %
- (message_descriptor.name, field_name))
-
- if field in self._fields:
- # To match the C++ implementation, we need to invalidate iterators
- # for map fields when ClearField() happens.
- if hasattr(self._fields[field], 'InvalidateIterators'):
- self._fields[field].InvalidateIterators()
-
- # Note: If the field is a sub-message, its listener will still point
- # at us. That's fine, because the worst than can happen is that it
- # will call _Modified() and invalidate our byte size. Big deal.
- del self._fields[field]
-
- if self._oneofs.get(field.containing_oneof, None) is field:
- del self._oneofs[field.containing_oneof]
-
- # Always call _Modified() -- even if nothing was changed, this is
- # a mutating method, and thus calling it should cause the field to become
- # present in the parent message.
- self._Modified()
-
-
-
-def DiscardUnknownFields(self)
-
-
-
-
-
-Expand source code
-
-
def _DiscardUnknownFields(self):
- self._unknown_fields = []
- self._unknown_field_set = None # pylint: disable=protected-access
- for field, value in self.ListFields():
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- if _IsMapField(field):
- if _IsMessageMapField(field):
- for key in value:
- value[key].DiscardUnknownFields()
- elif field.label == _FieldDescriptor.LABEL_REPEATED:
- for sub_message in value:
- sub_message.DiscardUnknownFields()
- else:
- value.DiscardUnknownFields()
-
-
-
-def FindInitializationErrors(self)
-
-
-
Finds required fields which are not initialized.
-
Returns
-
A list of strings.
-Each string is a path to an uninitialized field from
-the top-level message, e.g. "foo.bar[5].baz".
-
-
-Expand source code
-
-
def FindInitializationErrors(self):
- """Finds required fields which are not initialized.
-
- Returns:
- A list of strings. Each string is a path to an uninitialized field from
- the top-level message, e.g. "foo.bar[5].baz".
- """
-
- errors = [] # simplify things
-
- for field in required_fields:
- if not self.HasField(field.name):
- errors.append(field.name)
-
- for field, value in self.ListFields():
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- if field.is_extension:
- name = '(%s)' % field.full_name
- else:
- name = field.name
-
- if _IsMapField(field):
- if _IsMessageMapField(field):
- for key in value:
- element = value[key]
- prefix = '%s[%s].' % (name, key)
- sub_errors = element.FindInitializationErrors()
- errors += [prefix + error for error in sub_errors]
- else:
- # ScalarMaps can't have any initialization errors.
- pass
- elif field.label == _FieldDescriptor.LABEL_REPEATED:
- for i in range(len(value)):
- element = value[i]
- prefix = '%s[%d].' % (name, i)
- sub_errors = element.FindInitializationErrors()
- errors += [prefix + error for error in sub_errors]
- else:
- prefix = name + '.'
- sub_errors = value.FindInitializationErrors()
- errors += [prefix + error for error in sub_errors]
-
- return errors
-
-
-
-def HasField(self, field_name)
-
-
-
-
-
-Expand source code
-
-
def HasField(self, field_name):
- try:
- field = hassable_fields[field_name]
- except KeyError:
- raise ValueError(error_msg % (message_descriptor.full_name, field_name))
-
- if isinstance(field, descriptor_mod.OneofDescriptor):
- try:
- return HasField(self, self._oneofs[field].name)
- except KeyError:
- return False
- else:
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- value = self._fields.get(field)
- return value is not None and value._is_present_in_parent
- else:
- return field in self._fields
-
-
-
-def IsInitialized(self, errors=None)
-
-
-
Checks if all required fields of a message are set.
-
Args
-
-
errors
-
A list which, if provided, will be populated with the field
-paths of all missing required fields.
-
-
Returns
-
True iff the specified message has all required fields set.
-
-
-Expand source code
-
-
def IsInitialized(self, errors=None):
- """Checks if all required fields of a message are set.
-
- Args:
- errors: A list which, if provided, will be populated with the field
- paths of all missing required fields.
-
- Returns:
- True iff the specified message has all required fields set.
- """
-
- # Performance is critical so we avoid HasField() and ListFields().
-
- for field in required_fields:
- if (field not in self._fields or
- (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and
- not self._fields[field]._is_present_in_parent)):
- if errors is not None:
- errors.extend(self.FindInitializationErrors())
- return False
-
- for field, value in list(self._fields.items()): # dict can change size!
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- if field.label == _FieldDescriptor.LABEL_REPEATED:
- if (field.message_type.has_options and
- field.message_type.GetOptions().map_entry):
- continue
- for element in value:
- if not element.IsInitialized():
- if errors is not None:
- errors.extend(self.FindInitializationErrors())
- return False
- elif value._is_present_in_parent and not value.IsInitialized():
- if errors is not None:
- errors.extend(self.FindInitializationErrors())
- return False
-
- return True
-
-
-
-def ListFields(self)
-
-
-
-
-
-Expand source code
-
-
def ListFields(self):
- all_fields = [item for item in self._fields.items() if _IsPresent(item)]
- all_fields.sort(key = lambda item: item[0].number)
- return all_fields
-
-
-
-def MergeFrom(self, msg)
-
-
-
-
-
-Expand source code
-
-
def MergeFrom(self, msg):
- if not isinstance(msg, cls):
- raise TypeError(
- 'Parameter to MergeFrom() must be instance of same class: '
- 'expected %s got %s.' % (cls.__name__, msg.__class__.__name__))
-
- assert msg is not self
- self._Modified()
-
- fields = self._fields
-
- for field, value in msg._fields.items():
- if field.label == LABEL_REPEATED:
- field_value = fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
- fields[field] = field_value
- field_value.MergeFrom(value)
- elif field.cpp_type == CPPTYPE_MESSAGE:
- if value._is_present_in_parent:
- field_value = fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
- fields[field] = field_value
- field_value.MergeFrom(value)
- else:
- self._fields[field] = value
- if field.containing_oneof:
- self._UpdateOneofState(field)
-
- if msg._unknown_fields:
- if not self._unknown_fields:
- self._unknown_fields = []
- self._unknown_fields.extend(msg._unknown_fields)
- # pylint: disable=protected-access
- if self._unknown_field_set is None:
- self._unknown_field_set = containers.UnknownFieldSet()
- self._unknown_field_set._extend(msg._unknown_field_set)
-
-
-
-def MergeFromString(self, serialized)
-
-
-
-
-
-Expand source code
-
-
def MergeFromString(self, serialized):
- if isinstance(serialized, memoryview) and six.PY2:
- raise TypeError(
- 'memoryview not supported in Python 2 with the pure Python proto '
- 'implementation: this is to maintain compatibility with the C++ '
- 'implementation')
-
- serialized = memoryview(serialized)
- length = len(serialized)
- try:
- if self._InternalParse(serialized, 0, length) != length:
- # The only reason _InternalParse would return early is if it
- # encountered an end-group tag.
- raise message_mod.DecodeError('Unexpected end-group tag.')
- except (IndexError, TypeError):
- # Now ord(buf[p:p+1]) == ord('') gets TypeError.
- raise message_mod.DecodeError('Truncated message.')
- except struct.error as e:
- raise message_mod.DecodeError(e)
- return length # Return this for legacy reasons.
def SerializeToString(self, **kwargs):
- # Check if the message has all of its required fields set.
- if not self.IsInitialized():
- raise message_mod.EncodeError(
- 'Message %s is missing required fields: %s' % (
- self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors())))
- return self.SerializePartialToString(**kwargs)
-
-
-
-def SetInParent(self)
-
-
-
Sets the _cached_byte_size_dirty bit to true,
-and propagates this to our listener iff this was a state change.
-
-
-Expand source code
-
-
def Modified(self):
- """Sets the _cached_byte_size_dirty bit to true,
- and propagates this to our listener iff this was a state change.
- """
-
- # Note: Some callers check _cached_byte_size_dirty before calling
- # _Modified() as an extra optimization. So, if this method is ever
- # changed such that it does stuff even when _cached_byte_size_dirty is
- # already true, the callers need to be updated.
- if not self._cached_byte_size_dirty:
- self._cached_byte_size_dirty = True
- self._listener_for_children.dirty = True
- self._is_present_in_parent = True
- self._listener.Modified()
Returns the name of the currently set field inside a oneof, or None.
-
-
-Expand source code
-
-
def WhichOneof(self, oneof_name):
- """Returns the name of the currently set field inside a oneof, or None."""
- try:
- field = message_descriptor.oneofs_by_name[oneof_name]
- except KeyError:
- raise ValueError(
- 'Protocol message has no oneof "%s" field.' % oneof_name)
-
- nested_field = self._oneofs.get(field, None)
- if nested_field is not None and self.HasField(nested_field.name):
- return nested_field.name
- else:
- return None
-
+
Field LogRecord.time
class MeshPacket
-(**kwargs)
+(*args, **kwargs)
-
Abstract base class for protocol messages.
-
Protocol message classes are almost always generated by the protocol
-compiler.
-These generated types subclass Message and implement the methods
-shown below.
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field MeshPacket.channel_index
var decoded
-
Getter for decoded.
-
-
-Expand source code
-
-
def getter(self):
- field_value = self._fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
-
- # Atomically check if another thread has preempted us and, if not, swap
- # in the new object we just created. If someone has preempted us, we
- # take that object and discard ours.
- # WARNING: We are relying on setdefault() being atomic. This is true
- # in CPython but we haven't investigated others. This warning appears
- # in several other locations in this file.
- field_value = self._fields.setdefault(field, field_value)
- return field_value
-
+
Field MeshPacket.decoded
var encrypted
-
Getter for encrypted.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field MeshPacket.encrypted
var from
-
Getter for from.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field MeshPacket.from
var hop_limit
-
Getter for hop_limit.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field MeshPacket.hop_limit
var id
-
Getter for id.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field MeshPacket.id
var rx_snr
-
Getter for rx_snr.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field MeshPacket.rx_snr
var rx_time
-
Getter for rx_time.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field MeshPacket.rx_time
var to
-
Getter for to.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field MeshPacket.to
var want_ack
-
Getter for want_ack.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
-
-
-
Methods
-
-
-def ByteSize(self)
-
-
-
-
-
-Expand source code
-
-
def ByteSize(self):
- if not self._cached_byte_size_dirty:
- return self._cached_byte_size
-
- size = 0
- descriptor = self.DESCRIPTOR
- if descriptor.GetOptions().map_entry:
- # Fields of map entry should always be serialized.
- size = descriptor.fields_by_name['key']._sizer(self.key)
- size += descriptor.fields_by_name['value']._sizer(self.value)
- else:
- for field_descriptor, field_value in self.ListFields():
- size += field_descriptor._sizer(field_value)
- for tag_bytes, value_bytes in self._unknown_fields:
- size += len(tag_bytes) + len(value_bytes)
-
- self._cached_byte_size = size
- self._cached_byte_size_dirty = False
- self._listener_for_children.dirty = False
- return size
def ClearField(self, field_name):
- try:
- field = message_descriptor.fields_by_name[field_name]
- except KeyError:
- try:
- field = message_descriptor.oneofs_by_name[field_name]
- if field in self._oneofs:
- field = self._oneofs[field]
- else:
- return
- except KeyError:
- raise ValueError('Protocol message %s has no "%s" field.' %
- (message_descriptor.name, field_name))
-
- if field in self._fields:
- # To match the C++ implementation, we need to invalidate iterators
- # for map fields when ClearField() happens.
- if hasattr(self._fields[field], 'InvalidateIterators'):
- self._fields[field].InvalidateIterators()
-
- # Note: If the field is a sub-message, its listener will still point
- # at us. That's fine, because the worst than can happen is that it
- # will call _Modified() and invalidate our byte size. Big deal.
- del self._fields[field]
-
- if self._oneofs.get(field.containing_oneof, None) is field:
- del self._oneofs[field.containing_oneof]
-
- # Always call _Modified() -- even if nothing was changed, this is
- # a mutating method, and thus calling it should cause the field to become
- # present in the parent message.
- self._Modified()
-
-
-
-def DiscardUnknownFields(self)
-
-
-
-
-
-Expand source code
-
-
def _DiscardUnknownFields(self):
- self._unknown_fields = []
- self._unknown_field_set = None # pylint: disable=protected-access
- for field, value in self.ListFields():
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- if _IsMapField(field):
- if _IsMessageMapField(field):
- for key in value:
- value[key].DiscardUnknownFields()
- elif field.label == _FieldDescriptor.LABEL_REPEATED:
- for sub_message in value:
- sub_message.DiscardUnknownFields()
- else:
- value.DiscardUnknownFields()
-
-
-
-def FindInitializationErrors(self)
-
-
-
Finds required fields which are not initialized.
-
Returns
-
A list of strings.
-Each string is a path to an uninitialized field from
-the top-level message, e.g. "foo.bar[5].baz".
-
-
-Expand source code
-
-
def FindInitializationErrors(self):
- """Finds required fields which are not initialized.
-
- Returns:
- A list of strings. Each string is a path to an uninitialized field from
- the top-level message, e.g. "foo.bar[5].baz".
- """
-
- errors = [] # simplify things
-
- for field in required_fields:
- if not self.HasField(field.name):
- errors.append(field.name)
-
- for field, value in self.ListFields():
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- if field.is_extension:
- name = '(%s)' % field.full_name
- else:
- name = field.name
-
- if _IsMapField(field):
- if _IsMessageMapField(field):
- for key in value:
- element = value[key]
- prefix = '%s[%s].' % (name, key)
- sub_errors = element.FindInitializationErrors()
- errors += [prefix + error for error in sub_errors]
- else:
- # ScalarMaps can't have any initialization errors.
- pass
- elif field.label == _FieldDescriptor.LABEL_REPEATED:
- for i in range(len(value)):
- element = value[i]
- prefix = '%s[%d].' % (name, i)
- sub_errors = element.FindInitializationErrors()
- errors += [prefix + error for error in sub_errors]
- else:
- prefix = name + '.'
- sub_errors = value.FindInitializationErrors()
- errors += [prefix + error for error in sub_errors]
-
- return errors
-
-
-
-def HasField(self, field_name)
-
-
-
-
-
-Expand source code
-
-
def HasField(self, field_name):
- try:
- field = hassable_fields[field_name]
- except KeyError:
- raise ValueError(error_msg % (message_descriptor.full_name, field_name))
-
- if isinstance(field, descriptor_mod.OneofDescriptor):
- try:
- return HasField(self, self._oneofs[field].name)
- except KeyError:
- return False
- else:
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- value = self._fields.get(field)
- return value is not None and value._is_present_in_parent
- else:
- return field in self._fields
-
-
-
-def IsInitialized(self, errors=None)
-
-
-
Checks if all required fields of a message are set.
-
Args
-
-
errors
-
A list which, if provided, will be populated with the field
-paths of all missing required fields.
-
-
Returns
-
True iff the specified message has all required fields set.
-
-
-Expand source code
-
-
def IsInitialized(self, errors=None):
- """Checks if all required fields of a message are set.
-
- Args:
- errors: A list which, if provided, will be populated with the field
- paths of all missing required fields.
-
- Returns:
- True iff the specified message has all required fields set.
- """
-
- # Performance is critical so we avoid HasField() and ListFields().
-
- for field in required_fields:
- if (field not in self._fields or
- (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and
- not self._fields[field]._is_present_in_parent)):
- if errors is not None:
- errors.extend(self.FindInitializationErrors())
- return False
-
- for field, value in list(self._fields.items()): # dict can change size!
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- if field.label == _FieldDescriptor.LABEL_REPEATED:
- if (field.message_type.has_options and
- field.message_type.GetOptions().map_entry):
- continue
- for element in value:
- if not element.IsInitialized():
- if errors is not None:
- errors.extend(self.FindInitializationErrors())
- return False
- elif value._is_present_in_parent and not value.IsInitialized():
- if errors is not None:
- errors.extend(self.FindInitializationErrors())
- return False
-
- return True
-
-
-
-def ListFields(self)
-
-
-
-
-
-Expand source code
-
-
def ListFields(self):
- all_fields = [item for item in self._fields.items() if _IsPresent(item)]
- all_fields.sort(key = lambda item: item[0].number)
- return all_fields
-
-
-
-def MergeFrom(self, msg)
-
-
-
-
-
-Expand source code
-
-
def MergeFrom(self, msg):
- if not isinstance(msg, cls):
- raise TypeError(
- 'Parameter to MergeFrom() must be instance of same class: '
- 'expected %s got %s.' % (cls.__name__, msg.__class__.__name__))
-
- assert msg is not self
- self._Modified()
-
- fields = self._fields
-
- for field, value in msg._fields.items():
- if field.label == LABEL_REPEATED:
- field_value = fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
- fields[field] = field_value
- field_value.MergeFrom(value)
- elif field.cpp_type == CPPTYPE_MESSAGE:
- if value._is_present_in_parent:
- field_value = fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
- fields[field] = field_value
- field_value.MergeFrom(value)
- else:
- self._fields[field] = value
- if field.containing_oneof:
- self._UpdateOneofState(field)
-
- if msg._unknown_fields:
- if not self._unknown_fields:
- self._unknown_fields = []
- self._unknown_fields.extend(msg._unknown_fields)
- # pylint: disable=protected-access
- if self._unknown_field_set is None:
- self._unknown_field_set = containers.UnknownFieldSet()
- self._unknown_field_set._extend(msg._unknown_field_set)
-
-
-
-def MergeFromString(self, serialized)
-
-
-
-
-
-Expand source code
-
-
def MergeFromString(self, serialized):
- if isinstance(serialized, memoryview) and six.PY2:
- raise TypeError(
- 'memoryview not supported in Python 2 with the pure Python proto '
- 'implementation: this is to maintain compatibility with the C++ '
- 'implementation')
-
- serialized = memoryview(serialized)
- length = len(serialized)
- try:
- if self._InternalParse(serialized, 0, length) != length:
- # The only reason _InternalParse would return early is if it
- # encountered an end-group tag.
- raise message_mod.DecodeError('Unexpected end-group tag.')
- except (IndexError, TypeError):
- # Now ord(buf[p:p+1]) == ord('') gets TypeError.
- raise message_mod.DecodeError('Truncated message.')
- except struct.error as e:
- raise message_mod.DecodeError(e)
- return length # Return this for legacy reasons.
def SerializeToString(self, **kwargs):
- # Check if the message has all of its required fields set.
- if not self.IsInitialized():
- raise message_mod.EncodeError(
- 'Message %s is missing required fields: %s' % (
- self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors())))
- return self.SerializePartialToString(**kwargs)
-
-
-
-def SetInParent(self)
-
-
-
Sets the _cached_byte_size_dirty bit to true,
-and propagates this to our listener iff this was a state change.
-
-
-Expand source code
-
-
def Modified(self):
- """Sets the _cached_byte_size_dirty bit to true,
- and propagates this to our listener iff this was a state change.
- """
-
- # Note: Some callers check _cached_byte_size_dirty before calling
- # _Modified() as an extra optimization. So, if this method is ever
- # changed such that it does stuff even when _cached_byte_size_dirty is
- # already true, the callers need to be updated.
- if not self._cached_byte_size_dirty:
- self._cached_byte_size_dirty = True
- self._listener_for_children.dirty = True
- self._is_present_in_parent = True
- self._listener.Modified()
Returns the name of the currently set field inside a oneof, or None.
-
-
-Expand source code
-
-
def WhichOneof(self, oneof_name):
- """Returns the name of the currently set field inside a oneof, or None."""
- try:
- field = message_descriptor.oneofs_by_name[oneof_name]
- except KeyError:
- raise ValueError(
- 'Protocol message has no oneof "%s" field.' % oneof_name)
-
- nested_field = self._oneofs.get(field, None)
- if nested_field is not None and self.HasField(nested_field.name):
- return nested_field.name
- else:
- return None
-
+
Field MeshPacket.want_ack
class MyNodeInfo
-(**kwargs)
+(*args, **kwargs)
-
Abstract base class for protocol messages.
-
Protocol message classes are almost always generated by the protocol
-compiler.
-These generated types subclass Message and implement the methods
-shown below.
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field MyNodeInfo.current_packet_id
var error_address
-
Getter for error_address.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field MyNodeInfo.error_address
var error_code
-
Getter for error_code.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field MyNodeInfo.error_code
var error_count
-
Getter for error_count.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field MyNodeInfo.error_count
var firmware_version
-
Getter for firmware_version.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field MyNodeInfo.firmware_version
var has_gps
-
Getter for has_gps.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field MyNodeInfo.has_gps
var hw_model
-
Getter for hw_model.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field MyNodeInfo.hw_model
var message_timeout_msec
-
Getter for message_timeout_msec.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field MyNodeInfo.message_timeout_msec
var min_app_version
-
Getter for min_app_version.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field MyNodeInfo.min_app_version
var my_node_num
-
Getter for my_node_num.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field MyNodeInfo.my_node_num
var node_num_bits
-
Getter for node_num_bits.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field MyNodeInfo.node_num_bits
var num_channels
-
Getter for num_channels.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field MyNodeInfo.num_channels
var packet_id_bits
-
Getter for packet_id_bits.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field MyNodeInfo.packet_id_bits
var region
-
Getter for region.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
-
-
-
Methods
-
-
-def ByteSize(self)
-
-
-
-
-
-Expand source code
-
-
def ByteSize(self):
- if not self._cached_byte_size_dirty:
- return self._cached_byte_size
-
- size = 0
- descriptor = self.DESCRIPTOR
- if descriptor.GetOptions().map_entry:
- # Fields of map entry should always be serialized.
- size = descriptor.fields_by_name['key']._sizer(self.key)
- size += descriptor.fields_by_name['value']._sizer(self.value)
- else:
- for field_descriptor, field_value in self.ListFields():
- size += field_descriptor._sizer(field_value)
- for tag_bytes, value_bytes in self._unknown_fields:
- size += len(tag_bytes) + len(value_bytes)
-
- self._cached_byte_size = size
- self._cached_byte_size_dirty = False
- self._listener_for_children.dirty = False
- return size
def ClearField(self, field_name):
- try:
- field = message_descriptor.fields_by_name[field_name]
- except KeyError:
- try:
- field = message_descriptor.oneofs_by_name[field_name]
- if field in self._oneofs:
- field = self._oneofs[field]
- else:
- return
- except KeyError:
- raise ValueError('Protocol message %s has no "%s" field.' %
- (message_descriptor.name, field_name))
-
- if field in self._fields:
- # To match the C++ implementation, we need to invalidate iterators
- # for map fields when ClearField() happens.
- if hasattr(self._fields[field], 'InvalidateIterators'):
- self._fields[field].InvalidateIterators()
-
- # Note: If the field is a sub-message, its listener will still point
- # at us. That's fine, because the worst than can happen is that it
- # will call _Modified() and invalidate our byte size. Big deal.
- del self._fields[field]
-
- if self._oneofs.get(field.containing_oneof, None) is field:
- del self._oneofs[field.containing_oneof]
-
- # Always call _Modified() -- even if nothing was changed, this is
- # a mutating method, and thus calling it should cause the field to become
- # present in the parent message.
- self._Modified()
-
-
-
-def DiscardUnknownFields(self)
-
-
-
-
-
-Expand source code
-
-
def _DiscardUnknownFields(self):
- self._unknown_fields = []
- self._unknown_field_set = None # pylint: disable=protected-access
- for field, value in self.ListFields():
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- if _IsMapField(field):
- if _IsMessageMapField(field):
- for key in value:
- value[key].DiscardUnknownFields()
- elif field.label == _FieldDescriptor.LABEL_REPEATED:
- for sub_message in value:
- sub_message.DiscardUnknownFields()
- else:
- value.DiscardUnknownFields()
-
-
-
-def FindInitializationErrors(self)
-
-
-
Finds required fields which are not initialized.
-
Returns
-
A list of strings.
-Each string is a path to an uninitialized field from
-the top-level message, e.g. "foo.bar[5].baz".
-
-
-Expand source code
-
-
def FindInitializationErrors(self):
- """Finds required fields which are not initialized.
-
- Returns:
- A list of strings. Each string is a path to an uninitialized field from
- the top-level message, e.g. "foo.bar[5].baz".
- """
-
- errors = [] # simplify things
-
- for field in required_fields:
- if not self.HasField(field.name):
- errors.append(field.name)
-
- for field, value in self.ListFields():
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- if field.is_extension:
- name = '(%s)' % field.full_name
- else:
- name = field.name
-
- if _IsMapField(field):
- if _IsMessageMapField(field):
- for key in value:
- element = value[key]
- prefix = '%s[%s].' % (name, key)
- sub_errors = element.FindInitializationErrors()
- errors += [prefix + error for error in sub_errors]
- else:
- # ScalarMaps can't have any initialization errors.
- pass
- elif field.label == _FieldDescriptor.LABEL_REPEATED:
- for i in range(len(value)):
- element = value[i]
- prefix = '%s[%d].' % (name, i)
- sub_errors = element.FindInitializationErrors()
- errors += [prefix + error for error in sub_errors]
- else:
- prefix = name + '.'
- sub_errors = value.FindInitializationErrors()
- errors += [prefix + error for error in sub_errors]
-
- return errors
-
-
-
-def HasField(self, field_name)
-
-
-
-
-
-Expand source code
-
-
def HasField(self, field_name):
- try:
- field = hassable_fields[field_name]
- except KeyError:
- raise ValueError(error_msg % (message_descriptor.full_name, field_name))
-
- if isinstance(field, descriptor_mod.OneofDescriptor):
- try:
- return HasField(self, self._oneofs[field].name)
- except KeyError:
- return False
- else:
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- value = self._fields.get(field)
- return value is not None and value._is_present_in_parent
- else:
- return field in self._fields
-
-
-
-def IsInitialized(self, errors=None)
-
-
-
Checks if all required fields of a message are set.
-
Args
-
-
errors
-
A list which, if provided, will be populated with the field
-paths of all missing required fields.
-
-
Returns
-
True iff the specified message has all required fields set.
-
-
-Expand source code
-
-
def IsInitialized(self, errors=None):
- """Checks if all required fields of a message are set.
-
- Args:
- errors: A list which, if provided, will be populated with the field
- paths of all missing required fields.
-
- Returns:
- True iff the specified message has all required fields set.
- """
-
- # Performance is critical so we avoid HasField() and ListFields().
-
- for field in required_fields:
- if (field not in self._fields or
- (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and
- not self._fields[field]._is_present_in_parent)):
- if errors is not None:
- errors.extend(self.FindInitializationErrors())
- return False
-
- for field, value in list(self._fields.items()): # dict can change size!
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- if field.label == _FieldDescriptor.LABEL_REPEATED:
- if (field.message_type.has_options and
- field.message_type.GetOptions().map_entry):
- continue
- for element in value:
- if not element.IsInitialized():
- if errors is not None:
- errors.extend(self.FindInitializationErrors())
- return False
- elif value._is_present_in_parent and not value.IsInitialized():
- if errors is not None:
- errors.extend(self.FindInitializationErrors())
- return False
-
- return True
-
-
-
-def ListFields(self)
-
-
-
-
-
-Expand source code
-
-
def ListFields(self):
- all_fields = [item for item in self._fields.items() if _IsPresent(item)]
- all_fields.sort(key = lambda item: item[0].number)
- return all_fields
-
-
-
-def MergeFrom(self, msg)
-
-
-
-
-
-Expand source code
-
-
def MergeFrom(self, msg):
- if not isinstance(msg, cls):
- raise TypeError(
- 'Parameter to MergeFrom() must be instance of same class: '
- 'expected %s got %s.' % (cls.__name__, msg.__class__.__name__))
-
- assert msg is not self
- self._Modified()
-
- fields = self._fields
-
- for field, value in msg._fields.items():
- if field.label == LABEL_REPEATED:
- field_value = fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
- fields[field] = field_value
- field_value.MergeFrom(value)
- elif field.cpp_type == CPPTYPE_MESSAGE:
- if value._is_present_in_parent:
- field_value = fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
- fields[field] = field_value
- field_value.MergeFrom(value)
- else:
- self._fields[field] = value
- if field.containing_oneof:
- self._UpdateOneofState(field)
-
- if msg._unknown_fields:
- if not self._unknown_fields:
- self._unknown_fields = []
- self._unknown_fields.extend(msg._unknown_fields)
- # pylint: disable=protected-access
- if self._unknown_field_set is None:
- self._unknown_field_set = containers.UnknownFieldSet()
- self._unknown_field_set._extend(msg._unknown_field_set)
-
-
-
-def MergeFromString(self, serialized)
-
-
-
-
-
-Expand source code
-
-
def MergeFromString(self, serialized):
- if isinstance(serialized, memoryview) and six.PY2:
- raise TypeError(
- 'memoryview not supported in Python 2 with the pure Python proto '
- 'implementation: this is to maintain compatibility with the C++ '
- 'implementation')
-
- serialized = memoryview(serialized)
- length = len(serialized)
- try:
- if self._InternalParse(serialized, 0, length) != length:
- # The only reason _InternalParse would return early is if it
- # encountered an end-group tag.
- raise message_mod.DecodeError('Unexpected end-group tag.')
- except (IndexError, TypeError):
- # Now ord(buf[p:p+1]) == ord('') gets TypeError.
- raise message_mod.DecodeError('Truncated message.')
- except struct.error as e:
- raise message_mod.DecodeError(e)
- return length # Return this for legacy reasons.
def SerializeToString(self, **kwargs):
- # Check if the message has all of its required fields set.
- if not self.IsInitialized():
- raise message_mod.EncodeError(
- 'Message %s is missing required fields: %s' % (
- self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors())))
- return self.SerializePartialToString(**kwargs)
-
-
-
-def SetInParent(self)
-
-
-
Sets the _cached_byte_size_dirty bit to true,
-and propagates this to our listener iff this was a state change.
-
-
-Expand source code
-
-
def Modified(self):
- """Sets the _cached_byte_size_dirty bit to true,
- and propagates this to our listener iff this was a state change.
- """
-
- # Note: Some callers check _cached_byte_size_dirty before calling
- # _Modified() as an extra optimization. So, if this method is ever
- # changed such that it does stuff even when _cached_byte_size_dirty is
- # already true, the callers need to be updated.
- if not self._cached_byte_size_dirty:
- self._cached_byte_size_dirty = True
- self._listener_for_children.dirty = True
- self._is_present_in_parent = True
- self._listener.Modified()
Returns the name of the currently set field inside a oneof, or None.
-
-
-Expand source code
-
-
def WhichOneof(self, oneof_name):
- """Returns the name of the currently set field inside a oneof, or None."""
- try:
- field = message_descriptor.oneofs_by_name[oneof_name]
- except KeyError:
- raise ValueError(
- 'Protocol message has no oneof "%s" field.' % oneof_name)
-
- nested_field = self._oneofs.get(field, None)
- if nested_field is not None and self.HasField(nested_field.name):
- return nested_field.name
- else:
- return None
-
+
Field MyNodeInfo.region
class NodeInfo
-(**kwargs)
+(*args, **kwargs)
-
Abstract base class for protocol messages.
-
Protocol message classes are almost always generated by the protocol
-compiler.
-These generated types subclass Message and implement the methods
-shown below.
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field NodeInfo.next_hop
var num
-
Getter for num.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field NodeInfo.num
var position
-
Getter for position.
-
-
-Expand source code
-
-
def getter(self):
- field_value = self._fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
-
- # Atomically check if another thread has preempted us and, if not, swap
- # in the new object we just created. If someone has preempted us, we
- # take that object and discard ours.
- # WARNING: We are relying on setdefault() being atomic. This is true
- # in CPython but we haven't investigated others. This warning appears
- # in several other locations in this file.
- field_value = self._fields.setdefault(field, field_value)
- return field_value
-
+
Field NodeInfo.position
var snr
-
Getter for snr.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field NodeInfo.snr
var user
-
Getter for user.
-
-
-Expand source code
-
-
def getter(self):
- field_value = self._fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
-
- # Atomically check if another thread has preempted us and, if not, swap
- # in the new object we just created. If someone has preempted us, we
- # take that object and discard ours.
- # WARNING: We are relying on setdefault() being atomic. This is true
- # in CPython but we haven't investigated others. This warning appears
- # in several other locations in this file.
- field_value = self._fields.setdefault(field, field_value)
- return field_value
-
-
-
-
Methods
-
-
-def ByteSize(self)
-
-
-
-
-
-Expand source code
-
-
def ByteSize(self):
- if not self._cached_byte_size_dirty:
- return self._cached_byte_size
-
- size = 0
- descriptor = self.DESCRIPTOR
- if descriptor.GetOptions().map_entry:
- # Fields of map entry should always be serialized.
- size = descriptor.fields_by_name['key']._sizer(self.key)
- size += descriptor.fields_by_name['value']._sizer(self.value)
- else:
- for field_descriptor, field_value in self.ListFields():
- size += field_descriptor._sizer(field_value)
- for tag_bytes, value_bytes in self._unknown_fields:
- size += len(tag_bytes) + len(value_bytes)
-
- self._cached_byte_size = size
- self._cached_byte_size_dirty = False
- self._listener_for_children.dirty = False
- return size
def ClearField(self, field_name):
- try:
- field = message_descriptor.fields_by_name[field_name]
- except KeyError:
- try:
- field = message_descriptor.oneofs_by_name[field_name]
- if field in self._oneofs:
- field = self._oneofs[field]
- else:
- return
- except KeyError:
- raise ValueError('Protocol message %s has no "%s" field.' %
- (message_descriptor.name, field_name))
-
- if field in self._fields:
- # To match the C++ implementation, we need to invalidate iterators
- # for map fields when ClearField() happens.
- if hasattr(self._fields[field], 'InvalidateIterators'):
- self._fields[field].InvalidateIterators()
-
- # Note: If the field is a sub-message, its listener will still point
- # at us. That's fine, because the worst than can happen is that it
- # will call _Modified() and invalidate our byte size. Big deal.
- del self._fields[field]
-
- if self._oneofs.get(field.containing_oneof, None) is field:
- del self._oneofs[field.containing_oneof]
-
- # Always call _Modified() -- even if nothing was changed, this is
- # a mutating method, and thus calling it should cause the field to become
- # present in the parent message.
- self._Modified()
-
-
-
-def DiscardUnknownFields(self)
-
-
-
-
-
-Expand source code
-
-
def _DiscardUnknownFields(self):
- self._unknown_fields = []
- self._unknown_field_set = None # pylint: disable=protected-access
- for field, value in self.ListFields():
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- if _IsMapField(field):
- if _IsMessageMapField(field):
- for key in value:
- value[key].DiscardUnknownFields()
- elif field.label == _FieldDescriptor.LABEL_REPEATED:
- for sub_message in value:
- sub_message.DiscardUnknownFields()
- else:
- value.DiscardUnknownFields()
-
-
-
-def FindInitializationErrors(self)
-
-
-
Finds required fields which are not initialized.
-
Returns
-
A list of strings.
-Each string is a path to an uninitialized field from
-the top-level message, e.g. "foo.bar[5].baz".
-
-
-Expand source code
-
-
def FindInitializationErrors(self):
- """Finds required fields which are not initialized.
-
- Returns:
- A list of strings. Each string is a path to an uninitialized field from
- the top-level message, e.g. "foo.bar[5].baz".
- """
-
- errors = [] # simplify things
-
- for field in required_fields:
- if not self.HasField(field.name):
- errors.append(field.name)
-
- for field, value in self.ListFields():
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- if field.is_extension:
- name = '(%s)' % field.full_name
- else:
- name = field.name
-
- if _IsMapField(field):
- if _IsMessageMapField(field):
- for key in value:
- element = value[key]
- prefix = '%s[%s].' % (name, key)
- sub_errors = element.FindInitializationErrors()
- errors += [prefix + error for error in sub_errors]
- else:
- # ScalarMaps can't have any initialization errors.
- pass
- elif field.label == _FieldDescriptor.LABEL_REPEATED:
- for i in range(len(value)):
- element = value[i]
- prefix = '%s[%d].' % (name, i)
- sub_errors = element.FindInitializationErrors()
- errors += [prefix + error for error in sub_errors]
- else:
- prefix = name + '.'
- sub_errors = value.FindInitializationErrors()
- errors += [prefix + error for error in sub_errors]
-
- return errors
-
-
-
-def HasField(self, field_name)
-
-
-
-
-
-Expand source code
-
-
def HasField(self, field_name):
- try:
- field = hassable_fields[field_name]
- except KeyError:
- raise ValueError(error_msg % (message_descriptor.full_name, field_name))
-
- if isinstance(field, descriptor_mod.OneofDescriptor):
- try:
- return HasField(self, self._oneofs[field].name)
- except KeyError:
- return False
- else:
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- value = self._fields.get(field)
- return value is not None and value._is_present_in_parent
- else:
- return field in self._fields
-
-
-
-def IsInitialized(self, errors=None)
-
-
-
Checks if all required fields of a message are set.
-
Args
-
-
errors
-
A list which, if provided, will be populated with the field
-paths of all missing required fields.
-
-
Returns
-
True iff the specified message has all required fields set.
-
-
-Expand source code
-
-
def IsInitialized(self, errors=None):
- """Checks if all required fields of a message are set.
-
- Args:
- errors: A list which, if provided, will be populated with the field
- paths of all missing required fields.
-
- Returns:
- True iff the specified message has all required fields set.
- """
-
- # Performance is critical so we avoid HasField() and ListFields().
-
- for field in required_fields:
- if (field not in self._fields or
- (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and
- not self._fields[field]._is_present_in_parent)):
- if errors is not None:
- errors.extend(self.FindInitializationErrors())
- return False
-
- for field, value in list(self._fields.items()): # dict can change size!
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- if field.label == _FieldDescriptor.LABEL_REPEATED:
- if (field.message_type.has_options and
- field.message_type.GetOptions().map_entry):
- continue
- for element in value:
- if not element.IsInitialized():
- if errors is not None:
- errors.extend(self.FindInitializationErrors())
- return False
- elif value._is_present_in_parent and not value.IsInitialized():
- if errors is not None:
- errors.extend(self.FindInitializationErrors())
- return False
-
- return True
-
-
-
-def ListFields(self)
-
-
-
-
-
-Expand source code
-
-
def ListFields(self):
- all_fields = [item for item in self._fields.items() if _IsPresent(item)]
- all_fields.sort(key = lambda item: item[0].number)
- return all_fields
-
-
-
-def MergeFrom(self, msg)
-
-
-
-
-
-Expand source code
-
-
def MergeFrom(self, msg):
- if not isinstance(msg, cls):
- raise TypeError(
- 'Parameter to MergeFrom() must be instance of same class: '
- 'expected %s got %s.' % (cls.__name__, msg.__class__.__name__))
-
- assert msg is not self
- self._Modified()
-
- fields = self._fields
-
- for field, value in msg._fields.items():
- if field.label == LABEL_REPEATED:
- field_value = fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
- fields[field] = field_value
- field_value.MergeFrom(value)
- elif field.cpp_type == CPPTYPE_MESSAGE:
- if value._is_present_in_parent:
- field_value = fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
- fields[field] = field_value
- field_value.MergeFrom(value)
- else:
- self._fields[field] = value
- if field.containing_oneof:
- self._UpdateOneofState(field)
-
- if msg._unknown_fields:
- if not self._unknown_fields:
- self._unknown_fields = []
- self._unknown_fields.extend(msg._unknown_fields)
- # pylint: disable=protected-access
- if self._unknown_field_set is None:
- self._unknown_field_set = containers.UnknownFieldSet()
- self._unknown_field_set._extend(msg._unknown_field_set)
-
-
-
-def MergeFromString(self, serialized)
-
-
-
-
-
-Expand source code
-
-
def MergeFromString(self, serialized):
- if isinstance(serialized, memoryview) and six.PY2:
- raise TypeError(
- 'memoryview not supported in Python 2 with the pure Python proto '
- 'implementation: this is to maintain compatibility with the C++ '
- 'implementation')
-
- serialized = memoryview(serialized)
- length = len(serialized)
- try:
- if self._InternalParse(serialized, 0, length) != length:
- # The only reason _InternalParse would return early is if it
- # encountered an end-group tag.
- raise message_mod.DecodeError('Unexpected end-group tag.')
- except (IndexError, TypeError):
- # Now ord(buf[p:p+1]) == ord('') gets TypeError.
- raise message_mod.DecodeError('Truncated message.')
- except struct.error as e:
- raise message_mod.DecodeError(e)
- return length # Return this for legacy reasons.
def SerializeToString(self, **kwargs):
- # Check if the message has all of its required fields set.
- if not self.IsInitialized():
- raise message_mod.EncodeError(
- 'Message %s is missing required fields: %s' % (
- self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors())))
- return self.SerializePartialToString(**kwargs)
-
-
-
-def SetInParent(self)
-
-
-
Sets the _cached_byte_size_dirty bit to true,
-and propagates this to our listener iff this was a state change.
-
-
-Expand source code
-
-
def Modified(self):
- """Sets the _cached_byte_size_dirty bit to true,
- and propagates this to our listener iff this was a state change.
- """
-
- # Note: Some callers check _cached_byte_size_dirty before calling
- # _Modified() as an extra optimization. So, if this method is ever
- # changed such that it does stuff even when _cached_byte_size_dirty is
- # already true, the callers need to be updated.
- if not self._cached_byte_size_dirty:
- self._cached_byte_size_dirty = True
- self._listener_for_children.dirty = True
- self._is_present_in_parent = True
- self._listener.Modified()
Returns the name of the currently set field inside a oneof, or None.
-
-
-Expand source code
-
-
def WhichOneof(self, oneof_name):
- """Returns the name of the currently set field inside a oneof, or None."""
- try:
- field = message_descriptor.oneofs_by_name[oneof_name]
- except KeyError:
- raise ValueError(
- 'Protocol message has no oneof "%s" field.' % oneof_name)
-
- nested_field = self._oneofs.get(field, None)
- if nested_field is not None and self.HasField(nested_field.name):
- return nested_field.name
- else:
- return None
-
+
Field NodeInfo.user
class Position
-(**kwargs)
+(*args, **kwargs)
-
Abstract base class for protocol messages.
-
Protocol message classes are almost always generated by the protocol
-compiler.
-These generated types subclass Message and implement the methods
-shown below.
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field Position.altitude
var battery_level
-
Getter for battery_level.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field Position.battery_level
var latitude_i
-
Getter for latitude_i.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field Position.latitude_i
var longitude_i
-
Getter for longitude_i.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field Position.longitude_i
var time
-
Getter for time.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
-
-
-
Methods
-
-
-def ByteSize(self)
-
-
-
-
-
-Expand source code
-
-
def ByteSize(self):
- if not self._cached_byte_size_dirty:
- return self._cached_byte_size
-
- size = 0
- descriptor = self.DESCRIPTOR
- if descriptor.GetOptions().map_entry:
- # Fields of map entry should always be serialized.
- size = descriptor.fields_by_name['key']._sizer(self.key)
- size += descriptor.fields_by_name['value']._sizer(self.value)
- else:
- for field_descriptor, field_value in self.ListFields():
- size += field_descriptor._sizer(field_value)
- for tag_bytes, value_bytes in self._unknown_fields:
- size += len(tag_bytes) + len(value_bytes)
-
- self._cached_byte_size = size
- self._cached_byte_size_dirty = False
- self._listener_for_children.dirty = False
- return size
def ClearField(self, field_name):
- try:
- field = message_descriptor.fields_by_name[field_name]
- except KeyError:
- try:
- field = message_descriptor.oneofs_by_name[field_name]
- if field in self._oneofs:
- field = self._oneofs[field]
- else:
- return
- except KeyError:
- raise ValueError('Protocol message %s has no "%s" field.' %
- (message_descriptor.name, field_name))
-
- if field in self._fields:
- # To match the C++ implementation, we need to invalidate iterators
- # for map fields when ClearField() happens.
- if hasattr(self._fields[field], 'InvalidateIterators'):
- self._fields[field].InvalidateIterators()
-
- # Note: If the field is a sub-message, its listener will still point
- # at us. That's fine, because the worst than can happen is that it
- # will call _Modified() and invalidate our byte size. Big deal.
- del self._fields[field]
-
- if self._oneofs.get(field.containing_oneof, None) is field:
- del self._oneofs[field.containing_oneof]
-
- # Always call _Modified() -- even if nothing was changed, this is
- # a mutating method, and thus calling it should cause the field to become
- # present in the parent message.
- self._Modified()
-
-
-
-def DiscardUnknownFields(self)
-
-
-
-
-
-Expand source code
-
-
def _DiscardUnknownFields(self):
- self._unknown_fields = []
- self._unknown_field_set = None # pylint: disable=protected-access
- for field, value in self.ListFields():
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- if _IsMapField(field):
- if _IsMessageMapField(field):
- for key in value:
- value[key].DiscardUnknownFields()
- elif field.label == _FieldDescriptor.LABEL_REPEATED:
- for sub_message in value:
- sub_message.DiscardUnknownFields()
- else:
- value.DiscardUnknownFields()
-
-
-
-def FindInitializationErrors(self)
-
-
-
Finds required fields which are not initialized.
-
Returns
-
A list of strings.
-Each string is a path to an uninitialized field from
-the top-level message, e.g. "foo.bar[5].baz".
-
-
-Expand source code
-
-
def FindInitializationErrors(self):
- """Finds required fields which are not initialized.
-
- Returns:
- A list of strings. Each string is a path to an uninitialized field from
- the top-level message, e.g. "foo.bar[5].baz".
- """
-
- errors = [] # simplify things
-
- for field in required_fields:
- if not self.HasField(field.name):
- errors.append(field.name)
-
- for field, value in self.ListFields():
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- if field.is_extension:
- name = '(%s)' % field.full_name
- else:
- name = field.name
-
- if _IsMapField(field):
- if _IsMessageMapField(field):
- for key in value:
- element = value[key]
- prefix = '%s[%s].' % (name, key)
- sub_errors = element.FindInitializationErrors()
- errors += [prefix + error for error in sub_errors]
- else:
- # ScalarMaps can't have any initialization errors.
- pass
- elif field.label == _FieldDescriptor.LABEL_REPEATED:
- for i in range(len(value)):
- element = value[i]
- prefix = '%s[%d].' % (name, i)
- sub_errors = element.FindInitializationErrors()
- errors += [prefix + error for error in sub_errors]
- else:
- prefix = name + '.'
- sub_errors = value.FindInitializationErrors()
- errors += [prefix + error for error in sub_errors]
-
- return errors
-
-
-
-def HasField(self, field_name)
-
-
-
-
-
-Expand source code
-
-
def HasField(self, field_name):
- try:
- field = hassable_fields[field_name]
- except KeyError:
- raise ValueError(error_msg % (message_descriptor.full_name, field_name))
-
- if isinstance(field, descriptor_mod.OneofDescriptor):
- try:
- return HasField(self, self._oneofs[field].name)
- except KeyError:
- return False
- else:
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- value = self._fields.get(field)
- return value is not None and value._is_present_in_parent
- else:
- return field in self._fields
-
-
-
-def IsInitialized(self, errors=None)
-
-
-
Checks if all required fields of a message are set.
-
Args
-
-
errors
-
A list which, if provided, will be populated with the field
-paths of all missing required fields.
-
-
Returns
-
True iff the specified message has all required fields set.
-
-
-Expand source code
-
-
def IsInitialized(self, errors=None):
- """Checks if all required fields of a message are set.
-
- Args:
- errors: A list which, if provided, will be populated with the field
- paths of all missing required fields.
-
- Returns:
- True iff the specified message has all required fields set.
- """
-
- # Performance is critical so we avoid HasField() and ListFields().
-
- for field in required_fields:
- if (field not in self._fields or
- (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and
- not self._fields[field]._is_present_in_parent)):
- if errors is not None:
- errors.extend(self.FindInitializationErrors())
- return False
-
- for field, value in list(self._fields.items()): # dict can change size!
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- if field.label == _FieldDescriptor.LABEL_REPEATED:
- if (field.message_type.has_options and
- field.message_type.GetOptions().map_entry):
- continue
- for element in value:
- if not element.IsInitialized():
- if errors is not None:
- errors.extend(self.FindInitializationErrors())
- return False
- elif value._is_present_in_parent and not value.IsInitialized():
- if errors is not None:
- errors.extend(self.FindInitializationErrors())
- return False
-
- return True
-
-
-
-def ListFields(self)
-
-
-
-
-
-Expand source code
-
-
def ListFields(self):
- all_fields = [item for item in self._fields.items() if _IsPresent(item)]
- all_fields.sort(key = lambda item: item[0].number)
- return all_fields
-
-
-
-def MergeFrom(self, msg)
-
-
-
-
-
-Expand source code
-
-
def MergeFrom(self, msg):
- if not isinstance(msg, cls):
- raise TypeError(
- 'Parameter to MergeFrom() must be instance of same class: '
- 'expected %s got %s.' % (cls.__name__, msg.__class__.__name__))
-
- assert msg is not self
- self._Modified()
-
- fields = self._fields
-
- for field, value in msg._fields.items():
- if field.label == LABEL_REPEATED:
- field_value = fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
- fields[field] = field_value
- field_value.MergeFrom(value)
- elif field.cpp_type == CPPTYPE_MESSAGE:
- if value._is_present_in_parent:
- field_value = fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
- fields[field] = field_value
- field_value.MergeFrom(value)
- else:
- self._fields[field] = value
- if field.containing_oneof:
- self._UpdateOneofState(field)
-
- if msg._unknown_fields:
- if not self._unknown_fields:
- self._unknown_fields = []
- self._unknown_fields.extend(msg._unknown_fields)
- # pylint: disable=protected-access
- if self._unknown_field_set is None:
- self._unknown_field_set = containers.UnknownFieldSet()
- self._unknown_field_set._extend(msg._unknown_field_set)
-
-
-
-def MergeFromString(self, serialized)
-
-
-
-
-
-Expand source code
-
-
def MergeFromString(self, serialized):
- if isinstance(serialized, memoryview) and six.PY2:
- raise TypeError(
- 'memoryview not supported in Python 2 with the pure Python proto '
- 'implementation: this is to maintain compatibility with the C++ '
- 'implementation')
-
- serialized = memoryview(serialized)
- length = len(serialized)
- try:
- if self._InternalParse(serialized, 0, length) != length:
- # The only reason _InternalParse would return early is if it
- # encountered an end-group tag.
- raise message_mod.DecodeError('Unexpected end-group tag.')
- except (IndexError, TypeError):
- # Now ord(buf[p:p+1]) == ord('') gets TypeError.
- raise message_mod.DecodeError('Truncated message.')
- except struct.error as e:
- raise message_mod.DecodeError(e)
- return length # Return this for legacy reasons.
def SerializeToString(self, **kwargs):
- # Check if the message has all of its required fields set.
- if not self.IsInitialized():
- raise message_mod.EncodeError(
- 'Message %s is missing required fields: %s' % (
- self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors())))
- return self.SerializePartialToString(**kwargs)
-
-
-
-def SetInParent(self)
-
-
-
Sets the _cached_byte_size_dirty bit to true,
-and propagates this to our listener iff this was a state change.
-
-
-Expand source code
-
-
def Modified(self):
- """Sets the _cached_byte_size_dirty bit to true,
- and propagates this to our listener iff this was a state change.
- """
-
- # Note: Some callers check _cached_byte_size_dirty before calling
- # _Modified() as an extra optimization. So, if this method is ever
- # changed such that it does stuff even when _cached_byte_size_dirty is
- # already true, the callers need to be updated.
- if not self._cached_byte_size_dirty:
- self._cached_byte_size_dirty = True
- self._listener_for_children.dirty = True
- self._is_present_in_parent = True
- self._listener.Modified()
Returns the name of the currently set field inside a oneof, or None.
-
-
-Expand source code
-
-
def WhichOneof(self, oneof_name):
- """Returns the name of the currently set field inside a oneof, or None."""
- try:
- field = message_descriptor.oneofs_by_name[oneof_name]
- except KeyError:
- raise ValueError(
- 'Protocol message has no oneof "%s" field.' % oneof_name)
-
- nested_field = self._oneofs.get(field, None)
- if nested_field is not None and self.HasField(nested_field.name):
- return nested_field.name
- else:
- return None
-
+
Field Position.time
class RadioConfig
-(**kwargs)
+(*args, **kwargs)
-
Abstract base class for protocol messages.
-
Protocol message classes are almost always generated by the protocol
-compiler.
-These generated types subclass Message and implement the methods
-shown below.
+
A ProtocolMessage
Ancestors
+
google.protobuf.pyext._message.CMessage
google.protobuf.message.Message
Class variables
-
var CHANNEL_SETTINGS_FIELD_NUMBER
-
-
-
var DESCRIPTOR
-
var PREFERENCES_FIELD_NUMBER
-
-
-
var UserPreferences
-
Abstract base class for protocol messages.
-
Protocol message classes are almost always generated by the protocol
-compiler.
-These generated types subclass Message and implement the methods
-shown below.
def getter(self):
- field_value = self._fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
-
- # Atomically check if another thread has preempted us and, if not, swap
- # in the new object we just created. If someone has preempted us, we
- # take that object and discard ours.
- # WARNING: We are relying on setdefault() being atomic. This is true
- # in CPython but we haven't investigated others. This warning appears
- # in several other locations in this file.
- field_value = self._fields.setdefault(field, field_value)
- return field_value
-
+
Field RadioConfig.channel_settings
var preferences
-
Getter for preferences.
-
-
-Expand source code
-
-
def getter(self):
- field_value = self._fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
-
- # Atomically check if another thread has preempted us and, if not, swap
- # in the new object we just created. If someone has preempted us, we
- # take that object and discard ours.
- # WARNING: We are relying on setdefault() being atomic. This is true
- # in CPython but we haven't investigated others. This warning appears
- # in several other locations in this file.
- field_value = self._fields.setdefault(field, field_value)
- return field_value
-
-
-
-
Methods
-
-
-def ByteSize(self)
-
-
-
-
-
-Expand source code
-
-
def ByteSize(self):
- if not self._cached_byte_size_dirty:
- return self._cached_byte_size
-
- size = 0
- descriptor = self.DESCRIPTOR
- if descriptor.GetOptions().map_entry:
- # Fields of map entry should always be serialized.
- size = descriptor.fields_by_name['key']._sizer(self.key)
- size += descriptor.fields_by_name['value']._sizer(self.value)
- else:
- for field_descriptor, field_value in self.ListFields():
- size += field_descriptor._sizer(field_value)
- for tag_bytes, value_bytes in self._unknown_fields:
- size += len(tag_bytes) + len(value_bytes)
-
- self._cached_byte_size = size
- self._cached_byte_size_dirty = False
- self._listener_for_children.dirty = False
- return size
def ClearField(self, field_name):
- try:
- field = message_descriptor.fields_by_name[field_name]
- except KeyError:
- try:
- field = message_descriptor.oneofs_by_name[field_name]
- if field in self._oneofs:
- field = self._oneofs[field]
- else:
- return
- except KeyError:
- raise ValueError('Protocol message %s has no "%s" field.' %
- (message_descriptor.name, field_name))
-
- if field in self._fields:
- # To match the C++ implementation, we need to invalidate iterators
- # for map fields when ClearField() happens.
- if hasattr(self._fields[field], 'InvalidateIterators'):
- self._fields[field].InvalidateIterators()
-
- # Note: If the field is a sub-message, its listener will still point
- # at us. That's fine, because the worst than can happen is that it
- # will call _Modified() and invalidate our byte size. Big deal.
- del self._fields[field]
-
- if self._oneofs.get(field.containing_oneof, None) is field:
- del self._oneofs[field.containing_oneof]
-
- # Always call _Modified() -- even if nothing was changed, this is
- # a mutating method, and thus calling it should cause the field to become
- # present in the parent message.
- self._Modified()
-
-
-
-def DiscardUnknownFields(self)
-
-
-
-
-
-Expand source code
-
-
def _DiscardUnknownFields(self):
- self._unknown_fields = []
- self._unknown_field_set = None # pylint: disable=protected-access
- for field, value in self.ListFields():
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- if _IsMapField(field):
- if _IsMessageMapField(field):
- for key in value:
- value[key].DiscardUnknownFields()
- elif field.label == _FieldDescriptor.LABEL_REPEATED:
- for sub_message in value:
- sub_message.DiscardUnknownFields()
- else:
- value.DiscardUnknownFields()
-
-
-
-def FindInitializationErrors(self)
-
-
-
Finds required fields which are not initialized.
-
Returns
-
A list of strings.
-Each string is a path to an uninitialized field from
-the top-level message, e.g. "foo.bar[5].baz".
-
-
-Expand source code
-
-
def FindInitializationErrors(self):
- """Finds required fields which are not initialized.
-
- Returns:
- A list of strings. Each string is a path to an uninitialized field from
- the top-level message, e.g. "foo.bar[5].baz".
- """
-
- errors = [] # simplify things
-
- for field in required_fields:
- if not self.HasField(field.name):
- errors.append(field.name)
-
- for field, value in self.ListFields():
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- if field.is_extension:
- name = '(%s)' % field.full_name
- else:
- name = field.name
-
- if _IsMapField(field):
- if _IsMessageMapField(field):
- for key in value:
- element = value[key]
- prefix = '%s[%s].' % (name, key)
- sub_errors = element.FindInitializationErrors()
- errors += [prefix + error for error in sub_errors]
- else:
- # ScalarMaps can't have any initialization errors.
- pass
- elif field.label == _FieldDescriptor.LABEL_REPEATED:
- for i in range(len(value)):
- element = value[i]
- prefix = '%s[%d].' % (name, i)
- sub_errors = element.FindInitializationErrors()
- errors += [prefix + error for error in sub_errors]
- else:
- prefix = name + '.'
- sub_errors = value.FindInitializationErrors()
- errors += [prefix + error for error in sub_errors]
-
- return errors
-
-
-
-def HasField(self, field_name)
-
-
-
-
-
-Expand source code
-
-
def HasField(self, field_name):
- try:
- field = hassable_fields[field_name]
- except KeyError:
- raise ValueError(error_msg % (message_descriptor.full_name, field_name))
-
- if isinstance(field, descriptor_mod.OneofDescriptor):
- try:
- return HasField(self, self._oneofs[field].name)
- except KeyError:
- return False
- else:
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- value = self._fields.get(field)
- return value is not None and value._is_present_in_parent
- else:
- return field in self._fields
-
-
-
-def IsInitialized(self, errors=None)
-
-
-
Checks if all required fields of a message are set.
-
Args
-
-
errors
-
A list which, if provided, will be populated with the field
-paths of all missing required fields.
-
-
Returns
-
True iff the specified message has all required fields set.
-
-
-Expand source code
-
-
def IsInitialized(self, errors=None):
- """Checks if all required fields of a message are set.
-
- Args:
- errors: A list which, if provided, will be populated with the field
- paths of all missing required fields.
-
- Returns:
- True iff the specified message has all required fields set.
- """
-
- # Performance is critical so we avoid HasField() and ListFields().
-
- for field in required_fields:
- if (field not in self._fields or
- (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and
- not self._fields[field]._is_present_in_parent)):
- if errors is not None:
- errors.extend(self.FindInitializationErrors())
- return False
-
- for field, value in list(self._fields.items()): # dict can change size!
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- if field.label == _FieldDescriptor.LABEL_REPEATED:
- if (field.message_type.has_options and
- field.message_type.GetOptions().map_entry):
- continue
- for element in value:
- if not element.IsInitialized():
- if errors is not None:
- errors.extend(self.FindInitializationErrors())
- return False
- elif value._is_present_in_parent and not value.IsInitialized():
- if errors is not None:
- errors.extend(self.FindInitializationErrors())
- return False
-
- return True
-
-
-
-def ListFields(self)
-
-
-
-
-
-Expand source code
-
-
def ListFields(self):
- all_fields = [item for item in self._fields.items() if _IsPresent(item)]
- all_fields.sort(key = lambda item: item[0].number)
- return all_fields
-
-
-
-def MergeFrom(self, msg)
-
-
-
-
-
-Expand source code
-
-
def MergeFrom(self, msg):
- if not isinstance(msg, cls):
- raise TypeError(
- 'Parameter to MergeFrom() must be instance of same class: '
- 'expected %s got %s.' % (cls.__name__, msg.__class__.__name__))
-
- assert msg is not self
- self._Modified()
-
- fields = self._fields
-
- for field, value in msg._fields.items():
- if field.label == LABEL_REPEATED:
- field_value = fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
- fields[field] = field_value
- field_value.MergeFrom(value)
- elif field.cpp_type == CPPTYPE_MESSAGE:
- if value._is_present_in_parent:
- field_value = fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
- fields[field] = field_value
- field_value.MergeFrom(value)
- else:
- self._fields[field] = value
- if field.containing_oneof:
- self._UpdateOneofState(field)
-
- if msg._unknown_fields:
- if not self._unknown_fields:
- self._unknown_fields = []
- self._unknown_fields.extend(msg._unknown_fields)
- # pylint: disable=protected-access
- if self._unknown_field_set is None:
- self._unknown_field_set = containers.UnknownFieldSet()
- self._unknown_field_set._extend(msg._unknown_field_set)
-
-
-
-def MergeFromString(self, serialized)
-
-
-
-
-
-Expand source code
-
-
def MergeFromString(self, serialized):
- if isinstance(serialized, memoryview) and six.PY2:
- raise TypeError(
- 'memoryview not supported in Python 2 with the pure Python proto '
- 'implementation: this is to maintain compatibility with the C++ '
- 'implementation')
-
- serialized = memoryview(serialized)
- length = len(serialized)
- try:
- if self._InternalParse(serialized, 0, length) != length:
- # The only reason _InternalParse would return early is if it
- # encountered an end-group tag.
- raise message_mod.DecodeError('Unexpected end-group tag.')
- except (IndexError, TypeError):
- # Now ord(buf[p:p+1]) == ord('') gets TypeError.
- raise message_mod.DecodeError('Truncated message.')
- except struct.error as e:
- raise message_mod.DecodeError(e)
- return length # Return this for legacy reasons.
def SerializeToString(self, **kwargs):
- # Check if the message has all of its required fields set.
- if not self.IsInitialized():
- raise message_mod.EncodeError(
- 'Message %s is missing required fields: %s' % (
- self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors())))
- return self.SerializePartialToString(**kwargs)
-
-
-
-def SetInParent(self)
-
-
-
Sets the _cached_byte_size_dirty bit to true,
-and propagates this to our listener iff this was a state change.
-
-
-Expand source code
-
-
def Modified(self):
- """Sets the _cached_byte_size_dirty bit to true,
- and propagates this to our listener iff this was a state change.
- """
-
- # Note: Some callers check _cached_byte_size_dirty before calling
- # _Modified() as an extra optimization. So, if this method is ever
- # changed such that it does stuff even when _cached_byte_size_dirty is
- # already true, the callers need to be updated.
- if not self._cached_byte_size_dirty:
- self._cached_byte_size_dirty = True
- self._listener_for_children.dirty = True
- self._is_present_in_parent = True
- self._listener.Modified()
Returns the name of the currently set field inside a oneof, or None.
-
-
-Expand source code
-
-
def WhichOneof(self, oneof_name):
- """Returns the name of the currently set field inside a oneof, or None."""
- try:
- field = message_descriptor.oneofs_by_name[oneof_name]
- except KeyError:
- raise ValueError(
- 'Protocol message has no oneof "%s" field.' % oneof_name)
-
- nested_field = self._oneofs.get(field, None)
- if nested_field is not None and self.HasField(nested_field.name):
- return nested_field.name
- else:
- return None
-
+
Field RadioConfig.preferences
class RouteDiscovery
-(**kwargs)
+(*args, **kwargs)
-
Abstract base class for protocol messages.
-
Protocol message classes are almost always generated by the protocol
-compiler.
-These generated types subclass Message and implement the methods
-shown below.
def getter(self):
- field_value = self._fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
-
- # Atomically check if another thread has preempted us and, if not, swap
- # in the new object we just created. If someone has preempted us, we
- # take that object and discard ours.
- # WARNING: We are relying on setdefault() being atomic. This is true
- # in CPython but we haven't investigated others. This warning appears
- # in several other locations in this file.
- field_value = self._fields.setdefault(field, field_value)
- return field_value
-
-
-
-
Methods
-
-
-def ByteSize(self)
-
-
-
-
-
-Expand source code
-
-
def ByteSize(self):
- if not self._cached_byte_size_dirty:
- return self._cached_byte_size
-
- size = 0
- descriptor = self.DESCRIPTOR
- if descriptor.GetOptions().map_entry:
- # Fields of map entry should always be serialized.
- size = descriptor.fields_by_name['key']._sizer(self.key)
- size += descriptor.fields_by_name['value']._sizer(self.value)
- else:
- for field_descriptor, field_value in self.ListFields():
- size += field_descriptor._sizer(field_value)
- for tag_bytes, value_bytes in self._unknown_fields:
- size += len(tag_bytes) + len(value_bytes)
-
- self._cached_byte_size = size
- self._cached_byte_size_dirty = False
- self._listener_for_children.dirty = False
- return size
def ClearField(self, field_name):
- try:
- field = message_descriptor.fields_by_name[field_name]
- except KeyError:
- try:
- field = message_descriptor.oneofs_by_name[field_name]
- if field in self._oneofs:
- field = self._oneofs[field]
- else:
- return
- except KeyError:
- raise ValueError('Protocol message %s has no "%s" field.' %
- (message_descriptor.name, field_name))
-
- if field in self._fields:
- # To match the C++ implementation, we need to invalidate iterators
- # for map fields when ClearField() happens.
- if hasattr(self._fields[field], 'InvalidateIterators'):
- self._fields[field].InvalidateIterators()
-
- # Note: If the field is a sub-message, its listener will still point
- # at us. That's fine, because the worst than can happen is that it
- # will call _Modified() and invalidate our byte size. Big deal.
- del self._fields[field]
-
- if self._oneofs.get(field.containing_oneof, None) is field:
- del self._oneofs[field.containing_oneof]
-
- # Always call _Modified() -- even if nothing was changed, this is
- # a mutating method, and thus calling it should cause the field to become
- # present in the parent message.
- self._Modified()
-
-
-
-def DiscardUnknownFields(self)
-
-
-
-
-
-Expand source code
-
-
def _DiscardUnknownFields(self):
- self._unknown_fields = []
- self._unknown_field_set = None # pylint: disable=protected-access
- for field, value in self.ListFields():
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- if _IsMapField(field):
- if _IsMessageMapField(field):
- for key in value:
- value[key].DiscardUnknownFields()
- elif field.label == _FieldDescriptor.LABEL_REPEATED:
- for sub_message in value:
- sub_message.DiscardUnknownFields()
- else:
- value.DiscardUnknownFields()
-
-
-
-def FindInitializationErrors(self)
-
-
-
Finds required fields which are not initialized.
-
Returns
-
A list of strings.
-Each string is a path to an uninitialized field from
-the top-level message, e.g. "foo.bar[5].baz".
-
-
-Expand source code
-
-
def FindInitializationErrors(self):
- """Finds required fields which are not initialized.
-
- Returns:
- A list of strings. Each string is a path to an uninitialized field from
- the top-level message, e.g. "foo.bar[5].baz".
- """
-
- errors = [] # simplify things
-
- for field in required_fields:
- if not self.HasField(field.name):
- errors.append(field.name)
-
- for field, value in self.ListFields():
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- if field.is_extension:
- name = '(%s)' % field.full_name
- else:
- name = field.name
-
- if _IsMapField(field):
- if _IsMessageMapField(field):
- for key in value:
- element = value[key]
- prefix = '%s[%s].' % (name, key)
- sub_errors = element.FindInitializationErrors()
- errors += [prefix + error for error in sub_errors]
- else:
- # ScalarMaps can't have any initialization errors.
- pass
- elif field.label == _FieldDescriptor.LABEL_REPEATED:
- for i in range(len(value)):
- element = value[i]
- prefix = '%s[%d].' % (name, i)
- sub_errors = element.FindInitializationErrors()
- errors += [prefix + error for error in sub_errors]
- else:
- prefix = name + '.'
- sub_errors = value.FindInitializationErrors()
- errors += [prefix + error for error in sub_errors]
-
- return errors
-
-
-
-def HasField(self, field_name)
-
-
-
-
-
-Expand source code
-
-
def HasField(self, field_name):
- try:
- field = hassable_fields[field_name]
- except KeyError:
- raise ValueError(error_msg % (message_descriptor.full_name, field_name))
-
- if isinstance(field, descriptor_mod.OneofDescriptor):
- try:
- return HasField(self, self._oneofs[field].name)
- except KeyError:
- return False
- else:
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- value = self._fields.get(field)
- return value is not None and value._is_present_in_parent
- else:
- return field in self._fields
-
-
-
-def IsInitialized(self, errors=None)
-
-
-
Checks if all required fields of a message are set.
-
Args
-
-
errors
-
A list which, if provided, will be populated with the field
-paths of all missing required fields.
-
-
Returns
-
True iff the specified message has all required fields set.
-
-
-Expand source code
-
-
def IsInitialized(self, errors=None):
- """Checks if all required fields of a message are set.
-
- Args:
- errors: A list which, if provided, will be populated with the field
- paths of all missing required fields.
-
- Returns:
- True iff the specified message has all required fields set.
- """
-
- # Performance is critical so we avoid HasField() and ListFields().
-
- for field in required_fields:
- if (field not in self._fields or
- (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and
- not self._fields[field]._is_present_in_parent)):
- if errors is not None:
- errors.extend(self.FindInitializationErrors())
- return False
-
- for field, value in list(self._fields.items()): # dict can change size!
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- if field.label == _FieldDescriptor.LABEL_REPEATED:
- if (field.message_type.has_options and
- field.message_type.GetOptions().map_entry):
- continue
- for element in value:
- if not element.IsInitialized():
- if errors is not None:
- errors.extend(self.FindInitializationErrors())
- return False
- elif value._is_present_in_parent and not value.IsInitialized():
- if errors is not None:
- errors.extend(self.FindInitializationErrors())
- return False
-
- return True
-
-
-
-def ListFields(self)
-
-
-
-
-
-Expand source code
-
-
def ListFields(self):
- all_fields = [item for item in self._fields.items() if _IsPresent(item)]
- all_fields.sort(key = lambda item: item[0].number)
- return all_fields
-
-
-
-def MergeFrom(self, msg)
-
-
-
-
-
-Expand source code
-
-
def MergeFrom(self, msg):
- if not isinstance(msg, cls):
- raise TypeError(
- 'Parameter to MergeFrom() must be instance of same class: '
- 'expected %s got %s.' % (cls.__name__, msg.__class__.__name__))
-
- assert msg is not self
- self._Modified()
-
- fields = self._fields
-
- for field, value in msg._fields.items():
- if field.label == LABEL_REPEATED:
- field_value = fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
- fields[field] = field_value
- field_value.MergeFrom(value)
- elif field.cpp_type == CPPTYPE_MESSAGE:
- if value._is_present_in_parent:
- field_value = fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
- fields[field] = field_value
- field_value.MergeFrom(value)
- else:
- self._fields[field] = value
- if field.containing_oneof:
- self._UpdateOneofState(field)
-
- if msg._unknown_fields:
- if not self._unknown_fields:
- self._unknown_fields = []
- self._unknown_fields.extend(msg._unknown_fields)
- # pylint: disable=protected-access
- if self._unknown_field_set is None:
- self._unknown_field_set = containers.UnknownFieldSet()
- self._unknown_field_set._extend(msg._unknown_field_set)
-
-
-
-def MergeFromString(self, serialized)
-
-
-
-
-
-Expand source code
-
-
def MergeFromString(self, serialized):
- if isinstance(serialized, memoryview) and six.PY2:
- raise TypeError(
- 'memoryview not supported in Python 2 with the pure Python proto '
- 'implementation: this is to maintain compatibility with the C++ '
- 'implementation')
-
- serialized = memoryview(serialized)
- length = len(serialized)
- try:
- if self._InternalParse(serialized, 0, length) != length:
- # The only reason _InternalParse would return early is if it
- # encountered an end-group tag.
- raise message_mod.DecodeError('Unexpected end-group tag.')
- except (IndexError, TypeError):
- # Now ord(buf[p:p+1]) == ord('') gets TypeError.
- raise message_mod.DecodeError('Truncated message.')
- except struct.error as e:
- raise message_mod.DecodeError(e)
- return length # Return this for legacy reasons.
def SerializeToString(self, **kwargs):
- # Check if the message has all of its required fields set.
- if not self.IsInitialized():
- raise message_mod.EncodeError(
- 'Message %s is missing required fields: %s' % (
- self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors())))
- return self.SerializePartialToString(**kwargs)
-
-
-
-def SetInParent(self)
-
-
-
Sets the _cached_byte_size_dirty bit to true,
-and propagates this to our listener iff this was a state change.
-
-
-Expand source code
-
-
def Modified(self):
- """Sets the _cached_byte_size_dirty bit to true,
- and propagates this to our listener iff this was a state change.
- """
-
- # Note: Some callers check _cached_byte_size_dirty before calling
- # _Modified() as an extra optimization. So, if this method is ever
- # changed such that it does stuff even when _cached_byte_size_dirty is
- # already true, the callers need to be updated.
- if not self._cached_byte_size_dirty:
- self._cached_byte_size_dirty = True
- self._listener_for_children.dirty = True
- self._is_present_in_parent = True
- self._listener.Modified()
Returns the name of the currently set field inside a oneof, or None.
-
-
-Expand source code
-
-
def WhichOneof(self, oneof_name):
- """Returns the name of the currently set field inside a oneof, or None."""
- try:
- field = message_descriptor.oneofs_by_name[oneof_name]
- except KeyError:
- raise ValueError(
- 'Protocol message has no oneof "%s" field.' % oneof_name)
-
- nested_field = self._oneofs.get(field, None)
- if nested_field is not None and self.HasField(nested_field.name):
- return nested_field.name
- else:
- return None
-
+
Field RouteDiscovery.route
class SubPacket
-(**kwargs)
+(*args, **kwargs)
-
Abstract base class for protocol messages.
-
Protocol message classes are almost always generated by the protocol
-compiler.
-These generated types subclass Message and implement the methods
-shown below.
def getter(self):
- field_value = self._fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
-
- # Atomically check if another thread has preempted us and, if not, swap
- # in the new object we just created. If someone has preempted us, we
- # take that object and discard ours.
- # WARNING: We are relying on setdefault() being atomic. This is true
- # in CPython but we haven't investigated others. This warning appears
- # in several other locations in this file.
- field_value = self._fields.setdefault(field, field_value)
- return field_value
-
+
Field SubPacket.data
var dest
-
Getter for dest.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field SubPacket.dest
+
+
var error_reason
+
+
Field SubPacket.error_reason
var fail_id
-
Getter for fail_id.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field SubPacket.fail_id
var original_id
-
Getter for original_id.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field SubPacket.original_id
var position
-
Getter for position.
-
-
-Expand source code
-
-
def getter(self):
- field_value = self._fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
-
- # Atomically check if another thread has preempted us and, if not, swap
- # in the new object we just created. If someone has preempted us, we
- # take that object and discard ours.
- # WARNING: We are relying on setdefault() being atomic. This is true
- # in CPython but we haven't investigated others. This warning appears
- # in several other locations in this file.
- field_value = self._fields.setdefault(field, field_value)
- return field_value
-
-
-
var route_error
-
-
Getter for route_error.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field SubPacket.position
var route_reply
-
Getter for route_reply.
-
-
-Expand source code
-
-
def getter(self):
- field_value = self._fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
-
- # Atomically check if another thread has preempted us and, if not, swap
- # in the new object we just created. If someone has preempted us, we
- # take that object and discard ours.
- # WARNING: We are relying on setdefault() being atomic. This is true
- # in CPython but we haven't investigated others. This warning appears
- # in several other locations in this file.
- field_value = self._fields.setdefault(field, field_value)
- return field_value
-
+
Field SubPacket.route_reply
var route_request
-
Getter for route_request.
-
-
-Expand source code
-
-
def getter(self):
- field_value = self._fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
-
- # Atomically check if another thread has preempted us and, if not, swap
- # in the new object we just created. If someone has preempted us, we
- # take that object and discard ours.
- # WARNING: We are relying on setdefault() being atomic. This is true
- # in CPython but we haven't investigated others. This warning appears
- # in several other locations in this file.
- field_value = self._fields.setdefault(field, field_value)
- return field_value
-
+
Field SubPacket.route_request
var source
-
Getter for source.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field SubPacket.source
var success_id
-
Getter for success_id.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field SubPacket.success_id
var user
-
Getter for user.
-
-
-Expand source code
-
-
def getter(self):
- field_value = self._fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
-
- # Atomically check if another thread has preempted us and, if not, swap
- # in the new object we just created. If someone has preempted us, we
- # take that object and discard ours.
- # WARNING: We are relying on setdefault() being atomic. This is true
- # in CPython but we haven't investigated others. This warning appears
- # in several other locations in this file.
- field_value = self._fields.setdefault(field, field_value)
- return field_value
-
+
Field SubPacket.user
var want_response
-
Getter for want_response.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
-
-
-
Methods
-
-
-def ByteSize(self)
-
-
-
-
-
-Expand source code
-
-
def ByteSize(self):
- if not self._cached_byte_size_dirty:
- return self._cached_byte_size
-
- size = 0
- descriptor = self.DESCRIPTOR
- if descriptor.GetOptions().map_entry:
- # Fields of map entry should always be serialized.
- size = descriptor.fields_by_name['key']._sizer(self.key)
- size += descriptor.fields_by_name['value']._sizer(self.value)
- else:
- for field_descriptor, field_value in self.ListFields():
- size += field_descriptor._sizer(field_value)
- for tag_bytes, value_bytes in self._unknown_fields:
- size += len(tag_bytes) + len(value_bytes)
-
- self._cached_byte_size = size
- self._cached_byte_size_dirty = False
- self._listener_for_children.dirty = False
- return size
def ClearField(self, field_name):
- try:
- field = message_descriptor.fields_by_name[field_name]
- except KeyError:
- try:
- field = message_descriptor.oneofs_by_name[field_name]
- if field in self._oneofs:
- field = self._oneofs[field]
- else:
- return
- except KeyError:
- raise ValueError('Protocol message %s has no "%s" field.' %
- (message_descriptor.name, field_name))
-
- if field in self._fields:
- # To match the C++ implementation, we need to invalidate iterators
- # for map fields when ClearField() happens.
- if hasattr(self._fields[field], 'InvalidateIterators'):
- self._fields[field].InvalidateIterators()
-
- # Note: If the field is a sub-message, its listener will still point
- # at us. That's fine, because the worst than can happen is that it
- # will call _Modified() and invalidate our byte size. Big deal.
- del self._fields[field]
-
- if self._oneofs.get(field.containing_oneof, None) is field:
- del self._oneofs[field.containing_oneof]
-
- # Always call _Modified() -- even if nothing was changed, this is
- # a mutating method, and thus calling it should cause the field to become
- # present in the parent message.
- self._Modified()
-
-
-
-def DiscardUnknownFields(self)
-
-
-
-
-
-Expand source code
-
-
def _DiscardUnknownFields(self):
- self._unknown_fields = []
- self._unknown_field_set = None # pylint: disable=protected-access
- for field, value in self.ListFields():
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- if _IsMapField(field):
- if _IsMessageMapField(field):
- for key in value:
- value[key].DiscardUnknownFields()
- elif field.label == _FieldDescriptor.LABEL_REPEATED:
- for sub_message in value:
- sub_message.DiscardUnknownFields()
- else:
- value.DiscardUnknownFields()
-
-
-
-def FindInitializationErrors(self)
-
-
-
Finds required fields which are not initialized.
-
Returns
-
A list of strings.
-Each string is a path to an uninitialized field from
-the top-level message, e.g. "foo.bar[5].baz".
-
-
-Expand source code
-
-
def FindInitializationErrors(self):
- """Finds required fields which are not initialized.
-
- Returns:
- A list of strings. Each string is a path to an uninitialized field from
- the top-level message, e.g. "foo.bar[5].baz".
- """
-
- errors = [] # simplify things
-
- for field in required_fields:
- if not self.HasField(field.name):
- errors.append(field.name)
-
- for field, value in self.ListFields():
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- if field.is_extension:
- name = '(%s)' % field.full_name
- else:
- name = field.name
-
- if _IsMapField(field):
- if _IsMessageMapField(field):
- for key in value:
- element = value[key]
- prefix = '%s[%s].' % (name, key)
- sub_errors = element.FindInitializationErrors()
- errors += [prefix + error for error in sub_errors]
- else:
- # ScalarMaps can't have any initialization errors.
- pass
- elif field.label == _FieldDescriptor.LABEL_REPEATED:
- for i in range(len(value)):
- element = value[i]
- prefix = '%s[%d].' % (name, i)
- sub_errors = element.FindInitializationErrors()
- errors += [prefix + error for error in sub_errors]
- else:
- prefix = name + '.'
- sub_errors = value.FindInitializationErrors()
- errors += [prefix + error for error in sub_errors]
-
- return errors
-
-
-
-def HasField(self, field_name)
-
-
-
-
-
-Expand source code
-
-
def HasField(self, field_name):
- try:
- field = hassable_fields[field_name]
- except KeyError:
- raise ValueError(error_msg % (message_descriptor.full_name, field_name))
-
- if isinstance(field, descriptor_mod.OneofDescriptor):
- try:
- return HasField(self, self._oneofs[field].name)
- except KeyError:
- return False
- else:
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- value = self._fields.get(field)
- return value is not None and value._is_present_in_parent
- else:
- return field in self._fields
-
-
-
-def IsInitialized(self, errors=None)
-
-
-
Checks if all required fields of a message are set.
-
Args
-
-
errors
-
A list which, if provided, will be populated with the field
-paths of all missing required fields.
-
-
Returns
-
True iff the specified message has all required fields set.
-
-
-Expand source code
-
-
def IsInitialized(self, errors=None):
- """Checks if all required fields of a message are set.
-
- Args:
- errors: A list which, if provided, will be populated with the field
- paths of all missing required fields.
-
- Returns:
- True iff the specified message has all required fields set.
- """
-
- # Performance is critical so we avoid HasField() and ListFields().
-
- for field in required_fields:
- if (field not in self._fields or
- (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and
- not self._fields[field]._is_present_in_parent)):
- if errors is not None:
- errors.extend(self.FindInitializationErrors())
- return False
-
- for field, value in list(self._fields.items()): # dict can change size!
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- if field.label == _FieldDescriptor.LABEL_REPEATED:
- if (field.message_type.has_options and
- field.message_type.GetOptions().map_entry):
- continue
- for element in value:
- if not element.IsInitialized():
- if errors is not None:
- errors.extend(self.FindInitializationErrors())
- return False
- elif value._is_present_in_parent and not value.IsInitialized():
- if errors is not None:
- errors.extend(self.FindInitializationErrors())
- return False
-
- return True
-
-
-
-def ListFields(self)
-
-
-
-
-
-Expand source code
-
-
def ListFields(self):
- all_fields = [item for item in self._fields.items() if _IsPresent(item)]
- all_fields.sort(key = lambda item: item[0].number)
- return all_fields
-
-
-
-def MergeFrom(self, msg)
-
-
-
-
-
-Expand source code
-
-
def MergeFrom(self, msg):
- if not isinstance(msg, cls):
- raise TypeError(
- 'Parameter to MergeFrom() must be instance of same class: '
- 'expected %s got %s.' % (cls.__name__, msg.__class__.__name__))
-
- assert msg is not self
- self._Modified()
-
- fields = self._fields
-
- for field, value in msg._fields.items():
- if field.label == LABEL_REPEATED:
- field_value = fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
- fields[field] = field_value
- field_value.MergeFrom(value)
- elif field.cpp_type == CPPTYPE_MESSAGE:
- if value._is_present_in_parent:
- field_value = fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
- fields[field] = field_value
- field_value.MergeFrom(value)
- else:
- self._fields[field] = value
- if field.containing_oneof:
- self._UpdateOneofState(field)
-
- if msg._unknown_fields:
- if not self._unknown_fields:
- self._unknown_fields = []
- self._unknown_fields.extend(msg._unknown_fields)
- # pylint: disable=protected-access
- if self._unknown_field_set is None:
- self._unknown_field_set = containers.UnknownFieldSet()
- self._unknown_field_set._extend(msg._unknown_field_set)
-
-
-
-def MergeFromString(self, serialized)
-
-
-
-
-
-Expand source code
-
-
def MergeFromString(self, serialized):
- if isinstance(serialized, memoryview) and six.PY2:
- raise TypeError(
- 'memoryview not supported in Python 2 with the pure Python proto '
- 'implementation: this is to maintain compatibility with the C++ '
- 'implementation')
-
- serialized = memoryview(serialized)
- length = len(serialized)
- try:
- if self._InternalParse(serialized, 0, length) != length:
- # The only reason _InternalParse would return early is if it
- # encountered an end-group tag.
- raise message_mod.DecodeError('Unexpected end-group tag.')
- except (IndexError, TypeError):
- # Now ord(buf[p:p+1]) == ord('') gets TypeError.
- raise message_mod.DecodeError('Truncated message.')
- except struct.error as e:
- raise message_mod.DecodeError(e)
- return length # Return this for legacy reasons.
def SerializeToString(self, **kwargs):
- # Check if the message has all of its required fields set.
- if not self.IsInitialized():
- raise message_mod.EncodeError(
- 'Message %s is missing required fields: %s' % (
- self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors())))
- return self.SerializePartialToString(**kwargs)
-
-
-
-def SetInParent(self)
-
-
-
Sets the _cached_byte_size_dirty bit to true,
-and propagates this to our listener iff this was a state change.
-
-
-Expand source code
-
-
def Modified(self):
- """Sets the _cached_byte_size_dirty bit to true,
- and propagates this to our listener iff this was a state change.
- """
-
- # Note: Some callers check _cached_byte_size_dirty before calling
- # _Modified() as an extra optimization. So, if this method is ever
- # changed such that it does stuff even when _cached_byte_size_dirty is
- # already true, the callers need to be updated.
- if not self._cached_byte_size_dirty:
- self._cached_byte_size_dirty = True
- self._listener_for_children.dirty = True
- self._is_present_in_parent = True
- self._listener.Modified()
Returns the name of the currently set field inside a oneof, or None.
-
-
-Expand source code
-
-
def WhichOneof(self, oneof_name):
- """Returns the name of the currently set field inside a oneof, or None."""
- try:
- field = message_descriptor.oneofs_by_name[oneof_name]
- except KeyError:
- raise ValueError(
- 'Protocol message has no oneof "%s" field.' % oneof_name)
-
- nested_field = self._oneofs.get(field, None)
- if nested_field is not None and self.HasField(nested_field.name):
- return nested_field.name
- else:
- return None
-
+
Field SubPacket.want_response
class ToRadio
-(**kwargs)
+(*args, **kwargs)
-
Abstract base class for protocol messages.
-
Protocol message classes are almost always generated by the protocol
-compiler.
-These generated types subclass Message and implement the methods
-shown below.
def getter(self):
- field_value = self._fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
-
- # Atomically check if another thread has preempted us and, if not, swap
- # in the new object we just created. If someone has preempted us, we
- # take that object and discard ours.
- # WARNING: We are relying on setdefault() being atomic. This is true
- # in CPython but we haven't investigated others. This warning appears
- # in several other locations in this file.
- field_value = self._fields.setdefault(field, field_value)
- return field_value
-
+
Field ToRadio.packet
+
+
var set_channel
+
+
Field ToRadio.set_channel
var set_owner
-
Getter for set_owner.
-
-
-Expand source code
-
-
def getter(self):
- field_value = self._fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
-
- # Atomically check if another thread has preempted us and, if not, swap
- # in the new object we just created. If someone has preempted us, we
- # take that object and discard ours.
- # WARNING: We are relying on setdefault() being atomic. This is true
- # in CPython but we haven't investigated others. This warning appears
- # in several other locations in this file.
- field_value = self._fields.setdefault(field, field_value)
- return field_value
-
+
Field ToRadio.set_owner
var set_radio
-
Getter for set_radio.
-
-
-Expand source code
-
-
def getter(self):
- field_value = self._fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
-
- # Atomically check if another thread has preempted us and, if not, swap
- # in the new object we just created. If someone has preempted us, we
- # take that object and discard ours.
- # WARNING: We are relying on setdefault() being atomic. This is true
- # in CPython but we haven't investigated others. This warning appears
- # in several other locations in this file.
- field_value = self._fields.setdefault(field, field_value)
- return field_value
-
+
Field ToRadio.set_radio
var want_config_id
-
Getter for want_config_id.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
-
-
-
Methods
-
-
-def ByteSize(self)
-
-
-
-
-
-Expand source code
-
-
def ByteSize(self):
- if not self._cached_byte_size_dirty:
- return self._cached_byte_size
-
- size = 0
- descriptor = self.DESCRIPTOR
- if descriptor.GetOptions().map_entry:
- # Fields of map entry should always be serialized.
- size = descriptor.fields_by_name['key']._sizer(self.key)
- size += descriptor.fields_by_name['value']._sizer(self.value)
- else:
- for field_descriptor, field_value in self.ListFields():
- size += field_descriptor._sizer(field_value)
- for tag_bytes, value_bytes in self._unknown_fields:
- size += len(tag_bytes) + len(value_bytes)
-
- self._cached_byte_size = size
- self._cached_byte_size_dirty = False
- self._listener_for_children.dirty = False
- return size
def ClearField(self, field_name):
- try:
- field = message_descriptor.fields_by_name[field_name]
- except KeyError:
- try:
- field = message_descriptor.oneofs_by_name[field_name]
- if field in self._oneofs:
- field = self._oneofs[field]
- else:
- return
- except KeyError:
- raise ValueError('Protocol message %s has no "%s" field.' %
- (message_descriptor.name, field_name))
-
- if field in self._fields:
- # To match the C++ implementation, we need to invalidate iterators
- # for map fields when ClearField() happens.
- if hasattr(self._fields[field], 'InvalidateIterators'):
- self._fields[field].InvalidateIterators()
-
- # Note: If the field is a sub-message, its listener will still point
- # at us. That's fine, because the worst than can happen is that it
- # will call _Modified() and invalidate our byte size. Big deal.
- del self._fields[field]
-
- if self._oneofs.get(field.containing_oneof, None) is field:
- del self._oneofs[field.containing_oneof]
-
- # Always call _Modified() -- even if nothing was changed, this is
- # a mutating method, and thus calling it should cause the field to become
- # present in the parent message.
- self._Modified()
-
-
-
-def DiscardUnknownFields(self)
-
-
-
-
-
-Expand source code
-
-
def _DiscardUnknownFields(self):
- self._unknown_fields = []
- self._unknown_field_set = None # pylint: disable=protected-access
- for field, value in self.ListFields():
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- if _IsMapField(field):
- if _IsMessageMapField(field):
- for key in value:
- value[key].DiscardUnknownFields()
- elif field.label == _FieldDescriptor.LABEL_REPEATED:
- for sub_message in value:
- sub_message.DiscardUnknownFields()
- else:
- value.DiscardUnknownFields()
-
-
-
-def FindInitializationErrors(self)
-
-
-
Finds required fields which are not initialized.
-
Returns
-
A list of strings.
-Each string is a path to an uninitialized field from
-the top-level message, e.g. "foo.bar[5].baz".
-
-
-Expand source code
-
-
def FindInitializationErrors(self):
- """Finds required fields which are not initialized.
-
- Returns:
- A list of strings. Each string is a path to an uninitialized field from
- the top-level message, e.g. "foo.bar[5].baz".
- """
-
- errors = [] # simplify things
-
- for field in required_fields:
- if not self.HasField(field.name):
- errors.append(field.name)
-
- for field, value in self.ListFields():
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- if field.is_extension:
- name = '(%s)' % field.full_name
- else:
- name = field.name
-
- if _IsMapField(field):
- if _IsMessageMapField(field):
- for key in value:
- element = value[key]
- prefix = '%s[%s].' % (name, key)
- sub_errors = element.FindInitializationErrors()
- errors += [prefix + error for error in sub_errors]
- else:
- # ScalarMaps can't have any initialization errors.
- pass
- elif field.label == _FieldDescriptor.LABEL_REPEATED:
- for i in range(len(value)):
- element = value[i]
- prefix = '%s[%d].' % (name, i)
- sub_errors = element.FindInitializationErrors()
- errors += [prefix + error for error in sub_errors]
- else:
- prefix = name + '.'
- sub_errors = value.FindInitializationErrors()
- errors += [prefix + error for error in sub_errors]
-
- return errors
-
-
-
-def HasField(self, field_name)
-
-
-
-
-
-Expand source code
-
-
def HasField(self, field_name):
- try:
- field = hassable_fields[field_name]
- except KeyError:
- raise ValueError(error_msg % (message_descriptor.full_name, field_name))
-
- if isinstance(field, descriptor_mod.OneofDescriptor):
- try:
- return HasField(self, self._oneofs[field].name)
- except KeyError:
- return False
- else:
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- value = self._fields.get(field)
- return value is not None and value._is_present_in_parent
- else:
- return field in self._fields
-
-
-
-def IsInitialized(self, errors=None)
-
-
-
Checks if all required fields of a message are set.
-
Args
-
-
errors
-
A list which, if provided, will be populated with the field
-paths of all missing required fields.
-
-
Returns
-
True iff the specified message has all required fields set.
-
-
-Expand source code
-
-
def IsInitialized(self, errors=None):
- """Checks if all required fields of a message are set.
-
- Args:
- errors: A list which, if provided, will be populated with the field
- paths of all missing required fields.
-
- Returns:
- True iff the specified message has all required fields set.
- """
-
- # Performance is critical so we avoid HasField() and ListFields().
-
- for field in required_fields:
- if (field not in self._fields or
- (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and
- not self._fields[field]._is_present_in_parent)):
- if errors is not None:
- errors.extend(self.FindInitializationErrors())
- return False
-
- for field, value in list(self._fields.items()): # dict can change size!
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- if field.label == _FieldDescriptor.LABEL_REPEATED:
- if (field.message_type.has_options and
- field.message_type.GetOptions().map_entry):
- continue
- for element in value:
- if not element.IsInitialized():
- if errors is not None:
- errors.extend(self.FindInitializationErrors())
- return False
- elif value._is_present_in_parent and not value.IsInitialized():
- if errors is not None:
- errors.extend(self.FindInitializationErrors())
- return False
-
- return True
-
-
-
-def ListFields(self)
-
-
-
-
-
-Expand source code
-
-
def ListFields(self):
- all_fields = [item for item in self._fields.items() if _IsPresent(item)]
- all_fields.sort(key = lambda item: item[0].number)
- return all_fields
-
-
-
-def MergeFrom(self, msg)
-
-
-
-
-
-Expand source code
-
-
def MergeFrom(self, msg):
- if not isinstance(msg, cls):
- raise TypeError(
- 'Parameter to MergeFrom() must be instance of same class: '
- 'expected %s got %s.' % (cls.__name__, msg.__class__.__name__))
-
- assert msg is not self
- self._Modified()
-
- fields = self._fields
-
- for field, value in msg._fields.items():
- if field.label == LABEL_REPEATED:
- field_value = fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
- fields[field] = field_value
- field_value.MergeFrom(value)
- elif field.cpp_type == CPPTYPE_MESSAGE:
- if value._is_present_in_parent:
- field_value = fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
- fields[field] = field_value
- field_value.MergeFrom(value)
- else:
- self._fields[field] = value
- if field.containing_oneof:
- self._UpdateOneofState(field)
-
- if msg._unknown_fields:
- if not self._unknown_fields:
- self._unknown_fields = []
- self._unknown_fields.extend(msg._unknown_fields)
- # pylint: disable=protected-access
- if self._unknown_field_set is None:
- self._unknown_field_set = containers.UnknownFieldSet()
- self._unknown_field_set._extend(msg._unknown_field_set)
-
-
-
-def MergeFromString(self, serialized)
-
-
-
-
-
-Expand source code
-
-
def MergeFromString(self, serialized):
- if isinstance(serialized, memoryview) and six.PY2:
- raise TypeError(
- 'memoryview not supported in Python 2 with the pure Python proto '
- 'implementation: this is to maintain compatibility with the C++ '
- 'implementation')
-
- serialized = memoryview(serialized)
- length = len(serialized)
- try:
- if self._InternalParse(serialized, 0, length) != length:
- # The only reason _InternalParse would return early is if it
- # encountered an end-group tag.
- raise message_mod.DecodeError('Unexpected end-group tag.')
- except (IndexError, TypeError):
- # Now ord(buf[p:p+1]) == ord('') gets TypeError.
- raise message_mod.DecodeError('Truncated message.')
- except struct.error as e:
- raise message_mod.DecodeError(e)
- return length # Return this for legacy reasons.
def SerializeToString(self, **kwargs):
- # Check if the message has all of its required fields set.
- if not self.IsInitialized():
- raise message_mod.EncodeError(
- 'Message %s is missing required fields: %s' % (
- self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors())))
- return self.SerializePartialToString(**kwargs)
-
-
-
-def SetInParent(self)
-
-
-
Sets the _cached_byte_size_dirty bit to true,
-and propagates this to our listener iff this was a state change.
-
-
-Expand source code
-
-
def Modified(self):
- """Sets the _cached_byte_size_dirty bit to true,
- and propagates this to our listener iff this was a state change.
- """
-
- # Note: Some callers check _cached_byte_size_dirty before calling
- # _Modified() as an extra optimization. So, if this method is ever
- # changed such that it does stuff even when _cached_byte_size_dirty is
- # already true, the callers need to be updated.
- if not self._cached_byte_size_dirty:
- self._cached_byte_size_dirty = True
- self._listener_for_children.dirty = True
- self._is_present_in_parent = True
- self._listener.Modified()
Returns the name of the currently set field inside a oneof, or None.
-
-
-Expand source code
-
-
def WhichOneof(self, oneof_name):
- """Returns the name of the currently set field inside a oneof, or None."""
- try:
- field = message_descriptor.oneofs_by_name[oneof_name]
- except KeyError:
- raise ValueError(
- 'Protocol message has no oneof "%s" field.' % oneof_name)
-
- nested_field = self._oneofs.get(field, None)
- if nested_field is not None and self.HasField(nested_field.name):
- return nested_field.name
- else:
- return None
-
+
Field ToRadio.want_config_id
class User
-(**kwargs)
+(*args, **kwargs)
-
Abstract base class for protocol messages.
-
Protocol message classes are almost always generated by the protocol
-compiler.
-These generated types subclass Message and implement the methods
-shown below.
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field User.id
var long_name
-
Getter for long_name.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field User.long_name
var macaddr
-
Getter for macaddr.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field User.macaddr
var short_name
-
Getter for short_name.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
-
-
-
Methods
-
-
-def ByteSize(self)
-
-
-
-
-
-Expand source code
-
-
def ByteSize(self):
- if not self._cached_byte_size_dirty:
- return self._cached_byte_size
-
- size = 0
- descriptor = self.DESCRIPTOR
- if descriptor.GetOptions().map_entry:
- # Fields of map entry should always be serialized.
- size = descriptor.fields_by_name['key']._sizer(self.key)
- size += descriptor.fields_by_name['value']._sizer(self.value)
- else:
- for field_descriptor, field_value in self.ListFields():
- size += field_descriptor._sizer(field_value)
- for tag_bytes, value_bytes in self._unknown_fields:
- size += len(tag_bytes) + len(value_bytes)
-
- self._cached_byte_size = size
- self._cached_byte_size_dirty = False
- self._listener_for_children.dirty = False
- return size
def ClearField(self, field_name):
- try:
- field = message_descriptor.fields_by_name[field_name]
- except KeyError:
- try:
- field = message_descriptor.oneofs_by_name[field_name]
- if field in self._oneofs:
- field = self._oneofs[field]
- else:
- return
- except KeyError:
- raise ValueError('Protocol message %s has no "%s" field.' %
- (message_descriptor.name, field_name))
-
- if field in self._fields:
- # To match the C++ implementation, we need to invalidate iterators
- # for map fields when ClearField() happens.
- if hasattr(self._fields[field], 'InvalidateIterators'):
- self._fields[field].InvalidateIterators()
-
- # Note: If the field is a sub-message, its listener will still point
- # at us. That's fine, because the worst than can happen is that it
- # will call _Modified() and invalidate our byte size. Big deal.
- del self._fields[field]
-
- if self._oneofs.get(field.containing_oneof, None) is field:
- del self._oneofs[field.containing_oneof]
-
- # Always call _Modified() -- even if nothing was changed, this is
- # a mutating method, and thus calling it should cause the field to become
- # present in the parent message.
- self._Modified()
-
-
-
-def DiscardUnknownFields(self)
-
-
-
-
-
-Expand source code
-
-
def _DiscardUnknownFields(self):
- self._unknown_fields = []
- self._unknown_field_set = None # pylint: disable=protected-access
- for field, value in self.ListFields():
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- if _IsMapField(field):
- if _IsMessageMapField(field):
- for key in value:
- value[key].DiscardUnknownFields()
- elif field.label == _FieldDescriptor.LABEL_REPEATED:
- for sub_message in value:
- sub_message.DiscardUnknownFields()
- else:
- value.DiscardUnknownFields()
-
-
-
-def FindInitializationErrors(self)
-
-
-
Finds required fields which are not initialized.
-
Returns
-
A list of strings.
-Each string is a path to an uninitialized field from
-the top-level message, e.g. "foo.bar[5].baz".
-
-
-Expand source code
-
-
def FindInitializationErrors(self):
- """Finds required fields which are not initialized.
-
- Returns:
- A list of strings. Each string is a path to an uninitialized field from
- the top-level message, e.g. "foo.bar[5].baz".
- """
-
- errors = [] # simplify things
-
- for field in required_fields:
- if not self.HasField(field.name):
- errors.append(field.name)
-
- for field, value in self.ListFields():
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- if field.is_extension:
- name = '(%s)' % field.full_name
- else:
- name = field.name
-
- if _IsMapField(field):
- if _IsMessageMapField(field):
- for key in value:
- element = value[key]
- prefix = '%s[%s].' % (name, key)
- sub_errors = element.FindInitializationErrors()
- errors += [prefix + error for error in sub_errors]
- else:
- # ScalarMaps can't have any initialization errors.
- pass
- elif field.label == _FieldDescriptor.LABEL_REPEATED:
- for i in range(len(value)):
- element = value[i]
- prefix = '%s[%d].' % (name, i)
- sub_errors = element.FindInitializationErrors()
- errors += [prefix + error for error in sub_errors]
- else:
- prefix = name + '.'
- sub_errors = value.FindInitializationErrors()
- errors += [prefix + error for error in sub_errors]
-
- return errors
-
-
-
-def HasField(self, field_name)
-
-
-
-
-
-Expand source code
-
-
def HasField(self, field_name):
- try:
- field = hassable_fields[field_name]
- except KeyError:
- raise ValueError(error_msg % (message_descriptor.full_name, field_name))
-
- if isinstance(field, descriptor_mod.OneofDescriptor):
- try:
- return HasField(self, self._oneofs[field].name)
- except KeyError:
- return False
- else:
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- value = self._fields.get(field)
- return value is not None and value._is_present_in_parent
- else:
- return field in self._fields
-
-
-
-def IsInitialized(self, errors=None)
-
-
-
Checks if all required fields of a message are set.
-
Args
-
-
errors
-
A list which, if provided, will be populated with the field
-paths of all missing required fields.
-
-
Returns
-
True iff the specified message has all required fields set.
-
-
-Expand source code
-
-
def IsInitialized(self, errors=None):
- """Checks if all required fields of a message are set.
-
- Args:
- errors: A list which, if provided, will be populated with the field
- paths of all missing required fields.
-
- Returns:
- True iff the specified message has all required fields set.
- """
-
- # Performance is critical so we avoid HasField() and ListFields().
-
- for field in required_fields:
- if (field not in self._fields or
- (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and
- not self._fields[field]._is_present_in_parent)):
- if errors is not None:
- errors.extend(self.FindInitializationErrors())
- return False
-
- for field, value in list(self._fields.items()): # dict can change size!
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- if field.label == _FieldDescriptor.LABEL_REPEATED:
- if (field.message_type.has_options and
- field.message_type.GetOptions().map_entry):
- continue
- for element in value:
- if not element.IsInitialized():
- if errors is not None:
- errors.extend(self.FindInitializationErrors())
- return False
- elif value._is_present_in_parent and not value.IsInitialized():
- if errors is not None:
- errors.extend(self.FindInitializationErrors())
- return False
-
- return True
-
-
-
-def ListFields(self)
-
-
-
-
-
-Expand source code
-
-
def ListFields(self):
- all_fields = [item for item in self._fields.items() if _IsPresent(item)]
- all_fields.sort(key = lambda item: item[0].number)
- return all_fields
-
-
-
-def MergeFrom(self, msg)
-
-
-
-
-
-Expand source code
-
-
def MergeFrom(self, msg):
- if not isinstance(msg, cls):
- raise TypeError(
- 'Parameter to MergeFrom() must be instance of same class: '
- 'expected %s got %s.' % (cls.__name__, msg.__class__.__name__))
-
- assert msg is not self
- self._Modified()
-
- fields = self._fields
-
- for field, value in msg._fields.items():
- if field.label == LABEL_REPEATED:
- field_value = fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
- fields[field] = field_value
- field_value.MergeFrom(value)
- elif field.cpp_type == CPPTYPE_MESSAGE:
- if value._is_present_in_parent:
- field_value = fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
- fields[field] = field_value
- field_value.MergeFrom(value)
- else:
- self._fields[field] = value
- if field.containing_oneof:
- self._UpdateOneofState(field)
-
- if msg._unknown_fields:
- if not self._unknown_fields:
- self._unknown_fields = []
- self._unknown_fields.extend(msg._unknown_fields)
- # pylint: disable=protected-access
- if self._unknown_field_set is None:
- self._unknown_field_set = containers.UnknownFieldSet()
- self._unknown_field_set._extend(msg._unknown_field_set)
-
-
-
-def MergeFromString(self, serialized)
-
-
-
-
-
-Expand source code
-
-
def MergeFromString(self, serialized):
- if isinstance(serialized, memoryview) and six.PY2:
- raise TypeError(
- 'memoryview not supported in Python 2 with the pure Python proto '
- 'implementation: this is to maintain compatibility with the C++ '
- 'implementation')
-
- serialized = memoryview(serialized)
- length = len(serialized)
- try:
- if self._InternalParse(serialized, 0, length) != length:
- # The only reason _InternalParse would return early is if it
- # encountered an end-group tag.
- raise message_mod.DecodeError('Unexpected end-group tag.')
- except (IndexError, TypeError):
- # Now ord(buf[p:p+1]) == ord('') gets TypeError.
- raise message_mod.DecodeError('Truncated message.')
- except struct.error as e:
- raise message_mod.DecodeError(e)
- return length # Return this for legacy reasons.
def SerializeToString(self, **kwargs):
- # Check if the message has all of its required fields set.
- if not self.IsInitialized():
- raise message_mod.EncodeError(
- 'Message %s is missing required fields: %s' % (
- self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors())))
- return self.SerializePartialToString(**kwargs)
-
-
-
-def SetInParent(self)
-
-
-
Sets the _cached_byte_size_dirty bit to true,
-and propagates this to our listener iff this was a state change.
-
-
-Expand source code
-
-
def Modified(self):
- """Sets the _cached_byte_size_dirty bit to true,
- and propagates this to our listener iff this was a state change.
- """
-
- # Note: Some callers check _cached_byte_size_dirty before calling
- # _Modified() as an extra optimization. So, if this method is ever
- # changed such that it does stuff even when _cached_byte_size_dirty is
- # already true, the callers need to be updated.
- if not self._cached_byte_size_dirty:
- self._cached_byte_size_dirty = True
- self._listener_for_children.dirty = True
- self._is_present_in_parent = True
- self._listener.Modified()
Returns the name of the currently set field inside a oneof, or None.
-
-
-Expand source code
-
-
def WhichOneof(self, oneof_name):
- """Returns the name of the currently set field inside a oneof, or None."""
- try:
- field = message_descriptor.oneofs_by_name[oneof_name]
- except KeyError:
- raise ValueError(
- 'Protocol message has no oneof "%s" field.' % oneof_name)
-
- nested_field = self._oneofs.get(field, None)
- if nested_field is not None and self.HasField(nested_field.name):
- return nested_field.name
- else:
- return None
-
+
Field User.short_name
@@ -10840,215 +2655,71 @@ and propagates this to our listener iff this was a state change.
class HardwareMessage
-(**kwargs)
+(*args, **kwargs)
-
Abstract base class for protocol messages.
-
Protocol message classes are almost always generated by the protocol
-compiler.
-These generated types subclass Message and implement the methods
-shown below.
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field HardwareMessage.gpio_mask
var gpio_value
-
Getter for gpio_value.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
+
Field HardwareMessage.gpio_value
var typ
-
Getter for typ.
-
-
-Expand source code
-
-
def getter(self):
- # TODO(protobuf-team): This may be broken since there may not be
- # default_value. Combine with has_default_value somehow.
- return self._fields.get(field, default_value)
-
-
-
-
Methods
-
-
-def ByteSize(self)
-
-
-
-
-
-Expand source code
-
-
def ByteSize(self):
- if not self._cached_byte_size_dirty:
- return self._cached_byte_size
-
- size = 0
- descriptor = self.DESCRIPTOR
- if descriptor.GetOptions().map_entry:
- # Fields of map entry should always be serialized.
- size = descriptor.fields_by_name['key']._sizer(self.key)
- size += descriptor.fields_by_name['value']._sizer(self.value)
- else:
- for field_descriptor, field_value in self.ListFields():
- size += field_descriptor._sizer(field_value)
- for tag_bytes, value_bytes in self._unknown_fields:
- size += len(tag_bytes) + len(value_bytes)
-
- self._cached_byte_size = size
- self._cached_byte_size_dirty = False
- self._listener_for_children.dirty = False
- return size
def ClearField(self, field_name):
- try:
- field = message_descriptor.fields_by_name[field_name]
- except KeyError:
- try:
- field = message_descriptor.oneofs_by_name[field_name]
- if field in self._oneofs:
- field = self._oneofs[field]
- else:
- return
- except KeyError:
- raise ValueError('Protocol message %s has no "%s" field.' %
- (message_descriptor.name, field_name))
-
- if field in self._fields:
- # To match the C++ implementation, we need to invalidate iterators
- # for map fields when ClearField() happens.
- if hasattr(self._fields[field], 'InvalidateIterators'):
- self._fields[field].InvalidateIterators()
-
- # Note: If the field is a sub-message, its listener will still point
- # at us. That's fine, because the worst than can happen is that it
- # will call _Modified() and invalidate our byte size. Big deal.
- del self._fields[field]
-
- if self._oneofs.get(field.containing_oneof, None) is field:
- del self._oneofs[field.containing_oneof]
-
- # Always call _Modified() -- even if nothing was changed, this is
- # a mutating method, and thus calling it should cause the field to become
- # present in the parent message.
- self._Modified()
-
-
-
-def DiscardUnknownFields(self)
-
-
-
-
-
-Expand source code
-
-
def _DiscardUnknownFields(self):
- self._unknown_fields = []
- self._unknown_field_set = None # pylint: disable=protected-access
- for field, value in self.ListFields():
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- if _IsMapField(field):
- if _IsMessageMapField(field):
- for key in value:
- value[key].DiscardUnknownFields()
- elif field.label == _FieldDescriptor.LABEL_REPEATED:
- for sub_message in value:
- sub_message.DiscardUnknownFields()
- else:
- value.DiscardUnknownFields()
-
-
-
-def FindInitializationErrors(self)
-
-
-
Finds required fields which are not initialized.
-
Returns
-
A list of strings.
-Each string is a path to an uninitialized field from
-the top-level message, e.g. "foo.bar[5].baz".
-
-
-Expand source code
-
-
def FindInitializationErrors(self):
- """Finds required fields which are not initialized.
-
- Returns:
- A list of strings. Each string is a path to an uninitialized field from
- the top-level message, e.g. "foo.bar[5].baz".
- """
-
- errors = [] # simplify things
-
- for field in required_fields:
- if not self.HasField(field.name):
- errors.append(field.name)
-
- for field, value in self.ListFields():
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- if field.is_extension:
- name = '(%s)' % field.full_name
- else:
- name = field.name
-
- if _IsMapField(field):
- if _IsMessageMapField(field):
- for key in value:
- element = value[key]
- prefix = '%s[%s].' % (name, key)
- sub_errors = element.FindInitializationErrors()
- errors += [prefix + error for error in sub_errors]
- else:
- # ScalarMaps can't have any initialization errors.
- pass
- elif field.label == _FieldDescriptor.LABEL_REPEATED:
- for i in range(len(value)):
- element = value[i]
- prefix = '%s[%d].' % (name, i)
- sub_errors = element.FindInitializationErrors()
- errors += [prefix + error for error in sub_errors]
- else:
- prefix = name + '.'
- sub_errors = value.FindInitializationErrors()
- errors += [prefix + error for error in sub_errors]
-
- return errors
-
-
-
-def HasField(self, field_name)
-
-
-
-
-
-Expand source code
-
-
def HasField(self, field_name):
- try:
- field = hassable_fields[field_name]
- except KeyError:
- raise ValueError(error_msg % (message_descriptor.full_name, field_name))
-
- if isinstance(field, descriptor_mod.OneofDescriptor):
- try:
- return HasField(self, self._oneofs[field].name)
- except KeyError:
- return False
- else:
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- value = self._fields.get(field)
- return value is not None and value._is_present_in_parent
- else:
- return field in self._fields
-
-
-
-def IsInitialized(self, errors=None)
-
-
-
Checks if all required fields of a message are set.
-
Args
-
-
errors
-
A list which, if provided, will be populated with the field
-paths of all missing required fields.
-
-
Returns
-
True iff the specified message has all required fields set.
-
-
-Expand source code
-
-
def IsInitialized(self, errors=None):
- """Checks if all required fields of a message are set.
-
- Args:
- errors: A list which, if provided, will be populated with the field
- paths of all missing required fields.
-
- Returns:
- True iff the specified message has all required fields set.
- """
-
- # Performance is critical so we avoid HasField() and ListFields().
-
- for field in required_fields:
- if (field not in self._fields or
- (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and
- not self._fields[field]._is_present_in_parent)):
- if errors is not None:
- errors.extend(self.FindInitializationErrors())
- return False
-
- for field, value in list(self._fields.items()): # dict can change size!
- if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
- if field.label == _FieldDescriptor.LABEL_REPEATED:
- if (field.message_type.has_options and
- field.message_type.GetOptions().map_entry):
- continue
- for element in value:
- if not element.IsInitialized():
- if errors is not None:
- errors.extend(self.FindInitializationErrors())
- return False
- elif value._is_present_in_parent and not value.IsInitialized():
- if errors is not None:
- errors.extend(self.FindInitializationErrors())
- return False
-
- return True
-
-
-
-def ListFields(self)
-
-
-
-
-
-Expand source code
-
-
def ListFields(self):
- all_fields = [item for item in self._fields.items() if _IsPresent(item)]
- all_fields.sort(key = lambda item: item[0].number)
- return all_fields
-
-
-
-def MergeFrom(self, msg)
-
-
-
-
-
-Expand source code
-
-
def MergeFrom(self, msg):
- if not isinstance(msg, cls):
- raise TypeError(
- 'Parameter to MergeFrom() must be instance of same class: '
- 'expected %s got %s.' % (cls.__name__, msg.__class__.__name__))
-
- assert msg is not self
- self._Modified()
-
- fields = self._fields
-
- for field, value in msg._fields.items():
- if field.label == LABEL_REPEATED:
- field_value = fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
- fields[field] = field_value
- field_value.MergeFrom(value)
- elif field.cpp_type == CPPTYPE_MESSAGE:
- if value._is_present_in_parent:
- field_value = fields.get(field)
- if field_value is None:
- # Construct a new object to represent this field.
- field_value = field._default_constructor(self)
- fields[field] = field_value
- field_value.MergeFrom(value)
- else:
- self._fields[field] = value
- if field.containing_oneof:
- self._UpdateOneofState(field)
-
- if msg._unknown_fields:
- if not self._unknown_fields:
- self._unknown_fields = []
- self._unknown_fields.extend(msg._unknown_fields)
- # pylint: disable=protected-access
- if self._unknown_field_set is None:
- self._unknown_field_set = containers.UnknownFieldSet()
- self._unknown_field_set._extend(msg._unknown_field_set)
-
-
-
-def MergeFromString(self, serialized)
-
-
-
-
-
-Expand source code
-
-
def MergeFromString(self, serialized):
- if isinstance(serialized, memoryview) and six.PY2:
- raise TypeError(
- 'memoryview not supported in Python 2 with the pure Python proto '
- 'implementation: this is to maintain compatibility with the C++ '
- 'implementation')
-
- serialized = memoryview(serialized)
- length = len(serialized)
- try:
- if self._InternalParse(serialized, 0, length) != length:
- # The only reason _InternalParse would return early is if it
- # encountered an end-group tag.
- raise message_mod.DecodeError('Unexpected end-group tag.')
- except (IndexError, TypeError):
- # Now ord(buf[p:p+1]) == ord('') gets TypeError.
- raise message_mod.DecodeError('Truncated message.')
- except struct.error as e:
- raise message_mod.DecodeError(e)
- return length # Return this for legacy reasons.
def SerializeToString(self, **kwargs):
- # Check if the message has all of its required fields set.
- if not self.IsInitialized():
- raise message_mod.EncodeError(
- 'Message %s is missing required fields: %s' % (
- self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors())))
- return self.SerializePartialToString(**kwargs)
-
-
-
-def SetInParent(self)
-
-
-
Sets the _cached_byte_size_dirty bit to true,
-and propagates this to our listener iff this was a state change.
-
-
-Expand source code
-
-
def Modified(self):
- """Sets the _cached_byte_size_dirty bit to true,
- and propagates this to our listener iff this was a state change.
- """
-
- # Note: Some callers check _cached_byte_size_dirty before calling
- # _Modified() as an extra optimization. So, if this method is ever
- # changed such that it does stuff even when _cached_byte_size_dirty is
- # already true, the callers need to be updated.
- if not self._cached_byte_size_dirty:
- self._cached_byte_size_dirty = True
- self._listener_for_children.dirty = True
- self._is_present_in_parent = True
- self._listener.Modified()
Returns the name of the currently set field inside a oneof, or None.
-
-
-Expand source code
-
-
def WhichOneof(self, oneof_name):
- """Returns the name of the currently set field inside a oneof, or None."""
- try:
- field = message_descriptor.oneofs_by_name[oneof_name]
- except KeyError:
- raise ValueError(
- 'Protocol message has no oneof "%s" field.' % oneof_name)
-
- nested_field = self._oneofs.get(field, None)
- if nested_field is not None and self.HasField(nested_field.name):
- return nested_field.name
- else:
- return None
-
+
Field HardwareMessage.typ
@@ -791,35 +241,15 @@ and propagates this to our listener iff this was a state change.