/
niceSOFT
/
python3-hatchling
Обзор
Документация
Войти
/
niceSOFT
/
python3-hatchling
Код
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/hatch/plugin/utils.py
46 строк
1 KB
Dimitri Papadopoulos Orfanos
Upgrade Ruff to 0.13.2 (#1890)
28 сен 2025, 21:20
Не верифицирован
28 сен 2025, 21:20
aff6f39
Код
Авторство
О чём код?
from __future__ import annotations from typing import TYPE_CHECKING if TYPE_CHECKING: from hatch.env.collectors.plugin.interface import EnvironmentCollectorInterface def load_plugin_from_script( path: str, script_name: str, plugin_class: type[EnvironmentCollectorInterface], plugin_id: str ) -> type[EnvironmentCollectorInterface]: from importlib.util import module_from_spec, spec_from_file_location spec = spec_from_file_location(script_name, path) module = module_from_spec(spec) # type: ignore[arg-type] spec.loader.exec_module(module) # type: ignore[union-attr] plugin_finder = f"get_{plugin_id}" names = dir(module) if plugin_finder in names: return getattr(module, plugin_finder)() subclasses = [] for name in names: obj = getattr(module, name) if obj is plugin_class: continue try: if issubclass(obj, plugin_class): subclasses.append(obj) except TypeError: continue if not subclasses: message = f"Unable to find a subclass of `{plugin_class.__name__}` in `{script_name}`: {path}" raise ValueError(message) if len(subclasses) > 1: message = ( f"Multiple subclasses of `{plugin_class.__name__}` found in `{script_name}`, " f"select one by defining a function named `{plugin_finder}`: {path}" ) raise ValueError(message) return subclasses[0]