Exemplo n.º 1
0
    def MapTo(self, subject, key, value):
        """Map key-value pair to an objects attribute.

    Args:
      subject: _ObjectMapper of object that will receive new attribute.
      key: Key of attribute.
      value: Value of new attribute.

    Raises:
      UnexpectedAttribute when the key is not a validated attribute of
      the subject value class.
    """
        assert isinstance(subject.value, validation.ValidatedBase)

        try:
            attribute = subject.value.GetValidator(key)
        except validation.ValidationError as err:
            raise yaml_errors.UnexpectedAttribute(err)

        if isinstance(value, _ObjectMapper):

            value.set_value(attribute.expected_type())
            value = value.value
        elif isinstance(value, _ObjectSequencer):

            value.set_constructor(self._GetRepeated(attribute))
            value = value.value

        subject.see(key)
        try:
            subject.value.Set(key, value)
        except validation.ValidationError as e:

            try:
                error_str = str(e)
            except Exception:
                error_str = '<unknown>'

            try:
                value_str = str(value)
            except Exception:
                value_str = '<unknown>'

            e.message = ("Unable to assign value '%s' to attribute '%s':\n%s" %
                         (value_str, key, error_str))
            raise e
        except Exception as e:
            try:
                error_str = str(e)
            except Exception:
                error_str = '<unknown>'

            try:
                value_str = str(value)
            except Exception:
                value_str = '<unknown>'

            message = ("Unable to assign value '%s' to attribute '%s':\n%s" %
                       (value_str, key, error_str))
            raise validation.ValidationError(message, e)
Exemplo n.º 2
0
    def MapTo(self, subject, key, value):
        """Map key-value pair to an objects attribute.

    Args:
      subject: _ObjectMapper of object that will receive new attribute.
      key: Key of attribute.
      value: Value of new attribute.

    Raises:
      UnexpectedAttribute when the key is not a validated attribute of
      the subject value class.
    """
        assert isinstance(subject.value, validation.ValidatedBase)

        try:
            attribute = subject.value.GetValidator(key)
        except validation.ValidationError, err:
            raise yaml_errors.UnexpectedAttribute(err)
Exemplo n.º 3
0
  def MapTo(self, subject, key, value):
    """Map key-value pair to an objects attribute.

    Args:
      subject: _ObjectMapper of object that will receive new attribute.
      key: Key of attribute.
      value: Value of new attribute.

    Raises:
      UnexpectedAttribute when the key is not a validated attribute of
      the subject value class.
    """
    assert subject.value is not None
    if key not in subject.value.ATTRIBUTES:
      raise yaml_errors.UnexpectedAttribute(
          'Unexpected attribute \'%s\' for object of type %s.' %
          (key, str(subject.value.__class__)))

    if isinstance(value, _ObjectMapper):
      value.set_value(subject.value.GetAttribute(key).expected_type())
      value = value.value
    elif isinstance(value, _ObjectSequencer):
      value.set_constructor(self._GetRepeated(subject.value.ATTRIBUTES[key]))
      value = value.value

    subject.see(key)
    try:
      setattr(subject.value, key, value)
    except validation.ValidationError, e:
      try:
        error_str = str(e)
      except Exception:
        error_str = '<unknown>'

      try:
        value_str = str(value)
      except Exception:
        value_str = '<unknown>'

      e.message = ("Unable to assign value '%s' to attribute '%s':\n%s" %
                   (value_str, key, error_str))
      raise e