Example #1
0
def main():
    my_parser = argparse.ArgumentParser()
    my_parser.add_argument("-q",
                           dest="quiet",
                           default=False,
                           action="store_true",
                           help="be quiet [%(default)s]")
    my_parser.add_argument("--logger",
                           type=str,
                           default="stdout",
                           choices=["stdout", "logserver"],
                           help="choose logging facility [%(default)s]")
    my_parser.add_argument(
        "--logall",
        default=False,
        action="store_true",
        help="log all (no just warning / error), [%(default)s]")
    my_parser.add_argument("args",
                           nargs=argparse.REMAINDER,
                           help="commands to execute")
    opts = my_parser.parse_args()
    if opts.args:
        opts.quiet = True
    if not opts.quiet:
        print("Starting ICSW shell ... ", end="", flush=True)

    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "initat.cluster.settings")

    try:
        import django
        if not opts.quiet:
            print("django.setup() ... ", end="", flush=True)
        django.setup()
    except:
        django = None
    else:
        from initat.cluster.backbone import db_tools
        try:
            if not db_tools.is_reachable():
                django = None
        except:
            # when installing a newer icsw-client package on a machine with an old icsw-server package
            django = None

    from initat.icsw.magics import icsw_magics

    # First import the embeddable shell class
    from IPython.terminal.prompts import Prompts, Token
    from IPython.terminal.embed import InteractiveShellEmbed

    # Now create an instance of the embeddable shell. The first argument is a
    # string with options exactly as you would type them if you were starting
    # IPython at the system command line. Any parameters you want to define for
    # configuration can thus be specified here.
    ipshell = InteractiveShellEmbed(header="X", user_ns={"django": django})

    class ICSWPrompt(Prompts):
        def in_prompt_tokens(self, cli=None):
            return [
                (Token, "[CORVUS]"),
                (Token.Prompt, ">"),
            ]

    ipshell.prompts = ICSWPrompt(ipshell)

    ipshell.mouse_support = True
    ipshell.confirm_exit = False
    ipshell.autocall = 2
    # no banner
    ipshell.banner1 = ""
    ipshell.set_hook('complete_command',
                     icsw_magics.apt_completers,
                     str_key='icsw')

    if False:

        class st2(object):
            def __dir__(self):
                return ["bla", "blo"]

            def abc(self, var):
                print("*", var)

            def _ipython_key_completions_(self):
                return ["x", "y"]

            def bla(self):
                return "bla"

            def __call__(self, *args):
                return "C", args

        xicsw = st2()

        def stest(sthg):
            print("stest:", sthg)

    ipshell.register_magics(
        icsw_magics.ICSWMagics(ipshell, True if django else False))

    if opts.args:
        if "--" in opts.args:
            opts.args.remove("--")
        _args = ["icsw"]
        if opts.logall:
            _args.append("--logall")
        _args.append("--logger")
        _args.append(opts.logger)
        r = ipshell.run_cell(" ".join(_args + opts.args), silent=True)
        sys.exit(r.result)
    else:
        if not opts.quiet:
            print("done")
        from initat.cluster.backbone.models import device, device_group
        ipshell(header="Starting icsw", )