コード例 #1
0
def _DefaultValueConstructorForField(field):
    if field.label == _FieldDescriptor.LABEL_REPEATED:
        if field.has_default_value and field.default_value != []:
            raise ValueError(
                'Repeated field default value not empty list: %s' %
                field.default_value)
        if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
            message_type = field.message_type

            def MakeRepeatedMessageDefault(message):
                return containers.RepeatedCompositeFieldContainer(
                    message._listener_for_children, field.message_type)

            return MakeRepeatedMessageDefault
        type_checker = type_checkers.GetTypeChecker(field.cpp_type, field.type)

        def MakeRepeatedScalarDefault(message):
            return containers.RepeatedScalarFieldContainer(
                message._listener_for_children, type_checker)

        return MakeRepeatedScalarDefault
    if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
        message_type = field.message_type

        def MakeSubMessageDefault(message):
            result = message_type._concrete_class()
            result._SetListener(message._listener_for_children)
            return result

        return MakeSubMessageDefault

    def MakeScalarDefault(message):
        return field.default_value

    return MakeScalarDefault
コード例 #2
0
def _AddPropertiesForNonRepeatedScalarField(field, cls):
  """Adds a public property for a nonrepeated, scalar protocol message field.
  Clients can use this property to get and directly set the value of the field.
  Note that when the client sets the value of a field by using this property,
  all necessary "has" bits are set as a side-effect, and we also perform
  type-checking.

  Args:
    field: A FieldDescriptor for this field.
    cls: The class we're constructing.
  """
  proto_field_name = field.name
  property_name = _PropertyName(proto_field_name)
  type_checker = type_checkers.GetTypeChecker(field.cpp_type, field.type)
  default_value = field.default_value
  valid_values = set()

  def getter(self):
    return self._fields.get(field, default_value)
  getter.__module__ = None
  getter.__doc__ = 'Getter for %s.' % proto_field_name
  def setter(self, new_value):
    type_checker.CheckValue(new_value)
    self._fields[field] = new_value
    # Check _cached_byte_size_dirty inline to improve performance, since scalar
    # setters are called frequently.
    if not self._cached_byte_size_dirty:
      self._Modified()

  setter.__module__ = None
  setter.__doc__ = 'Setter for %s.' % proto_field_name

  # Add a property to encapsulate the getter/setter.
  doc = 'Magic attribute generated for "%s" proto field.' % proto_field_name
  setattr(cls, property_name, property(getter, setter, doc=doc))
コード例 #3
0
 def __setitem__(self, extension_handle, value):
     _VerifyExtensionHandle(self._extended_message, extension_handle)
     if extension_handle.label == _FieldDescriptor.LABEL_REPEATED or extension_handle.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
         raise TypeError('Cannot assign to extension "%s" because it is a repeated or composite type.' % extension_handle.full_name)
     type_checker = type_checkers.GetTypeChecker(extension_handle.cpp_type, extension_handle.type)
     type_checker.CheckValue(value)
     self._extended_message._fields[extension_handle] = value
     self._extended_message._Modified()
コード例 #4
0
def _DefaultValueConstructorForField(field):
    """Returns a function which returns a default value for a field.

  Args:
    field: FieldDescriptor object for this field.

  The returned function has one argument:
    message: Message instance containing this field, or a weakref proxy
      of same.

  That function in turn returns a default value for this field.  The default
    value may refer back to |message| via a weak reference.
  """

    if field.label == _FieldDescriptor.LABEL_REPEATED:
        if field.has_default_value and field.default_value != []:
            raise ValueError(
                'Repeated field default value not empty list: %s' %
                (field.default_value))
        if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
            # We can't look at _concrete_class yet since it might not have
            # been set.  (Depends on order in which we initialize the classes).
            message_type = field.message_type

            def MakeRepeatedMessageDefault(message):
                return containers.RepeatedCompositeFieldContainer(
                    message._listener_for_children, field.message_type)

            return MakeRepeatedMessageDefault
        else:
            type_checker = type_checkers.GetTypeChecker(
                field.cpp_type, field.type)

            def MakeRepeatedScalarDefault(message):
                return containers.RepeatedScalarFieldContainer(
                    message._listener_for_children, type_checker)

            return MakeRepeatedScalarDefault

    if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
        # _concrete_class may not yet be initialized.
        message_type = field.message_type

        def MakeSubMessageDefault(message):
            result = message_type._concrete_class()
            result._SetListener(message._listener_for_children)
            return result

        return MakeSubMessageDefault

    def MakeScalarDefault(message):
        # TODO(protobuf-team): This may be broken since there may not be
        # default_value.  Combine with has_default_value somehow.
        return field.default_value

    return MakeScalarDefault
