Coverage for arrakis_server/backends/__init__.py: 81.2%

1# Copyright (c) 2022, California Institute of Technology and contributors

2#

3# You should have received a copy of the licensing terms for this

4# software included in the file "LICENSE" located in the top-level

5# directory of this package. If you did not, you can view a copy at

6# https://git.ligo.org/ngdd/arrakis-server/-/raw/main/LICENSE

7

8 import traceback

9 import warnings

10 from enum import Enum

11 from importlib.metadata import entry_points

12

13 from ..traits import MaybeBackend

14

15

16 def _discover_backends() -> dict[str, MaybeBackend]:

17 backends: dict[str, MaybeBackend] = {"NONE": None}

18 entrypoints = entry_points(group="arrakis-server-backend")

19 for backend_name in entrypoints.names:

20 try:

21 backends[backend_name.upper()] = entrypoints[backend_name].load()

22 except ImportError:

23 # FIXME: should implement some kind of hook to expose

24 # plugin errors?

25 warnings.warn(

26 f"{backend_name} backend could not be loaded",

27 category=ImportWarning,

28 )

29 traceback.print_exc()

30 return backends

31

32

33 BackendType = Enum("BackendType", _discover_backends()) # type: ignore[misc]