import importlib


def is_openeye_installed(oetools=('oechem', 'oequacpac', 'oeiupac', 'oeomega')):
    """
    Check if a given OpenEye tool is installed and Licensed.

    If the OpenEye toolkit is not installed, returns False.

    Parameters
    ----------
    oetools : str or iterable of strings, Optional, Default: ('oechem', 'oequacpac', 'oeiupac', 'oeomega')
        Set of tools to check by their string name. Defaults to the
        complete set that YANK *could* use, depending on feature requested.

        Only checks the subset of tools if passed. Also accepts a single
        tool to check as a string instead of an iterable of length 1.

    Returns
    -------
    all_installed : bool
        True if all tools in ``oetools`` are installed and licensed, False otherwise.
    """
    # Complete list of module: License function name.
    tools_license = {'oechem': 'OEChemIsLicensed',
                     'oequacpac': 'OEQuacPacIsLicensed',
                     'oeiupac': 'OEIUPACIsLicensed',
                     'oeomega': 'OEOmegaIsLicensed'}

    # Cast oetools to tuple if its a single string.
    if type(oetools) is str:
        oetools = (oetools,)
    # Check if the input oetools are known.
    if not set(oetools).issubset(set(tools_license)):
        raise ValueError("Expected an OpenEye tools subset of {}, but instead "
                         "got {}".format(tuple(tools_license), oetools))

    # Try loading the module.
    for tool in oetools:
        try:
            module = importlib.import_module('openeye.' + tool)
        except ImportError:
            return False
        # Check that we have the license.
        if not getattr(module, tools_license[tool])():
            return False
    return True