Merge remote branch 'remotes/jwandborg/f571_closing_storage_objects'

This commit is contained in:
Christopher Allan Webber 2011-09-05 21:14:22 -05:00
commit 949c7afd37
2 changed files with 34 additions and 16 deletions

View File

@ -16,7 +16,6 @@
import Image import Image
from contextlib import contextmanager
from celery.task import Task from celery.task import Task
from celery import registry from celery import registry
@ -36,14 +35,6 @@ def create_pub_filepath(entry, filename):
filename]) filename])
@contextmanager
def closing(callback):
try:
yield callback
finally:
pass
################################ ################################
# Media processing initial steps # Media processing initial steps
################################ ################################
@ -144,7 +135,7 @@ def process_image(entry):
thumb_filepath = create_pub_filepath(entry, 'thumbnail.jpg') thumb_filepath = create_pub_filepath(entry, 'thumbnail.jpg')
thumb_file = mgg.public_store.get_file(thumb_filepath, 'w') thumb_file = mgg.public_store.get_file(thumb_filepath, 'w')
with closing(thumb_file): with thumb_file:
thumb.save(thumb_file, "JPEG", quality=90) thumb.save(thumb_file, "JPEG", quality=90)
# If the size of the original file exceeds the specified size of a `medium` # If the size of the original file exceeds the specified size of a `medium`
@ -162,7 +153,7 @@ def process_image(entry):
medium_filepath = create_pub_filepath(entry, 'medium.jpg') medium_filepath = create_pub_filepath(entry, 'medium.jpg')
medium_file = mgg.public_store.get_file(medium_filepath, 'w') medium_file = mgg.public_store.get_file(medium_filepath, 'w')
with closing(medium_file): with medium_file:
medium.save(medium_file, "JPEG", quality=90) medium.save(medium_file, "JPEG", quality=90)
medium_processed = True medium_processed = True
@ -173,7 +164,7 @@ def process_image(entry):
with queued_file: with queued_file:
original_filepath = create_pub_filepath(entry, queued_filepath[-1]) original_filepath = create_pub_filepath(entry, queued_filepath[-1])
with closing(mgg.public_store.get_file(original_filepath, 'wb')) as original_file: with mgg.public_store.get_file(original_filepath, 'wb') as original_file:
original_file.write(queued_file.read()) original_file.write(queued_file.read())
mgg.queue_store.delete_file(queued_filepath) mgg.queue_store.delete_file(queued_filepath)

View File

@ -291,7 +291,7 @@ class CloudFilesStorage(StorageInterface):
if mimetype: if mimetype:
obj.content_type = mimetype[0] obj.content_type = mimetype[0]
return StorageObjectWrapper(obj, *args, **kwargs) return CloudFilesStorageObjectWrapper(obj, *args, **kwargs)
def delete_file(self, filepath): def delete_file(self, filepath):
# TODO: Also delete unused directories if empty (safely, with # TODO: Also delete unused directories if empty (safely, with
@ -305,7 +305,7 @@ class CloudFilesStorage(StorageInterface):
self._resolve_filepath(filepath)]) self._resolve_filepath(filepath)])
class StorageObjectWrapper(): class CloudFilesStorageObjectWrapper():
""" """
Wrapper for python-cloudfiles's cloudfiles.storage_object.Object Wrapper for python-cloudfiles's cloudfiles.storage_object.Object
used to circumvent the mystic `medium.jpg` corruption issue, where used to circumvent the mystic `medium.jpg` corruption issue, where
@ -322,11 +322,38 @@ class StorageObjectWrapper():
return self.storage_object.read(*args, **kwargs) return self.storage_object.read(*args, **kwargs)
def write(self, data, *args, **kwargs): def write(self, data, *args, **kwargs):
"""
write data to the cloudfiles storage object
The original motivation for this wrapper is to ensure
that buffered writing to a cloudfiles storage object does not overwrite
any preexisting data.
Currently this method does not support any write modes except "append".
However if we should need it it would be easy implement.
"""
if self.storage_object.size and type(data) == str: if self.storage_object.size and type(data) == str:
data = self.read() + data data = self.read() + data
self.storage_object.write(data, *args, **kwargs) self.storage_object.write(data, *args, **kwargs)
def close(self):
pass
def __enter__(self):
"""
Context Manager API implementation
http://docs.python.org/library/stdtypes.html#context-manager-types
"""
return self
def __exit__(self, *exc_info):
"""
Context Manger API implementation
see self.__enter__()
"""
self.close()
# ------------ # ------------
# MountStorage # MountStorage