def _assert_valid_utf8(string, desc):
    """Asserts that a string is valid UTF8.

  Args:
    string: string to check
    desc: description of the string (used in error message)

  Raises:
    ValidationError: if the string is not valid UTF8
  """
    _assert_condition(datastore_pbs.is_valid_utf8(string), "The %s is not valid UTF-8." % desc)
def _assert_valid_utf8(string, desc):
    """Asserts that a string is valid UTF8.

  Args:
    string: string to check
    desc: description of the string (used in error message)

  Raises:
    ValidationError: if the string is not valid UTF8
  """
    _assert_condition(datastore_pbs.is_valid_utf8(string),
                      'The %s is not valid UTF-8.' % desc)
    def v3_property_to_v1_value(self, v3_property, indexed, v1_value):
        v1_value.Clear()
        # TODO
        v3_property_value = v3_property.value()
        v3_meaning = v3_property.meaning()
        v3_uri_meaning = None
        if v3_property.meaning_uri():
            v3_uri_meaning = v3_property.meaning_uri()

        if not self.__is_v3_property_value_union_valid(v3_property_value):
            v3_meaning = None
            v3_uri_meaning = None
        elif v3_meaning == entity_pb.Property.NO_MEANING:
            v3_meaning = None
        elif not self.__is_v3_property_value_meaning_valid(v3_property_value, v3_meaning):
            v3_meaning = None

        is_zlib_value = False
        if v3_uri_meaning:
            if v3_uri_meaning == datastore_pbs.URI_MEANING_ZLIB:
                if v3_property_value.has_stringvalue():
                    is_zlib_value = True
                    if v3_meaning != entity_pb.Property.BLOB:
                        v3_meaning = entity_pb.Property.BLOB
                else:
                    pass
            else:
                pass

        if v3_property_value.has_booleanvalue():
            v1_value.boolean_value = v3_property_value.booleanvalue()
        elif v3_property_value.has_int64value():
            if v3_meaning == entity_pb.Property.GD_WHEN:
                v1_value.timestamp_microseconds_value = v3_property_value.int64value()
                v3_meaning = None
            else:
                v1_value.integer_value = v3_property_value.int64value()
        elif v3_property_value.has_doublevalue():
            v1_value.double_value = v3_property_value.double_value()
        elif v3_property_value.has_referencevalue():
            v3_ref = entity_pb.Reference()
            self.__v3_reference_value_to_v3_reference(v3_property_value.referencevalue(), v3_ref)
            self.v3_to_v1_key(v3_ref, v1_value.key)
        elif v3_property_value.has_stringvalue():
            if v3_meaning == entity_pb.Property.ENTITY_PROTO:
                serialized_entity_v3 = v3_property_value.stringvalue()
                v3_entity = entity_pb.EntityProto()

                v3_entity.ParsePartialFromString(serialized_entity_v3)
                self.v3_to_v1_entity(v3_entity, v1_value.entity_value)
                v3_meaning = None
            elif (v3_meaning == entity_pb.Property.BLOB or v3_meaning == entity_pb.Property.BYTESTRING):
                v1_value.blob_value = v3_property_value.stringvalue()

                if indexed or v3_meaning == entity_pb.Property.BLOB:
                    v3_meaning = None
            else:
                string_value = v3_property_value.stringvalue()
                if datastore_pbs.is_valid_utf8(string_value):
                    if v3_meaning == entity_pb.Property.BLOBKEY:
                        v1_value.blob_key_value = unicode(string_value, 'utf8')
                        v3_meaning= None
                    else:
                        v1_value.string_value = unicode(string_value, 'utf8')
                else:
                    v1_value.blob_value = string_value

                    if v3_meaning != entity_pb.Property.INDEX_VALUE:
                        v3_meaning = None
        elif v3_property_value.has_pointvalue():
            self.__v3_to_v1_point_entity(v3_property_value.pointvalue(), v1_value.entity_value)
            if v3_meaning != entity_pb.Property.GEORESS_POINT:
                v1_value.meaning = datastore_pbs.MEANING_PREDEFINED_ENTITY_POINT
                v3_meaning = None
        elif v3_property_value.has_uservalue():
            self.__v3_to_v1_user_entity(v3_property_value.uservalue(), v1_value.entity_value)
            v1_value.meaning = datastore_pbs.MEANING_PREDEFINED_ENTITY_USER
        else:
            pass

        if is_zlib_value:
            v1_value.meaning = datastore_pbs.MEANING_ZLIB
        elif v3_meaning:
            v1_value.meaning = v3_meaning

        if indexed != v1_value.indexed:
            v1_value.indexed = indexed
