Skip to content

Commit 2036f7a

Browse files
committed
libmidi_io: backends: Add backend utils
Change-Id: I87dc9953c00fc087863312d626d2e78f5c3f0f6e
1 parent 162bb65 commit 2036f7a

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

Diff for: libmidi_io/backends/__init__.py

+35
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,38 @@
44
# SPDX-License-Identifier: LGPL-3.0-or-later
55
#
66
"""MIDI I/O backends."""
7+
8+
from importlib import import_module
9+
from pathlib import Path
10+
from pkgutil import iter_modules
11+
from typing import Dict
12+
13+
from libmidi_io.types.backend import BackendModule
14+
15+
backends_path = Path(__file__).parent
16+
17+
def get_available_backends() -> Dict[str, BackendModule]:
18+
"""Return a list of available MIDI I/O backends."""
19+
backends = {}
20+
21+
for _, name, _ in iter_modules([str(backends_path)]):
22+
try:
23+
module = import_module(f'{__name__}.{name}')
24+
except Exception:
25+
continue
26+
27+
if not hasattr(module, 'get_devices') or not hasattr(module, 'Port'):
28+
continue
29+
30+
backends[name] = module
31+
32+
return backends
33+
34+
def get_backend(name: str) -> BackendModule:
35+
"""Return a backend module by name."""
36+
backends = get_available_backends()
37+
38+
if name not in backends:
39+
raise ValueError(f"Unknown backend {name!r}")
40+
41+
return backends[name]

0 commit comments

Comments
 (0)