2023-10-05 16:53:51 +02:00
|
|
|
import importlib.util
|
2023-10-09 08:32:43 +02:00
|
|
|
|
2023-10-05 16:53:51 +02:00
|
|
|
from ..utils.tools import get_class
|
2023-10-09 08:32:43 +02:00
|
|
|
from .base_model import BaseModel
|
2023-10-05 16:53:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_model(name):
|
|
|
|
import_paths = [
|
|
|
|
name,
|
|
|
|
f"{__name__}.{name}",
|
|
|
|
f"{__name__}.extractors.{name}", # backward compatibility
|
|
|
|
f"{__name__}.matchers.{name}", # backward compatibility
|
|
|
|
]
|
|
|
|
for path in import_paths:
|
|
|
|
try:
|
|
|
|
spec = importlib.util.find_spec(path)
|
|
|
|
except ModuleNotFoundError:
|
|
|
|
spec = None
|
|
|
|
if spec is not None:
|
|
|
|
try:
|
|
|
|
return get_class(path, BaseModel)
|
|
|
|
except AssertionError:
|
|
|
|
mod = __import__(path, fromlist=[""])
|
|
|
|
try:
|
|
|
|
return mod.__main_model__
|
|
|
|
except AttributeError as exc:
|
|
|
|
print(exc)
|
|
|
|
continue
|
|
|
|
|
|
|
|
raise RuntimeError(f'Model {name} not found in any of [{" ".join(import_paths)}]')
|