# -*- coding: utf-8 -*-

"""Tools for loading entry points."""

from functools import lru_cache
from typing import Callable, Iterable, Mapping

from .struct import Obo

__all__ = [
    "has_nomenclature_plugin",
    "run_nomenclature_plugin",
    "iter_nomenclature_plugins",
]


@lru_cache()
def _get_nomenclature_plugins() -> Mapping[str, Callable[[], Obo]]:
    from .sources import ontology_resolver

    return ontology_resolver.lookup_dict


def has_nomenclature_plugin(prefix: str) -> bool:
    """Check if there's a plugin for converting the prefix."""
    from .sources import ontology_resolver

    try:
        ontology_resolver.lookup(prefix)
    except KeyError:
        return False
    else:
        return True


def run_nomenclature_plugin(prefix: str) -> Obo:
    """Get a converted PyOBO source."""
    from .sources import ontology_resolver

    return ontology_resolver.make(prefix)


def iter_nomenclature_plugins() -> Iterable[Obo]:
    """Get all modules in the PyOBO sources."""
    from .sources import ontology_resolver

    for _prefix, cls in sorted(ontology_resolver.lookup_dict.items()):
        yield cls()

# -*- coding: utf-8 -*-

"""Tools for loading entry points."""

from functools import lru_cache
from typing import Callable, Iterable, Mapping

from .struct import Obo

__all__ = [
    "has_nomenclature_plugin",
    "run_nomenclature_plugin",
    "iter_nomenclature_plugins",
]


@lru_cache()
def _get_nomenclature_plugins() -> Mapping[str, Callable[[], Obo]]:
    from .sources import ontology_resolver

    return ontology_resolver.lookup_dict


def has_nomenclature_plugin(prefix: str) -> bool:
    """Check if there's a plugin for converting the prefix."""
    from .sources import ontology_resolver

    try:
        ontology_resolver.lookup(prefix)
    except KeyError:
        return False
    else:
        return True


def run_nomenclature_plugin(prefix: str) -> Obo:
    """Get a converted PyOBO source."""
    from .sources import ontology_resolver

    return ontology_resolver.make(prefix)


def iter_nomenclature_plugins() -> Iterable[Obo]:
    """Get all modules in the PyOBO sources."""
    from .sources import ontology_resolver

    for _prefix, cls in sorted(ontology_resolver.lookup_dict.items()):
        yield cls()

# -*- coding: utf-8 -*- Tools for loading entry points. Check if there's a plugin for converting the prefix. Get a converted PyOBO source. Get all modules in the PyOBO sources.