<s> from __future__ import unicode_literals <EOL> from functools import reduce <EOL> import os <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from . settings import ( <EOL> IMAGE_SETS , <EOL> QUAL , <EOL> VERSATILEIMAGEFIELD_POST_PROCESSOR , <EOL> VERSATILEIMAGEFIELD_SIZED_DIRNAME , <EOL> VERSATILEIMAGEFIELD_FILTERED_DIRNAME <EOL> ) <EOL> BMP = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> DCX = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> EPS = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> GIF = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> JPEG = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> PCD = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> PCX = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> PDF = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> PNG = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> PPM = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> PSD = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> TIFF = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> XBM = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> XPM = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> FILE_EXTENSION_MAP = { <EOL> '<STR_LIT>' : PNG , <EOL> '<STR_LIT>' : JPEG , <EOL> '<STR_LIT>' : JPEG , <EOL> '<STR_LIT>' : JPEG , <EOL> '<STR_LIT>' : GIF , <EOL> '<STR_LIT>' : BMP , <EOL> '<STR_LIT>' : BMP , <EOL> '<STR_LIT>' : DCX , <EOL> '<STR_LIT>' : EPS , <EOL> '<STR_LIT>' : EPS , <EOL> '<STR_LIT>' : PCD , <EOL> '<STR_LIT>' : PCX , <EOL> '<STR_LIT>' : PDF , <EOL> '<STR_LIT>' : PPM , <EOL> '<STR_LIT>' : PPM , <EOL> '<STR_LIT>' : PPM , <EOL> '<STR_LIT>' : PSD , <EOL> '<STR_LIT>' : TIFF , <EOL> '<STR_LIT>' : TIFF , <EOL> '<STR_LIT>' : XBM , <EOL> '<STR_LIT>' : XPM <EOL> } <EOL> class InvalidSizeKeySet ( Exception ) : <EOL> pass <EOL> class InvalidSizeKey ( Exception ) : <EOL> pass <EOL> def post_process_image_key ( image_key ) : <EOL> """<STR_LIT>""" <EOL> if VERSATILEIMAGEFIELD_POST_PROCESSOR is None : <EOL> return image_key <EOL> else : <EOL> return VERSATILEIMAGEFIELD_POST_PROCESSOR ( image_key ) <EOL> def get_resized_filename ( filename , width , height , filename_key ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> image_name , ext = filename . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> except ValueError : <EOL> image_name = filename <EOL> ext = '<STR_LIT>' <EOL> resized_template = "<STR_LIT>" <EOL> if ext . lower ( ) in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> resized_template = resized_template + "<STR_LIT>" <EOL> resized_key = resized_template % ( { <EOL> '<STR_LIT>' : filename_key , <EOL> '<STR_LIT:width>' : width , <EOL> '<STR_LIT>' : height , <EOL> '<STR_LIT>' : QUAL <EOL> } ) <EOL> return "<STR_LIT>" % ( { <EOL> '<STR_LIT>' : image_name , <EOL> '<STR_LIT>' : post_process_image_key ( resized_key ) , <EOL> '<STR_LIT>' : ext <EOL> } ) <EOL> def get_resized_path ( path_to_image , width , height , <EOL> filename_key , storage ) : <EOL> """<STR_LIT>""" <EOL> containing_folder , filename = os . path . split ( path_to_image ) <EOL> resized_filename = get_resized_filename ( <EOL> filename , <EOL> width , <EOL> height , <EOL> filename_key <EOL> ) <EOL> joined_path = os . path . join ( * [ <EOL> VERSATILEIMAGEFIELD_SIZED_DIRNAME , <EOL> containing_folder , <EOL> resized_filename <EOL> ] ) . replace ( '<STR_LIT:U+0020>' , '<STR_LIT>' ) <EOL> return ( <EOL> joined_path , <EOL> storage . url ( joined_path ) <EOL> ) <EOL> def get_filtered_filename ( filename , filename_key ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> image_name , ext = filename . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> except ValueError : <EOL> image_name = filename <EOL> ext = '<STR_LIT>' <EOL> return "<STR_LIT>" % ( { <EOL> '<STR_LIT>' : image_name , <EOL> '<STR_LIT>' : filename_key , <EOL> '<STR_LIT>' : ext <EOL> } ) <EOL> def get_filtered_path ( path_to_image , filename_key , storage ) : <EOL> """<STR_LIT>""" <EOL> containing_folder , filename = os . path . split ( path_to_image ) <EOL> filtered_filename = get_filtered_filename ( filename , filename_key ) <EOL> path_to_return = os . path . join ( * [ <EOL> containing_folder , <EOL> VERSATILEIMAGEFIELD_FILTERED_DIRNAME , <EOL> filtered_filename <EOL> ] ) <EOL> path_to_return = path_to_return . replace ( '<STR_LIT:U+0020>' , '<STR_LIT>' ) <EOL> return ( <EOL> path_to_return , <EOL> storage . url ( path_to_return ) <EOL> ) <EOL> def get_image_metadata_from_file_ext ( file_ext ) : <EOL> """<STR_LIT>""" <EOL> return FILE_EXTENSION_MAP . get ( file_ext , JPEG ) <EOL> def validate_versatileimagefield_sizekey_list ( sizes ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> for key , size_key in sizes : <EOL> size_key_split = size_key . split ( '<STR_LIT>' ) <EOL> if size_key_split [ - <NUM_LIT:1> ] != '<STR_LIT:url>' and ( <EOL> '<STR_LIT:x>' not in size_key_split [ - <NUM_LIT:1> ] <EOL> ) : <EOL> raise InvalidSizeKey ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( size_key ) <EOL> ) <EOL> except ValueError : <EOL> raise InvalidSizeKeySet ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( str ( sizes ) ) <EOL> ) <EOL> return list ( set ( sizes ) ) <EOL> def get_url_from_image_key ( image_instance , image_key ) : <EOL> """<STR_LIT>""" <EOL> img_key_split = image_key . split ( '<STR_LIT>' ) <EOL> if '<STR_LIT:x>' in img_key_split [ - <NUM_LIT:1> ] : <EOL> size_key = img_key_split . pop ( - <NUM_LIT:1> ) <EOL> else : <EOL> size_key = None <EOL> img_url = reduce ( getattr , img_key_split , image_instance ) <EOL> if size_key : <EOL> img_url = img_url [ size_key ] . url <EOL> return img_url <EOL> def build_versatileimagefield_url_set ( image_instance , size_set , request = None ) : <EOL> """<STR_LIT>""" <EOL> size_set = validate_versatileimagefield_sizekey_list ( size_set ) <EOL> to_return = { } <EOL> if image_instance : <EOL> for key , image_key in size_set : <EOL> img_url = get_url_from_image_key ( image_instance , image_key ) <EOL> if request is not None : <EOL> img_url = request . build_absolute_uri ( img_url ) <EOL> to_return [ key ] = img_url <EOL> return to_return <EOL> def get_rendition_key_set ( key ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> rendition_key_set = IMAGE_SETS [ key ] <EOL> except KeyError : <EOL> raise ImproperlyConfigured ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( key ) <EOL> ) <EOL> else : <EOL> return validate_versatileimagefield_sizekey_list ( rendition_key_set ) </s>
