summaryrefslogtreecommitdiffstats
path: root/tasker
diff options
context:
space:
mode:
authorJoel Andres Granados <jgranado@redhat.com>2007-11-21 11:13:34 +0100
committerJoel Andres Granados <jgranado@redhat.com>2007-11-21 11:13:34 +0100
commit02250bc1f3f41ce27e2625bca6346cf515fbcb9e (patch)
treeddee0672e02f55826c04a3c6790d25d4e1008538 /tasker
parenta99422fb962c12adb99cfd62dd9442699b8c09be (diff)
downloadfirstaidkit-02250bc1f3f41ce27e2625bca6346cf515fbcb9e.tar.gz
firstaidkit-02250bc1f3f41ce27e2625bca6346cf515fbcb9e.tar.xz
firstaidkit-02250bc1f3f41ce27e2625bca6346cf515fbcb9e.zip
Implement the error class.
Implement the Return code class. Implement the manage flow stuff. Implement some examples of pligins
Diffstat (limited to 'tasker')
-rw-r--r--tasker/errors.py29
-rw-r--r--tasker/plugins.py58
-rw-r--r--tasker/returns.py42
3 files changed, 112 insertions, 17 deletions
diff --git a/tasker/errors.py b/tasker/errors.py
new file mode 100644
index 0000000..f2cf61d
--- /dev/null
+++ b/tasker/errors.py
@@ -0,0 +1,29 @@
+# First Aid Kit - diagnostic and repair tool for Linux
+# Copyright (C) 2007 Martin Sivak <msivak@redhat.com>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+
+class InvalidFlowStateException(Exception):
+ def __init__(self, flow):
+ self.message="There appears to be an inconsistency with the %s varialbe. " % flow
+ def __str__(self):
+ return self.message
+
+class InvalidFlowNameException(Exception):
+ def __init__(self, name, flow):
+ self.message="%s does not exist in % flow" % (name, flow)
+ def __str__(self):
+ return self.message
diff --git a/tasker/plugins.py b/tasker/plugins.py
index 02b0da7..ae49f2b 100644
--- a/tasker/plugins.py
+++ b/tasker/plugins.py
@@ -16,6 +16,7 @@
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
from configuration import Config
+from returns import *
import FirstAidKit
from log import Logger
@@ -47,22 +48,26 @@ class Plugin(object):
#
# Dictionary that holds all the flows. The keys for each flow is its
- # name. The flow will be addressed by this name.
+ # name. The flow will be addressed by this name. The plugin developer
+ # Can add as many flows as he wants. The developer must use the instance.
+ # obj._flows["name"] = SomeFlow. Be aware that you can overwirhte
+ # previously added flows.
#
self._flows = {}
#
# This is the default flow that all classes deriving from plugin must
- # implement.
+ # implement. As the initial state has no return value it will be indexed
+ # with the parent of all ReturnValue classes.
#
self._defflow = {
- self.initial : {True: "init"},
- "init" : {True: "diagnose"},
- "diagnose" : {True: "purge", False: "backup"},
- "backup" : {True: "fix", False: "purge"},
- "fix" : {True: "purge", False: "restore"},
- "restore" : {True: "purge", False: "purge"},
- "purge" : {True: self.final}
+ self.initial : {ReturnValue: "init"},
+ "init" : {ReturnValueTrue: "diagnose"},
+ "diagnose" : {ReturnValueTrue: "purge", ReturnValueFalse: "backup"},
+ "backup" : {ReturnValueTrue: "fix", ReturnValueFalse: "purge"},
+ "fix" : {ReturnValueTrue: "purge", ReturnValueFalse: "restore"},
+ "restore" : {ReturnValueTrue: "purge", ReturnValueFalse: "purge"},
+ "purge" : {ReturnValueTrue: self.final}
}
self._flows["default"] = self._defflow
@@ -86,6 +91,18 @@ class Plugin(object):
"""Returns tuple (Plugin name, Plugin version, Plugin author)"""
return ("Dummy plugin", "0.0.1", "Martin Sivak <msivak@redhat.com>")
+ def changeFlow(self, name):
+ """Changes the current flow to name.
+
+ name -- name of flow
+ returns true upon completion, raises a InvalidFlowNameError.
+ """
+ try:
+ self.cflow = self._flows[name]
+ except KeyError:
+ raise InvalidFlowNameError(name, self._flow)
+ return True
+
#list of all actions provided
def actions(self):
"""Returns list of available actions"""
@@ -106,14 +123,19 @@ class Plugin(object):
state=self._state
result=self._result
# The self.initial state does not have any return code.
- # It will only work with True.
- if state == self.initial:
- self._state = self.cflow[self.initial][True]
- else:
- self._state = self.cflow[state][result]
- return self._state
-
+ # It will only work with the ReturnValue.
+ try:
+ if state == self.initial:
+ self._state = self.cflow[self.initial][ReturnValue]
+ else:
+ self._state = self.cflow[state][result]
+ return self._state
+ except KeyError:
+ raise InvalidFlowStateException(self.cflow)
+
+ #
#iterate protocol allows us to use loops
+ #
def __iter__(self):
self._state = self.initial
self._result = None
@@ -132,7 +154,9 @@ class Plugin(object):
self._result = getattr(self, func)()
return (self._state, self._result)
+ #
#default (mandatory) plugin actions
+ #
def init(self):
"""Initial actions.
@@ -269,7 +293,7 @@ class PluginSystem(object):
p = self._plugins[plugin].get_plugin() #get instance of plugin
for (step, rv) in p: #autorun all the needed steps
Logger.info("Running step %s in plugin %s ...", step, plugin)
- Logger.info("%s is current step and % is result of that step." % (step, rv))
+ Logger.info("%s is current step and %s is result of that step." % (step, rv))
#try:
diff --git a/tasker/returns.py b/tasker/returns.py
new file mode 100644
index 0000000..c169b17
--- /dev/null
+++ b/tasker/returns.py
@@ -0,0 +1,42 @@
+# First Aid Kit - diagnostic and repair tool for Linux
+# Copyright (C) 2007 Martin Sivak <msivak@redhat.com>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+
+#
+# These classes expressed here are to be the keys in the flow dictionary.
+# In most default cases the values are unimportant. They are placed there
+# for printing or other purposes.
+
+class ReturnValue:
+ """Its just a parent class for any Return class that might be create.
+
+ The parent has no value.
+ """
+ def __init__(self):
+ pass
+
+class ReturnValueTrue(ReturnValue):
+ def __init__(self):
+ self.value = True
+
+class ReturnValueFalse(ReturnValue):
+ def __init__(self):
+ self.value = False
+
+class ReturnValueNone(ReturnValue):
+ def __init__(self):
+ self.value = None