owenrusk.dev

errands

small jobs the runtime does for the studio.

git clone https://owenrusk.dev/errands.git

errands / errands/__main__.py -rw-r--r-- · 1051 bytes

 1 # python -m errands run      the jobs that run on their own
 2 # python -m errands serve    tool calls on stdin, one json object per line
 3 # python -m errands tools    what it can call
 4 import logging
 5 import sys
 6 
 7 from . import jobs, settings, tools
 8 
 9 
10 def main(argv: list[str]) -> int:
11     command = argv[1] if len(argv) > 1 else ""
12     if command == "run":
13         settings.LOGS.mkdir(parents=True, exist_ok=True)
14         logging.basicConfig(
15             level=logging.INFO,
16             format="%(asctime)s %(name)s %(message)s",
17             handlers=[logging.StreamHandler(), logging.FileHandler(settings.LOGS / "errands.log")],
18         )
19         jobs.forever()
20         return 0
21     if command == "serve":
22         for line in sys.stdin:
23             if line.strip():
24                 print(tools.call(line), flush=True)
25         return 0
26     if command == "tools":
27         for name in sorted(tools.TOOLS):
28             print(name)
29         return 0
30     print("usage: python -m errands run|serve|tools", file=sys.stderr)
31     return 2
32 
33 
34 sys.exit(main(sys.argv))