def __set__(self, model_instance, value): """Compresses the value when the property is set.""" if not value: self.length = 0 super(CompressedUtf8BlobProperty, self).__set__(model_instance, value) else: self.length = len(value) value = compress.CompressedText(value, encoding='utf-8').Compressed() super(CompressedUtf8BlobProperty, self).__set__(model_instance, value)
def get_value_for_datastore(self, model_instance): """Compresses the blob value on it's way to Datastore.""" value = super(CompressedUtf8BlobProperty, self).get_value_for_datastore(model_instance) if value is None: self.length = 0 else: self.length = len(value) return db.Blob( compress.CompressedText(value, encoding='utf-8').Compressed())
def __get__(self, model_instance, model_class): """Decompresses the blob value when the property is accessed.""" value = super(CompressedUtf8BlobProperty, self).__get__(model_instance, model_class) # __get__ super returns self when the model_instance is None, which happens # when the property is accessed by a static member of a class, as opposed to # by an instance. When this happens, return our class instance. if value is self: return self return unicode(compress.CompressedText( value, encoding='utf-8')).encode('utf-8')
def _GetInstance(self, arg=None, compression_threshold=0, **kwargs): """Return an instance of compress.CompressedText to test.""" return compress.CompressedText( arg=arg, compression_threshold=compression_threshold, **kwargs)