from pkg_resources import get_distribution, parse_version

try:
    import psycopg2  # noqa: F401
    if get_distribution('psycopg2').parsed_version < parse_version('2.5'):
        raise ImportError('Minimum required version for psycopg2 is 2.5')
except ImportError:
    raise ImportError(
        'No module named psycopg2. Please install either '
        'psycopg2 or psycopg2-binary package for CPython '
        'or psycopg2cffi for Pypy.'
    )

__version__ = get_distribution('sqlalchemy-redshift').version

from sqlalchemy.dialects import registry

registry.register("redshift", "sqlalchemy_redshift.dialect", "RedshiftDialect")
registry.register(
    "redshift.psycopg2", "sqlalchemy_redshift.dialect", "RedshiftDialect"
)

from pkg_resources import DistributionNotFound, get_distribution, parse_version

try:
    import psycopg2  # noqa: F401
except ImportError:
    raise ImportError(
        'No module named psycopg2. Please install either '
        'psycopg2 or psycopg2-binary package for CPython '
        'or psycopg2cffi for Pypy.'
    ) from None

for package in ['psycopg2', 'psycopg2-binary', 'psycopg2cffi']:
    try:
        if get_distribution(package).parsed_version < parse_version('2.5'):
            raise ImportError('Minimum required version for psycopg2 is 2.5')
        break
    except DistributionNotFound:
        pass
else:
    raise ImportError(
        'A module was found named psycopg2, '
        'but the version of it could not be checked '
        'as it was neither the Python package psycopg2, '
        'psycopg2-binary or psycopg2cffi.'
    )

__version__ = get_distribution('sqlalchemy-redshift').version

from sqlalchemy.dialects import registry

registry.register("redshift", "sqlalchemy_redshift.dialect", "RedshiftDialect")
registry.register(
    "redshift.psycopg2", "sqlalchemy_redshift.dialect", "RedshiftDialect"
)

Check the version of any of the supported Psycopg2 packages
Check the version of any of the supported Psycopg2 packages

A check was introduced in commit 8e0c4857a1c08f257b95d3b1ee5f6eb795d55cdc which
would check what version of the 'psycopg2' Python (pip) package was installed
as the dependency was removed from setup.py.

The check would however only check the 'psycopg2' package and not the other two
supported providers of the psycopg2 module, which meant importing the
sqlalchemy_redshift module would throw an exception, even though they were
installed.

This changes the check to check for either of the three supported psycopg2
packages and throws an exception if any of them fail to validate.

- from pkg_resources import get_distribution, parse_version
+ from pkg_resources import DistributionNotFound, get_distribution, parse_version
  
  try:
      import psycopg2  # noqa: F401
-     if get_distribution('psycopg2').parsed_version < parse_version('2.5'):
-         raise ImportError('Minimum required version for psycopg2 is 2.5')
  except ImportError:
      raise ImportError(
          'No module named psycopg2. Please install either '
          'psycopg2 or psycopg2-binary package for CPython '
          'or psycopg2cffi for Pypy.'
+     ) from None
+ 
+ for package in ['psycopg2', 'psycopg2-binary', 'psycopg2cffi']:
+     try:
+         if get_distribution(package).parsed_version < parse_version('2.5'):
+             raise ImportError('Minimum required version for psycopg2 is 2.5')
+         break
+     except DistributionNotFound:
+         pass
+ else:
+     raise ImportError(
+         'A module was found named psycopg2, '
+         'but the version of it could not be checked '
+         'as it was neither the Python package psycopg2, '
+         'psycopg2-binary or psycopg2cffi.'
      )
  
  __version__ = get_distribution('sqlalchemy-redshift').version
  
  from sqlalchemy.dialects import registry
  
  registry.register("redshift", "sqlalchemy_redshift.dialect", "RedshiftDialect")
  registry.register(
      "redshift.psycopg2", "sqlalchemy_redshift.dialect", "RedshiftDialect"
  )
  
Check the version of any of the supported Psycopg2 packages
## Code Before:
from pkg_resources import get_distribution, parse_version

try:
    import psycopg2  # noqa: F401
    if get_distribution('psycopg2').parsed_version < parse_version('2.5'):
        raise ImportError('Minimum required version for psycopg2 is 2.5')
except ImportError:
    raise ImportError(
        'No module named psycopg2. Please install either '
        'psycopg2 or psycopg2-binary package for CPython '
        'or psycopg2cffi for Pypy.'
    )

__version__ = get_distribution('sqlalchemy-redshift').version

from sqlalchemy.dialects import registry

registry.register("redshift", "sqlalchemy_redshift.dialect", "RedshiftDialect")
registry.register(
    "redshift.psycopg2", "sqlalchemy_redshift.dialect", "RedshiftDialect"
)

## Instruction:
Check the version of any of the supported Psycopg2 packages
## Code After:
from pkg_resources import DistributionNotFound, get_distribution, parse_version

try:
    import psycopg2  # noqa: F401
except ImportError:
    raise ImportError(
        'No module named psycopg2. Please install either '
        'psycopg2 or psycopg2-binary package for CPython '
        'or psycopg2cffi for Pypy.'
    ) from None

for package in ['psycopg2', 'psycopg2-binary', 'psycopg2cffi']:
    try:
        if get_distribution(package).parsed_version < parse_version('2.5'):
            raise ImportError('Minimum required version for psycopg2 is 2.5')
        break
    except DistributionNotFound:
        pass
else:
    raise ImportError(
        'A module was found named psycopg2, '
        'but the version of it could not be checked '
        'as it was neither the Python package psycopg2, '
        'psycopg2-binary or psycopg2cffi.'
    )

__version__ = get_distribution('sqlalchemy-redshift').version

from sqlalchemy.dialects import registry

registry.register("redshift", "sqlalchemy_redshift.dialect", "RedshiftDialect")
registry.register(
    "redshift.psycopg2", "sqlalchemy_redshift.dialect", "RedshiftDialect"
)
