-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathawan.py
More file actions
59 lines (39 loc) · 1.74 KB
/
Copy pathawan.py
File metadata and controls
59 lines (39 loc) · 1.74 KB
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
"""Drive the `awan` character from Python — a thin wrapper over the binary.
The binary is the integration surface, so there is no SDK to speak of: every
function here just spawns `awan`. Install the binary first (any of):
npm i -g awan # prebuilt, no toolchain
cargo install awan-cli
# or download from https://github.com/codewithwan/awan/releases
See docs/INTEGRATE.md for the full event vocabulary.
"""
from __future__ import annotations
import shutil
import subprocess
from typing import Optional
BIN = shutil.which("awan") or "awan"
def _char(character: Optional[str]) -> list[str]:
return ["-c", character] if character else []
def react(event: str, character: Optional[str] = None) -> None:
"""Play the one-shot reaction to *event* (e.g. "task.done"), then return."""
subprocess.run([BIN, "react", event, *_char(character)], check=False)
def busy(label: str, character: Optional[str] = None) -> subprocess.Popen:
"""Start the "working…" loop with *label*; call .terminate() when done."""
return subprocess.Popen([BIN, "busy", label, *_char(character)])
class Watch:
"""An ambient companion you feed events to over time.
with Watch() as buddy:
buddy.emit("cmd.start")
buddy.emit("cmd.ok")
"""
def __init__(self, character: Optional[str] = None) -> None:
self._p = subprocess.Popen([BIN, "watch", *_char(character)], stdin=subprocess.PIPE)
def emit(self, event: str) -> None:
assert self._p.stdin is not None
self._p.stdin.write(f"{event}\n".encode())
self._p.stdin.flush()
def stop(self) -> None:
self._p.terminate()
def __enter__(self) -> "Watch":
return self
def __exit__(self, *_exc) -> None:
self.stop()