File size: 807 Bytes
91394e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import importlib
import pkgutil
from pathlib import Path

from .base import MelodyDatasetHandler

_registry = {}

for _, module_name, _ in pkgutil.iter_modules([str(Path(__file__).parent)]):
    if module_name in ("__init__", "base"):
        continue

    module = importlib.import_module(f"{__name__}.{module_name}")
    for attr_name in dir(module):
        attr = getattr(module, attr_name)
        if (
            isinstance(attr, type)
            and issubclass(attr, MelodyDatasetHandler)
            and attr is not MelodyDatasetHandler
        ):
            _registry[attr.name] = attr  # 注册 class 本身


def get_melody_handler(name: str) -> type[MelodyDatasetHandler]:
    if name not in _registry:
        raise ValueError(f"Melody source '{name}' not found")
    return _registry[name]