Commit ac6d9542 authored by payno's avatar payno
Browse files

[utils] fix get_registered_add_ons_classes. Tested on 'ppfhelloworld' add-on test unit

parent 8e42547f
......@@ -31,6 +31,7 @@ __date__ = "22/04/2020"
import logging
import pkgutil
import inspect
import importlib
_logger = logging.getLogger(__name__)
......@@ -50,21 +51,48 @@ def get_registered_add_ons():
return modules
def get_registered_add_ons_classes():
"""Return the list of registered add-on classes"""
def _check_module_for_class(module, depth=0):
classes = []
for m in inspect.getmembers(module):
if inspect.isclass(m[1]):
if (m[1].__module__!= 'pypushflow.addon.classes'):
classes.append(m[1])
if inspect.ismodule(m[1]):
if inspect.isclass(m[1]):
classes.append(m[1])
for importer, modname, ispkg in pkgutil.iter_modules(
m[1].__path__):
module_name = module.__name__
submod_name = '.'.join((module_name, m[0], modname))
try:
mod = importlib.import_module(submod_name)
except Exception:
pass
else:
classes.extend(
_check_module_for_class(module=mod,
depth=depth+1))
return classes
def get_registered_add_ons_classes():
"""Return a dictionary with the list of registered add-on classes for each
add-on"""
classes = {}
add_on_modules = get_registered_add_ons()
for module in add_on_modules:
package_name = '.'.join(('ppfaddon', module))
for module_name in add_on_modules:
package_name = '.'.join(('ppfaddon', module_name))
try:
mymodule = __import__(package_name, fromlist=['foo'])
for element_name in dir(mymodule):
element = getattr(mymodule, element_name)
if inspect.isclass(element):
classes.append(element)
except ImportError as e:
_logger.error(str(e))
module = __import__(package_name)
except:
_logger.error('fail to import {}'.format(package_name))
return []
classes[module_name] = _check_module_for_class(module=module)
return classes
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment