Created
January 16, 2018 19:41
-
-
Save charlee/af54632272e08e42257ee047064c2d3b to your computer and use it in GitHub Desktop.
traverse modules and objects in python
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
import sys | |
import pkgutil | |
import importlib | |
from .base import BaseCommand, CommandExecutor | |
def discover_commands(): | |
executor = CommandExecutor() | |
current_module = sys.modules[__name__] | |
__path__ = pkgutil.extend_path(current_module.__path__, __name__) | |
for importer, modname, ispkg in pkgutil.walk_packages(path=__path__, prefix=__name__+'.'): | |
mod = importlib.import_module(modname) | |
for name, clazz in mod.__dict__.items(): | |
if type(clazz) == type and issubclass(clazz, BaseCommand) and clazz != BaseCommand: | |
executor.add_command(clazz) | |
print(executor.command_map) | |
return executor |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment