def get_or_create_module(self, fullname):
"""
Given a name and a path it will return a module instance
if found.
When the module could not be found it will raise ImportError
"""
LOGGER.info('Loading module {0}'.format(fullname))
parent, _, module_name = fullname.rpartition('.')
if fullname in modules:
LOGGER.info('Found cache entry for {0}'.format(fullname))
return modules[fullname]
module = modules.setdefault(fullname, imp.new_module(fullname))
if len(fullname.strip('.')) > 3:
absolute_from_root = fullname.split('.', 3)[-1]
modules.setdefault(absolute_from_root, module)
if len(fullname.split('.')) == 4:
# add the root of the project
modules[fullname.split('.')[-1]] = module
# required by PEP 302
module.__file__ = self.get_filename(fullname)
LOGGER.info('Created module {0} with fullname {1}'.format(self.get_filename(fullname), fullname))
module.__name__ = fullname
module.__loader__ = self
module.__path__ = self.path
if self.is_package(fullname):
module.__path__ = self.path
module.__package__ = fullname
else:
module.__package__ = fullname.rpartition('.')[0]
LOGGER.debug('loading file {0}'.format(self.get_filename(fullname)))
source = self.get_source(fullname)
try:
exec(source, module.__dict__)
except Exception as ex:
ipdb.set_trace()
return module
评论列表
文章目录