Exemplo n.º 1
0
def interactive_loop(args):
    client = CliClient(args)
    done = False
    while not done:
        try:
            prompt = "> "
            print(prompt, end="", flush=False)
            user_input = input()
            items = user_input.split()
            if not items:
                continue
            input_command = items[0]
            candidates = [c for c in commands if c.startswith(input_command)]
            if len(candidates) == 0:
                print(f"Command not recognised : {input_command}.")
                help()
                continue
            if len(candidates) >= 2:
                print(
                    f"ambigous command {input_command} : found {candidates}.")
                continue

            command = candidates[0].split()[0]
            command_args = items[1:]
            if input_command != command:
                print(command, command_args)
            if command == "connect":
                if client is None or not client.is_connected():
                    client = CliClient(args)
                else:
                    print("Error : already connected. Use disconnect first")
            elif command == "exit":
                done = True
            elif command == "help":
                help()
            else:
                if client is None or not client.is_connected():
                    raise RuntimeError('Not connected, use "connect" first')
                if command == "listrooms":
                    client.list_rooms()
                elif command == "listclients":
                    client.list_clients()
                elif command == "join":
                    client.join_room(command_args[0])
                elif command == "leave":
                    client.leave_room(command_args[0])
                elif command == "setclientname":
                    client.set_client_attributes(
                        {common.ClientAttributes.USERNAME: command_args[0]})
                elif command == "disconnect":
                    client.disconnect()
                    client = None
                else:
                    pass
        except Exception as e:
            logger.error(f"Exception: {e!r}", exc_info=True)
Exemplo n.º 2
0
def process_client_command(args):
    client = None

    try:
        if args.command == "list":
            client = CliClient(args)
            client.list_clients()
    except ServerError as e:
        logger.error(e, exc_info=True)
    finally:
        if client is not None:
            client.disconnect()