2018-11-24 19:00:02 +00:00
|
|
|
# pragma pylint: disable=attribute-defined-outside-init
|
|
|
|
|
|
|
|
"""
|
2018-11-24 19:02:29 +00:00
|
|
|
This module load custom objects
|
2018-11-24 19:00:02 +00:00
|
|
|
"""
|
|
|
|
import importlib.util
|
|
|
|
import inspect
|
|
|
|
import logging
|
2018-11-24 19:39:16 +00:00
|
|
|
from pathlib import Path
|
2018-11-25 09:07:06 +00:00
|
|
|
from typing import Optional, Type, Any
|
2018-11-24 19:00:02 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class IResolver(object):
|
|
|
|
"""
|
2018-11-25 08:55:36 +00:00
|
|
|
This class contains all the logic to load custom classes
|
2018-11-24 19:00:02 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
@staticmethod
|
2018-11-25 09:07:06 +00:00
|
|
|
def _get_valid_object(object_type, module_path: Path,
|
|
|
|
object_name: str) -> Optional[Type[Any]]:
|
2018-11-24 19:00:02 +00:00
|
|
|
"""
|
2018-11-25 09:07:06 +00:00
|
|
|
Returns the first object with matching object_type and object_name in the path given.
|
2018-11-24 19:00:02 +00:00
|
|
|
:param object_type: object_type (class)
|
|
|
|
:param module_path: absolute path to the module
|
|
|
|
:param object_name: Class name of the object
|
2018-11-25 09:07:06 +00:00
|
|
|
:return: class or None
|
2018-11-24 19:00:02 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
# Generate spec based on absolute path
|
2018-11-24 19:39:16 +00:00
|
|
|
spec = importlib.util.spec_from_file_location('unknown', str(module_path))
|
2018-11-24 19:00:02 +00:00
|
|
|
module = importlib.util.module_from_spec(spec)
|
|
|
|
spec.loader.exec_module(module) # type: ignore # importlib does not use typehints
|
|
|
|
|
|
|
|
valid_objects_gen = (
|
|
|
|
obj for name, obj in inspect.getmembers(module, inspect.isclass)
|
|
|
|
if object_name == name and object_type in obj.__bases__
|
|
|
|
)
|
|
|
|
return next(valid_objects_gen, None)
|
|
|
|
|
|
|
|
@staticmethod
|
2018-11-24 19:39:16 +00:00
|
|
|
def _search_object(directory: Path, object_type, object_name: str,
|
2018-11-24 19:14:08 +00:00
|
|
|
kwargs: dict = {}) -> Optional[Any]:
|
2018-11-24 19:00:02 +00:00
|
|
|
"""
|
|
|
|
Search for the objectname in the given directory
|
|
|
|
:param directory: relative or absolute directory path
|
|
|
|
:return: object instance
|
|
|
|
"""
|
2019-02-19 21:27:20 +00:00
|
|
|
logger.debug("Searching for %s %s in '%s'", object_type.__name__, object_name, directory)
|
2018-11-24 19:39:16 +00:00
|
|
|
for entry in directory.iterdir():
|
2018-11-24 19:00:02 +00:00
|
|
|
# Only consider python files
|
2018-11-24 19:39:16 +00:00
|
|
|
if not str(entry).endswith('.py'):
|
2018-11-24 19:00:02 +00:00
|
|
|
logger.debug('Ignoring %s', entry)
|
|
|
|
continue
|
2018-11-25 09:07:06 +00:00
|
|
|
obj = IResolver._get_valid_object(
|
2018-11-24 19:39:16 +00:00
|
|
|
object_type, Path.resolve(directory.joinpath(entry)), object_name
|
2018-11-24 19:00:02 +00:00
|
|
|
)
|
|
|
|
if obj:
|
|
|
|
return obj(**kwargs)
|
|
|
|
return None
|