<s> import os <EOL> import datetime <EOL> import six <EOL> from django . core . files . storage import Storage , default_storage <EOL> from django . core . files . uploadedfile import InMemoryUploadedFile <EOL> class InMemoryStorage ( Storage ) : <EOL> def __init__ ( self ) : <EOL> self . files = { } <EOL> def _open ( self , name , mode ) : <EOL> return self . files [ name ] <EOL> def _save ( self , name , content ) : <EOL> self . files [ name ] = content <EOL> return name <EOL> def delete ( self , name ) : <EOL> del self . files [ name ] <EOL> def exists ( self , name ) : <EOL> return name in self . files <EOL> def listdir ( self , path ) : <EOL> directories , files = set ( ) , [ ] <EOL> for name in self . files : <EOL> if name . startswith ( path ) : <EOL> without_path = name [ len ( path ) : ] <EOL> try : <EOL> slash_index = without_path . index ( '<STR_LIT:/>' ) <EOL> directories . add ( without_path [ : slash_index ] ) <EOL> except ValueError : <EOL> files . append ( without_path ) <EOL> return list ( directories ) , files <EOL> def size ( self , name ) : <EOL> file_instance = self . files [ name ] <EOL> file_instance . seek ( <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> return file_instance . tell ( ) <EOL> def url ( self , name ) : <EOL> return name <EOL> def accessed_time ( self , name ) : <EOL> return datetime . datetime . now ( ) <EOL> def created_time ( self , name ) : <EOL> return datetime . datetime . now ( ) <EOL> def modified_time ( self , name ) : <EOL> return datetime . datetime . now ( ) <EOL> def clear ( self ) : <EOL> self . files = { } <EOL> class InMemoryStorageMixin ( object ) : <EOL> storage = InMemoryStorage <EOL> def _pre_setup ( self ) : <EOL> self . _wrapped_storage = default_storage . _wrapped <EOL> default_storage . _wrapped = self . storage ( ) <EOL> super ( InMemoryStorageMixin , self ) . _pre_setup ( ) <EOL> def _post_teardown ( self ) : <EOL> super ( InMemoryStorageMixin , self ) . _post_teardown ( ) <EOL> try : <EOL> default_storage . clear ( ) <EOL> except AttributeError : <EOL> pass <EOL> default_storage . _wrapped = self . _wrapped_storage <EOL> def create_inmemory_file ( file_name = '<STR_LIT>' , content = b'<STR_LIT>' , content_type = None ) : <EOL> stream = six . BytesIO ( ) <EOL> stream . write ( content ) <EOL> file = InMemoryUploadedFile ( stream , None , file_name , content_type , stream . tell ( ) , None ) <EOL> file . seek ( <NUM_LIT:0> ) <EOL> return file <EOL> def create_inmemory_image ( file_name = '<STR_LIT>' , format = None , width = <NUM_LIT:200> , height = <NUM_LIT:200> , content_type = None ) : <EOL> from PIL import Image <EOL> if not format : <EOL> _ , extension = os . path . splitext ( file_name ) <EOL> format = extension [ <NUM_LIT:1> : ] . upper ( ) <EOL> if not content_type : <EOL> content_type = '<STR_LIT>' . format ( format ) <EOL> stream = six . BytesIO ( ) <EOL> size = ( width , height ) <EOL> color = ( <NUM_LIT:255> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> image = Image . new ( '<STR_LIT>' , size , color ) <EOL> image . save ( stream , format = format ) <EOL> image_file = InMemoryUploadedFile ( stream , None , file_name , content_type , stream . tell ( ) , None ) <EOL> image_file . seek ( <NUM_LIT:0> ) <EOL> return image_file </s>
