added TaskList Threading, minimized minimal.json

This commit is contained in:
RafTim 2013-11-03 13:00:09 +01:00
commit da07b27087
3 changed files with 121 additions and 16 deletions

View file

@ -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
}
},

View file

@ -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:
<PAGE> Show manual page <PAGE>
''')
''')

View file

@ -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 <http://www.gnu.org/licenses/>.
"""
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()