<s> import json <EOL> import struct <EOL> import sys <EOL> import requests <EOL> import requests . exceptions <EOL> import six <EOL> import websocket <EOL> from . import api <EOL> from . import constants <EOL> from . import errors <EOL> from . auth import auth <EOL> from . unixconn import unixconn <EOL> from . ssladapter import ssladapter <EOL> from . utils import utils , check_resource , update_headers , kwargs_from_env <EOL> from . tls import TLSConfig <EOL> def from_env ( ** kwargs ) : <EOL> return Client . from_env ( ** kwargs ) <EOL> class Client ( <EOL> requests . Session , <EOL> api . BuildApiMixin , <EOL> api . ContainerApiMixin , <EOL> api . DaemonApiMixin , <EOL> api . ExecApiMixin , <EOL> api . ImageApiMixin , <EOL> api . VolumeApiMixin , <EOL> api . NetworkApiMixin ) : <EOL> def __init__ ( self , base_url = None , version = None , <EOL> timeout = constants . DEFAULT_TIMEOUT_SECONDS , tls = False ) : <EOL> super ( Client , self ) . __init__ ( ) <EOL> if tls and not base_url : <EOL> raise errors . TLSParameterError ( <EOL> '<STR_LIT>' <EOL> ) <EOL> self . base_url = base_url <EOL> self . timeout = timeout <EOL> self . _auth_configs = auth . load_config ( ) <EOL> base_url = utils . parse_host ( base_url , sys . platform , tls = bool ( tls ) ) <EOL> if base_url . startswith ( '<STR_LIT>' ) : <EOL> self . _custom_adapter = unixconn . UnixAdapter ( base_url , timeout ) <EOL> self . mount ( '<STR_LIT>' , self . _custom_adapter ) <EOL> self . base_url = '<STR_LIT>' <EOL> else : <EOL> if isinstance ( tls , TLSConfig ) : <EOL> tls . configure_client ( self ) <EOL> elif tls : <EOL> self . _custom_adapter = ssladapter . SSLAdapter ( ) <EOL> self . mount ( '<STR_LIT>' , self . _custom_adapter ) <EOL> self . base_url = base_url <EOL> if version is None : <EOL> self . _version = constants . DEFAULT_DOCKER_API_VERSION <EOL> elif isinstance ( version , six . string_types ) : <EOL> if version . lower ( ) == '<STR_LIT>' : <EOL> self . _version = self . _retrieve_server_version ( ) <EOL> else : <EOL> self . _version = version <EOL> else : <EOL> raise errors . DockerException ( <EOL> '<STR_LIT>' . format ( <EOL> type ( version ) . __name__ <EOL> ) <EOL> ) <EOL> @ classmethod <EOL> def from_env ( cls , ** kwargs ) : <EOL> return cls ( ** kwargs_from_env ( ** kwargs ) ) <EOL> def _retrieve_server_version ( self ) : <EOL> try : <EOL> return self . version ( api_version = False ) [ "<STR_LIT>" ] <EOL> except KeyError : <EOL> raise errors . DockerException ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> except Exception as e : <EOL> raise errors . DockerException ( <EOL> '<STR_LIT>' . format ( e ) <EOL> ) <EOL> def _set_request_timeout ( self , kwargs ) : <EOL> """<STR_LIT>""" <EOL> kwargs . setdefault ( '<STR_LIT>' , self . timeout ) <EOL> return kwargs <EOL> @ update_headers <EOL> def _post ( self , url , ** kwargs ) : <EOL> return self . post ( url , ** self . _set_request_timeout ( kwargs ) ) <EOL> @ update_headers <EOL> def _get ( self , url , ** kwargs ) : <EOL> return self . get ( url , ** self . _set_request_timeout ( kwargs ) ) <EOL> @ update_headers <EOL> def _put ( self , url , ** kwargs ) : <EOL> return self . put ( url , ** self . _set_request_timeout ( kwargs ) ) <EOL> @ update_headers <EOL> def _delete ( self , url , ** kwargs ) : <EOL> return self . delete ( url , ** self . _set_request_timeout ( kwargs ) ) <EOL> def _url ( self , pathfmt , * args , ** kwargs ) : <EOL> for arg in args : <EOL> if not isinstance ( arg , six . string_types ) : <EOL> raise ValueError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( arg , type ( arg ) ) <EOL> ) <EOL> args = map ( six . moves . urllib . parse . quote_plus , args ) <EOL> if kwargs . get ( '<STR_LIT>' , True ) : <EOL> return '<STR_LIT>' . format ( <EOL> self . base_url , self . _version , pathfmt . format ( * args ) <EOL> ) <EOL> else : <EOL> return '<STR_LIT>' . format ( self . base_url , pathfmt . format ( * args ) ) <EOL> def _raise_for_status ( self , response , explanation = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> response . raise_for_status ( ) <EOL> except requests . exceptions . HTTPError as e : <EOL> if e . response . status_code == <NUM_LIT> : <EOL> raise errors . NotFound ( e , response , explanation = explanation ) <EOL> raise errors . APIError ( e , response , explanation = explanation ) <EOL> def _result ( self , response , json = False , binary = False ) : <EOL> assert not ( json and binary ) <EOL> self . _raise_for_status ( response ) <EOL> if json : <EOL> return response . json ( ) <EOL> if binary : <EOL> return response . content <EOL> return response . text <EOL> def _post_json ( self , url , data , ** kwargs ) : <EOL> data2 = { } <EOL> if data is not None : <EOL> for k , v in six . iteritems ( data ) : <EOL> if v is not None : <EOL> data2 [ k ] = v <EOL> if '<STR_LIT>' not in kwargs : <EOL> kwargs [ '<STR_LIT>' ] = { } <EOL> kwargs [ '<STR_LIT>' ] [ '<STR_LIT:Content-Type>' ] = '<STR_LIT:application/json>' <EOL> return self . _post ( url , data = json . dumps ( data2 ) , ** kwargs ) <EOL> def _attach_params ( self , override = None ) : <EOL> return override or { <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:1> <EOL> } <EOL> @ check_resource <EOL> def _attach_websocket ( self , container , params = None ) : <EOL> url = self . _url ( "<STR_LIT>" , container ) <EOL> req = requests . Request ( "<STR_LIT:POST>" , url , params = self . _attach_params ( params ) ) <EOL> full_url = req . prepare ( ) . url <EOL> full_url = full_url . replace ( "<STR_LIT>" , "<STR_LIT>" , <NUM_LIT:1> ) <EOL> full_url = full_url . replace ( "<STR_LIT>" , "<STR_LIT>" , <NUM_LIT:1> ) <EOL> return self . _create_websocket_connection ( full_url ) <EOL> def _create_websocket_connection ( self , url ) : <EOL> return websocket . create_connection ( url ) <EOL> def _get_raw_response_socket ( self , response ) : <EOL> self . _raise_for_status ( response ) <EOL> if six . PY3 : <EOL> sock = response . raw . _fp . fp . raw <EOL> if self . base_url . startswith ( "<STR_LIT>" ) : <EOL> sock = sock . _sock <EOL> else : <EOL> sock = response . raw . _fp . fp . _sock <EOL> try : <EOL> sock . _response = response <EOL> except AttributeError : <EOL> pass <EOL> return sock <EOL> def _stream_helper ( self , response , decode = False ) : <EOL> """<STR_LIT>""" <EOL> if response . raw . _fp . chunked : <EOL> reader = response . raw <EOL> while not reader . closed : <EOL> data = reader . read ( <NUM_LIT:1> ) <EOL> if not data : <EOL> break <EOL> if reader . _fp . chunk_left : <EOL> data += reader . read ( reader . _fp . chunk_left ) <EOL> if decode : <EOL> if six . PY3 : <EOL> data = data . decode ( '<STR_LIT:utf-8>' ) <EOL> data = json . loads ( data ) <EOL> yield data <EOL> else : <EOL> yield self . _result ( response ) <EOL> def _multiplexed_buffer_helper ( self , response ) : <EOL> """<STR_LIT>""" <EOL> buf = self . _result ( response , binary = True ) <EOL> walker = <NUM_LIT:0> <EOL> while True : <EOL> if len ( buf [ walker : ] ) < <NUM_LIT:8> : <EOL> break <EOL> _ , length = struct . unpack_from ( '<STR_LIT>' , buf [ walker : ] ) <EOL> start = walker + constants . STREAM_HEADER_SIZE_BYTES <EOL> end = start + length <EOL> walker = end <EOL> yield buf [ start : end ] <EOL> def _multiplexed_response_stream_helper ( self , response ) : <EOL> """<STR_LIT>""" <EOL> socket = self . _get_raw_response_socket ( response ) <EOL> self . _disable_socket_timeout ( socket ) <EOL> while True : <EOL> header = response . raw . read ( constants . STREAM_HEADER_SIZE_BYTES ) <EOL> if not header : <EOL> break <EOL> _ , length = struct . unpack ( '<STR_LIT>' , header ) <EOL> if not length : <EOL> continue <EOL> data = response . raw . read ( length ) <EOL> if not data : <EOL> break <EOL> yield data <EOL> def _stream_raw_result_old ( self , response ) : <EOL> '''<STR_LIT>''' <EOL> self . _raise_for_status ( response ) <EOL> for line in response . iter_lines ( chunk_size = <NUM_LIT:1> , <EOL> decode_unicode = True ) : <EOL> if line : <EOL> yield line <EOL> def _stream_raw_result ( self , response ) : <EOL> '''<STR_LIT>''' <EOL> self . _raise_for_status ( response ) <EOL> for out in response . iter_content ( chunk_size = <NUM_LIT:1> , decode_unicode = True ) : <EOL> yield out <EOL> def _disable_socket_timeout ( self , socket ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( socket , "<STR_LIT>" ) : <EOL> socket . settimeout ( None ) <EOL> if hasattr ( socket , "<STR_LIT>" ) and hasattr ( socket . _sock , "<STR_LIT>" ) : <EOL> socket . _sock . settimeout ( None ) <EOL> def _get_result ( self , container , stream , res ) : <EOL> cont = self . inspect_container ( container ) <EOL> return self . _get_result_tty ( stream , res , cont [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> def _get_result_tty ( self , stream , res , is_tty ) : <EOL> if utils . compare_version ( '<STR_LIT>' , self . _version ) < <NUM_LIT:0> : <EOL> return self . _stream_raw_result_old ( res ) <EOL> if is_tty : <EOL> return self . _stream_raw_result ( res ) if stream else self . _result ( res , binary = True ) <EOL> self . _raise_for_status ( res ) <EOL> sep = six . binary_type ( ) <EOL> if stream : <EOL> return self . _multiplexed_response_stream_helper ( res ) <EOL> else : <EOL> return sep . join ( <EOL> [ x for x in self . _multiplexed_buffer_helper ( res ) ] <EOL> ) <EOL> def get_adapter ( self , url ) : <EOL> try : <EOL> return super ( Client , self ) . get_adapter ( url ) <EOL> except requests . exceptions . InvalidSchema as e : <EOL> if self . _custom_adapter : <EOL> return self . _custom_adapter <EOL> else : <EOL> raise e <EOL> @ property <EOL> def api_version ( self ) : <EOL> return self . _version <EOL> class AutoVersionClient ( Client ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> if '<STR_LIT:version>' in kwargs and kwargs [ '<STR_LIT:version>' ] : <EOL> raise errors . DockerException ( <EOL> '<STR_LIT>' <EOL> ) <EOL> kwargs [ '<STR_LIT:version>' ] = '<STR_LIT>' <EOL> super ( AutoVersionClient , self ) . __init__ ( * args , ** kwargs ) </s>
