"""Drives Kneedeck from Python, on the same PC:

    import kneedeck
    kneedeck.send({"tab": "Carte LFXX", "page": 3})
    kneedeck.send([{"profile": "Mirage F1"}, {"action": "show"}])

Or from a command line: python kneedeck.py "{\"action\": \"next-page\"}"
Kneedeck listens on this PC only, on port 47831, and answers nothing.
"""
import json
import socket

PORT = 47831


def send(message, port=PORT):
    """Sends one message (a dict) or several in their order (a list) to Kneedeck on this PC."""
    data = json.dumps(message).encode("utf-8")
    with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
        sock.sendto(data, ("127.0.0.1", port))


if __name__ == "__main__":
    import sys

    send(json.loads(sys.argv[1]) if len(sys.argv) > 1 else {"action": "toggle"})
