<s> """<STR_LIT>""" <EOL> import sys <EOL> import json <EOL> import socket <EOL> import base64 <EOL> import logging <EOL> import ssl <EOL> try : <EOL> from http . client import HTTPConnection , HTTPSConnection <EOL> except ImportError : <EOL> from httplib import HTTPConnection , HTTPSConnection <EOL> from pyeapi . utils import debug , make_iterable <EOL> _LOGGER = logging . getLogger ( __name__ ) <EOL> DEFAULT_HTTP_PORT = <NUM_LIT> <EOL> DEFAULT_HTTPS_PORT = <NUM_LIT> <EOL> DEFAULT_HTTP_LOCAL_PORT = <NUM_LIT> <EOL> DEFAULT_HTTP_PATH = '<STR_LIT>' <EOL> DEFAULT_UNIX_SOCKET = '<STR_LIT>' <EOL> def https_connection_factory ( path , host , port , context = None , timeout = <NUM_LIT> ) : <EOL> if sys . hexversion < <NUM_LIT> : <EOL> return HttpsConnection ( path , host , port , timeout = timeout ) <EOL> return HttpsConnection ( path , host , port , context = context , timeout = timeout ) <EOL> class EapiError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , message , commands = None ) : <EOL> self . message = message <EOL> self . commands = commands <EOL> super ( EapiError , self ) . __init__ ( message ) <EOL> class CommandError ( EapiError ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , code , message , ** kwargs ) : <EOL> cmd_err = kwargs . get ( '<STR_LIT>' ) <EOL> if int ( code ) in [ <NUM_LIT:1000> , <NUM_LIT> , <NUM_LIT> ] : <EOL> msg_fmt = '<STR_LIT>' . format ( code , message , cmd_err ) <EOL> else : <EOL> msg_fmt = '<STR_LIT>' . format ( code , message ) <EOL> super ( CommandError , self ) . __init__ ( msg_fmt ) <EOL> self . error_code = code <EOL> self . error_text = message <EOL> self . command_error = cmd_err <EOL> self . commands = kwargs . get ( '<STR_LIT>' ) <EOL> self . output = kwargs . get ( '<STR_LIT>' ) <EOL> self . message = msg_fmt <EOL> @ property <EOL> def trace ( self ) : <EOL> return self . get_trace ( ) <EOL> def get_trace ( self ) : <EOL> trace = list ( ) <EOL> index = None <EOL> for index , out in enumerate ( self . output ) : <EOL> _entry = { '<STR_LIT>' : self . commands [ index ] , '<STR_LIT>' : out } <EOL> trace . append ( _entry ) <EOL> if index : <EOL> index += <NUM_LIT:1> <EOL> for cmd in self . commands [ index : ] : <EOL> _entry = { '<STR_LIT>' : cmd , '<STR_LIT>' : None } <EOL> trace . append ( _entry ) <EOL> return trace <EOL> class ConnectionError ( EapiError ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , connection_type , message , commands = None ) : <EOL> self . message = message <EOL> self . connection_type = connection_type <EOL> self . commands = commands <EOL> super ( ConnectionError , self ) . __init__ ( message ) <EOL> class SocketConnection ( HTTPConnection ) : <EOL> def __init__ ( self , path , timeout = <NUM_LIT> ) : <EOL> HTTPConnection . __init__ ( self , '<STR_LIT:localhost>' ) <EOL> self . path = path <EOL> self . timeout = timeout <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % self . path <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % self . path <EOL> def connect ( self ) : <EOL> self . sock = socket . socket ( socket . AF_UNIX , socket . SOCK_STREAM ) <EOL> self . sock . settimeout ( self . timeout ) <EOL> self . sock . connect ( self . path ) <EOL> class HttpConnection ( HTTPConnection ) : <EOL> def __init__ ( self , path , * args , ** kwargs ) : <EOL> HTTPConnection . __init__ ( self , * args , ** kwargs ) <EOL> self . path = path <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( self . host , self . port , self . path ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . host , self . port , self . path ) <EOL> class HttpsConnection ( HTTPSConnection ) : <EOL> def __init__ ( self , path , * args , ** kwargs ) : <EOL> HTTPSConnection . __init__ ( self , * args , ** kwargs ) <EOL> self . path = path <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( self . host , self . port , self . path ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . host , self . port , self . path ) <EOL> class EapiConnection ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . transport = None <EOL> self . error = None <EOL> self . socket_error = None <EOL> self . _auth = None <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % str ( self . transport ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % repr ( self . transport ) <EOL> def authentication ( self , username , password ) : <EOL> """<STR_LIT>""" <EOL> if int ( sys . version [ <NUM_LIT:0> ] ) > <NUM_LIT:2> : <EOL> _auth_text = '<STR_LIT>' . format ( username , password ) <EOL> _auth_bin = base64 . encodebytes ( _auth_text . encode ( ) ) <EOL> _auth = _auth_bin . decode ( ) <EOL> _auth = _auth . replace ( '<STR_LIT:\n>' , '<STR_LIT>' ) <EOL> self . _auth = _auth <EOL> else : <EOL> _auth = base64 . encodestring ( '<STR_LIT>' . format ( username , password ) ) <EOL> self . _auth = str ( _auth ) . replace ( '<STR_LIT:\n>' , '<STR_LIT>' ) <EOL> _LOGGER . debug ( '<STR_LIT>' . format ( self . _auth ) ) <EOL> def request ( self , commands , encoding = None , reqid = None ) : <EOL> """<STR_LIT>""" <EOL> commands = make_iterable ( commands ) <EOL> reqid = id ( self ) if reqid is None else reqid <EOL> params = { "<STR_LIT:version>" : <NUM_LIT:1> , "<STR_LIT>" : commands , "<STR_LIT>" : encoding } <EOL> return json . dumps ( { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : params , "<STR_LIT:id>" : str ( reqid ) } ) <EOL> def send ( self , data ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> _LOGGER . debug ( "<STR_LIT>" . format ( data ) ) <EOL> self . transport . putrequest ( '<STR_LIT:POST>' , '<STR_LIT>' ) <EOL> self . transport . putheader ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . transport . putheader ( '<STR_LIT>' , '<STR_LIT>' % len ( data ) ) <EOL> if self . _auth : <EOL> self . transport . putheader ( '<STR_LIT>' , <EOL> '<STR_LIT>' % ( self . _auth ) ) <EOL> if int ( sys . version [ <NUM_LIT:0> ] ) > <NUM_LIT:2> : <EOL> data = data . encode ( ) <EOL> self . transport . endheaders ( message_body = data ) <EOL> try : <EOL> response = self . transport . getresponse ( buffering = True ) <EOL> except TypeError : <EOL> response = self . transport . getresponse ( ) <EOL> response_content = response . read ( ) <EOL> _LOGGER . debug ( "<STR_LIT>" . format ( <EOL> status = response . status , <EOL> reason = response . reason ) ) <EOL> _LOGGER . debug ( "<STR_LIT>" . format ( response_content ) ) <EOL> if not type ( response_content ) == str : <EOL> response_content = response_content . decode ( ) <EOL> decoded = json . loads ( response_content ) <EOL> _LOGGER . debug ( '<STR_LIT>' % decoded ) <EOL> if '<STR_LIT:error>' in decoded : <EOL> ( code , msg , err , out ) = self . _parse_error_message ( decoded ) <EOL> raise CommandError ( code , msg , command_error = err , output = out ) <EOL> return decoded <EOL> except ( socket . error , ValueError ) as exc : <EOL> if isinstance ( exc , socket . error ) : <EOL> self . socket_error = exc <EOL> _LOGGER . exception ( exc ) <EOL> self . error = exc <EOL> raise ConnectionError ( str ( self ) , '<STR_LIT>' ) <EOL> finally : <EOL> self . transport . close ( ) <EOL> def _parse_error_message ( self , message ) : <EOL> """<STR_LIT>""" <EOL> msg = message [ '<STR_LIT:error>' ] [ '<STR_LIT:message>' ] <EOL> code = message [ '<STR_LIT:error>' ] [ '<STR_LIT:code>' ] <EOL> err = None <EOL> out = None <EOL> if '<STR_LIT:data>' in message [ '<STR_LIT:error>' ] : <EOL> err = '<STR_LIT:U+0020>' . join ( message [ '<STR_LIT:error>' ] [ '<STR_LIT:data>' ] [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] ) <EOL> out = message [ '<STR_LIT:error>' ] [ '<STR_LIT:data>' ] <EOL> return ( code , msg , err , out ) <EOL> def execute ( self , commands , encoding = '<STR_LIT>' , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if encoding not in ( '<STR_LIT>' , '<STR_LIT:text>' ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> try : <EOL> self . error = None <EOL> request = self . request ( commands , encoding = encoding , ** kwargs ) <EOL> response = self . send ( request ) <EOL> return response <EOL> except ( ConnectionError , CommandError , TypeError ) as exc : <EOL> exc . commands = commands <EOL> self . error = exc <EOL> raise <EOL> class SocketEapiConnection ( EapiConnection ) : <EOL> def __init__ ( self , path = None , timeout = <NUM_LIT> , ** kwargs ) : <EOL> super ( SocketEapiConnection , self ) . __init__ ( ) <EOL> path = path or DEFAULT_UNIX_SOCKET <EOL> self . transport = SocketConnection ( path , timeout ) <EOL> class HttpLocalEapiConnection ( EapiConnection ) : <EOL> def __init__ ( self , port = None , path = None , timeout = <NUM_LIT> , ** kwargs ) : <EOL> super ( HttpLocalEapiConnection , self ) . __init__ ( ) <EOL> port = port or DEFAULT_HTTP_LOCAL_PORT <EOL> path = path or DEFAULT_HTTP_PATH <EOL> self . transport = HttpConnection ( path , '<STR_LIT:localhost>' , port , <EOL> timeout = timeout ) <EOL> class HttpEapiConnection ( EapiConnection ) : <EOL> def __init__ ( self , host , port = None , path = None , username = None , <EOL> password = None , timeout = <NUM_LIT> , ** kwargs ) : <EOL> super ( HttpEapiConnection , self ) . __init__ ( ) <EOL> port = port or DEFAULT_HTTP_PORT <EOL> path = path or DEFAULT_HTTP_PATH <EOL> self . transport = HttpConnection ( path , host , port , timeout = timeout ) <EOL> self . authentication ( username , password ) <EOL> class HttpsEapiConnection ( EapiConnection ) : <EOL> def __init__ ( self , host , port = None , path = None , username = None , <EOL> password = None , context = None , timeout = <NUM_LIT> , ** kwargs ) : <EOL> super ( HttpsEapiConnection , self ) . __init__ ( ) <EOL> port = port or DEFAULT_HTTPS_PORT <EOL> path = path or DEFAULT_HTTP_PATH <EOL> enforce_verification = kwargs . get ( '<STR_LIT>' ) <EOL> if context is None and not enforce_verification : <EOL> context = self . disable_certificate_verification ( ) <EOL> self . transport = https_connection_factory ( path , host , port , <EOL> context , timeout ) <EOL> self . authentication ( username , password ) <EOL> def disable_certificate_verification ( self ) : <EOL> if hasattr ( ssl , '<STR_LIT>' ) : <EOL> return ssl . _create_unverified_context ( ) </s>
