Add hardware type for Ampere Hawk server
[ta/hw-detector.git] / src / hw_detector / hw_types / __init__.py
1 # Copyright 2019 Nokia
2
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import os
16 import sys
17 import inspect
18 import hw_detector.hw_type as hwbase_type
19
20 def _import_classes(module_name, module_type):
21     classes = []
22     try:
23         __import__(module_name)
24     except ImportError as e:
25         print("Failed import in {0} skipping {1}".format(module_name, e))
26         return None
27     module = sys.modules[module_name]
28     for obj_name in dir(module):
29         # Skip objects that are meant to be private.
30         if obj_name.startswith('_'):
31             continue
32         elif obj_name == module_type.__name__:
33             continue
34         itm = getattr(module, obj_name)
35         if inspect.isclass(itm) and issubclass(itm, module_type):
36             classes.append(itm)
37     return classes
38
39 def get_libs(directory, module_type):
40     classes = []
41     if directory not in sys.path:
42             sys.path.append(directory)
43
44     for fname in os.listdir(directory):
45         root, ext = os.path.splitext(fname)
46         if ext != '.py' or root == '__init__':
47             continue
48         module_name = "%s" % (root)
49         for iclass in _import_classes(module_name, module_type):
50             classes.append(iclass())
51     return classes
52
53 types = get_libs('%s/hw_types' % os.path.dirname(hwbase_type.__file__), hwbase_type.HWType)