コード例 #5
0
def _AddPropertiesForNonRepeatedScalarField(field, cls):
    """Adds a public property for a nonrepeated, scalar protocol message field.
  Clients can use this property to get and directly set the value of the field.
  Note that when the client sets the value of a field by using this property,
  all necessary "has" bits are set as a side-effect, and we also perform
  type-checking.

  Args:
    field: A FieldDescriptor for this field.
    cls: The class we're constructing.
  """
    proto_field_name = field.name
    property_name = _PropertyName(proto_field_name)
    type_checker = type_checkers.GetTypeChecker(field)
    default_value = field.default_value
    valid_values = set()

    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)

    getter.__module__ = None
    getter.__doc__ = 'Getter for %s.' % proto_field_name

    def field_setter(self, new_value):
        # pylint: disable=protected-access
        self._fields[field] = type_checker.CheckValue(new_value)
        # Check _cached_byte_size_dirty inline to improve performance, since scalar
        # setters are called frequently.
        if not self._cached_byte_size_dirty:
            self._Modified()

    if field.containing_oneof is not None:

        def setter(self, new_value):
            field_setter(self, new_value)
            self._UpdateOneofState(field)
    else:
        setter = field_setter

    setter.__module__ = None
    setter.__doc__ = 'Setter for %s.' % proto_field_name

    # Add a property to encapsulate the getter/setter.
    doc = 'Magic attribute generated for "%s" proto field.' % proto_field_name
    setattr(cls, property_name, property(getter, setter, doc=doc))
コード例 #6
0
    def __setitem__(self, extension_handle, value):
        """If extension_handle specifies a non-repeated, scalar extension
    field, sets the value of that field.
    """

        _VerifyExtensionHandle(self._extended_message, extension_handle)

        if (extension_handle.label == _FieldDescriptor.LABEL_REPEATED or
                extension_handle.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE):
            raise TypeError(
                'Cannot assign to extension "%s" because it is a repeated or '
                'composite type.' % extension_handle.full_name)

        # It's slightly wasteful to lookup the type checker each time,
        # but we expect this to be a vanishingly uncommon case anyway.
        type_checker = type_checkers.GetTypeChecker(extension_handle.cpp_type,
                                                    extension_handle.type)
        type_checker.CheckValue(value)
        self._extended_message._fields[extension_handle] = value
        self._extended_message._Modified()
コード例 #7
0
def _AddPropertiesForNonRepeatedScalarField(field, cls):
    proto_field_name = field.name
    property_name = _PropertyName(proto_field_name)
    type_checker = type_checkers.GetTypeChecker(field.cpp_type, field.type)
    default_value = field.default_value
    valid_values = set()

    def getter(self):
        return self._fields.get(field, default_value)

    getter.__module__ = None
    getter.__doc__ = 'Getter for %s.' % proto_field_name

    def setter(self, new_value):
        type_checker.CheckValue(new_value)
        self._fields[field] = new_value
        if not self._cached_byte_size_dirty:
            self._Modified()

    setter.__module__ = None
    setter.__doc__ = 'Setter for %s.' % proto_field_name
    doc = 'Magic attribute generated for "%s" proto field.' % proto_field_name
    setattr(cls, property_name, property(getter, setter, doc=doc))