<s> from __future__ import unicode_literals <EOL> import logging <EOL> import os <EOL> import re <EOL> from sorl . thumbnail . compat import string_type , text_type <EOL> from sorl . thumbnail . conf import settings , defaults as default_settings <EOL> from sorl . thumbnail . helpers import tokey , serialize <EOL> from sorl . thumbnail . images import ImageFile , DummyImageFile <EOL> from sorl . thumbnail import default <EOL> from sorl . thumbnail . parsers import parse_geometry <EOL> logger = logging . getLogger ( __name__ ) <EOL> EXTENSIONS = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> class ThumbnailBackend ( object ) : <EOL> """<STR_LIT>""" <EOL> default_options = { <EOL> '<STR_LIT>' : settings . THUMBNAIL_FORMAT , <EOL> '<STR_LIT>' : settings . THUMBNAIL_QUALITY , <EOL> '<STR_LIT>' : settings . THUMBNAIL_COLORSPACE , <EOL> '<STR_LIT>' : settings . THUMBNAIL_UPSCALE , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : settings . THUMBNAIL_PADDING , <EOL> '<STR_LIT>' : settings . THUMBNAIL_PADDING_COLOR , <EOL> } <EOL> extra_options = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) <EOL> def file_extension ( self , source ) : <EOL> return os . path . splitext ( source . name ) [ <NUM_LIT:1> ] . lower ( ) <EOL> def _get_format ( self , source ) : <EOL> file_extension = self . file_extension ( source ) <EOL> if file_extension == '<STR_LIT>' or file_extension == '<STR_LIT>' : <EOL> return '<STR_LIT>' <EOL> elif file_extension == '<STR_LIT>' : <EOL> return '<STR_LIT>' <EOL> elif file_extension == '<STR_LIT>' : <EOL> return '<STR_LIT>' <EOL> else : <EOL> from django . conf import settings <EOL> return getattr ( settings , '<STR_LIT>' , default_settings . THUMBNAIL_FORMAT ) <EOL> def get_thumbnail ( self , file_ , geometry_string , ** options ) : <EOL> """<STR_LIT>""" <EOL> logger . debug ( text_type ( '<STR_LIT>' ) , file_ , geometry_string ) <EOL> if file_ : <EOL> source = ImageFile ( file_ ) <EOL> elif settings . THUMBNAIL_DUMMY : <EOL> return DummyImageFile ( geometry_string ) <EOL> else : <EOL> return None <EOL> if settings . THUMBNAIL_PRESERVE_FORMAT : <EOL> options . setdefault ( '<STR_LIT>' , self . _get_format ( source ) ) <EOL> for key , value in self . default_options . items ( ) : <EOL> options . setdefault ( key , value ) <EOL> for key , attr in self . extra_options : <EOL> value = getattr ( settings , attr ) <EOL> if value != getattr ( default_settings , attr ) : <EOL> options . setdefault ( key , value ) <EOL> name = self . _get_thumbnail_filename ( source , geometry_string , options ) <EOL> thumbnail = ImageFile ( name , default . storage ) <EOL> cached = default . kvstore . get ( thumbnail ) <EOL> if cached : <EOL> return cached <EOL> if settings . THUMBNAIL_FORCE_OVERWRITE or not thumbnail . exists ( ) : <EOL> try : <EOL> source_image = default . engine . get_image ( source ) <EOL> except IOError as e : <EOL> logger . exception ( e ) <EOL> if settings . THUMBNAIL_DUMMY : <EOL> return DummyImageFile ( geometry_string ) <EOL> else : <EOL> logger . warn ( text_type ( '<STR_LIT>' ) , <EOL> file_ , geometry_string ) <EOL> return thumbnail <EOL> image_info = default . engine . get_image_info ( source_image ) <EOL> options [ '<STR_LIT>' ] = image_info <EOL> size = default . engine . get_image_size ( source_image ) <EOL> source . set_size ( size ) <EOL> try : <EOL> self . _create_thumbnail ( source_image , geometry_string , options , <EOL> thumbnail ) <EOL> self . _create_alternative_resolutions ( source_image , geometry_string , <EOL> options , thumbnail . name ) <EOL> finally : <EOL> default . engine . cleanup ( source_image ) <EOL> default . kvstore . get_or_set ( source ) <EOL> default . kvstore . set ( thumbnail , source ) <EOL> return thumbnail <EOL> def delete ( self , file_ , delete_file = True ) : <EOL> """<STR_LIT>""" <EOL> image_file = ImageFile ( file_ ) <EOL> if delete_file : <EOL> image_file . delete ( ) <EOL> default . kvstore . delete ( image_file ) <EOL> def _create_thumbnail ( self , source_image , geometry_string , options , <EOL> thumbnail ) : <EOL> """<STR_LIT>""" <EOL> logger . debug ( text_type ( '<STR_LIT>' ) , <EOL> thumbnail . name , geometry_string , options ) <EOL> ratio = default . engine . get_image_ratio ( source_image , options ) <EOL> geometry = parse_geometry ( geometry_string , ratio ) <EOL> image = default . engine . create ( source_image , geometry , options ) <EOL> default . engine . write ( image , options , thumbnail ) <EOL> size = default . engine . get_image_size ( image ) <EOL> thumbnail . set_size ( size ) <EOL> def _create_alternative_resolutions ( self , source_image , geometry_string , <EOL> options , name ) : <EOL> """<STR_LIT>""" <EOL> ratio = default . engine . get_image_ratio ( source_image , options ) <EOL> geometry = parse_geometry ( geometry_string , ratio ) <EOL> file_name , dot_file_ext = os . path . splitext ( name ) <EOL> for resolution in settings . THUMBNAIL_ALTERNATIVE_RESOLUTIONS : <EOL> resolution_geometry = ( int ( geometry [ <NUM_LIT:0> ] * resolution ) , int ( geometry [ <NUM_LIT:1> ] * resolution ) ) <EOL> resolution_options = options . copy ( ) <EOL> if '<STR_LIT>' in options and isinstance ( options [ '<STR_LIT>' ] , string_type ) : <EOL> crop = options [ '<STR_LIT>' ] . split ( "<STR_LIT:U+0020>" ) <EOL> for i in range ( len ( crop ) ) : <EOL> s = re . match ( "<STR_LIT>" , crop [ i ] ) <EOL> if s : <EOL> crop [ i ] = "<STR_LIT>" % int ( int ( s . group ( <NUM_LIT:1> ) ) * resolution ) <EOL> resolution_options [ '<STR_LIT>' ] = "<STR_LIT:U+0020>" . join ( crop ) <EOL> image = default . engine . create ( source_image , resolution_geometry , options ) <EOL> thumbnail_name = '<STR_LIT>' % { <EOL> '<STR_LIT>' : file_name , <EOL> '<STR_LIT>' : '<STR_LIT>' % resolution , <EOL> '<STR_LIT>' : dot_file_ext <EOL> } <EOL> thumbnail = ImageFile ( thumbnail_name , default . storage ) <EOL> default . engine . write ( image , resolution_options , thumbnail ) <EOL> size = default . engine . get_image_size ( image ) <EOL> thumbnail . set_size ( size ) <EOL> def _get_thumbnail_filename ( self , source , geometry_string , options ) : <EOL> """<STR_LIT>""" <EOL> key = tokey ( source . key , geometry_string , serialize ( options ) ) <EOL> path = '<STR_LIT>' % ( key [ : <NUM_LIT:2> ] , key [ <NUM_LIT:2> : <NUM_LIT:4> ] , key ) <EOL> return '<STR_LIT>' % ( settings . THUMBNAIL_PREFIX , path , EXTENSIONS [ options [ '<STR_LIT>' ] ] ) </s>
