<s> from copy import copy <EOL> from django . conf import settings <EOL> from django . core . files import File <EOL> from django . core . files . images import ImageFile <EOL> from django . utils . functional import SimpleLazyObject <EOL> from . . files import BaseIKFile <EOL> from . . registry import generator_registry <EOL> from . . signals import content_required , existence_required <EOL> from . . utils import get_logger , get_singleton , generate , get_by_qname <EOL> class ImageCacheFile ( BaseIKFile , ImageFile ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , generator , name = None , storage = None , cachefile_backend = None , cachefile_strategy = None ) : <EOL> """<STR_LIT>""" <EOL> self . generator = generator <EOL> if not name : <EOL> try : <EOL> name = generator . cachefile_name <EOL> except AttributeError : <EOL> fn = get_by_qname ( settings . IMAGEKIT_CACHEFILE_NAMER , '<STR_LIT>' ) <EOL> name = fn ( generator ) <EOL> self . name = name <EOL> storage = storage or getattr ( generator , '<STR_LIT>' , <EOL> None ) or get_singleton ( settings . IMAGEKIT_DEFAULT_FILE_STORAGE , <EOL> '<STR_LIT>' ) <EOL> self . cachefile_backend = ( <EOL> cachefile_backend <EOL> or getattr ( generator , '<STR_LIT>' , None ) <EOL> or get_singleton ( settings . IMAGEKIT_DEFAULT_CACHEFILE_BACKEND , <EOL> '<STR_LIT>' ) ) <EOL> self . cachefile_strategy = ( <EOL> cachefile_strategy <EOL> or getattr ( generator , '<STR_LIT>' , None ) <EOL> or get_singleton ( settings . IMAGEKIT_DEFAULT_CACHEFILE_STRATEGY , <EOL> '<STR_LIT>' ) <EOL> ) <EOL> super ( ImageCacheFile , self ) . __init__ ( storage = storage ) <EOL> def _require_file ( self ) : <EOL> if getattr ( self , '<STR_LIT>' , None ) is None : <EOL> content_required . send ( sender = self , file = self ) <EOL> self . _file = self . storage . open ( self . name , '<STR_LIT:rb>' ) <EOL> def _storage_attr ( self , attr ) : <EOL> if getattr ( self , '<STR_LIT>' , None ) is None : <EOL> existence_required . send ( sender = self , file = self ) <EOL> fn = getattr ( self . storage , attr ) <EOL> return fn ( self . name ) <EOL> @ property <EOL> def path ( self ) : <EOL> return self . _storage_attr ( '<STR_LIT:path>' ) <EOL> @ property <EOL> def url ( self ) : <EOL> return self . _storage_attr ( '<STR_LIT:url>' ) <EOL> def generate ( self , force = False ) : <EOL> """<STR_LIT>""" <EOL> if force or getattr ( self , '<STR_LIT>' , None ) is None : <EOL> self . cachefile_backend . generate ( self , force ) <EOL> def _generate ( self ) : <EOL> content = generate ( self . generator ) <EOL> actual_name = self . storage . save ( self . name , content ) <EOL> content . seek ( <NUM_LIT:0> ) <EOL> self . file = File ( content ) <EOL> if actual_name != self . name : <EOL> get_logger ( ) . warning ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( <EOL> self . storage , <EOL> self . name , actual_name , <EOL> self . cachefile_backend <EOL> ) <EOL> ) <EOL> def __bool__ ( self ) : <EOL> if not self . name : <EOL> return False <EOL> existence_required . send ( sender = self , file = self ) <EOL> try : <EOL> check = self . cachefile_strategy . should_verify_existence ( self ) <EOL> except AttributeError : <EOL> check = getattr ( self . cachefile_backend , '<STR_LIT>' , False ) <EOL> return self . cachefile_backend . exists ( self ) if check else True <EOL> def __getstate__ ( self ) : <EOL> state = copy ( self . __dict__ ) <EOL> state . pop ( '<STR_LIT>' , None ) <EOL> return state <EOL> def __nonzero__ ( self ) : <EOL> return self . __bool__ ( ) <EOL> class LazyImageCacheFile ( SimpleLazyObject ) : <EOL> def __init__ ( self , generator_id , * args , ** kwargs ) : <EOL> def setup ( ) : <EOL> generator = generator_registry . get ( generator_id , * args , ** kwargs ) <EOL> return ImageCacheFile ( generator ) <EOL> super ( LazyImageCacheFile , self ) . __init__ ( setup ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . __class__ . __name__ , str ( self ) or '<STR_LIT:None>' ) </s>
