define plugins interface
This commit is contained in:
parent
b7e136b624
commit
f213faab8b
4 changed files with 86 additions and 0 deletions
5
tuned/plugins/__init__.py
Normal file
5
tuned/plugins/__init__.py
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
import interface
|
||||
import repository
|
||||
|
||||
def get_repository():
|
||||
return repository.PluginRepository.get_instance()
|
||||
22
tuned/plugins/exception.py
Normal file
22
tuned/plugins/exception.py
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
import tuned.exceptions
|
||||
|
||||
class LoadPluginException(tuned.exceptions.TunedException):
|
||||
def __init__(self, plugin_name, inner_exception = None):
|
||||
super(self.__class__, self).__init__()
|
||||
self._plugin_name = plugin_name
|
||||
self._inner_exception = inner_exception
|
||||
|
||||
@property
|
||||
def plugin_name(self):
|
||||
return self._plugin_name
|
||||
|
||||
@property
|
||||
def inner_exception(self):
|
||||
return self._inner_exception
|
||||
|
||||
def __str__(self):
|
||||
message = "Unable to load plugin '%s'" % self._plugin_name
|
||||
if self._inner_exception is not None:
|
||||
message += " (%s)" % str(self._inner_exception)
|
||||
|
||||
return message[0].lower() + message[1:]
|
||||
26
tuned/plugins/interface.py
Normal file
26
tuned/plugins/interface.py
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
class PluginInterface(object):
|
||||
@classmethod
|
||||
def _get_default_options(cls):
|
||||
return {}
|
||||
|
||||
def __init__(self, options = None):
|
||||
self._options = self._get_default_options()
|
||||
if options is not None:
|
||||
self._merge_options(options)
|
||||
|
||||
def __del__(self):
|
||||
try:
|
||||
self.cleanup()
|
||||
except:
|
||||
pass
|
||||
|
||||
def cleanup(self):
|
||||
pass
|
||||
|
||||
def _merge_options(self, options):
|
||||
for key in options:
|
||||
if key in self._options:
|
||||
self._options[key] = options[key]
|
||||
|
||||
def update_tuning(self):
|
||||
raise NotImplementedError()
|
||||
33
tuned/plugins/repository.py
Normal file
33
tuned/plugins/repository.py
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import tuned.patterns
|
||||
import tuned.logs
|
||||
import tuned.utils
|
||||
import tuned.plugins.exception
|
||||
|
||||
log = tuned.logs.get()
|
||||
|
||||
class PluginRepository(tuned.patterns.Singleton):
|
||||
def __init__(self):
|
||||
super(self.__class__, self).__init__()
|
||||
self._loader = tuned.utils.PluginLoader("tuned.plugins", "plugin_", tuned.plugins.interface.PluginInterface)
|
||||
self._plugins = set()
|
||||
|
||||
def create(self, plugin_name):
|
||||
log.debug("creating plugin %s" % plugin_name)
|
||||
try:
|
||||
plugin_cls = self._loader.load(plugin_name)
|
||||
plugin_instance = plugin_cls()
|
||||
self._plugins.add(plugin_instance)
|
||||
return plugin_instance
|
||||
except Exception as exception:
|
||||
plugin_exception = tuned.plugins.exception.LoadPluginException(plugin_name, exception)
|
||||
raise plugin_exception
|
||||
|
||||
def delete(self, plugin):
|
||||
assert type(plugin) is self._loader.interface
|
||||
plugin.cleanup()
|
||||
self._plugins.remove(plugin)
|
||||
|
||||
def update(self):
|
||||
for plugin in self._plugins:
|
||||
log.debug("updating %s" % plugin)
|
||||
plugin.update_tuning()
|
||||
Loading…
Reference in a new issue