Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
44 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
# make the import path nice | ||
from malcolm.controllers.clientcontroller import ClientController # noqa | ||
from malcolm.controllers.countercontroller import CounterController # noqa | ||
from malcolm.controllers.hellocontroller import HelloController # noqa | ||
from malcolm.controllers.scanpointtickercontroller import \ | ||
ScanPointTickerController # noqa | ||
from malcolm.util import import_child_packages | ||
|
||
class_dict = import_child_packages("controllers") | ||
|
||
globals().update(class_dict) | ||
__all__ = list(class_dict) | ||
|
||
del class_dict | ||
del import_child_packages |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 |