From 9f6fb54cf2aae8dc4718090c4ee702900724fe78 Mon Sep 17 00:00:00 2001 From: "Rafael.Timmerberg" Date: Sat, 17 Aug 2013 03:38:48 +0200 Subject: [PATCH] Oh I got even more love for lish. less code, more power :) --- lib/frontends/lish.py | 171 ++++++++++++++++++++++++++++++++++++++---- linspector | 5 +- 2 files changed, 159 insertions(+), 17 deletions(-) diff --git a/lib/frontends/lish.py b/lib/frontends/lish.py index 747634f..6abead6 100644 --- a/lib/frontends/lish.py +++ b/lib/frontends/lish.py @@ -6,28 +6,171 @@ Linspector in a screen session and not as daemon, why not? BTW.: Daemonization i cancelled, because it makes no sense to daemonize everything. Linspector is a user software which will run in any screen session perfectly. """ +from requests.status_codes import title + +''' + #see http://docs.python.org/dev/library/argparse.html + + Cheat Sheet: + + ++++++++++++ Argument Parser creation +++++++++++++++++ + prog - The name of the program (default: sys.argv[0]) + usage - The string describing the program usage (default: generated from arguments added to parser) + description - Text to display before the argument help (default: none) + epilog - Text to display after the argument help (default: none) + parents - A list of ArgumentParser objects whose arguments should also be included + formatter_class - A class for customizing the help output + prefix_chars - The set of characters that prefix optional arguments (default: -) + fromfile_prefix_chars - The set of characters that prefix files from which additional arguments should be read (default: None) + argument_default - The global default value for arguments (default: None) + conflict_handler - The strategy for resolving conflicting optionals (usually unnecessary) + + + ++++++++++++++++++ add_argument() +++++++++++++++++++++ + name or flags - Either a name or a list of option strings, e.g. foo or -f, --foo. + action - The basic type of action to be taken when this argument is encountered at the command line. + nargs - The number of command-line arguments that should be consumed. + const - A constant value required by some action and nargs selections. + default - The value produced if the argument is absent from the command line. + type - The type to which the command-line argument should be converted. + choices - A container of the allowable values for the argument. + required - Whether or not the command-line option may be omitted (optionals only). + help - A brief description of what the argument does. + metavar - A name for the argument in usage messages. + dest - The name of the attribute to be added to the object returned by parse_args(). + + +++++++ add_subparsers()-> obj with one method -> add_parser() +++++++++ + + +''' from lib.frontends.frontend import Frontend -import argparse +import argparse, os +from shlex import split as shsplit +from cmd import Cmd + +VERSION = "0.1" class LishFrontend(Frontend): def __init__(self, **kwargs): - print("linspector interactive shell: Enter h or help for commands") - parser = argparse.ArgumentParser() - jobs = kwargs["jobs"] - parser.add_argument("action", choices=["start", "stop"]) - parser.add_argument("-l", "--list", help="list current jobs") - while True: + print(kwargs) + #self.jobs = kwargs["jobs"] + ns = argparse.Namespace() - rawInput = raw_input() - print(rawInput) - args = None + commander = LishCommander(kwargs) + run = True + while run: try: - args = parser.parse_args(rawInput.split(" ")) - except: - pass + commander.cmdloop("LISH - Linspector interactive shell") + except KeyboardInterrupt, ki: + run = False + except Exception, err: + print(err) + + if commander.can_exit(): + run = False + + +class Exit(Cmd, object): + def __init__(self): + super(Exit, self).__init__() + self._canExit = False + + def can_exit(self): + return self._canExit + + def do_exit(self, text): + self.exit = True + return self.can_exit() + + def help_exit(self): + print("exits linspector") + + do_EOF = do_exit + help_EOF = help_exit + + +class ShellCommander(Cmd, object): + def do_shell(self, text): + os.system(text) + + def help_shell(self): + print("execute any shell command. Can also be archieved by a '!' postfix") + + +class HostgroupCommander(Exit, object): + def __init__(self, hostgroup): + super(HostgroupCommander, self).__init__() + self.prompt = "" % hostgroup.get_name() + self._hostgroup = hostgroup + + def do_member(self, text): + print("dear maintainer, ") + print("I typed '%s', and would appreciate, if you could stay away from outside to implement it" % text) + print("must be kidding me!!!") + return True + + def help_member(self): + print("gives you control over a member of the hostgroup") + + + + +class LishCommander(Exit, ShellCommander): + + def __init__(self, kwargs): + + super(LishCommander, self).__init__() + + self.prompt = ": " + + self._layouts = kwargs["layouts"] + self._jobs = kwargs["jobs"] + self._scheduler = kwargs["scheduler"] + + self._hostgroupArgs = ["list", "select"] + + def do_hostgroup(self, text): + args = shsplit(text) + + if args[0] == "list": + print("current active Hostgroups:\n") + for l in self._layouts: + if l.is_enabled(): + print l.get_name() + space = 4 * " " + for hg in l.get_hostgroups(): + print space + hg.get_name() + print 3 * "\n" + elif args[0] == "select": + hostgroupName = args[1] + if len(hostgroupName) == 0: + print "must select an hostgroup" + for layout in self._layouts: + for lhg in layout.get_hostgroups(): + if lhg.get_name() == hostgroupName: + try: + hgCommander = HostgroupCommander(lhg) + hgCommander.cmdloop("Entering Hostmode of " + hostgroupName) + except KeyboardInterrupt, ke: + pass + + + + def help_hostgroup(self): + print ''' + help for Hostgroup + ''' + + def complete_hostgroup(self, text, line, begidx, endidx): + "hostgroup + ' '" + if begidx == 10: + return [x for x in self._hostgroupArgs if x.startswith(text)] if len(text) > 0 else self._hostgroupArgs + + + + - print(str(args)) diff --git a/linspector b/linspector index 5ccb04c..8d58269 100755 --- a/linspector +++ b/linspector @@ -74,9 +74,8 @@ def main(): job.set_logger(log) jobs.append(job) - while True: - #Todo: implement user handle - LishFrontend(jobs= jobs, scheduler=scheduler) + frontend = LishFrontend(jobs=jobs, scheduler=scheduler, layouts=layouts) + elif args.action == "stop": log.i("stopping linspector is currently unsupported")