Пример #1
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

        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))
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 _IsMapField(field):
    return _GetInitializeDefaultForMap(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
    else:
      type_checker = type_checkers.GetTypeChecker(field)
      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):
      assert getattr(message_type, '_concrete_class', None), (
          'Uninitialized concrete class found for field %r (message type %r)'
          % (field.full_name, message_type.full_name))
      result = message_type._concrete_class()
      result._SetListener(
          _OneofListener(message, field)
          if field.containing_oneof is not None
          else message._listener_for_children)
      return result
    return MakeSubMessageDefault

  def MakeScalarDefault(message):


    return field.default_value
  return MakeScalarDefault
Пример #3
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.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
        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:

        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
def _GetInitializeDefaultForMap(field):
  if field.label != _FieldDescriptor.LABEL_REPEATED:
    raise ValueError('map_entry set on non-repeated field %s' % (
        field.name))
  fields_by_name = field.message_type.fields_by_name
  key_checker = type_checkers.GetTypeChecker(fields_by_name['key'])

  value_field = fields_by_name['value']
  if _IsMessageMapField(field):
    def MakeMessageMapDefault(message):
      return containers.MessageMap(
          message._listener_for_children, value_field.message_type, key_checker,
          field.message_type)
    return MakeMessageMapDefault
  else:
    value_checker = type_checkers.GetTypeChecker(value_field)
    def MakePrimitiveMapDefault(message):
      return containers.ScalarMap(
          message._listener_for_children, key_checker, value_checker,
          field.message_type)
    return MakePrimitiveMapDefault
Пример #5
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)

        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()