def load_file(self, filename):
# Saves the name and the extension of the file. It assumes that the class
# to be loaded has the same name of the module (.py or .pyc file).
mod_name, file_ext = os.path.splitext(os.path.split(filename)[-1])
py_mod = None
# If the file has .py extension, then it loads that module.
if file_ext.lower() == '.py':
py_mod = imp.load_source(mod_name, filename)
# If the file has .pyc extension, then it loads that module.
elif file_ext.lower() == '.pyc':
py_mod = imp.load_compiled(mod_name, filename)
else:
raise EvaluateException('Unable to find the file ' + filename + '!')
# Builds the list of the classes contained in the module.
classes = [u for (v, u) in getmembers(py_mod, isclass) if v.startswith(mod_name)]
# Loads the classes contained in the module.
for c in classes:
self.load_class(c)
评论列表
文章目录