-
Notifications
You must be signed in to change notification settings - Fork 14
/
util.py
31 lines (28 loc) · 1.16 KB
/
util.py
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
import os
import importlib
import logging
def import_child_packages(package_name):
"""Prepare a package namespace by importing all subclasses following PEP8
rules that have @takes decorated functions"""
class_dict = {}
# this is the path to the package
package_path = os.path.join(os.path.dirname(__file__), package_name)
for f in os.listdir(package_path):
if f.endswith(".py") and f != "__init__.py":
# import it and see what it produces
module_name = f[:-3]
module = importlib.import_module(
"malcolm.%s.%s" % (package_name, module_name))
for cls in find_decorated_classes(module):
class_dict[cls.__name__] = cls
return class_dict
def find_decorated_classes(module):
for n in dir(module):
cls = getattr(module, n)
if hasattr(cls, "Method"):
module_name = module.__name__.split(".")[-1]
if n.lower() != module_name:
logging.warning("Classname %s when lower cased should be %s" %
(n, module_name))
logging.debug("Found child class %s" % cls)
yield cls