<s> import httplib <EOL> import urllib <EOL> import time <EOL> import hashlib <EOL> import StringIO <EOL> import ssl <EOL> import os <EOL> import socket <EOL> import struct <EOL> from pipes import quote as pquote <EOL> import libcloud <EOL> from libcloud . httplib_ssl import LibcloudHTTPSConnection <EOL> from httplib import HTTPConnection as LibcloudHTTPConnection <EOL> class RawResponse ( object ) : <EOL> def __init__ ( self , response = None ) : <EOL> self . _status = None <EOL> self . _response = None <EOL> self . _headers = { } <EOL> self . _error = None <EOL> self . _reason = None <EOL> @ property <EOL> def response ( self ) : <EOL> if not self . _response : <EOL> self . _response = self . connection . connection . getresponse ( ) <EOL> return self . _response <EOL> @ property <EOL> def status ( self ) : <EOL> if not self . _status : <EOL> self . _status = self . response . status <EOL> return self . _status <EOL> @ property <EOL> def headers ( self ) : <EOL> if not self . _headers : <EOL> self . _headers = dict ( self . response . getheaders ( ) ) <EOL> return self . _headers <EOL> @ property <EOL> def reason ( self ) : <EOL> if not self . _reason : <EOL> self . _reason = self . response . reason <EOL> return self . _reason <EOL> class Response ( object ) : <EOL> """<STR_LIT>""" <EOL> NODE_STATE_MAP = { } <EOL> object = None <EOL> body = None <EOL> status = httplib . OK <EOL> headers = { } <EOL> error = None <EOL> connection = None <EOL> def __init__ ( self , response ) : <EOL> self . body = response . read ( ) <EOL> self . status = response . status <EOL> self . headers = dict ( response . getheaders ( ) ) <EOL> self . error = response . reason <EOL> if not self . success ( ) : <EOL> raise Exception ( self . parse_error ( ) ) <EOL> self . object = self . parse_body ( ) <EOL> def parse_body ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . body <EOL> def parse_error ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . body <EOL> def success ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . status == httplib . OK or self . status == httplib . CREATED <EOL> class LoggingConnection ( ) : <EOL> """<STR_LIT>""" <EOL> log = None <EOL> def _log_response ( self , r ) : <EOL> rv = "<STR_LIT>" % ( id ( self ) , id ( r ) ) <EOL> ht = "<STR_LIT>" <EOL> v = r . version <EOL> if r . version == <NUM_LIT:10> : <EOL> v = "<STR_LIT>" <EOL> if r . version == <NUM_LIT:11> : <EOL> v = "<STR_LIT>" <EOL> ht += "<STR_LIT>" % ( v , r . status , r . reason ) <EOL> body = r . read ( ) <EOL> for h in r . getheaders ( ) : <EOL> ht += "<STR_LIT>" % ( h [ <NUM_LIT:0> ] . title ( ) , h [ <NUM_LIT:1> ] ) <EOL> ht += "<STR_LIT:\r\n>" <EOL> class fakesock : <EOL> def __init__ ( self , s ) : <EOL> self . s = s <EOL> def makefile ( self , mode , foo ) : <EOL> return StringIO . StringIO ( self . s ) <EOL> rr = r <EOL> if r . chunked : <EOL> ht += "<STR_LIT>" % ( len ( body ) ) <EOL> ht += body <EOL> ht += "<STR_LIT>" <EOL> else : <EOL> ht += body <EOL> rr = httplib . HTTPResponse ( fakesock ( ht ) , <EOL> method = r . _method , <EOL> debuglevel = r . debuglevel ) <EOL> rr . begin ( ) <EOL> rv += ht <EOL> rv += ( "<STR_LIT>" <EOL> % ( id ( self ) , id ( r ) ) ) <EOL> return ( rr , rv ) <EOL> def _log_curl ( self , method , url , body , headers ) : <EOL> cmd = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> cmd . extend ( [ "<STR_LIT>" , pquote ( method ) ] ) <EOL> for h in headers : <EOL> cmd . extend ( [ "<STR_LIT>" , pquote ( "<STR_LIT>" % ( h , headers [ h ] ) ) ] ) <EOL> if body is not None and len ( body ) > <NUM_LIT:0> : <EOL> cmd . extend ( [ "<STR_LIT>" , pquote ( body ) ] ) <EOL> cmd . extend ( [ pquote ( "<STR_LIT>" % ( self . host , self . port , url ) ) ] ) <EOL> return "<STR_LIT:U+0020>" . join ( cmd ) <EOL> class LoggingHTTPSConnection ( LoggingConnection , LibcloudHTTPSConnection ) : <EOL> """<STR_LIT>""" <EOL> def getresponse ( self ) : <EOL> r = LibcloudHTTPSConnection . getresponse ( self ) <EOL> if self . log is not None : <EOL> r , rv = self . _log_response ( r ) <EOL> self . log . write ( rv + "<STR_LIT:\n>" ) <EOL> self . log . flush ( ) <EOL> return r <EOL> def request ( self , method , url , body = None , headers = None ) : <EOL> headers . update ( { '<STR_LIT>' : str ( id ( self ) ) } ) <EOL> if self . log is not None : <EOL> pre = "<STR_LIT>" % id ( self ) <EOL> self . log . write ( pre + <EOL> self . _log_curl ( method , url , body , headers ) + "<STR_LIT:\n>" ) <EOL> self . log . flush ( ) <EOL> return LibcloudHTTPSConnection . request ( self , method , url , body , headers ) <EOL> class LoggingHTTPConnection ( LoggingConnection , LibcloudHTTPConnection ) : <EOL> """<STR_LIT>""" <EOL> def getresponse ( self ) : <EOL> r = LibcloudHTTPConnection . getresponse ( self ) <EOL> if self . log is not None : <EOL> r , rv = self . _log_response ( r ) <EOL> self . log . write ( rv + "<STR_LIT:\n>" ) <EOL> self . log . flush ( ) <EOL> return r <EOL> def request ( self , method , url , body = None , headers = None ) : <EOL> headers . update ( { '<STR_LIT>' : str ( id ( self ) ) } ) <EOL> if self . log is not None : <EOL> pre = "<STR_LIT>" % id ( self ) <EOL> self . log . write ( pre + <EOL> self . _log_curl ( method , url , body , headers ) + "<STR_LIT:\n>" ) <EOL> self . log . flush ( ) <EOL> return LibcloudHTTPConnection . request ( self , method , url , <EOL> body , headers ) <EOL> class ConnectionKey ( object ) : <EOL> """<STR_LIT>""" <EOL> conn_classes = ( LibcloudHTTPConnection , LibcloudHTTPSConnection ) <EOL> responseCls = Response <EOL> rawResponseCls = RawResponse <EOL> connection = None <EOL> host = '<STR_LIT:127.0.0.1>' <EOL> port = ( <NUM_LIT> , <NUM_LIT> ) <EOL> secure = <NUM_LIT:1> <EOL> driver = None <EOL> action = None <EOL> def __init__ ( self , key , secure = True , host = None , force_port = None ) : <EOL> """<STR_LIT>""" <EOL> self . key = key <EOL> self . secure = secure and <NUM_LIT:1> or <NUM_LIT:0> <EOL> self . ua = [ ] <EOL> if host : <EOL> self . host = host <EOL> if force_port : <EOL> self . port = ( force_port , force_port ) <EOL> def connect ( self , host = None , port = None ) : <EOL> """<STR_LIT>""" <EOL> host = host or self . host <EOL> port = port or self . port [ self . secure ] <EOL> kwargs = { '<STR_LIT:host>' : host , '<STR_LIT:port>' : port } <EOL> connection = self . conn_classes [ self . secure ] ( ** kwargs ) <EOL> self . connection = connection <EOL> def _user_agent ( self ) : <EOL> return '<STR_LIT>' % ( <EOL> libcloud . __version__ , <EOL> self . driver . name , <EOL> "<STR_LIT>" . join ( [ "<STR_LIT>" % x for x in self . ua ] ) ) <EOL> def user_agent_append ( self , token ) : <EOL> """<STR_LIT>""" <EOL> self . ua . append ( token ) <EOL> def request ( self , <EOL> action , <EOL> params = None , <EOL> data = '<STR_LIT>' , <EOL> headers = None , <EOL> method = '<STR_LIT:GET>' , <EOL> raw = False ) : <EOL> """<STR_LIT>""" <EOL> if params is None : <EOL> params = { } <EOL> if headers is None : <EOL> headers = { } <EOL> self . action = action <EOL> self . method = method <EOL> params = self . add_default_params ( params ) <EOL> headers = self . add_default_headers ( headers ) <EOL> headers . update ( { '<STR_LIT>' : self . _user_agent ( ) } ) <EOL> headers . update ( { '<STR_LIT>' : self . host } ) <EOL> if data != '<STR_LIT>' and data != None : <EOL> data = self . encode_data ( data ) <EOL> if data is not None : <EOL> headers . update ( { '<STR_LIT>' : str ( len ( data ) ) } ) <EOL> if params : <EOL> url = '<STR_LIT:?>' . join ( ( action , urllib . urlencode ( params ) ) ) <EOL> else : <EOL> url = action <EOL> self . connect ( ) <EOL> try : <EOL> if raw : <EOL> self . connection . putrequest ( method , action ) <EOL> for key , value in headers . iteritems ( ) : <EOL> self . connection . putheader ( key , value ) <EOL> self . connection . endheaders ( ) <EOL> else : <EOL> self . connection . request ( method = method , url = url , body = data , <EOL> headers = headers ) <EOL> except ssl . SSLError , e : <EOL> raise ssl . SSLError ( str ( e ) ) <EOL> if raw : <EOL> response = self . rawResponseCls ( ) <EOL> else : <EOL> response = self . responseCls ( self . connection . getresponse ( ) ) <EOL> response . connection = self <EOL> return response <EOL> def add_default_params ( self , params ) : <EOL> """<STR_LIT>""" <EOL> return params <EOL> def add_default_headers ( self , headers ) : <EOL> """<STR_LIT>""" <EOL> return headers <EOL> def encode_data ( self , data ) : <EOL> """<STR_LIT>""" <EOL> return data <EOL> class ConnectionUserAndKey ( ConnectionKey ) : <EOL> """<STR_LIT>""" <EOL> user_id = None <EOL> def __init__ ( self , user_id , key , secure = True , host = None , port = None ) : <EOL> super ( ConnectionUserAndKey , self ) . __init__ ( key , secure , host , port ) <EOL> self . user_id = user_id </s>
