iwla/iplugin.py

85 lines
2.6 KiB
Python

import importlib
import inspect
import traceback
class IPlugin(object):
def __init__(self, iwla):
self.iwla = iwla
self.requires = []
self.API_VERSION = 1
self.ANALYSIS_CLASS = 'HTTP'
def isValid(self, analysis_class, api_version):
if analysis_class != self.ANALYSIS_CLASS: return False
# For now there is only version 1
if self.API_VERSION != api_version:
return False
return True
def getRequirements(self):
return self.requires
def load(self):
return True
def hook(self):
pass
def preloadPlugins(plugins, iwla):
cache_plugins = {}
print "==> Preload plugins"
for root in plugins.keys():
for plugin_filename in plugins[root]:
plugin_path = root + '.' + plugin_filename
try:
mod = importlib.import_module(plugin_path)
classes = [c for _,c in inspect.getmembers(mod)\
if inspect.isclass(c) and \
issubclass(c, IPlugin) and \
c.__name__ != 'IPlugin'
]
if not classes:
print 'No plugin defined in %s' % (plugin_path)
continue
plugin = classes[0](iwla)
plugin_name = plugin.__class__.__name__
if not plugin.isValid(iwla.ANALYSIS_CLASS, iwla.API_VERSION):
#print 'Plugin not valid %s' % (plugin_filename)
continue
#print 'Load plugin %s' % (plugin_name)
requirements = plugin.getRequirements()
if requirements:
requirement_validated = False
for r in requirements:
for (_,p) in cache_plugins.items():
if p.__class__.__name__ == r:
requirement_validated = True
break
if not requirement_validated:
print 'Missing requirements for plugin %s' % (plugin_path)
break
if not requirement_validated: continue
if not plugin.load():
print 'Plugin %s load failed' % (plugin_path)
continue
print '\tRegister %s' % (plugin_path)
cache_plugins[plugin_path] = plugin
except Exception as e:
print 'Error loading \'%s\' => %s' % (plugin_path, e)
traceback.print_exc()
return cache_plugins