From 55889db0b0fd866199c73f42ce259a0939fb0fb6 Mon Sep 17 00:00:00 2001 From: HanzZ Date: Fri, 6 Apr 2012 11:00:52 +0200 Subject: [PATCH] Untested repository/base class for Commands. It will be used to rewrite functions from bash to Python --- tuned/commands/__init__.py | 9 ++++++++ tuned/commands/base.py | 32 +++++++++++++++++++++++++++ tuned/commands/exception.py | 22 ++++++++++++++++++ tuned/commands/repository.py | 43 ++++++++++++++++++++++++++++++++++++ 4 files changed, 106 insertions(+) create mode 100644 tuned/commands/__init__.py create mode 100644 tuned/commands/base.py create mode 100644 tuned/commands/exception.py create mode 100644 tuned/commands/repository.py diff --git a/tuned/commands/__init__.py b/tuned/commands/__init__.py new file mode 100644 index 0000000..4e7dc2b --- /dev/null +++ b/tuned/commands/__init__.py @@ -0,0 +1,9 @@ +import base +import repository + +__all__ = ["repository", "get_repository", "Command"] + +def get_repository(): + return repository.CommandRepository.get_instance() + +Command = base.Command diff --git a/tuned/commands/base.py b/tuned/commands/base.py new file mode 100644 index 0000000..744da5d --- /dev/null +++ b/tuned/commands/base.py @@ -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() diff --git a/tuned/commands/exception.py b/tuned/commands/exception.py new file mode 100644 index 0000000..413965a --- /dev/null +++ b/tuned/commands/exception.py @@ -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:] diff --git a/tuned/commands/repository.py b/tuned/commands/repository.py new file mode 100644 index 0000000..28eb95c --- /dev/null +++ b/tuned/commands/repository.py @@ -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]