예제 #4
0
    def v3_property_to_v1_value(self, v3_property, indexed, v1_value):
        v1_value.Clear()
        # TODO
        v3_property_value = v3_property.value()
        v3_meaning = v3_property.meaning()
        v3_uri_meaning = None
        if v3_property.meaning_uri():
            v3_uri_meaning = v3_property.meaning_uri()

        if not self.__is_v3_property_value_union_valid(v3_property_value):
            v3_meaning = None
            v3_uri_meaning = None
        elif v3_meaning == entity_pb.Property.NO_MEANING:
            v3_meaning = None
        elif not self.__is_v3_property_value_meaning_valid(
                v3_property_value, v3_meaning):
            v3_meaning = None

        is_zlib_value = False
        if v3_uri_meaning:
            if v3_uri_meaning == datastore_pbs.URI_MEANING_ZLIB:
                if v3_property_value.has_stringvalue():
                    is_zlib_value = True
                    if v3_meaning != entity_pb.Property.BLOB:
                        v3_meaning = entity_pb.Property.BLOB
                else:
                    pass
            else:
                pass

        if v3_property_value.has_booleanvalue():
            v1_value.boolean_value = v3_property_value.booleanvalue()
        elif v3_property_value.has_int64value():
            if v3_meaning == entity_pb.Property.GD_WHEN:
                v1_value.timestamp_microseconds_value = v3_property_value.int64value(
                )
                v3_meaning = None
            else:
                v1_value.integer_value = v3_property_value.int64value()
        elif v3_property_value.has_doublevalue():
            v1_value.double_value = v3_property_value.double_value()
        elif v3_property_value.has_referencevalue():
            v3_ref = entity_pb.Reference()
            self.__v3_reference_value_to_v3_reference(
                v3_property_value.referencevalue(), v3_ref)
            self.v3_to_v1_key(v3_ref, v1_value.key)
        elif v3_property_value.has_stringvalue():
            if v3_meaning == entity_pb.Property.ENTITY_PROTO:
                serialized_entity_v3 = v3_property_value.stringvalue()
                v3_entity = entity_pb.EntityProto()

                v3_entity.ParsePartialFromString(serialized_entity_v3)
                self.v3_to_v1_entity(v3_entity, v1_value.entity_value)
                v3_meaning = None
            elif (v3_meaning == entity_pb.Property.BLOB
                  or v3_meaning == entity_pb.Property.BYTESTRING):
                v1_value.blob_value = v3_property_value.stringvalue()

                if indexed or v3_meaning == entity_pb.Property.BLOB:
                    v3_meaning = None
            else:
                string_value = v3_property_value.stringvalue()
                if datastore_pbs.is_valid_utf8(string_value):
                    if v3_meaning == entity_pb.Property.BLOBKEY:
                        v1_value.blob_key_value = unicode(string_value, 'utf8')
                        v3_meaning = None
                    else:
                        v1_value.string_value = unicode(string_value, 'utf8')
                else:
                    v1_value.blob_value = string_value

                    if v3_meaning != entity_pb.Property.INDEX_VALUE:
                        v3_meaning = None
        elif v3_property_value.has_pointvalue():
            self.__v3_to_v1_point_entity(v3_property_value.pointvalue(),
                                         v1_value.entity_value)
            if v3_meaning != entity_pb.Property.GEORESS_POINT:
                v1_value.meaning = datastore_pbs.MEANING_PREDEFINED_ENTITY_POINT
                v3_meaning = None
        elif v3_property_value.has_uservalue():
            self.__v3_to_v1_user_entity(v3_property_value.uservalue(),
                                        v1_value.entity_value)
            v1_value.meaning = datastore_pbs.MEANING_PREDEFINED_ENTITY_USER
        else:
            pass

        if is_zlib_value:
            v1_value.meaning = datastore_pbs.MEANING_ZLIB
        elif v3_meaning:
            v1_value.meaning = v3_meaning

        if indexed != v1_value.indexed:
            v1_value.indexed = indexed