-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAntInterpreter2.py
87 lines (61 loc) · 2.35 KB
/
AntInterpreter2.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
from TkTerm.tkterm.backend.InterpreterInterface import InterpreterInterface
from TkTerm.tkterm.backend.KThread import KThread
from Ant.src.lib.api import ANT_API
import re
import subprocess
import os
import sys
# Adjust this according to where ANT is found
ANT_EXECUTABLE = f"{sys.executable} -u Ant/src/ant.py"
API = ANT_API()
class AntInterpreter2(InterpreterInterface):
def __init__(self, gui_element: object = None):
super().__init__()
self.gui_element = gui_element
self.history = []
self.process_options = {
"shell" : True,
"stdout" : subprocess.PIPE,
"stderr" : subprocess.PIPE,
"universal_newlines" : True
}
# Ignore utf-8 decode error which sometimes happens on early terminating
if os.name != "nt":
self.process_options["errors"] = "ignore"
def execute(self, command):
if command == "exit":
self.gui_element.destroy()
return subprocess.Popen(f"{ANT_EXECUTABLE} -c '{command}'", cwd=os.getcwd(), **self.process_options)
def terminate(self, processThread):
stdout = ""
stderr = ""
if (os.name == 'nt'):
process = subprocess.Popen(
"TASKKILL /F /PID {} /T".format(processThread.pid),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True
)
for line in process.stdout:
stdout += line
for line in process.stderr:
stderr += line
else:
try:
os.system("pkill -TERM -P %s" % processThread.pid)
os.system("kill -2 {}".format(processThread.pid))
os.system("kill -9 {}".format(processThread.pid))
except:
pass
return (stdout, stderr)
def get_return_code(self, process):
return process.poll()
def get_prompt(self):
# config = API.get_config()
# Strip color ANSI code
# return re.sub(r'\x1b\[([0-9,A-Z]{1,2}(;[0-9]{1,2})?(;[0-9]{3})?)?[m|K]?', '', config["PROMPT"])
return os.getcwd() + ">> "
def get_history(self):
# TODO: Still not able to get history properly
# return API.get_history()
return self.history