1
0
Fork 0

Untested repository/base class for Commands. It will be used to rewrite functions from bash to Python

This commit is contained in:
HanzZ 2012-04-06 11:00:52 +02:00
parent 99a863f517
commit 55889db0b0
4 changed files with 106 additions and 0 deletions

View file

@ -0,0 +1,9 @@
import base
import repository
__all__ = ["repository", "get_repository", "Command"]
def get_repository():
return repository.CommandRepository.get_instance()
Command = base.Command

32
tuned/commands/base.py Normal file
View file

@ -0,0 +1,32 @@
class Command(object):
"""
Base class for all commands.
Commands contain algorithms and system commands to change the tuning on live
system and are also able to revert the changes they have done.
Methods requiring reimplementation:
- execute(self, args)
- revert(self, args)
"""
def __init__(self, name, desc = ""):
self._name = name
self._desc = desc
@property
def name(self):
return self._desc
@property
def desc(self):
return self._desc
@desc.setter
def desc(self, desc):
self._desc = desc
def execute(self, args):
raise NotImplementedError()
def revert(self, args):
raise NotImplementedError()

View file

@ -0,0 +1,22 @@
import tuned.exceptions
class LoadCommandException(tuned.exceptions.TunedException):
def __init__(self, command_name, inner_exception = None):
super(self.__class__, self).__init__()
self._command_name = command_name
self._inner_exception = inner_exception
@property
def command_name(self):
return self._command_name
@property
def inner_exception(self):
return self._inner_exception
def __str__(self):
message = "Unable to load command '%s'" % self._command_name
if self._inner_exception is not None:
message += " (%s)" % str(self._inner_exception)
return message[0].lower() + message[1:]

View file

@ -0,0 +1,43 @@
import tuned.patterns
import tuned.logs
import tuned.utils
import tuned.commands
import tuned.commands.exception
import tuned.utils.storage
log = tuned.logs.get()
class CommandRepository(tuned.patterns.Singleton):
def __init__(self):
super(self.__class__, self).__init__()
self._loader = tuned.utils.PluginLoader("tuned.commands", "command_", tuned.plugins.Command)
self._commands = {}
def execute(self, command_name, args):
# Load command on-the-fly
if not self._commands.has_key(command_name):
self._load_command(command_name)
# Execute command and get the previously set values.
log.debug("executing command %s with args %s" % (command_name, unicode(args)))
previous_args = self._commands[command_name].execute(args)
# Store the previously set values
storage = tuned.utils.storage.Storage.get_instance()
storage.data[command_name] = previous_args
storage.save()
def _load_command(self, command_name)
log.debug("loading command %s" % command_name)
try:
command_cls = self._loader.load(command_name)
command_instance = command_cls()
self._commands[command_instance.name](command_instance)
return command_instance
except Exception as exception:
command_exception = tuned.plugins.exception.LoadCommandException(command_name, exception)
raise command_exception
def delete(self, command_name):
log.debug("removing command %s" % command_name)
del self._commands[command_name]