Skip to content

Instantly share code, notes, and snippets.

@ResidentMario
Created February 22, 2019 02:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ResidentMario/70b1b856f271930c1fa6a6f00cba3f56 to your computer and use it in GitHub Desktop.
Save ResidentMario/70b1b856f271930c1fa6a6f00cba3f56 to your computer and use it in GitHub Desktop.
from t4 import list_packages, Package
MODULE_PATH = []
class DataPackageImporter:
"""
Data package module loader. Executes package import code and adds the package to the
module cache.
"""
@classmethod
def create_module(cls, spec):
"""
Module creator. Returning None causes Python to use the default module creator.
"""
return None
@classmethod
def exec_module(cls, module):
"""
Module executor.
"""
name_parts = module.__name__.split('.')
if module.__name__ == 't4.data':
# __path__ must be set even if the package is virtual, but can be set to [].
module.__path__ = MODULE_PATH
return module
elif len(name_parts) == 3: # e.g. module.__name__ == t4.data.foo
namespace = name_parts[2]
for pkg in list_packages():
pkg_user, pkg_name = pkg.split('/')
if pkg_user == namespace:
module.__dict__[pkg_name] = Package.browse(pkg)
module.__path__ = MODULE_PATH
return module
else:
assert False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment