<s> __all__ = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , ) <EOL> import socket <EOL> import urllib <EOL> import cPickle <EOL> from simpleapi . message import formatters , wrappers <EOL> class ClientException ( Exception ) : pass <EOL> class ConnectionException ( ClientException ) : pass <EOL> class RemoteException ( ClientException ) : pass <EOL> class Client ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ns , access_key = None , version = '<STR_LIT:default>' , <EOL> transport_type = '<STR_LIT>' , wrapper_type = '<STR_LIT:default>' , timeout = None ) : <EOL> if timeout is not None : <EOL> socket . setdefaulttimeout ( timeout ) <EOL> self . ns = ns <EOL> self . access_key = access_key <EOL> self . version = version <EOL> assert transport_type in formatters <EOL> self . transport_type = transport_type <EOL> assert wrapper_type in wrappers <EOL> self . wrapper_type = wrapper_type <EOL> def _handle_remote_call ( self , fname ) : <EOL> def do_call ( ** kwargs ) : <EOL> data = { <EOL> '<STR_LIT>' : fname , <EOL> '<STR_LIT>' : self . transport_type , <EOL> '<STR_LIT>' : self . transport_type , <EOL> '<STR_LIT>' : self . wrapper_type , <EOL> '<STR_LIT>' : self . access_key or '<STR_LIT>' , <EOL> '<STR_LIT>' : self . version <EOL> } <EOL> formatter = formatters [ self . transport_type ] ( None , None ) <EOL> for key , value in kwargs . iteritems ( ) : <EOL> kwargs [ key ] = formatter . kwargs ( value ) <EOL> data . update ( kwargs ) <EOL> try : <EOL> response = urllib . urlopen ( self . ns , <EOL> urllib . urlencode ( data ) ) <EOL> assert response . getcode ( ) in [ <NUM_LIT:200> , ] , u'<STR_LIT>' % response . getcode ( ) <EOL> response_buffer = response . read ( ) <EOL> except IOError , e : <EOL> raise ConnectionException ( e ) <EOL> try : <EOL> response = formatter . parse ( response_buffer ) <EOL> except ( cPickle . UnpicklingError , EOFError ) , _ : <EOL> raise ClientException ( <EOL> u'<STR_LIT>' '<STR_LIT>' '<STR_LIT>' <EOL> ) <EOL> except ValueError , e : <EOL> raise ConnectionException , e <EOL> if response . get ( '<STR_LIT:success>' ) : <EOL> return response . get ( '<STR_LIT:result>' ) <EOL> else : <EOL> raise RemoteException ( "<STR_LIT>" . join ( response . get ( '<STR_LIT>' ) ) ) <EOL> return do_call <EOL> def __getattr__ ( self , name ) : <EOL> return self . _handle_remote_call ( name ) <EOL> def set_version ( self , version ) : <EOL> """<STR_LIT>""" <EOL> self . version = int ( version ) <EOL> def set_ns ( self , ns ) : <EOL> """<STR_LIT>""" <EOL> self . ns = ns </s>
