import logging
import os
import ssl

from docker import Client
from docker import tls

from ..const import HTTP_TIMEOUT

log = logging.getLogger(__name__)


def docker_client():
    """
    Returns a docker-py client configured using environment variables
    according to the same logic as the official Docker client.
    """
    cert_path = os.environ.get('DOCKER_CERT_PATH', '')
    if cert_path == '':
        cert_path = os.path.join(os.environ.get('HOME', ''), '.docker')

    base_url = os.environ.get('DOCKER_HOST')
    api_version = os.environ.get('COMPOSE_API_VERSION', '1.19')

    tls_config = None

    if os.environ.get('DOCKER_TLS_VERIFY', '') != '':
        parts = base_url.split('://', 1)
        base_url = '%s://%s' % ('https', parts[1])

        client_cert = (os.path.join(cert_path, 'cert.pem'), os.path.join(cert_path, 'key.pem'))
        ca_cert = os.path.join(cert_path, 'ca.pem')

        tls_config = tls.TLSConfig(
            ssl_version=ssl.PROTOCOL_TLSv1,
            verify=True,
            assert_hostname=False,
            client_cert=client_cert,
            ca_cert=ca_cert,
        )

    if 'DOCKER_CLIENT_TIMEOUT' in os.environ:
        log.warn('The DOCKER_CLIENT_TIMEOUT environment variable is deprecated. Please use COMPOSE_HTTP_TIMEOUT instead.')

    return Client(base_url=base_url, tls=tls_config, version=api_version, timeout=HTTP_TIMEOUT)

import logging
import os

from docker import Client
from docker.utils import kwargs_from_env

from ..const import HTTP_TIMEOUT

log = logging.getLogger(__name__)


def docker_client():
    """
    Returns a docker-py client configured using environment variables
    according to the same logic as the official Docker client.
    """
    if 'DOCKER_CLIENT_TIMEOUT' in os.environ:
        log.warn('The DOCKER_CLIENT_TIMEOUT environment variable is deprecated. Please use COMPOSE_HTTP_TIMEOUT instead.')

    kwargs = kwargs_from_env(assert_hostname=False)
    kwargs['version'] = os.environ.get('COMPOSE_API_VERSION', '1.19')
    kwargs['timeout'] = HTTP_TIMEOUT
    return Client(**kwargs)

Remove custom docker client initialization logic
Remove custom docker client initialization logic

Signed-off-by: Aanand Prasad <94fc4f3e3d0be608b3ed7b8529ff28d2a445cce1@gmail.com>

  import logging
  import os
- import ssl
  
  from docker import Client
- from docker import tls
+ from docker.utils import kwargs_from_env
  
  from ..const import HTTP_TIMEOUT
  
  log = logging.getLogger(__name__)
  
  
  def docker_client():
      """
      Returns a docker-py client configured using environment variables
      according to the same logic as the official Docker client.
      """
-     cert_path = os.environ.get('DOCKER_CERT_PATH', '')
-     if cert_path == '':
-         cert_path = os.path.join(os.environ.get('HOME', ''), '.docker')
- 
-     base_url = os.environ.get('DOCKER_HOST')
-     api_version = os.environ.get('COMPOSE_API_VERSION', '1.19')
- 
-     tls_config = None
- 
-     if os.environ.get('DOCKER_TLS_VERIFY', '') != '':
-         parts = base_url.split('://', 1)
-         base_url = '%s://%s' % ('https', parts[1])
- 
-         client_cert = (os.path.join(cert_path, 'cert.pem'), os.path.join(cert_path, 'key.pem'))
-         ca_cert = os.path.join(cert_path, 'ca.pem')
- 
-         tls_config = tls.TLSConfig(
-             ssl_version=ssl.PROTOCOL_TLSv1,
-             verify=True,
-             assert_hostname=False,
-             client_cert=client_cert,
-             ca_cert=ca_cert,
-         )
- 
      if 'DOCKER_CLIENT_TIMEOUT' in os.environ:
          log.warn('The DOCKER_CLIENT_TIMEOUT environment variable is deprecated. Please use COMPOSE_HTTP_TIMEOUT instead.')
  
-     return Client(base_url=base_url, tls=tls_config, version=api_version, timeout=HTTP_TIMEOUT)
+     kwargs = kwargs_from_env(assert_hostname=False)
+     kwargs['version'] = os.environ.get('COMPOSE_API_VERSION', '1.19')
+     kwargs['timeout'] = HTTP_TIMEOUT
+     return Client(**kwargs)
  
Remove custom docker client initialization logic
## Code Before:
import logging
import os
import ssl

from docker import Client
from docker import tls

from ..const import HTTP_TIMEOUT

log = logging.getLogger(__name__)


def docker_client():
    """
    Returns a docker-py client configured using environment variables
    according to the same logic as the official Docker client.
    """
    cert_path = os.environ.get('DOCKER_CERT_PATH', '')
    if cert_path == '':
        cert_path = os.path.join(os.environ.get('HOME', ''), '.docker')

    base_url = os.environ.get('DOCKER_HOST')
    api_version = os.environ.get('COMPOSE_API_VERSION', '1.19')

    tls_config = None

    if os.environ.get('DOCKER_TLS_VERIFY', '') != '':
        parts = base_url.split('://', 1)
        base_url = '%s://%s' % ('https', parts[1])

        client_cert = (os.path.join(cert_path, 'cert.pem'), os.path.join(cert_path, 'key.pem'))
        ca_cert = os.path.join(cert_path, 'ca.pem')

        tls_config = tls.TLSConfig(
            ssl_version=ssl.PROTOCOL_TLSv1,
            verify=True,
            assert_hostname=False,
            client_cert=client_cert,
            ca_cert=ca_cert,
        )

    if 'DOCKER_CLIENT_TIMEOUT' in os.environ:
        log.warn('The DOCKER_CLIENT_TIMEOUT environment variable is deprecated. Please use COMPOSE_HTTP_TIMEOUT instead.')

    return Client(base_url=base_url, tls=tls_config, version=api_version, timeout=HTTP_TIMEOUT)

## Instruction:
Remove custom docker client initialization logic
## Code After:
import logging
import os

from docker import Client
from docker.utils import kwargs_from_env

from ..const import HTTP_TIMEOUT

log = logging.getLogger(__name__)


def docker_client():
    """
    Returns a docker-py client configured using environment variables
    according to the same logic as the official Docker client.
    """
    if 'DOCKER_CLIENT_TIMEOUT' in os.environ:
        log.warn('The DOCKER_CLIENT_TIMEOUT environment variable is deprecated. Please use COMPOSE_HTTP_TIMEOUT instead.')

    kwargs = kwargs_from_env(assert_hostname=False)
    kwargs['version'] = os.environ.get('COMPOSE_API_VERSION', '1.19')
    kwargs['timeout'] = HTTP_TIMEOUT
    return Client(**kwargs)
