Move instanciation out of search_object

This commit is contained in:
Matthias 2019-12-24 14:01:28 +01:00
parent 25e6d6a7bf
commit 5a11ca86bb
2 changed files with 7 additions and 11 deletions

View File

@ -66,13 +66,13 @@ class IResolver:
return valid_objects_gen return valid_objects_gen
@classmethod @classmethod
def _search_object(cls, directory: Path, object_name: str, def _search_object(cls, directory: Path, object_name: str
kwargs: dict = {}) -> Union[Tuple[Any, Path], Tuple[None, None]]: ) -> Union[Tuple[Any, Path], Tuple[None, None]]:
""" """
Search for the objectname in the given directory Search for the objectname in the given directory
:param directory: relative or absolute directory path :param directory: relative or absolute directory path
:param object_name: ClassName of the object to load :param object_name: ClassName of the object to load
:return: object instance :return: object class
""" """
logger.debug("Searching for %s %s in '%s'", logger.debug("Searching for %s %s in '%s'",
cls.object_type.__name__, object_name, directory) cls.object_type.__name__, object_name, directory)
@ -86,7 +86,7 @@ class IResolver:
obj = next(cls._get_valid_object(module_path, object_name), None) obj = next(cls._get_valid_object(module_path, object_name), None)
if obj: if obj:
return (obj(**kwargs), module_path) return (obj, module_path)
return (None, None) return (None, None)
@classmethod @classmethod
@ -99,13 +99,12 @@ class IResolver:
for _path in paths: for _path in paths:
try: try:
(module, module_path) = cls._search_object(directory=_path, (module, module_path) = cls._search_object(directory=_path,
object_name=object_name, object_name=object_name)
kwargs=kwargs)
if module: if module:
logger.info( logger.info(
f"Using resolved {cls.object_type.__name__.lower()[1:]} {object_name} " f"Using resolved {cls.object_type.__name__.lower()[1:]} {object_name} "
f"from '{module_path}'...") f"from '{module_path}'...")
return module return module(**kwargs)
except FileNotFoundError: except FileNotFoundError:
logger.warning('Path "%s" does not exist.', _path.resolve()) logger.warning('Path "%s" does not exist.', _path.resolve())

View File

@ -15,19 +15,16 @@ from tests.conftest import log_has, log_has_re
def test_search_strategy(): def test_search_strategy():
default_config = {}
default_location = Path(__file__).parent.parent.joinpath('strategy').resolve() default_location = Path(__file__).parent.parent.joinpath('strategy').resolve()
s, _ = StrategyResolver._search_object( s, _ = StrategyResolver._search_object(
directory=default_location, directory=default_location,
kwargs={'config': default_config},
object_name='DefaultStrategy' object_name='DefaultStrategy'
) )
assert isinstance(s, IStrategy) assert issubclass(s, IStrategy)
s, _ = StrategyResolver._search_object( s, _ = StrategyResolver._search_object(
directory=default_location, directory=default_location,
kwargs={'config': default_config},
object_name='NotFoundStrategy' object_name='NotFoundStrategy'
) )
assert s is None assert s is None