예제 #1
0
def main(args):
    global_opts, items = parse_rc_args(args)

    if not items:
        from kitty.shell import main
        main(global_opts)
        return
    cmd = items[0]
    try:
        func = cmap[cmd]
    except KeyError:
        raise SystemExit(
            '{} is not a known command. Known commands are: {}'.format(
                emph(cmd), ', '.join(all_commands)))
    opts, items = parse_subcommand_cli(func, items)
    payload = func(global_opts, opts, items)
    send = {
        'cmd': cmd,
        'version': version,
    }
    if payload is not None:
        send['payload'] = payload
    response = do_io(global_opts.to, send, func.no_response)
    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 func.string_return_is_error and isinstance(data, str):
            raise SystemExit(data)
        print(data)
예제 #2
0
def main(args):
    all_commands = tuple(sorted(cmap))
    cmds = ('  |G {}|\n    {}'.format(cmap[c].name, cmap[c].short_desc)
            for c in all_commands)
    msg = ('Control {appname} by sending it commands. Add'
           ' |_ allow_remote_control yes| to kitty.conf for this'
           ' to work.\n\n|T Commands|:\n{cmds}\n\n'
           'You can get help for each individual command by using:\n'
           '{appname} @ |_ command| -h').format(appname=appname,
                                                cmds='\n'.join(cmds))

    global_opts, items = parse_args(args[1:], global_options_spec,
                                    'command ...', msg, '{} @'.format(appname))

    if not items:
        from kitty.shell import main
        main(global_opts)
        return
    cmd = items[0]
    try:
        func = cmap[cmd]
    except KeyError:
        raise SystemExit(
            '{} is not a known command. Known commands are: {}'.format(
                emph(cmd), ', '.join(all_commands)))
    opts, items = parse_subcommand_cli(func, items)
    payload = func(global_opts, opts, items)
    send = {
        'cmd': cmd,
        'version': version,
    }
    if func.no_response and isinstance(payload, types.GeneratorType):
        for item in payload:
            send['payload'] = item
            do_io(global_opts.to, send, func.no_response)
        return
    if payload is not None:
        send['payload'] = payload
    response = do_io(global_opts.to, send, func.no_response)
    if not response.get('ok'):
        if response.get('tb'):
            print(response['tb'], file=sys.stderr)
        raise SystemExit(response['error'])
    if 'data' in response:
        print(response['data'])