<s> """<STR_LIT>""" <EOL> from cloud_browser . app_settings import settings <EOL> from cloud_browser . cloud import errors , base <EOL> from cloud_browser . common import SEP , check_version , requires , dt_from_header <EOL> RS_MIN_CLOUDFILES_VERSION = ( <NUM_LIT:1> , <NUM_LIT:7> , <NUM_LIT:4> ) <EOL> RS_MAX_LIST_OBJECTS_LIMIT = <NUM_LIT> <EOL> RS_MAX_LIST_CONTAINERS_LIMIT = <NUM_LIT> <EOL> try : <EOL> import cloudfiles <EOL> check_version ( cloudfiles , RS_MIN_CLOUDFILES_VERSION ) <EOL> except ImportError : <EOL> cloudfiles = None <EOL> class RackspaceExceptionWrapper ( errors . CloudExceptionWrapper ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> @ requires ( cloudfiles , '<STR_LIT>' ) <EOL> def lazy_translations ( cls ) : <EOL> """<STR_LIT>""" <EOL> return { <EOL> cloudfiles . errors . NoSuchContainer : errors . NoContainerException , <EOL> cloudfiles . errors . NoSuchObject : errors . NoObjectException , <EOL> } <EOL> class RackspaceObject ( base . CloudObject ) : <EOL> """<STR_LIT>""" <EOL> wrap_rs_errors = RackspaceExceptionWrapper ( ) <EOL> subdir_types = set ( ( <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ) ) <EOL> @ wrap_rs_errors <EOL> def _get_object ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . container . native_container . get_object ( self . name ) <EOL> @ wrap_rs_errors <EOL> def _read ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . native_obj . read ( ) <EOL> @ classmethod <EOL> def from_info ( cls , container , info_obj ) : <EOL> """<STR_LIT>""" <EOL> create_fn = cls . from_subdir if '<STR_LIT>' in info_obj else cls . from_file_info <EOL> return create_fn ( container , info_obj ) <EOL> @ classmethod <EOL> def from_subdir ( cls , container , info_obj ) : <EOL> """<STR_LIT>""" <EOL> return cls ( container , <EOL> info_obj [ '<STR_LIT>' ] , <EOL> obj_type = cls . type_cls . SUBDIR ) <EOL> @ classmethod <EOL> def choose_type ( cls , content_type ) : <EOL> """<STR_LIT>""" <EOL> return cls . type_cls . SUBDIR if content_type in cls . subdir_types else cls . type_cls . FILE <EOL> @ classmethod <EOL> def from_file_info ( cls , container , info_obj ) : <EOL> """<STR_LIT>""" <EOL> return cls ( container , <EOL> name = info_obj [ '<STR_LIT:name>' ] , <EOL> size = info_obj [ '<STR_LIT>' ] , <EOL> content_type = info_obj [ '<STR_LIT>' ] , <EOL> last_modified = dt_from_header ( info_obj [ '<STR_LIT>' ] ) , <EOL> obj_type = cls . choose_type ( info_obj [ '<STR_LIT>' ] ) ) <EOL> @ classmethod <EOL> def from_obj ( cls , container , file_obj ) : <EOL> """<STR_LIT>""" <EOL> return cls ( container , <EOL> name = file_obj . name , <EOL> size = file_obj . size , <EOL> content_type = file_obj . content_type , <EOL> last_modified = dt_from_header ( file_obj . last_modified ) , <EOL> obj_type = cls . choose_type ( file_obj . content_type ) ) <EOL> class RackspaceContainer ( base . CloudContainer ) : <EOL> """<STR_LIT>""" <EOL> obj_cls = RackspaceObject <EOL> wrap_rs_errors = RackspaceExceptionWrapper ( ) <EOL> max_list = ( RS_MAX_LIST_OBJECTS_LIMIT - <NUM_LIT:1> ) / <NUM_LIT:2> <EOL> @ wrap_rs_errors <EOL> def _get_container ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . conn . native_conn . get_container ( self . name ) <EOL> @ wrap_rs_errors <EOL> def get_objects ( self , path , marker = None , <EOL> limit = settings . CLOUD_BROWSER_DEFAULT_LIST_LIMIT ) : <EOL> """<STR_LIT>""" <EOL> object_infos , full_query = self . _get_object_infos ( path , marker , limit ) <EOL> if full_query and len ( object_infos ) < limit : <EOL> object_infos , _ = self . _get_object_infos ( path , marker , <NUM_LIT:2> * limit ) <EOL> object_infos = object_infos [ : limit ] <EOL> return [ self . obj_cls . from_info ( self , x ) for x in object_infos ] <EOL> @ wrap_rs_errors <EOL> def _get_object_infos ( self , path , marker = None , <EOL> limit = settings . CLOUD_BROWSER_DEFAULT_LIST_LIMIT ) : <EOL> """<STR_LIT>""" <EOL> orig_limit = limit <EOL> limit += <NUM_LIT:1> <EOL> if limit > RS_MAX_LIST_OBJECTS_LIMIT : <EOL> raise errors . CloudException ( "<STR_LIT>" % <EOL> RS_MAX_LIST_OBJECTS_LIMIT ) <EOL> def _collapse ( infos ) : <EOL> """<STR_LIT>""" <EOL> name = None <EOL> for info in infos : <EOL> name = info . get ( '<STR_LIT:name>' , name ) <EOL> subdir = info . get ( '<STR_LIT>' , '<STR_LIT>' ) . strip ( SEP ) <EOL> if not name or subdir != name : <EOL> yield info <EOL> path = path + SEP if path else '<STR_LIT>' <EOL> object_infos = self . native_container . list_objects_info ( <EOL> limit = limit , delimiter = SEP , prefix = path , marker = marker ) <EOL> full_query = len ( object_infos ) == limit <EOL> if object_infos : <EOL> if ( marker and <EOL> object_infos [ <NUM_LIT:0> ] . get ( '<STR_LIT>' , '<STR_LIT>' ) . strip ( SEP ) == marker ) : <EOL> object_infos = object_infos [ <NUM_LIT:1> : ] <EOL> object_infos = list ( _collapse ( object_infos ) ) <EOL> if len ( object_infos ) > orig_limit : <EOL> object_infos = object_infos [ : orig_limit ] <EOL> return object_infos , full_query <EOL> @ wrap_rs_errors <EOL> def get_object ( self , path ) : <EOL> """<STR_LIT>""" <EOL> obj = self . native_container . get_object ( path ) <EOL> return self . obj_cls . from_obj ( self , obj ) <EOL> class RackspaceConnection ( base . CloudConnection ) : <EOL> """<STR_LIT>""" <EOL> cont_cls = RackspaceContainer <EOL> wrap_rs_errors = RackspaceExceptionWrapper ( ) <EOL> max_list = RS_MAX_LIST_CONTAINERS_LIMIT <EOL> def __init__ ( self , account , secret_key , servicenet = False , authurl = None ) : <EOL> """<STR_LIT>""" <EOL> super ( RackspaceConnection , self ) . __init__ ( account , secret_key ) <EOL> self . servicenet = servicenet <EOL> self . authurl = authurl <EOL> @ wrap_rs_errors <EOL> @ requires ( cloudfiles , '<STR_LIT>' ) <EOL> def _get_connection ( self ) : <EOL> """<STR_LIT>""" <EOL> kwargs = { <EOL> '<STR_LIT:username>' : self . account , <EOL> '<STR_LIT>' : self . secret_key , <EOL> } <EOL> if self . servicenet : <EOL> kwargs [ '<STR_LIT>' ] = True <EOL> if self . authurl : <EOL> kwargs [ '<STR_LIT>' ] = self . authurl <EOL> return cloudfiles . get_connection ( ** kwargs ) <EOL> @ wrap_rs_errors <EOL> def _get_containers ( self ) : <EOL> """<STR_LIT>""" <EOL> infos = self . native_conn . list_containers_info ( ) <EOL> return [ self . cont_cls ( self , i [ '<STR_LIT:name>' ] , i [ '<STR_LIT:count>' ] , i [ '<STR_LIT>' ] ) <EOL> for i in infos ] <EOL> @ wrap_rs_errors <EOL> def _get_container ( self , path ) : <EOL> """<STR_LIT>""" <EOL> cont = self . native_conn . get_container ( path ) <EOL> return self . cont_cls ( self , <EOL> cont . name , <EOL> cont . object_count , <EOL> cont . size_used ) </s>
