diff options
Diffstat (limited to 'Biz/Que')
-rwxr-xr-x | Biz/Que/Client.py | 34 |
1 files changed, 19 insertions, 15 deletions
diff --git a/Biz/Que/Client.py b/Biz/Que/Client.py index 671b464..6248a79 100755 --- a/Biz/Que/Client.py +++ b/Biz/Que/Client.py @@ -22,10 +22,17 @@ RETRIES = 10 DELAY = 3 BACKOFF = 1 +LOGLEVEL = logging.ERROR +LOG = logging.basicConfig( + format="%(asctime)s: %(levelname)s: %(message)s", + level=LOGLEVEL, + datefmt="%Y.%m.%d..%H.%M.%S", +) + def auth(args: argparse.Namespace) -> str | None: """Return the auth key for the given ns from ~/.config/que.conf.""" - logging.debug("auth") + LOG.debug("auth") namespace = args.target.split("/")[0] if namespace == "pub": return None @@ -48,7 +55,7 @@ def autodecode(bytestring: bytes) -> typing.Any: <https://docs.python.org/3/library/codecs.html#standard-encodings> """ - logging.debug("autodecode") + LOG.debug("autodecode") codecs = ["utf-8", "ascii"] for codec in codecs: try: @@ -75,8 +82,8 @@ def retry( try: return func(*args, **kwargs) except exception as ex: - logging.debug(ex) - logging.debug("retrying...") + LOG.debug(ex) + LOG.debug("retrying...") time.sleep(mdelay) mtries -= 1 mdelay *= backoff @@ -93,7 +100,7 @@ def retry( @retry(http.client.RemoteDisconnected) def send(args: argparse.Namespace) -> None: """Send a message to the que.""" - logging.debug("send") + LOG.debug("send") key = auth(args) data = args.infile req = request.Request(f"{args.host}/{args.target}") @@ -102,7 +109,7 @@ def send(args: argparse.Namespace) -> None: if key: req.add_header("Authorization", key) if args.serve: - logging.debug("serve") + LOG.debug("serve") while not time.sleep(1): request.urlopen(req, data=data, timeout=MAX_TIMEOUT) else: @@ -112,7 +119,7 @@ def send(args: argparse.Namespace) -> None: def then(args: argparse.Namespace, msg: str) -> None: """Perform an action when passed `--then`.""" if args.then: - logging.debug("then") + LOG.debug("then") subprocess.run( # noqa: S602 args.then.format(msg=msg, que=args.target), check=False, @@ -131,7 +138,7 @@ def recv(args: argparse.Namespace) -> None: Raises: ValueError: if url is malformed """ - logging.debug("recv on: %s", args.target) + LOG.debug("recv on: %s", args.target) if args.poll: req = request.Request(f"{args.host}/{args.target}/stream") else: @@ -145,12 +152,12 @@ def recv(args: argparse.Namespace) -> None: raise ValueError(msg) with request.urlopen(req) as _req: if args.poll: - logging.debug("polling") + LOG.debug("polling") while not time.sleep(1): reply = _req.readline() if reply: msg = autodecode(reply) - logging.debug("read") + LOG.debug("read") sys.stdout.write(msg) then(args, msg) else: @@ -223,11 +230,8 @@ def main() -> None: sys.stdout.write("ok\n") sys.exit() if argv.debug: - logging.basicConfig( - format="%(asctime)s: %(levelname)s: %(message)s", - level=logging.DEBUG, - datefmt="%Y.%m.%d..%H.%M.%S", - ) + global LOGLEVEL # noqa: PLW0603 + LOGLEVEL = logging.DEBUG try: if argv.infile: send(argv) |