-
Notifications
You must be signed in to change notification settings - Fork 0
/
term.py
96 lines (82 loc) · 2.61 KB
/
term.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"""
term.py
prints the output of julia code
by: Calacuda | MIT Licence
"""
import pty
import os
import re
import json
from mycroft.messagebus import send
pass_file = "/tmp/julia-voice-programming.txt"
hist_file = pass_file[:-4]+"_hist.txt"
err_file = pass_file[:-4]+"_err.txt"
succ_file = pass_file[:-4]+"_succ.txt"
shell = "julia"
ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')
# carage_retuirn = re.compile(r's/^M//')
# print(hist_file)
def verbalize_old():
"""
depricated, does not work as intended
"""
#print("verbalized called")
with open(pass_file, 'r') as df:
#print("pass_file opened")
lines = df.readlines()
i = -1
#try:
if len(lines) > 0:
line = lines[i].strip().replace('("', " ").replace('")', " ")
else:
print("death")
return
#except IndexError:
# return
if line[0:6] != "julia>":
print("trying to speak")
try:
# print(line)
json_str = "{\"utterance\": \"" + line + "\"}"
send("speak", json.loads(json_str))
#with open(err_file, 'a') as ef:
# ef.write(f"line : <{line}>")
except json.decoder.JSONDecodeError as error:
with open(err_file, 'a') as ef:
ef.write(json_str + "\n\n")
ef.write(str(error) + "\n\n")
def verbalize_new():
with open(pass_file, 'r') as df:
#print("pass_file opened")
lines = [l for l in df.readlines() if l != "\n"]
if len(lines) > 0:
line = lines[-1].strip().replace('("', " ").replace('")', " ")
else:
#print("lines variable non exsistant")
return
if line[0:6] != "julia>" and len(line) > 1:
#print("trying to speak")
json_str = "{\"utterance\": \"" + line + "\"}"
try:
send("speak", json.loads(json_str))
except json.decoder.JSONDecodeError:
#print("json error : ", line)
pass
def read(fd):
data = os.read(fd, 1024)
readable = ansi_escape.sub('', data.decode("utf-8"))
bare = readable.replace("\r", "")
if bare[0:6] != "julia>":
with open(pass_file, 'a') as pf:
pf.write(bare + "\n" if bare else "")
with open(hist_file, 'a') as hf:
hf.write(readable)
else:
with open(pass_file, 'w') as pf:
pass
#print("before verbalize")
verbalize_new()
#print("after verbalize")
return data
pty.spawn(shell, read)
print("\n\n<ended>\n\n")