<s> """<STR_LIT>""" <EOL> import abc <EOL> import copy <EOL> import six <EOL> import six . moves . urllib . parse as urlparse <EOL> from ironicclient . common . apiclient import base <EOL> from ironicclient import exc <EOL> def getid ( obj ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return obj . id <EOL> except AttributeError : <EOL> return obj <EOL> @ six . add_metaclass ( abc . ABCMeta ) <EOL> class Manager ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , api ) : <EOL> self . api = api <EOL> def _path ( self , resource_id = None ) : <EOL> """<STR_LIT>""" <EOL> return ( '<STR_LIT>' % ( self . _resource_name , resource_id ) <EOL> if resource_id else '<STR_LIT>' % self . _resource_name ) <EOL> @ abc . abstractproperty <EOL> def resource_class ( self ) : <EOL> """<STR_LIT>""" <EOL> @ abc . abstractproperty <EOL> def _resource_name ( self ) : <EOL> """<STR_LIT>""" <EOL> def _get ( self , resource_id , fields = None ) : <EOL> """<STR_LIT>""" <EOL> if fields is not None : <EOL> resource_id = '<STR_LIT>' % resource_id <EOL> resource_id += '<STR_LIT:U+002C>' . join ( fields ) <EOL> try : <EOL> return self . _list ( self . _path ( resource_id ) ) [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> return None <EOL> def _format_body_data ( self , body , response_key ) : <EOL> if response_key : <EOL> try : <EOL> data = body [ response_key ] <EOL> except KeyError : <EOL> return [ ] <EOL> else : <EOL> data = body <EOL> if not isinstance ( data , list ) : <EOL> data = [ data ] <EOL> return data <EOL> def _list_pagination ( self , url , response_key = None , obj_class = None , <EOL> limit = None ) : <EOL> """<STR_LIT>""" <EOL> if obj_class is None : <EOL> obj_class = self . resource_class <EOL> if limit is not None : <EOL> limit = int ( limit ) <EOL> object_list = [ ] <EOL> object_count = <NUM_LIT:0> <EOL> limit_reached = False <EOL> while url : <EOL> resp , body = self . api . json_request ( '<STR_LIT:GET>' , url ) <EOL> data = self . _format_body_data ( body , response_key ) <EOL> for obj in data : <EOL> object_list . append ( obj_class ( self , obj , loaded = True ) ) <EOL> object_count += <NUM_LIT:1> <EOL> if limit and object_count >= limit : <EOL> limit_reached = True <EOL> break <EOL> if limit_reached : <EOL> break <EOL> url = body . get ( '<STR_LIT>' ) <EOL> if url : <EOL> url_parts = list ( urlparse . urlparse ( url ) ) <EOL> url_parts [ <NUM_LIT:0> ] = url_parts [ <NUM_LIT:1> ] = '<STR_LIT>' <EOL> url = urlparse . urlunparse ( url_parts ) <EOL> return object_list <EOL> def _list ( self , url , response_key = None , obj_class = None , body = None ) : <EOL> resp , body = self . api . json_request ( '<STR_LIT:GET>' , url ) <EOL> if obj_class is None : <EOL> obj_class = self . resource_class <EOL> data = self . _format_body_data ( body , response_key ) <EOL> return [ obj_class ( self , res , loaded = True ) for res in data if res ] <EOL> def _update ( self , resource_id , patch , method = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> url = self . _path ( resource_id ) <EOL> resp , body = self . api . json_request ( method , url , body = patch ) <EOL> if body : <EOL> return self . resource_class ( self , body ) <EOL> def _delete ( self , resource_id ) : <EOL> """<STR_LIT>""" <EOL> self . api . raw_request ( '<STR_LIT>' , self . _path ( resource_id ) ) <EOL> @ six . add_metaclass ( abc . ABCMeta ) <EOL> class CreateManager ( Manager ) : <EOL> """<STR_LIT>""" <EOL> @ abc . abstractproperty <EOL> def _creation_attributes ( self ) : <EOL> """<STR_LIT>""" <EOL> def create ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> new = { } <EOL> for ( key , value ) in kwargs . items ( ) : <EOL> if key in self . _creation_attributes : <EOL> new [ key ] = value <EOL> else : <EOL> raise exc . InvalidAttribute ( ) <EOL> url = self . _path ( ) <EOL> resp , body = self . api . json_request ( '<STR_LIT:POST>' , url , body = new ) <EOL> if body : <EOL> return self . resource_class ( self , body ) <EOL> class Resource ( base . Resource ) : <EOL> """<STR_LIT>""" <EOL> def to_dict ( self ) : <EOL> return copy . deepcopy ( self . _info ) </s>
