<s> from kamaki . clients import Client , ClientError , quote <EOL> from kamaki . clients . utils import path4url <EOL> def _format_image_headers ( headers ) : <EOL> reply = dict ( properties = dict ( ) ) <EOL> meta_prefix = '<STR_LIT>' <EOL> property_prefix = '<STR_LIT>' <EOL> for key , val in headers . items ( ) : <EOL> key = key . lower ( ) <EOL> if key . startswith ( property_prefix ) : <EOL> key = key [ len ( property_prefix ) : ] . replace ( '<STR_LIT:->' , '<STR_LIT:_>' ) <EOL> reply [ '<STR_LIT>' ] [ key ] = val <EOL> elif key . startswith ( meta_prefix ) : <EOL> key = key [ len ( meta_prefix ) : ] <EOL> reply [ key ] = val <EOL> return reply <EOL> class ImageClient ( Client ) : <EOL> """<STR_LIT>""" <EOL> service_type = '<STR_LIT:image>' <EOL> def __init__ ( self , endpoint_url , token ) : <EOL> super ( ImageClient , self ) . __init__ ( endpoint_url , token ) <EOL> self . request_headers_to_quote = [ '<STR_LIT>' , ] <EOL> self . request_header_prefices_to_quote = [ '<STR_LIT>' , ] <EOL> self . response_headers = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> self . response_header_prefices = [ '<STR_LIT>' , ] <EOL> def list_public ( self , detail = False , filters = { } , order = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> path = path4url ( '<STR_LIT>' , '<STR_LIT>' ) if detail else ( <EOL> '<STR_LIT>' % path4url ( '<STR_LIT>' ) ) <EOL> async_params = { } <EOL> if isinstance ( filters , dict ) : <EOL> for key , value in filters . items ( ) : <EOL> if value : <EOL> async_params [ key ] = value <EOL> if order and order . startswith ( '<STR_LIT:->' ) : <EOL> async_params [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> order = order [ <NUM_LIT:1> : ] <EOL> else : <EOL> async_params [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> if order : <EOL> async_params [ '<STR_LIT>' ] = order <EOL> r = self . get ( path , async_params = async_params , success = <NUM_LIT:200> ) <EOL> return r . json <EOL> def get_meta ( self , image_id ) : <EOL> """<STR_LIT>""" <EOL> path = path4url ( '<STR_LIT>' , image_id ) <EOL> r = self . head ( path , success = <NUM_LIT:200> ) <EOL> return _format_image_headers ( r . headers ) <EOL> def register ( self , name , location , params = { } , properties = { } ) : <EOL> """<STR_LIT>""" <EOL> path = '<STR_LIT>' % path4url ( '<STR_LIT>' ) <EOL> self . set_header ( '<STR_LIT>' , name ) <EOL> location = location if ( <EOL> isinstance ( location , str ) or isinstance ( location , unicode ) ) else ( <EOL> '<STR_LIT>' % '<STR_LIT:/>' . join ( location ) ) <EOL> prefix = '<STR_LIT>' <EOL> if location . startswith ( prefix ) : <EOL> lvalues = ( location [ len ( prefix ) : ] ) . split ( '<STR_LIT:/>' ) <EOL> location = '<STR_LIT>' % ( prefix , '<STR_LIT:/>' . join ( [ <EOL> quote ( s . encode ( '<STR_LIT:utf-8>' ) ) for s in lvalues ] ) ) <EOL> else : <EOL> lvalues = location . split ( '<STR_LIT:/>' ) <EOL> location = '<STR_LIT:.>' . join ( [ quote ( s . encode ( '<STR_LIT:utf-8>' ) ) for s in lvalues ] ) <EOL> self . set_header ( '<STR_LIT>' , location ) <EOL> async_headers = { } <EOL> for key , val in params . items ( ) : <EOL> if key in ( '<STR_LIT:store>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT:size>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) and val : <EOL> key = '<STR_LIT>' + key . replace ( '<STR_LIT:_>' , '<STR_LIT:->' ) <EOL> async_headers [ key ] = val <EOL> for key , val in properties . items ( ) : <EOL> async_headers [ '<STR_LIT>' % key ] = val <EOL> r = self . post ( path , success = <NUM_LIT:200> , async_headers = async_headers ) <EOL> return _format_image_headers ( r . headers ) <EOL> def unregister ( self , image_id ) : <EOL> """<STR_LIT>""" <EOL> path = path4url ( '<STR_LIT>' , image_id ) <EOL> r = self . delete ( path , success = <NUM_LIT> ) <EOL> return r . headers <EOL> def list_members ( self , image_id ) : <EOL> """<STR_LIT>""" <EOL> path = path4url ( '<STR_LIT>' , image_id , '<STR_LIT>' ) <EOL> r = self . get ( path , success = <NUM_LIT:200> ) <EOL> return r . json [ '<STR_LIT>' ] <EOL> def list_shared ( self , member ) : <EOL> """<STR_LIT>""" <EOL> path = path4url ( '<STR_LIT>' , member ) <EOL> r = self . get ( path , success = <NUM_LIT:200> ) <EOL> return r . json [ '<STR_LIT>' ] <EOL> def add_member ( self , image_id , member ) : <EOL> """<STR_LIT>""" <EOL> path = path4url ( '<STR_LIT>' , image_id , '<STR_LIT>' , member ) <EOL> self . set_header ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> r = self . put ( path , success = <NUM_LIT> ) <EOL> return r . headers <EOL> def remove_member ( self , image_id , member ) : <EOL> """<STR_LIT>""" <EOL> path = path4url ( '<STR_LIT>' , image_id , '<STR_LIT>' , member ) <EOL> r = self . delete ( path , success = <NUM_LIT> ) <EOL> return r . headers <EOL> def set_members ( self , image_id , members ) : <EOL> """<STR_LIT>""" <EOL> path = path4url ( '<STR_LIT>' , image_id , '<STR_LIT>' ) <EOL> req = { '<STR_LIT>' : [ { '<STR_LIT>' : member } for member in members ] } <EOL> r = self . put ( path , json = req , success = <NUM_LIT> ) <EOL> return r . headers <EOL> def update_image ( <EOL> self , image_id , <EOL> name = None , disk_format = None , container_format = None , <EOL> status = None , public = None , owner_id = None , ** properties ) : <EOL> path = path4url ( '<STR_LIT>' , image_id ) <EOL> if name is not None : <EOL> self . set_header ( '<STR_LIT>' , name ) <EOL> if disk_format is not None : <EOL> self . set_header ( '<STR_LIT>' , disk_format ) <EOL> if container_format is not None : <EOL> self . set_header ( '<STR_LIT>' , container_format ) <EOL> if status is not None : <EOL> self . set_header ( '<STR_LIT>' , status ) <EOL> if public is not None : <EOL> self . set_header ( '<STR_LIT>' , bool ( public ) ) <EOL> if owner_id is not None : <EOL> self . set_header ( '<STR_LIT>' , owner_id ) <EOL> for k , v in properties . items ( ) : <EOL> self . set_header ( '<STR_LIT>' % k , v ) <EOL> self . set_header ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> r = self . put ( path , success = <NUM_LIT:200> ) <EOL> return r . headers </s>
