コード例 #1
0
def pulp_shell(ctx: click.Context) -> None:
    """Activate an interactive shell-mode"""
    make_click_shell(
        ctx.parent,
        prompt="pulp> ",
        intro="Starting Pulp3 interactive shell...",
        hist_file=os.path.join(click.utils.get_app_dir("pulp"), "cli-history"),
    ).cmdloop()
コード例 #2
0
    async def shell(ctx):
        """Interactive shell (cli commands in shell mode)."""

        import asyncio
        import nest_asyncio
        from functools import update_wrapper
        loop = asyncio.get_event_loop()
        nest_asyncio.apply(loop)

        def get_invoke(command):
            """
            Get the Cmd main method from the click command
            :param command: The click Command object
            :return: the do_* method for Cmd
            :rtype: function
            """

            assert isinstance(command, click.Command)

            def invoke_(self, arg):  # pylint: disable=unused-argument
                try:
                    import shlex
                    r = command.main(args=shlex.split(arg),
                                     prog_name=command.name,
                                     standalone_mode=False,
                                     parent=self.ctx)
                    if asyncio.iscoroutine(r):
                        loop.run_until_complete(r)
                except click.ClickException as e:
                    # Show the error message
                    e.show()
                except click.Abort:
                    # We got an EOF or Keyboard interrupt.  Just silence it
                    pass
                except SystemExit:
                    # Catch this an return the code instead. All of click's help commands do a sys.exit(),
                    # and that's not ideal when running in a shell.
                    pass
                except Exception as e:
                    traceback.print_exception(type(e), e, None)
                    click_shell.core.logger.warning(traceback.format_exc())

                # Always return False so the shell doesn't exit
                return False

            invoke_ = update_wrapper(invoke_, command.callback)
            invoke_.__name__ = 'do_%s' % command.name
            return invoke_

        # patch shell_core.get_invoke
        click_shell.core.get_invoke = get_invoke

        name = root_project().name
        ctx.command = cli
        sh = click_shell.make_click_shell(ctx,
                                          prompt=f'{name} $ ',
                                          intro=f'Entering {name} shell...')
        sh.cmdloop()
コード例 #3
0
def main(ctx):
    """Start an interactive shell for running the encapsia commands."""
    host = ctx.obj.get("host")
    if host:
        os.environ["ENCAPSIA_HOST"] = host
        prompt = f"encapsia {host}> "
    else:
        prompt = "encapsia> "
    shell = click_shell.make_click_shell(
        ctx.parent,
        prompt=prompt,
        intro="Starting interactive shell...\nType help for help!",
    )
    shell.cmdloop()
コード例 #4
0
def main(ctx, host):
    """Start an interactive shell for running the encapsia commands.

    The --host option internally sets the ENCAPSIA_HOST environment variable,
    which subsequent commands will pick up if a --host option is not set again.

    """
    if host:
        os.environ["ENCAPSIA_HOST"] = host
        prompt = f"encapsia {host}> "
    else:
        prompt = "encapsia> "
    shell = click_shell.make_click_shell(
        ctx.parent,
        prompt=prompt,
        intro="Starting interactive shell...\nType help for help!",
    )
    shell.cmdloop()
コード例 #5
0
def main(instance, email, password, config, profile, notifications):
    configpath = os.path.expanduser(config)
    if os.path.isfile(configpath) and not os.access(configpath, os.W_OK):
        # warn the user before they're asked for input
        cprint("Config file does not appear to be writable: " + configpath,
               fg('red'))

    set_configfile(configpath)
    cfg = parse_config()
    if not cfg.has_section(profile):
        cfg.add_section(profile)

    instance, client_id, client_secret, token = parse_or_input_profile(
        profile, instance, email, password)
    if not token:
        print_error("Could not log you in.  Please try again later.")
        sys.exit(1)

    mastodon = Mastodon(client_id=client_id,
                        client_secret=client_secret,
                        access_token=token,
                        api_base_url="https://" + instance)

    if notifications:
        set_notifications()
        kick_new_process(mastodon.user_stream,
                         TootDesktopNotifications(profile))

    cfg[profile] = {
        'instance': instance,
        'client_id': client_id,
        'client_secret': client_secret,
        'token': token
    }

    set_active_mastodon(mastodon)
    set_active_profile(profile)
    save_config()

    cprint(
        "Welcome to tootstream! Two-Factor-Authentication is currently not supported.",
        fg('blue'))
    print("You are connected to ", end="")
    cprint(instance, fg('green') + attr('bold'))
    print("Enter a command. Use 'help' for a list of commands.")
    print("\n")

    user = mastodon.account_verify_credentials()
    prompt = stylePrompt(user['username'], profile, fg('blue'), fg('cyan'))

    # setup shell
    #ctx = _tootstreamCommands.make_context('tootstreamShell', [], parent=click.get_current_context())
    ctx = _tootstream.make_context('tootstreamShell', [],
                                   parent=click.get_current_context())
    shell = make_click_shell(ctx,
                             prompt=prompt,
                             intro='',
                             hist_file='/dev/null')
    # push shell object onto the context to enable dynamic prompt
    set_shell(shell)
    set_prompt(prompt)
    # and go
    shell.cmdloop()
    print("Goodbye!")