diff options
Diffstat (limited to 'Biz')
-rwxr-xr-x | Biz/Que/Client.py | 34 | ||||
-rwxr-xr-x | Biz/Storybook.py | 22 |
2 files changed, 30 insertions, 26 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) diff --git a/Biz/Storybook.py b/Biz/Storybook.py index 7d8f326..dbaf82a 100755 --- a/Biz/Storybook.py +++ b/Biz/Storybook.py @@ -56,6 +56,7 @@ PORT = int(os.environ.get("PORT", "3000")) area = App.from_env() app = ludic.web.LudicApp(debug=area == App.Area.Test) +log = Log.setup(logging.DEBUG if area == App.Area.Test else logging.ERROR) Sqids = sqids.Sqids() @@ -70,14 +71,13 @@ def main() -> None: def move(area: App.Area) -> None: """Run the application.""" - Log.setup(logging.DEBUG if area == App.Area.Test else logging.ERROR) - logging.info("area: %s", area) + log.info("area: %s", area) # during test, bind to beryllium's VPN address, else localhost host = "100.127.197.132" if area == App.Area.Test else "127.0.0.1" uvicorn.run(app, host=host, port=PORT) -def test(area: App.Area = App.Area.Test) -> None: +def test(area: App.Area) -> None: """Run the unittest suite manually.""" Test.run(area, [IndexTest, StoryTest]) @@ -238,7 +238,7 @@ def _openai_generate_text( }, ] client = openai.OpenAI() - logging.debug("calling openai.chat.completions.create") + log.debug("calling openai.chat.completions.create") return client.chat.completions.create( model="gpt-4o-mini", messages=messages, @@ -293,15 +293,15 @@ def generate_image( Raises: InternalServerError: when OpenAI API fails """ - logging.info("generating image %s.%s", story_id, page) + log.info("generating image %s.%s", story_id, page) url = None if area == App.Area.Test: time.sleep(1) url = "https://placehold.co/1024.png" else: client = openai.OpenAI() - logging.debug("calling openai.images.generate") - logging.debug("prompt: %s", image_prompt) + log.debug("calling openai.images.generate") + log.debug("prompt: %s", image_prompt) image_response = client.images.generate( model="dall-e-3", prompt=image_prompt, @@ -312,7 +312,7 @@ def generate_image( url = image_response.data[0].url if url is None: msg = "error getting image from OpenAI" - logging.error(msg) + log.error(msg) raise ludic.web.exceptions.InternalServerError(msg) image = Image( story_id=story_id, @@ -349,7 +349,7 @@ def generate_story_in_background( inputs, ), ) - logging.info("starting job %s", job_id) + log.info("starting job %s", job_id) thread.start() story = Story(id=story_id, inputs=inputs) # save stuff @@ -364,7 +364,7 @@ def generate_story_pages( inputs: StoryInputs, ) -> list[Page]: """Upsert a new story.""" - logging.info("generating story pages %s", story_id) + log.info("generating story pages %s", story_id) story_resp = generate_pages(inputs) pages = [ Page( @@ -527,7 +527,7 @@ def images_static(story_id: str, page: int) -> ludic.web.responses.Response: if image["path"].exists(): return ludic.web.responses.FileResponse(image["path"]) msg = "images_static: image not found" - logging.error(msg) + log.error(msg) raise ludic.web.exceptions.NotFoundError(msg) |