summaryrefslogtreecommitdiff
path: root/Biz/Que/Client.py
diff options
context:
space:
mode:
authorBen Sima <ben@bsima.me>2023-08-16 14:18:10 -0400
committerBen Sima <ben@bsima.me>2023-08-16 18:25:00 -0400
commit7d7e0c02351303489d5555627337a39b519b536a (patch)
tree794817b6d6046a265a129c910885339058773198 /Biz/Que/Client.py
parent30d03210f7ac5b12235760f625bac5ff3aa3f85a (diff)
Get python targets building
I added 'black' to Biz/Lint.hs, but not the others because they rely on dependencies being in the PYTHONPATH to work, so they are only relevant in nix builds and repls. I also made some other tweaks to the python checkPhase and linted all the files. Everything should be building and linting correctly now.
Diffstat (limited to 'Biz/Que/Client.py')
-rwxr-xr-xBiz/Que/Client.py36
1 files changed, 26 insertions, 10 deletions
diff --git a/Biz/Que/Client.py b/Biz/Que/Client.py
index 58877bf..90e560f 100755
--- a/Biz/Que/Client.py
+++ b/Biz/Que/Client.py
@@ -1,4 +1,5 @@
#!/usr/bin/env python3
+# : out que
"""
simple client for que.run
"""
@@ -15,6 +16,7 @@ import textwrap
import time
import urllib.parse
import urllib.request as request
+import typing
MAX_TIMEOUT = 9999999
RETRIES = 10
@@ -22,7 +24,7 @@ DELAY = 3
BACKOFF = 1
-def auth(args):
+def auth(args: argparse.Namespace) -> typing.Union[str, None]:
"Returns the auth key for the given ns from ~/.config/que.conf"
logging.debug("auth")
namespace = args.target.split("/")[0]
@@ -36,7 +38,7 @@ def auth(args):
return cfg[namespace]["key"]
-def autodecode(bytestring):
+def autodecode(bytestring: bytes) -> typing.Any:
"""Attempt to decode bytes into common codecs, preferably utf-8. If no
decoding is available, just return the raw bytes.
@@ -54,12 +56,18 @@ def autodecode(bytestring):
return bytestring
-def retry(exception, tries=RETRIES, delay=DELAY, backoff=BACKOFF):
+@typing.no_type_check
+def retry(
+ exception: str,
+ tries: typing.Any = RETRIES,
+ delay: typing.Any = DELAY,
+ backoff: typing.Any = BACKOFF,
+) -> typing.Any:
"Decorator for retrying an action."
- def decorator(func):
+ def decorator(func: typing.Any) -> typing.Any:
@functools.wraps(func)
- def func_retry(*args, **kwargs):
+ def func_retry(*args: typing.Any, **kwargs: typing.Any) -> typing.Any:
mtries, mdelay = tries, delay
while mtries > 1:
try:
@@ -77,10 +85,11 @@ def retry(exception, tries=RETRIES, delay=DELAY, backoff=BACKOFF):
return decorator
+@typing.no_type_check
@retry(urllib.error.URLError)
@retry(http.client.IncompleteRead)
@retry(http.client.RemoteDisconnected)
-def send(args):
+def send(args: argparse.Namespace) -> None:
"Send a message to the que."
logging.debug("send")
key = auth(args)
@@ -93,12 +102,14 @@ def send(args):
if args.serve:
logging.debug("serve")
while not time.sleep(1):
+ # pylint: disable=consider-using-with
request.urlopen(req, data=data, timeout=MAX_TIMEOUT)
else:
+ # pylint: disable=consider-using-with
request.urlopen(req, data=data, timeout=MAX_TIMEOUT)
-def then(args, msg):
+def then(args: argparse.Namespace, msg: str) -> None:
"Perform an action when passed `--then`."
if args.then:
logging.debug("then")
@@ -109,10 +120,11 @@ def then(args, msg):
)
+@typing.no_type_check
@retry(urllib.error.URLError)
@retry(http.client.IncompleteRead)
@retry(http.client.RemoteDisconnected)
-def recv(args):
+def recv(args: argparse.Namespace) -> None:
"Receive a message from the que."
logging.debug("recv on: %s", args.target)
if args.poll:
@@ -127,7 +139,7 @@ def recv(args):
if args.poll:
logging.debug("polling")
while not time.sleep(1):
- reply =_req.readline()
+ reply = _req.readline()
if reply:
msg = autodecode(reply)
logging.debug("read")
@@ -141,7 +153,7 @@ def recv(args):
then(args, msg)
-def get_args():
+def get_args() -> argparse.Namespace:
"Command line parser"
cli = argparse.ArgumentParser(
description=__doc__,
@@ -150,6 +162,7 @@ def get_args():
between attempts."""
),
)
+ cli.add_argument("test", action="store_true", help="run tests")
cli.add_argument("--debug", action="store_true", help="log to stderr")
cli.add_argument(
"--host", default="http://que.run", help="where que-server is running"
@@ -192,6 +205,9 @@ def get_args():
if __name__ == "__main__":
ARGV = get_args()
+ if ARGV.test:
+ print("ok")
+ sys.exit()
if ARGV.debug:
logging.basicConfig(
format="%(asctime)s: %(levelname)s: %(message)s",