-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
pod-lispyclouds-sqlite.py
executable file
·61 lines (45 loc) · 1.36 KB
/
pod-lispyclouds-sqlite.py
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
60
61
#!/usr/bin/env python3
import json
import sqlite3
import sys
from bcoding import bencode, bdecode
def read():
return dict(bdecode(sys.stdin.buffer))
def write(obj):
sys.stdout.buffer.write(bencode(obj))
sys.stdout.flush()
def debug(*msg):
with open("/tmp/debug.log", "a") as f:
f.write(str(msg) + "\n")
def main():
while True:
msg = read()
debug("msg", msg)
op = msg["op"]
if op == "describe":
write(
{
"format": "json",
"namespaces": [{"name": "pod.lispyclouds.sqlite",
"vars": [{"name": "execute!"}]}]}
)
elif op == "invoke":
var = msg["var"]
id = msg["id"]
args = json.loads(msg["args"])
debug(args)
conn = sqlite3.connect("/tmp/babashka.db")
c = conn.cursor()
result = None
if var == "pod.lispyclouds.sqlite/execute!":
try:
result = c.execute(*args)
except Exception as e:
debug(e)
value = json.dumps(result.fetchall())
debug("value", value)
write({"value": value, "id": id, "status": ["done"]})
conn.commit()
conn.close()
if __name__ == "__main__":
main()