From da07b270879124297e3e421a4fc514976e27b7a0 Mon Sep 17 00:00:00 2001 From: RafTim Date: Sun, 3 Nov 2013 13:00:09 +0100 Subject: [PATCH] added TaskList Threading, minimized minimal.json --- examples/minimal.json | 4 +- linspector/frontends/lish.py | 106 +++++++++++++++++++++++++++++++---- linspector/tasks/TaskList.py | 27 +++++++-- 3 files changed, 121 insertions(+), 16 deletions(-) diff --git a/examples/minimal.json b/examples/minimal.json index b64f217..b6406f4 100644 --- a/examples/minimal.json +++ b/examples/minimal.json @@ -26,7 +26,7 @@ "hostgroups":{ "a":{ "members": ["root"], - "hosts": [ "a", "b", "c", "d", "e"], + "hosts": [ "a"], "services":[ { "class": "dummy", "args": { "sleep": 1, "fail": 1 }, "periods": ["fast"], "threshold": 1 } ] @@ -34,7 +34,7 @@ }, "layouts":{ "main":{ - "hostgroups": [ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o" ], + "hostgroups": [ "a" ], "enabled": true } }, diff --git a/linspector/frontends/lish.py b/linspector/frontends/lish.py index becccbe..6d58287 100644 --- a/linspector/frontends/lish.py +++ b/linspector/frontends/lish.py @@ -94,16 +94,102 @@ Exits Linspector ''') -class Command(object): - def __init__(self, name, command, helpText, children=None): - self.name = name - self.command = command - self.helpText = helpText +class BaseCommand(object): + + def set_command(self, cmd): + self.command = cmd + + def can_execute(self): + if self.command.children is not None: + return False + else: + return True + + def execute(self, text): + text = shsplit(text) + if len(text) == 0 and self.can_execute(): + self.do_action() + else: + pass + + def do_action(self): + pass + + +def get_list(): + return [] + + +class Command(Cmd, object): + def __init__(self, alias, interface, shortHelp=None, extendedHelp=None, completion=get_list, func=None, children=None): + super(Command, self).__init__() + self.interface = interface + self.alias = alias + self.shortHelp = shortHelp + self.completion = completion + self.extendedHelp = extendedHelp + self.func = func self.children = children - self.completion = [] - if isinstance(children, list): - for child in children: - self.completion.append((child.get_name())) + + def get_short_help(self): + if self.shortHelp is not None: + return str(self.shortHelp) + else: + return "undocumented. see help %s for further information." % str(self.alias) + + def get_children(self): + if self.children is None: + self.children = [] + return self.children + + def find_child_by_alias(self, alias): + pass + + def get_extended_help(self): + return self.extendedHelp + + def print_help(self): + help = self.get_extended_help() + if help is not None: + print help + elif len(self.get_children()) > 0: + print "%s usage:\n" % str(self.alias) + for child in self.get_children(): + print child.alias + "\t\t" + child.short_help() + "\n" + else: + print "invalid or unsupported syntax" + print "\n" + + def default(self, line): + if len(line) == 0: + if self.func is not None: + return self.func(self.interface) + else: + self.print_help() + else: + line = shsplit(line) + + if len(line) > 0 and line[0] in [child.alias for child in self.get_children()]: + pass + + + def completenames(self, text, *ignored): + ret = super(Command, self).completenames(text, *ignored) + if len(self.get_children()) > 0: + func = lambda alias: True + if len(text) > 0: + func = lambda alias: alias.startswith(text) + ret.extend([cmd.alias for cmd in self.children if func(cmd.alias)]) + return ret + + def completedefault(self, text, *ignored): + pass + + def do_help(self, arg): + if arg: + pass + else: + pass class CommandTree(object): @@ -338,4 +424,4 @@ Show license information print(''' Usage: Show manual page -''') \ No newline at end of file +''') diff --git a/linspector/tasks/TaskList.py b/linspector/tasks/TaskList.py index de0fe4e..799d8e1 100644 --- a/linspector/tasks/TaskList.py +++ b/linspector/tasks/TaskList.py @@ -16,10 +16,31 @@ GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . """ +from threading import Event, Thread class TaskList(object): def __init__(self, tasks): self.tasks = tasks + self.event = Event() + self.taskInfos = [] + Thread(target=self._run_worker_thread).start() + + def _run_worker_thread(self): + while True: + if len(self.taskInfos) == 0: + self.event.clear() + self.event.wait() + msg, taskInfos = self.taskInfos[0] + del self.taskInfos[0] + try: + + for taskInfo in taskInfos: + task = self.find_task_by_name(taskInfo["class"]) + if task: + task.execute(msg, taskInfo["args"]) + except: + pass + def find_task_by_name(self, clazzName): for task in self.tasks: @@ -27,10 +48,8 @@ class TaskList(object): return task def execute_task_infos(self, msg, taskInfos): - for taskInfo in taskInfos: - task = self.find_task_by_name(taskInfo["class"]) - if task: - task.execute(msg, taskInfo["args"]) + self.taskInfos.append((msg, taskInfos)) + self.event.set()