Convert resolvers to classmethods
This commit is contained in:
@@ -7,7 +7,7 @@ import importlib.util
|
||||
import inspect
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from typing import Any, List, Optional, Tuple, Union, Generator
|
||||
from typing import Any, Generator, List, Optional, Tuple, Type, Union
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -16,6 +16,8 @@ class IResolver:
|
||||
"""
|
||||
This class contains all the logic to load custom classes
|
||||
"""
|
||||
# Childclasses need to override this
|
||||
object_type: Type[Any]
|
||||
|
||||
@staticmethod
|
||||
def build_search_paths(config, current_path: Path, user_subdir: Optional[str] = None,
|
||||
@@ -32,12 +34,11 @@ class IResolver:
|
||||
|
||||
return abs_paths
|
||||
|
||||
@staticmethod
|
||||
def _get_valid_object(object_type, module_path: Path,
|
||||
@classmethod
|
||||
def _get_valid_object(cls, module_path: Path,
|
||||
object_name: str) -> Generator[Any, None, None]:
|
||||
"""
|
||||
Generator returning objects with matching object_type and object_name in the path given.
|
||||
:param object_type: object_type (class)
|
||||
:param module_path: absolute path to the module
|
||||
:param object_name: Class name of the object
|
||||
:return: generator containing matching objects
|
||||
@@ -55,19 +56,21 @@ class IResolver:
|
||||
|
||||
valid_objects_gen = (
|
||||
obj for name, obj in inspect.getmembers(module, inspect.isclass)
|
||||
if object_name == name and object_type in obj.__bases__
|
||||
if object_name == name and cls.object_type in obj.__bases__
|
||||
)
|
||||
return valid_objects_gen
|
||||
|
||||
@staticmethod
|
||||
def _search_object(directory: Path, object_type, object_name: str,
|
||||
@classmethod
|
||||
def _search_object(cls, directory: Path, object_name: str,
|
||||
kwargs: dict = {}) -> Union[Tuple[Any, Path], Tuple[None, None]]:
|
||||
"""
|
||||
Search for the objectname in the given directory
|
||||
:param directory: relative or absolute directory path
|
||||
:param object_name: ClassName of the object to load
|
||||
:return: object instance
|
||||
"""
|
||||
logger.debug("Searching for %s %s in '%s'", object_type.__name__, object_name, directory)
|
||||
logger.debug("Searching for %s %s in '%s'",
|
||||
cls.object_type.__name__, object_name, directory)
|
||||
for entry in directory.iterdir():
|
||||
# Only consider python files
|
||||
if not str(entry).endswith('.py'):
|
||||
@@ -75,14 +78,14 @@ class IResolver:
|
||||
continue
|
||||
module_path = entry.resolve()
|
||||
|
||||
obj = next(IResolver._get_valid_object(object_type, module_path, object_name), None)
|
||||
obj = next(cls._get_valid_object(module_path, object_name), None)
|
||||
|
||||
if obj:
|
||||
return (obj(**kwargs), module_path)
|
||||
return (None, None)
|
||||
|
||||
@staticmethod
|
||||
def _load_object(paths: List[Path], object_type, object_name: str,
|
||||
@classmethod
|
||||
def _load_object(cls, paths: List[Path], object_name: str,
|
||||
kwargs: dict = {}) -> Optional[Any]:
|
||||
"""
|
||||
Try to load object from path list.
|
||||
@@ -90,13 +93,12 @@ class IResolver:
|
||||
|
||||
for _path in paths:
|
||||
try:
|
||||
(module, module_path) = IResolver._search_object(directory=_path,
|
||||
object_type=object_type,
|
||||
object_name=object_name,
|
||||
kwargs=kwargs)
|
||||
(module, module_path) = cls._search_object(directory=_path,
|
||||
object_name=object_name,
|
||||
kwargs=kwargs)
|
||||
if module:
|
||||
logger.info(
|
||||
f"Using resolved {object_type.__name__.lower()[1:]} {object_name} "
|
||||
f"Using resolved {cls.object_type.__name__.lower()[1:]} {object_name} "
|
||||
f"from '{module_path}'...")
|
||||
return module
|
||||
except FileNotFoundError:
|
||||
|
Reference in New Issue
Block a user