<s> import httplib <EOL> import urlparse <EOL> import os . path <EOL> import urllib <EOL> try : <EOL> import json <EOL> except : <EOL> import simplejson as json <EOL> from libcloud import utils <EOL> from libcloud . common . types import MalformedResponseError , LibcloudError <EOL> from libcloud . common . types import InvalidCredsError <EOL> from libcloud . common . base import ConnectionUserAndKey , Response <EOL> from libcloud . storage . providers import Provider <EOL> from libcloud . storage . base import Object , Container , StorageDriver <EOL> from libcloud . storage . types import ContainerAlreadyExistsError <EOL> from libcloud . storage . types import ContainerDoesNotExistError <EOL> from libcloud . storage . types import ContainerIsNotEmptyError <EOL> from libcloud . storage . types import ObjectDoesNotExistError <EOL> from libcloud . storage . types import ObjectHashMismatchError <EOL> from libcloud . storage . types import InvalidContainerNameError <EOL> from libcloud . common . rackspace import AUTH_HOST_US , AUTH_HOST_UK , RackspaceBaseConnection <EOL> API_VERSION = '<STR_LIT>' <EOL> class CloudFilesResponse ( Response ) : <EOL> valid_response_codes = [ httplib . NOT_FOUND , httplib . CONFLICT ] <EOL> def success ( self ) : <EOL> i = int ( self . status ) <EOL> return i >= <NUM_LIT:200> and i <= <NUM_LIT> or i in self . valid_response_codes <EOL> def parse_body ( self ) : <EOL> if not self . body : <EOL> return None <EOL> if '<STR_LIT>' in self . headers : <EOL> key = '<STR_LIT>' <EOL> elif '<STR_LIT:Content-Type>' in self . headers : <EOL> key = '<STR_LIT:Content-Type>' <EOL> else : <EOL> raise LibcloudError ( '<STR_LIT>' ) <EOL> content_type = self . headers [ key ] <EOL> if content_type . find ( '<STR_LIT:;>' ) != - <NUM_LIT:1> : <EOL> content_type = content_type . split ( '<STR_LIT:;>' ) [ <NUM_LIT:0> ] <EOL> if content_type == '<STR_LIT:application/json>' : <EOL> try : <EOL> data = json . loads ( self . body ) <EOL> except : <EOL> raise MalformedResponseError ( '<STR_LIT>' , <EOL> body = self . body , <EOL> driver = CloudFilesStorageDriver ) <EOL> elif content_type == '<STR_LIT>' : <EOL> data = self . body <EOL> else : <EOL> data = self . body <EOL> return data <EOL> class CloudFilesConnection ( RackspaceBaseConnection ) : <EOL> """<STR_LIT>""" <EOL> responseCls = CloudFilesResponse <EOL> auth_host = None <EOL> _url_key = "<STR_LIT>" <EOL> def __init__ ( self , user_id , key , secure = True ) : <EOL> super ( CloudFilesConnection , self ) . __init__ ( user_id , key , secure = secure ) <EOL> self . api_version = API_VERSION <EOL> self . accept_format = '<STR_LIT:application/json>' <EOL> def request ( self , action , params = None , data = '<STR_LIT>' , headers = None , method = '<STR_LIT:GET>' , <EOL> raw = False ) : <EOL> if not headers : <EOL> headers = { } <EOL> if not params : <EOL> params = { } <EOL> if self . request_path : <EOL> action = self . request_path + action <EOL> params [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> if method in [ '<STR_LIT:POST>' , '<STR_LIT>' ] : <EOL> headers = { '<STR_LIT:Content-Type>' : '<STR_LIT>' } <EOL> return super ( CloudFilesConnection , self ) . request ( <EOL> action = action , <EOL> params = params , data = data , <EOL> method = method , headers = headers , <EOL> raw = raw <EOL> ) <EOL> class CloudFilesUSConnection ( CloudFilesConnection ) : <EOL> """<STR_LIT>""" <EOL> auth_host = AUTH_HOST_US <EOL> class CloudFilesUKConnection ( CloudFilesConnection ) : <EOL> """<STR_LIT>""" <EOL> auth_host = AUTH_HOST_UK <EOL> class CloudFilesStorageDriver ( StorageDriver ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> connectionCls = CloudFilesConnection <EOL> hash_type = '<STR_LIT>' <EOL> def get_meta_data ( self ) : <EOL> response = self . connection . request ( '<STR_LIT>' , method = '<STR_LIT>' ) <EOL> if response . status == httplib . NO_CONTENT : <EOL> container_count = response . headers . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> object_count = response . headers . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> bytes_used = response . headers . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> return { '<STR_LIT>' : int ( container_count ) , <EOL> '<STR_LIT>' : int ( object_count ) , <EOL> '<STR_LIT>' : int ( bytes_used ) } <EOL> raise LibcloudError ( '<STR_LIT>' % ( response . status ) ) <EOL> def list_containers ( self ) : <EOL> response = self . connection . request ( '<STR_LIT>' ) <EOL> if response . status == httplib . NO_CONTENT : <EOL> return [ ] <EOL> elif response . status == httplib . OK : <EOL> return self . _to_container_list ( json . loads ( response . body ) ) <EOL> raise LibcloudError ( '<STR_LIT>' % ( response . status ) ) <EOL> def list_container_objects ( self , container ) : <EOL> response = self . connection . request ( '<STR_LIT>' % ( container . name ) ) <EOL> if response . status == httplib . NO_CONTENT : <EOL> return [ ] <EOL> elif response . status == httplib . OK : <EOL> return self . _to_object_list ( json . loads ( response . body ) , container ) <EOL> raise LibcloudError ( '<STR_LIT>' % ( response . status ) ) <EOL> def get_container ( self , container_name ) : <EOL> response = self . connection . request ( '<STR_LIT>' % ( container_name ) , <EOL> method = '<STR_LIT>' ) <EOL> if response . status == httplib . NO_CONTENT : <EOL> container = self . _headers_to_container ( container_name , response . headers ) <EOL> return container <EOL> elif response . status == httplib . NOT_FOUND : <EOL> raise ContainerDoesNotExistError ( None , self , container_name ) <EOL> raise LibcloudError ( '<STR_LIT>' % ( response . status ) ) <EOL> def get_object ( self , container_name , object_name ) : <EOL> container = self . get_container ( container_name ) <EOL> response = self . connection . request ( '<STR_LIT>' % ( container_name , <EOL> object_name ) , <EOL> method = '<STR_LIT>' ) <EOL> if response . status in [ httplib . OK , httplib . NO_CONTENT ] : <EOL> obj = self . _headers_to_object ( object_name , container , response . headers ) <EOL> return obj <EOL> elif response . status == httplib . NOT_FOUND : <EOL> raise ObjectDoesNotExistError ( None , self , object_name ) <EOL> raise LibcloudError ( '<STR_LIT>' % ( response . status ) ) <EOL> def create_container ( self , container_name ) : <EOL> container_name = self . _clean_container_name ( container_name ) <EOL> response = self . connection . request ( '<STR_LIT>' % ( container_name ) , method = '<STR_LIT>' ) <EOL> if response . status == httplib . CREATED : <EOL> extra = { '<STR_LIT>' : <NUM_LIT:0> } <EOL> container = Container ( name = container_name , extra = extra , driver = self ) <EOL> return container <EOL> elif response . status == httplib . ACCEPTED : <EOL> error = ContainerAlreadyExistsError ( None , self , container_name ) <EOL> raise error <EOL> raise LibcloudError ( '<STR_LIT>' % ( response . status ) ) <EOL> def delete_container ( self , container ) : <EOL> name = self . _clean_container_name ( container . name ) <EOL> response = self . connection . request ( '<STR_LIT>' % ( name ) , method = '<STR_LIT>' ) <EOL> if response . status == httplib . NO_CONTENT : <EOL> return True <EOL> elif response . status == httplib . NOT_FOUND : <EOL> raise ContainerDoesNotExistError ( value = '<STR_LIT>' , <EOL> container_name = name , driver = self ) <EOL> elif response . status == httplib . CONFLICT : <EOL> raise ContainerIsNotEmptyError ( value = '<STR_LIT>' , <EOL> container_name = name , driver = self ) <EOL> def download_object ( self , obj , destination_path , overwrite_existing = False , <EOL> delete_on_failure = True ) : <EOL> return self . _get_object ( obj , self . _save_object , <EOL> { '<STR_LIT>' : obj , <EOL> '<STR_LIT>' : destination_path , <EOL> '<STR_LIT>' : overwrite_existing , <EOL> '<STR_LIT>' : delete_on_failure } ) <EOL> def download_object_as_stream ( self , obj , chunk_size = None ) : <EOL> return self . _get_object ( obj , self . _get_object_as_stream , <EOL> { '<STR_LIT>' : chunk_size } ) <EOL> def upload_object ( self , file_path , container , object_name , extra = None , <EOL> file_hash = None ) : <EOL> """<STR_LIT>""" <EOL> upload_func = self . _upload_file <EOL> upload_func_args = { '<STR_LIT>' : file_path } <EOL> return self . _put_object ( container = container , file_path = file_path , <EOL> object_name = object_name , extra = extra , <EOL> upload_func = upload_func , <EOL> upload_func_args = upload_func_args ) <EOL> def upload_object_via_stream ( self , iterator , container , object_name , extra = None ) : <EOL> if isinstance ( iterator , file ) : <EOL> iterator = iter ( iterator ) <EOL> upload_func = self . _stream_data <EOL> upload_func_args = { '<STR_LIT>' : iterator } <EOL> return self . _put_object ( container = container , iterator = iterator , <EOL> object_name = object_name , extra = extra , <EOL> upload_func = upload_func , <EOL> upload_func_args = upload_func_args ) <EOL> def delete_object ( self , obj ) : <EOL> container_name = self . _clean_container_name ( obj . container . name ) <EOL> object_name = self . _clean_object_name ( obj . name ) <EOL> response = self . connection . request ( '<STR_LIT>' % ( container_name , <EOL> object_name ) , method = '<STR_LIT>' ) <EOL> if response . status == httplib . NO_CONTENT : <EOL> return True <EOL> elif response . status == httplib . NOT_FOUND : <EOL> raise ObjectDoesNotExistError ( value = '<STR_LIT>' , object_name = object_name , <EOL> driver = self ) <EOL> raise LibcloudError ( '<STR_LIT>' % ( response . status ) ) <EOL> def _get_object ( self , obj , callback , callback_args ) : <EOL> container_name = obj . container . name <EOL> object_name = obj . name <EOL> response = self . connection . request ( '<STR_LIT>' % ( container_name , <EOL> object_name ) , <EOL> raw = True ) <EOL> callback_args [ '<STR_LIT>' ] = response . response <EOL> if response . status == httplib . OK : <EOL> return callback ( ** callback_args ) <EOL> elif response . status == httplib . NOT_FOUND : <EOL> raise ObjectDoesNotExistError ( name = object_name ) <EOL> raise LibcloudError ( '<STR_LIT>' % ( response . status ) ) <EOL> def _put_object ( self , upload_func , upload_func_args , container , object_name , <EOL> extra = None , file_path = None , iterator = None , file_hash = None ) : <EOL> container_name_cleaned = self . _clean_container_name ( container . name ) <EOL> object_name_cleaned = self . _clean_object_name ( object_name ) <EOL> extra = extra or { } <EOL> content_type = extra . get ( '<STR_LIT>' , None ) <EOL> meta_data = extra . get ( '<STR_LIT>' , None ) <EOL> if not content_type : <EOL> if file_path : <EOL> name = file_path <EOL> else : <EOL> name = object_name <EOL> content_type , _ = utils . guess_file_mime_type ( name ) <EOL> if not content_type : <EOL> raise AttributeError ( '<STR_LIT>' + <EOL> '<STR_LIT>' ) <EOL> headers = { } <EOL> if iterator : <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> upload_func_args [ '<STR_LIT>' ] = True <EOL> else : <EOL> file_size = os . path . getsize ( file_path ) <EOL> headers [ '<STR_LIT>' ] = file_size <EOL> upload_func_args [ '<STR_LIT>' ] = False <EOL> if file_hash : <EOL> headers [ '<STR_LIT>' ] = file_hash <EOL> headers [ '<STR_LIT:Content-Type>' ] = content_type <EOL> if meta_data : <EOL> for key , value in meta_data . iteritems ( ) : <EOL> key = '<STR_LIT>' % ( key ) <EOL> headers [ key ] = value <EOL> response = self . connection . request ( '<STR_LIT>' % ( container_name_cleaned , <EOL> object_name_cleaned ) , <EOL> method = '<STR_LIT>' , data = None , <EOL> headers = headers , raw = True ) <EOL> upload_func_args [ '<STR_LIT>' ] = response <EOL> success , data_hash , bytes_transferred = upload_func ( ** upload_func_args ) <EOL> if not success : <EOL> raise LibcloudError ( '<STR_LIT>' ) <EOL> response = response . response <EOL> if response . status == httplib . EXPECTATION_FAILED : <EOL> raise LibcloudError ( '<STR_LIT>' ) <EOL> elif response . status == httplib . UNPROCESSABLE_ENTITY : <EOL> raise ObjectHashMismatchError ( value = '<STR_LIT>' , <EOL> object_name = object_name , driver = self ) <EOL> elif response . status == httplib . CREATED : <EOL> obj = Object ( name = object_name , size = bytes_transferred , hash = file_hash , <EOL> extra = None , meta_data = meta_data , container = container , <EOL> driver = self ) <EOL> return obj <EOL> def _clean_container_name ( self , name ) : <EOL> """<STR_LIT>""" <EOL> if name . startswith ( '<STR_LIT:/>' ) : <EOL> name = name [ <NUM_LIT:1> : ] <EOL> name = urllib . quote ( name ) <EOL> if name . find ( '<STR_LIT:/>' ) != - <NUM_LIT:1> : <EOL> raise InvalidContainerNameError ( value = '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> container_name = name , driver = self ) <EOL> if len ( name ) > <NUM_LIT> : <EOL> raise InvalidContainerNameError ( value = '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> container_name = name , driver = self ) <EOL> return name <EOL> def _clean_object_name ( self , name ) : <EOL> name = urllib . quote ( name ) <EOL> return name <EOL> def _to_container_list ( self , response ) : <EOL> containers = [ ] <EOL> for container in response : <EOL> extra = { '<STR_LIT>' : int ( container [ '<STR_LIT:count>' ] ) , <EOL> '<STR_LIT:size>' : int ( container [ '<STR_LIT>' ] ) } <EOL> containers . append ( Container ( name = container [ '<STR_LIT:name>' ] , extra = extra , <EOL> driver = self ) ) <EOL> return containers <EOL> def _to_object_list ( self , response , container ) : <EOL> objects = [ ] <EOL> for obj in response : <EOL> name = obj [ '<STR_LIT:name>' ] <EOL> size = int ( obj [ '<STR_LIT>' ] ) <EOL> hash = obj [ '<STR_LIT>' ] <EOL> extra = { '<STR_LIT>' : obj [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : obj [ '<STR_LIT>' ] } <EOL> objects . append ( Object ( name = name , size = size , hash = hash , extra = extra , <EOL> meta_data = None , container = container , driver = self ) ) <EOL> return objects <EOL> def _headers_to_container ( self , name , headers ) : <EOL> size = int ( headers . get ( '<STR_LIT>' , <NUM_LIT:0> ) ) <EOL> object_count = int ( headers . get ( '<STR_LIT>' , <NUM_LIT:0> ) ) <EOL> extra = { '<STR_LIT>' : object_count , <EOL> '<STR_LIT:size>' : size } <EOL> container = Container ( name = name , extra = extra , driver = self ) <EOL> return container <EOL> def _headers_to_object ( self , name , container , headers ) : <EOL> size = int ( headers . pop ( '<STR_LIT>' , <NUM_LIT:0> ) ) <EOL> last_modified = headers . pop ( '<STR_LIT>' , None ) <EOL> etag = headers . pop ( '<STR_LIT>' , None ) <EOL> content_type = headers . pop ( '<STR_LIT>' , None ) <EOL> meta_data = { } <EOL> for key , value in headers . iteritems ( ) : <EOL> if key . find ( '<STR_LIT>' ) != - <NUM_LIT:1> : <EOL> key = key . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> meta_data [ key ] = value <EOL> extra = { '<STR_LIT>' : content_type , '<STR_LIT>' : last_modified , <EOL> '<STR_LIT>' : etag } <EOL> obj = Object ( name = name , size = size , hash = None , extra = extra , <EOL> meta_data = meta_data , container = container , driver = self ) <EOL> return obj <EOL> class CloudFilesUSStorageDriver ( CloudFilesStorageDriver ) : <EOL> """<STR_LIT>""" <EOL> type = Provider . CLOUDFILES_US <EOL> name = '<STR_LIT>' <EOL> connectionCls = CloudFilesUSConnection <EOL> class CloudFilesUKStorageDriver ( CloudFilesStorageDriver ) : <EOL> """<STR_LIT>""" <EOL> type = Provider . CLOUDFILES_UK <EOL> name = '<STR_LIT>' <EOL> connectionCls = CloudFilesUKConnection </s>
