checkmappings.py 1.09 KB
#!/usr/bin/env python
from pkgutil import walk_packages
from os import path

__all__ = []
__pkg_prefix = "%s." % __name__
__pkg_path = path.abspath('floraconcierge/mapping/')

for loader, modname, _ in walk_packages([__pkg_path]):
    # load the module / package
    print('Trying to import floraconcierge.mapping.%s' % modname)
    module = loader.find_module(modname).load_module(modname)
    modname = modname[len(__pkg_prefix):]  # strip package prefix from name
    # append all toplevel modules and packages to __all__
    if "." not in modname:
        __all__.append(modname)
        globals()[modname] = module
    # set everything else as an attribute of their parent package
    else:
        # get the toplevel package from globals()
        pkg_name, rest = modname.split(".", 1)
        pkg = globals()[pkg_name]
        # recursively get the modules parent package via getattr
        while "." in rest:
            subpkg, rest = rest.split(".", 1)
            pkg = getattr(pkg, subpkg)
        # set the module (or package) as an attribute of its parent package
        setattr(pkg, rest, module)