forked from webui-dev/python-webui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathandershell3000.py
More file actions
55 lines (49 loc) · 1.46 KB
/
Copy pathandershell3000.py
File metadata and controls
55 lines (49 loc) · 1.46 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
# Install WebUI
# pip install --upgrade webui2
from webui import webui
import subprocess
import time
class CommandExecutor:
def __init__(self):
self.shell = subprocess.Popen(
["bash"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
bufsize=1
)
def execute(self, command):
sentinel = "_SHELL_END_OF_COMMAND_OUTPUT_"
self.shell.stdin.write(f"{command}; echo {sentinel}\n")
self.shell.stdin.flush()
output : str = ""
while True:
line = self.shell.stdout.readline().strip()
if sentinel in line:
break
output = output + line + "\n"
result = str(output).strip()
result = result.replace('\r\n', '\n') # Byte
result = result.replace('\\r\\n', '\\n') # Character
return result + "\n"
def close(self):
self.shell.stdin.close()
self.shell.terminate()
self.shell.wait()
def run_command(e : webui.event):
cmd = e.window.get_str(e, 0)
if cmd == "exit":
webui.exit()
try:
command_result = executor.execute(cmd)
return command_result
except FileNotFoundError:
return "Command not found '{cmd}'"
executor = CommandExecutor()
MyWindow = webui.window()
MyWindow.bind("Run", run_command)
MyWindow.set_root_folder("ui/")
MyWindow.show('index.html')
webui.wait()
executor.close()