summaryrefslogtreecommitdiff
path: root/Que/client.py
blob: 6958576906b3959fbb25db609b1e6ece316b56be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env python3
"""
simple client for que.run
"""

import argparse
import configparser
import http.client
import os
import subprocess
import sys
import time
import urllib.parse
import urllib.request as request

MAX_TIMEOUT = 99999999  # basically never timeout


def auth(args):
    "Returns the auth key for the given ns from ~/.config/que.conf"
    namespace = args.target.split("/")[0]
    if namespace == "pub":
        return None
    conf_file = os.path.expanduser("~/.config/que.conf")
    if not os.path.exists(conf_file):
        sys.exit("you need a ~/.config/que.conf")
        cfg = configparser.ConfigParser()
        cfg.read(conf_file)
    return cfg[namespace]["key"]


def send(args):
    "Send a message to the que."
    key = auth(args)
    data = args.infile
    req = request.Request(f"{args.host}/{args.target}")
    req.add_header("User-AgenT", "Que/Client")
    if key:
        req.add_header("Authorization", key)
    if args.serve:
        while not time.sleep(1):
            request.urlopen(req, data=data, timeout=MAX_TIMEOUT)

    else:
        request.urlopen(req, data=data, timeout=MAX_TIMEOUT)


def recv(args):
    "Receive a message from the que."

    def _recv(_req):
        msg = autodecode(_req.read())
        print(msg)
        if args.then:
            subprocess.run(
                args.then.replace(r"\msg", msg).replace(r"que", args.target),
                shell=True,
                check=False,
            )

    params = urllib.parse.urlencode({"poll": args.poll})
    req = request.Request(f"{args.host}/{args.target}?{params}")
    req.add_header("User-Agent", "Que/Client")
    key = auth(args)
    if key:
        req.add_header("Authorization", key)
    with request.urlopen(req) as _req:
        if args.poll:
            while not time.sleep(1):
                _recv(_req)
        else:
            _recv(_req)


def autodecode(bytestring):
    """Attempt to decode bytes `bs` into common codecs, preferably utf-8. If
    no decoding is available, just return the raw bytes.

    For all available codecs, see:
    <https://docs.python.org/3/library/codecs.html#standard-encodings>

    """
    codecs = ["utf-8", "ascii"]
    for codec in codecs:
        try:
            return bytestring.decode(codec)
        except UnicodeDecodeError:
            pass
    return bytestring


def get_args():
    "Command line parser"
    cli = argparse.ArgumentParser(description=__doc__)
    cli.add_argument(
        "--host", default="http://que.run", help="where que-server is running"
    )
    cli.add_argument(
        "--poll", default=False, action="store_true", help="stream data from the que"
    )
    cli.add_argument(
        "--then",
        help=" ".join(
            [
                "when polling, run this shell command after each response,",
                "presumably for side effects,"
                r"replacing '\que' with the target and '\msg' with the body of the response",
            ]
        ),
    )
    cli.add_argument(
        "--serve",
        default=False,
        action="store_true",
        help=" ".join(
            [
                "when posting to the que, do so continuously in a loop.",
                "this can be used for serving a webpage or other file continuously",
            ]
        ),
    )
    cli.add_argument(
        "target", help="namespace and path of the que, like 'ns/path/subpath'"
    )
    cli.add_argument(
        "infile",
        nargs="?",
        type=argparse.FileType("rb"),
        help="data to put on the que. Use '-' for stdin, otherwise should be a readable file",
    )
    return cli.parse_args()


if __name__ == "__main__":
    ARGV = get_args()
    try:
        if ARGV.infile:
            send(ARGV)
        else:
            recv(ARGV)
    except KeyboardInterrupt:
        sys.exit(0)
    except urllib.error.HTTPError as err:
        print(err)
        sys.exit(1)
    except http.client.RemoteDisconnected as err:
        print("disconnected... retrying in 5 seconds")
        time.sleep(5)
        if ARGV.infile:
            send(ARGV)
        else:
            recv(ARGV)