"""Cloud configuration."""
from cloud_browser.cloud.rackspace import RackspaceConnection


class Config(object):
    """Cloud configuration helper."""
    conn_cls = RackspaceConnection
    __singleton = None

    def __init__(self, connection):
        """Initializer."""
        self.connection = connection

    @classmethod
    def from_settings(cls):
        """Create configuration from Django settings or environment."""
        from cloud_browser.app_settings import settings
        from django.core.exceptions import ImproperlyConfigured

        account = settings.CLOUD_BROWSER_RACKSPACE_ACCOUNT
        secret_key = settings.CLOUD_BROWSER_RACKSPACE_SECRET_KEY
        servicenet = settings.CLOUD_BROWSER_RACKSPACE_SERVICENET

        if not (account and secret_key):
            raise ImproperlyConfigured("No suitable credentials found.")

        conn = cls.conn_cls(account, secret_key, servicenet)
        return cls(conn)

    @classmethod
    def singleton(cls):
        """Get singleton object."""
        if cls.__singleton is None:
            cls.__singleton = cls.from_settings()
        return cls.__singleton

"""Cloud configuration."""


class Config(object):
    """Cloud configuration helper."""
    __singleton = None

    def __init__(self, connection):
        """Initializer."""
        self.connection = connection

    @classmethod
    def from_settings(cls):
        """Create configuration from Django settings or environment."""
        from cloud_browser.app_settings import settings
        from django.core.exceptions import ImproperlyConfigured

        conn = None
        if conn is None:
            # Try Rackspace
            account = settings.CLOUD_BROWSER_RACKSPACE_ACCOUNT
            secret_key = settings.CLOUD_BROWSER_RACKSPACE_SECRET_KEY
            servicenet = settings.CLOUD_BROWSER_RACKSPACE_SERVICENET
            if (account and secret_key):
                from cloud_browser.cloud.rackspace import RackspaceConnection
                conn = RackspaceConnection(account, secret_key, servicenet)

        if not conn:
            raise ImproperlyConfigured("No suitable credentials found.")

        return cls(conn)

    @classmethod
    def singleton(cls):
        """Get singleton object."""
        if cls.__singleton is None:
            cls.__singleton = cls.from_settings()
        return cls.__singleton

Refactor to allow different connection class bindings.
Config: Refactor to allow different connection class bindings.

  """Cloud configuration."""
- from cloud_browser.cloud.rackspace import RackspaceConnection
  
  
  class Config(object):
      """Cloud configuration helper."""
-     conn_cls = RackspaceConnection
      __singleton = None
  
      def __init__(self, connection):
          """Initializer."""
          self.connection = connection
  
      @classmethod
      def from_settings(cls):
          """Create configuration from Django settings or environment."""
          from cloud_browser.app_settings import settings
          from django.core.exceptions import ImproperlyConfigured
  
+         conn = None
+         if conn is None:
+             # Try Rackspace
-         account = settings.CLOUD_BROWSER_RACKSPACE_ACCOUNT
+             account = settings.CLOUD_BROWSER_RACKSPACE_ACCOUNT
-         secret_key = settings.CLOUD_BROWSER_RACKSPACE_SECRET_KEY
+             secret_key = settings.CLOUD_BROWSER_RACKSPACE_SECRET_KEY
-         servicenet = settings.CLOUD_BROWSER_RACKSPACE_SERVICENET
+             servicenet = settings.CLOUD_BROWSER_RACKSPACE_SERVICENET
+             if (account and secret_key):
+                 from cloud_browser.cloud.rackspace import RackspaceConnection
+                 conn = RackspaceConnection(account, secret_key, servicenet)
  
-         if not (account and secret_key):
+         if not conn:
              raise ImproperlyConfigured("No suitable credentials found.")
  
-         conn = cls.conn_cls(account, secret_key, servicenet)
          return cls(conn)
  
      @classmethod
      def singleton(cls):
          """Get singleton object."""
          if cls.__singleton is None:
              cls.__singleton = cls.from_settings()
          return cls.__singleton
  
Refactor to allow different connection class bindings.
## Code Before:
"""Cloud configuration."""
from cloud_browser.cloud.rackspace import RackspaceConnection


class Config(object):
    """Cloud configuration helper."""
    conn_cls = RackspaceConnection
    __singleton = None

    def __init__(self, connection):
        """Initializer."""
        self.connection = connection

    @classmethod
    def from_settings(cls):
        """Create configuration from Django settings or environment."""
        from cloud_browser.app_settings import settings
        from django.core.exceptions import ImproperlyConfigured

        account = settings.CLOUD_BROWSER_RACKSPACE_ACCOUNT
        secret_key = settings.CLOUD_BROWSER_RACKSPACE_SECRET_KEY
        servicenet = settings.CLOUD_BROWSER_RACKSPACE_SERVICENET

        if not (account and secret_key):
            raise ImproperlyConfigured("No suitable credentials found.")

        conn = cls.conn_cls(account, secret_key, servicenet)
        return cls(conn)

    @classmethod
    def singleton(cls):
        """Get singleton object."""
        if cls.__singleton is None:
            cls.__singleton = cls.from_settings()
        return cls.__singleton

## Instruction:
Refactor to allow different connection class bindings.
## Code After:
"""Cloud configuration."""


class Config(object):
    """Cloud configuration helper."""
    __singleton = None

    def __init__(self, connection):
        """Initializer."""
        self.connection = connection

    @classmethod
    def from_settings(cls):
        """Create configuration from Django settings or environment."""
        from cloud_browser.app_settings import settings
        from django.core.exceptions import ImproperlyConfigured

        conn = None
        if conn is None:
            # Try Rackspace
            account = settings.CLOUD_BROWSER_RACKSPACE_ACCOUNT
            secret_key = settings.CLOUD_BROWSER_RACKSPACE_SECRET_KEY
            servicenet = settings.CLOUD_BROWSER_RACKSPACE_SERVICENET
            if (account and secret_key):
                from cloud_browser.cloud.rackspace import RackspaceConnection
                conn = RackspaceConnection(account, secret_key, servicenet)

        if not conn:
            raise ImproperlyConfigured("No suitable credentials found.")

        return cls(conn)

    @classmethod
    def singleton(cls):
        """Get singleton object."""
        if cls.__singleton is None:
            cls.__singleton = cls.from_settings()
        return cls.__singleton
