<s> from __future__ import unicode_literals <EOL> import base64 <EOL> import logging <EOL> import threading <EOL> import spotify <EOL> from spotify import ffi , lib , serialized , utils <EOL> __all__ = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> logger = logging . getLogger ( __name__ ) <EOL> class Image ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( <EOL> self , session , uri = None , sp_image = None , add_ref = True , <EOL> callback = None ) : <EOL> assert uri or sp_image , '<STR_LIT>' <EOL> self . _session = session <EOL> if uri is not None : <EOL> image = spotify . Link ( self . _session , uri = uri ) . as_image ( ) <EOL> if image is None : <EOL> raise ValueError ( <EOL> '<STR_LIT>' % uri ) <EOL> sp_image = image . _sp_image <EOL> add_ref = True <EOL> if add_ref : <EOL> lib . sp_image_add_ref ( sp_image ) <EOL> self . _sp_image = ffi . gc ( sp_image , lib . sp_image_release ) <EOL> self . loaded_event = threading . Event ( ) <EOL> handle = ffi . new_handle ( ( self . _session , self , callback ) ) <EOL> self . _session . _callback_handles . add ( handle ) <EOL> spotify . Error . maybe_raise ( lib . sp_image_add_load_callback ( <EOL> self . _sp_image , _image_load_callback , handle ) ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % self . link . uri <EOL> def __eq__ ( self , other ) : <EOL> if isinstance ( other , self . __class__ ) : <EOL> return self . _sp_image == other . _sp_image <EOL> else : <EOL> return False <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> def __hash__ ( self ) : <EOL> return hash ( self . _sp_image ) <EOL> loaded_event = None <EOL> """<STR_LIT>""" <EOL> @ property <EOL> def is_loaded ( self ) : <EOL> """<STR_LIT>""" <EOL> return bool ( lib . sp_image_is_loaded ( self . _sp_image ) ) <EOL> @ property <EOL> def error ( self ) : <EOL> """<STR_LIT>""" <EOL> return spotify . ErrorType ( lib . sp_image_error ( self . _sp_image ) ) <EOL> def load ( self , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> return utils . load ( self . _session , self , timeout = timeout ) <EOL> @ property <EOL> def format ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . is_loaded : <EOL> return None <EOL> return ImageFormat ( lib . sp_image_format ( self . _sp_image ) ) <EOL> @ property <EOL> @ serialized <EOL> def data ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . is_loaded : <EOL> return None <EOL> data_size_ptr = ffi . new ( '<STR_LIT>' ) <EOL> data = lib . sp_image_data ( self . _sp_image , data_size_ptr ) <EOL> buffer_ = ffi . buffer ( data , data_size_ptr [ <NUM_LIT:0> ] ) <EOL> data_bytes = buffer_ [ : ] <EOL> assert len ( data_bytes ) == data_size_ptr [ <NUM_LIT:0> ] , '<STR_LIT>' % ( <EOL> len ( data_bytes ) , data_size_ptr [ <NUM_LIT:0> ] ) <EOL> return data_bytes <EOL> @ property <EOL> def data_uri ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . is_loaded : <EOL> return None <EOL> if self . format is not ImageFormat . JPEG : <EOL> raise ValueError ( '<STR_LIT>' % self . format ) <EOL> return '<STR_LIT>' % ( <EOL> base64 . b64encode ( self . data ) . decode ( '<STR_LIT:ascii>' ) ) <EOL> @ property <EOL> def link ( self ) : <EOL> """<STR_LIT>""" <EOL> return spotify . Link ( <EOL> self . _session , <EOL> sp_link = lib . sp_link_create_from_image ( self . _sp_image ) , <EOL> add_ref = False ) <EOL> @ ffi . callback ( '<STR_LIT>' ) <EOL> @ serialized <EOL> def _image_load_callback ( sp_image , handle ) : <EOL> logger . debug ( '<STR_LIT>' ) <EOL> if handle == ffi . NULL : <EOL> logger . warning ( '<STR_LIT>' ) <EOL> return <EOL> ( session , image , callback ) = ffi . from_handle ( handle ) <EOL> session . _callback_handles . remove ( handle ) <EOL> image . loaded_event . set ( ) <EOL> if callback is not None : <EOL> callback ( image ) <EOL> lib . sp_image_remove_load_callback ( sp_image , _image_load_callback , handle ) <EOL> @ utils . make_enum ( '<STR_LIT>' ) <EOL> class ImageFormat ( utils . IntEnum ) : <EOL> pass <EOL> @ utils . make_enum ( '<STR_LIT>' ) <EOL> class ImageSize ( utils . IntEnum ) : <EOL> pass </s>
