Skip to content

Commit

Permalink
feat: add api to get backend entry points
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud authored and kszucs committed Sep 12, 2022
1 parent 7513d94 commit 0152f5e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
18 changes: 4 additions & 14 deletions ibis/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
"""Initialize Ibis module."""
from __future__ import annotations

import importlib.metadata as _importlib_metadata

# Converting an Ibis schema to a pandas DataFrame requires registering
# some type conversions that are currently registered in the pandas backend
import ibis.backends.pandas
Expand All @@ -23,20 +21,10 @@
_KNOWN_BACKENDS = ['bigquery', 'heavyai']


def _get_backend_entrypoints() -> list[_importlib_metadata.EntryPoint]:
"""Get the list of installed `ibis.backend` entrypoints"""
import sys

if sys.version_info < (3, 10):
return list(_importlib_metadata.entry_points()['ibis.backends'])
else:
return list(_importlib_metadata.entry_points(group="ibis.backends"))


def __dir__() -> list[str]:
"""Adds tab completion for ibis backends to the top-level module"""
out = set(__all__)
out.update(ep.name for ep in _get_backend_entrypoints())
out.update(ep.name for ep in util.backend_entry_points())
return sorted(out)


Expand All @@ -55,7 +43,9 @@ def __getattr__(name: str) -> BaseBackend:
the `ibis.backends` entrypoints. If successful, the `ibis.sqlite`
attribute is "cached", so this function is only called the first time.
"""
entry_points = {ep for ep in _get_backend_entrypoints() if ep.name == name}
entry_points = {
ep for ep in util.backend_entry_points() if ep.name == name
}

if not entry_points:
msg = f"module 'ibis' has no attribute '{name}'. "
Expand Down
12 changes: 12 additions & 0 deletions ibis/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import abc
import collections
import functools
import importlib.metadata as _importlib_metadata
import itertools
import logging
import operator
import os
import sys
import textwrap
import types
import warnings
Expand Down Expand Up @@ -523,3 +525,13 @@ class ToFrame(abc.ABC):
@abc.abstractmethod
def to_frame(self) -> pd.DataFrame:
...


def backend_entry_points() -> list[_importlib_metadata.EntryPoint]:
"""Get the list of installed `ibis.backend` entrypoints"""

if sys.version_info < (3, 10):
eps = _importlib_metadata.entry_points()["ibis.backends"]
else:
eps = _importlib_metadata.entry_points(group="ibis.backends")
return sorted(eps)

0 comments on commit 0152f5e

Please sign in to comment.