def _import_module(self, module_name):
logger.info('Importing module "%s"', module_name)
file_ = None
# Import the module
try:
file_, pathname, description = imp.find_module(module_name)
except ImportError as e:
msg = 'Failed to import module "{}": {}'.format(module_name, e)
raise FlexerException(msg, type(e))
# Do not allow using builtin modules as handlers
_, _, module_type = description
if file_ is None:
if module_type == imp.C_BUILTIN:
msg = ('Built-in module "{}" cannot be a handler module'
.format(module_name))
raise FlexerException(msg, Exception)
# Load the module
try:
module = imp.load_module(module_name, file_, pathname, description)
except ImportError as e:
msg = 'Failed to import module "{}": {}'.format(module_name, e)
stack_trace = format_stack_trace(sys.exc_info())
raise FlexerException(msg, type(e), stack_trace)
except SyntaxError as e:
stack_trace = (
'File \"%s\", line %s\n\t%s' % (e.filename.split('/')[-1],
e.lineno,
e.text)
)
msg = 'Syntax error in module "{}": {}'.format(module_name, e)
raise FlexerException(msg, type(e), stack_trace)
except Exception as e:
stack_trace = format_stack_trace(sys.exc_info())
msg = 'Failed to initialise "{}": {}'.format(module_name, e)
raise FlexerException(msg, type(e), stack_trace)
finally:
if file_ is not None:
file_.close()
return module
# utility methods
评论列表
文章目录