Пример #1
0
        async def check_args(server, message, **data):  # type: ignore
            """Check arguments."""
            try:
                data = vol_schema(data)
            except vol.Invalid as exc:
                err = humanize_error(data, exc)
                LOGGER.error("Received invalid data for command %s: %s",
                             message.command, err)
                return Message(client=message.client,
                               command="invalid",
                               data=data)

            return await func(server, message, **data)
Пример #2
0
def func_wrapper(create_callback: Callable, conn: Connection,
                 *args: Any) -> None:
    """Wrap a function with connection to receive and send data."""
    running = True

    # pylint: disable=unused-argument
    def handle_signal(signum: int, frame: Any) -> None:
        """Handle signal."""
        nonlocal running
        running = False
        conn.close()

    signal.signal(signal.SIGTERM, handle_signal)
    signal.signal(signal.SIGINT, handle_signal)

    try:
        callback = create_callback(*args)
    except Exception as exc:  # pylint: disable=broad-except
        LOGGER.error("Failed to create callback: %s", exc)
        return

    while running:

        while running:
            if conn.poll():
                break
            sleep(0.5)

        try:
            data = conn.recv()
        except EOFError:
            LOGGER.debug("Nothing more to receive")
            break
        except OSError:
            LOGGER.debug("Connection is closed")
            break
        try:
            result = callback(data)
        except Exception as exc:  # pylint: disable=broad-except
            LOGGER.error("Failed to run callback: %s", exc)
            break

        if not running:
            break
        try:
            conn.send(result)
        except ValueError:
            LOGGER.error("Failed to send result %s", result)
        except OSError:
            LOGGER.debug("Connection is closed")
            break

    LOGGER.debug("Exiting process")