utils.py 3.5 KB
Newer Older
payno's avatar
payno committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2019-2020 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ###########################################################################*/

__authors__ = ["H. Payno"]
__license__ = "MIT"
__date__ = "22/04/2020"


import logging
import pkgutil
import inspect
34
import importlib
payno's avatar
payno committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

_logger = logging.getLogger(__name__)


def get_registered_add_ons():
    """Return the list of registered add-on"""
    try:
        import ppfaddon
    except ImportError:
        _logger.info('no add on found')
        return []
    else:
        modules = []
        for importer, modname, ispkg in pkgutil.iter_modules(ppfaddon.__path__):
            if ispkg:
                modules.append(modname)
        return modules


54
def _check_module_for_class(module, depth=0):
payno's avatar
payno committed
55
    classes = []
56
57
58
59
60
61
62
63
64
    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])
65
66
67
68
69
70
71
72
73
74
75
76
77
            elif hasattr(m[1], '__path__'):
                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))
78
79
80
81
82
83
84
    return classes


def get_registered_add_ons_classes():
    """Return a dictionary with the list of registered add-on classes for each
    add-on"""
    classes = {}
payno's avatar
payno committed
85
86

    add_on_modules = get_registered_add_ons()
87
88
    for module_name in add_on_modules:
        package_name = '.'.join(('ppfaddon', module_name))
payno's avatar
payno committed
89
        try:
90
91
92
93
94
95
            module = __import__(package_name)
        except:
            _logger.error('fail to import {}'.format(package_name))
            return []

        classes[module_name] = _check_module_for_class(module=module)
payno's avatar
payno committed
96
97
98
99
100
101
    return classes


def is_add_on_class_relative_to(add_on_class, myclass):
    """Return the list of registered add-on classes"""
    return isinstance(myclass, add_on_class.target)