File tree 1 file changed +35
-0
lines changed
1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change 4
4
# SPDX-License-Identifier: LGPL-3.0-or-later
5
5
#
6
6
"""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 ]
You can’t perform that action at this time.
0 commit comments