Beispiel #1
0
def main(args: List[str]) -> None:
    global_opts, items = parse_rc_args(args)
    global_opts.no_command_response = None

    if not items:
        from kitty.shell import main as smain
        smain(global_opts)
        return
    cmd = items[0]
    try:
        c = command_for_name(cmd)
    except KeyError:
        raise SystemExit('{} is not a known command. Known commands are: {}'.format(
            emph(cmd), ', '.join(x.replace('_', '-') for x in all_command_names())))
    opts, items = parse_subcommand_cli(c, items)
    payload = c.message_to_kitty(global_opts, opts, items)
    if global_opts.no_command_response is not None:
        no_response = global_opts.no_command_response  # type: ignore
    else:
        no_response = c.no_response
    send = create_basic_command(cmd, payload=payload, no_response=no_response)
    if not global_opts.to and 'KITTY_LISTEN_ON' in os.environ:
        global_opts.to = os.environ['KITTY_LISTEN_ON']
    response = do_io(global_opts.to, send, no_response)
    if no_response:
        return
    if not response.get('ok'):
        if response.get('tb'):
            print(response['tb'], file=sys.stderr)
        raise SystemExit(response['error'])
    data = response.get('data')
    if data is not None:
        if c.string_return_is_error and isinstance(data, str):
            raise SystemExit(data)
        print(data)
Beispiel #2
0
def main(args: List[str]) -> None:
    global_opts, items = parse_rc_args(args)

    if not items:
        from kitty.shell import main as smain
        smain(global_opts)
        return
    cmd = items[0]
    try:
        c = command_for_name(cmd)
    except KeyError:
        raise SystemExit('{} is not a known command. Known commands are: {}'.format(
            emph(cmd), ', '.join(x.replace('_', '-') for x in all_command_names())))
    opts, items = parse_subcommand_cli(c, items)
    try:
        payload = c.message_to_kitty(global_opts, opts, items)
    except ParsingOfArgsFailed as err:
        exit(str(err))
    no_response = c.no_response
    if hasattr(opts, 'no_response'):
        no_response = opts.no_response
    response_timeout = c.response_timeout
    if hasattr(opts, 'response_timeout'):
        response_timeout = opts.response_timeout
    send = create_basic_command(cmd, payload=payload, no_response=no_response, is_asynchronous=c.is_asynchronous)
    listen_on_from_env = False
    if not global_opts.to and 'KITTY_LISTEN_ON' in os.environ:
        global_opts.to = os.environ['KITTY_LISTEN_ON']
        listen_on_from_env = False
    if global_opts.to:
        try:
            parse_address_spec(global_opts.to)
        except Exception:
            msg = f'Invalid listen on address: {global_opts.to}'
            if listen_on_from_env:
                msg += '. The KITTY_LISTEN_ON environment variable is set incorrectly'
            exit(msg)
    import socket
    try:
        response = do_io(global_opts.to, send, no_response, response_timeout)
    except (TimeoutError, socket.timeout):
        send.pop('payload', None)
        send['cancel_async'] = True
        do_io(global_opts.to, send, True, 10)
        raise SystemExit(f'Timed out after {response_timeout} seconds waiting for response from kitty')
    if no_response:
        return
    if not response.get('ok'):
        if response.get('tb'):
            print(response['tb'], file=sys.stderr)
        raise SystemExit(response['error'])
    data = response.get('data')
    if data is not None:
        if c.string_return_is_error and isinstance(data, str):
            raise SystemExit(data)
        print(data)