<s> from rackclient . lib import RACK_CTX <EOL> from rackclient import exceptions <EOL> from swiftclient import client as swift_client <EOL> from swiftclient import exceptions as swift_exc <EOL> import json <EOL> import logging <EOL> import tempfile <EOL> LOG = logging . getLogger ( __name__ ) <EOL> SWIFT_PORT = <NUM_LIT> <EOL> def _get_swift_client ( ) : <EOL> if RACK_CTX . fs_endpoint : <EOL> try : <EOL> d = json . loads ( RACK_CTX . fs_endpoint ) <EOL> credentials = { <EOL> "<STR_LIT:user>" : d [ "<STR_LIT>" ] , <EOL> "<STR_LIT:key>" : d [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : d [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : d [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : "<STR_LIT:2>" <EOL> } <EOL> return swift_client . Connection ( ** credentials ) <EOL> except ( ValueError , KeyError ) : <EOL> msg = "<STR_LIT>" <EOL> raise exceptions . InvalidFSEndpointError ( msg ) <EOL> else : <EOL> authurl = "<STR_LIT>" % ( RACK_CTX . proxy_ip , SWIFT_PORT ) <EOL> credentials = { <EOL> "<STR_LIT:user>" : "<STR_LIT>" , <EOL> "<STR_LIT:key>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : authurl <EOL> } <EOL> swift = swift_client . Connection ( ** credentials ) <EOL> authurl , token = swift . get_auth ( ) <EOL> return swift_client . Connection ( preauthurl = authurl , preauthtoken = token ) <EOL> def listdir ( directory ) : <EOL> swift = _get_swift_client ( ) <EOL> directory = directory . strip ( '<STR_LIT:/>' ) <EOL> files = [ ] <EOL> try : <EOL> objects = swift . get_container ( directory ) [ <NUM_LIT:1> ] <EOL> for o in objects : <EOL> file_path = '<STR_LIT:/>' + directory + '<STR_LIT:/>' + o [ '<STR_LIT:name>' ] <EOL> files . append ( File ( file_path ) ) <EOL> except swift_exc . ClientException as e : <EOL> if e . http_status == <NUM_LIT> : <EOL> msg = "<STR_LIT>" % directory <EOL> raise exceptions . InvalidDirectoryError ( msg ) <EOL> else : <EOL> raise exceptions . FileSystemAccessError ( ) <EOL> return files <EOL> class File ( object ) : <EOL> def __init__ ( self , file_path , mode = "<STR_LIT:r>" ) : <EOL> self . path = file_path <EOL> self . file = None <EOL> if mode not in ( '<STR_LIT:r>' , '<STR_LIT:w>' ) : <EOL> raise ValueError ( <EOL> "<STR_LIT>" % mode ) <EOL> else : <EOL> self . mode = mode <EOL> def get_name ( self ) : <EOL> return self . path . strip ( '<STR_LIT:/>' ) . split ( '<STR_LIT:/>' , <NUM_LIT:1> ) [ <NUM_LIT:1> ] <EOL> def get_directory ( self ) : <EOL> return self . path . strip ( '<STR_LIT:/>' ) . split ( '<STR_LIT:/>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> def load ( self , chunk_size = None ) : <EOL> if self . file : <EOL> return <EOL> if self . mode == '<STR_LIT:r>' : <EOL> self . file = tempfile . TemporaryFile ( ) <EOL> swift = _get_swift_client ( ) <EOL> try : <EOL> _ , contents = swift . get_object ( self . get_directory ( ) , <EOL> self . get_name ( ) , chunk_size ) <EOL> if chunk_size : <EOL> for c in contents : <EOL> self . file . write ( c ) <EOL> else : <EOL> self . file . write ( contents ) <EOL> self . file . flush ( ) <EOL> self . file . seek ( <NUM_LIT:0> ) <EOL> except swift_exc . ClientException as e : <EOL> if e . http_status == <NUM_LIT> : <EOL> msg = "<STR_LIT>" % self . path <EOL> raise exceptions . InvalidFilePathError ( msg ) <EOL> else : <EOL> raise exceptions . FileSystemAccessError ( ) <EOL> def write ( self , * args , ** kwargs ) : <EOL> if not self . file : <EOL> self . file = tempfile . TemporaryFile ( ) <EOL> self . file . write ( * args , ** kwargs ) <EOL> def close ( self ) : <EOL> if self . mode == '<STR_LIT:w>' : <EOL> swift = _get_swift_client ( ) <EOL> try : <EOL> swift . put_container ( self . get_directory ( ) ) <EOL> self . file . seek ( <NUM_LIT:0> ) <EOL> swift . put_object ( self . get_directory ( ) , self . get_name ( ) , <EOL> self . file ) <EOL> except swift_exc . ClientException as e : <EOL> if e . http_status == <NUM_LIT> : <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % self . get_directory ( ) ) <EOL> raise exceptions . InvalidDirectoryError ( msg ) <EOL> else : <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> raise exceptions . FileSystemAccessError ( msg ) <EOL> finally : <EOL> self . file . close ( ) <EOL> self . file . close ( ) <EOL> def __getattr__ ( self , name ) : <EOL> if self . file : <EOL> return getattr ( self . file , name ) <EOL> else : <EOL> raise AttributeError ( "<STR_LIT>" , <EOL> self . __class__ . __name__ , name ) </s>
