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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
18 import hw_detector.hw_type as hwbase_type
20 def _import_classes(module_name, module_type):
23 __import__(module_name)
24 except ImportError as e:
25 print("Failed import in {0} skipping {1}".format(module_name, e))
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('_'):
32 elif obj_name == module_type.__name__:
34 itm = getattr(module, obj_name)
35 if inspect.isclass(itm) and issubclass(itm, module_type):
39 def get_libs(directory, module_type):
41 if directory not in sys.path:
42 sys.path.append(directory)
44 for fname in os.listdir(directory):
45 root, ext = os.path.splitext(fname)
46 if ext != '.py' or root == '__init__':
48 module_name = "%s" % (root)
49 for iclass in _import_classes(module_name, module_type):
50 classes.append(iclass())
53 types = get_libs('%s/hw_types' % os.path.dirname(hwbase_type.__file__), hwbase_type.HWType)