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
@classmethod
def _search_object(cls, directory: Path, object_name: str,
kwargs: dict = {}) -> Union[Tuple[Any, Path], Tuple[None, None]]:
def _search_object(cls, directory: Path, object_name: str
) -> 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
:return: object class
"""
logger.debug("Searching for %s %s in '%s'",
cls.object_type.__name__, object_name, directory)
@ -86,7 +86,7 @@ class IResolver:
obj = next(cls._get_valid_object(module_path, object_name), None)
if obj:
return (obj(**kwargs), module_path)
return (obj, module_path)
return (None, None)
@classmethod
@ -99,13 +99,12 @@ class IResolver:
for _path in paths:
try:
(module, module_path) = cls._search_object(directory=_path,
object_name=object_name,
kwargs=kwargs)
object_name=object_name)
if module:
logger.info(
f"Using resolved {cls.object_type.__name__.lower()[1:]} {object_name} "
f"from '{module_path}'...")
return module
return module(**kwargs)
except FileNotFoundError:
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():
default_config = {}
default_location = Path(__file__).parent.parent.joinpath('strategy').resolve()
s, _ = StrategyResolver._search_object(
directory=default_location,
kwargs={'config': default_config},
object_name='DefaultStrategy'
)
assert isinstance(s, IStrategy)
assert issubclass(s, IStrategy)
s, _ = StrategyResolver._search_object(
directory=default_location,
kwargs={'config': default_config},
object_name='NotFoundStrategy'
)
assert s is None