-
Notifications
You must be signed in to change notification settings - Fork 0
/
wterminal.py
164 lines (128 loc) · 5.38 KB
/
wterminal.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import sys
import os
import importlib
import PySide.QtGui as QtGui
from PySide.QtCore import QUrl
from PySide.QtWebKit import QWebView,QWebPage,QWebSettings
from PySide.QtCore import Signal
class pluginManager:
def __init__(self, path):
self._path = path
self._modules = {}
self._loadmodules()
def _loadmodules(self):
for filepy in os.listdir(self._path):
if filepy == "__init__.py":
continue
if filepy[-2:] == "py":
modulename = os.path.join(self._path,filepy[:-3]).replace(os.sep,".")
self._modules[filepy[:-3]] = importlib.import_module(modulename)
def reloadModules(self):
for key in self._modules:
reload(self._modules[key])
@property
def modules(self):
return self._modules
def classes(self):
for key in self._modules:
my_class = getattr(self._modules[key], key)
yield my_class()
##############################################################################3
class _LoggedPage(QWebPage):
obj = [] # synchronous
commandRecieved = Signal(str)
helpNeeded = Signal(str)
tabPressed = Signal(str)
def javaScriptConsoleMessage(self, msg, line, source):
l = msg.split(",")
self.obj = l
if msg[:3] =="TAB":
self.tabPressed.emit(msg[3:])
elif msg[:3] =="CMD":
if msg[3:7] == "help":
self.helpNeeded.emit(msg[3:])
else:
self.commandRecieved.emit(msg[3:])
print ('JS: %s line %d: %s' % (source, line, msg))
class Example(QtGui.QFrame):
def __init__(self):
super(Example, self).__init__()
self.initUI()
self.cwd = os.getcwd()
self._command = []
self.registerCmd()
def registerCommand(self, command, method):
self._command[commamd] = method
def initUI(self):
hbox = QtGui.QVBoxLayout()
#hbox.addStretch()
plot_view = QWebView()
self.page = _LoggedPage()
#page.newData.connect(onNewData)
self.page.commandRecieved.connect(self.commandRecieved)
self.page.helpNeeded.connect(self.helpNeeded)
self.page.tabPressed.connect(self.tabPressed)
plot_view.setPage(self.page)
#plot_path = 'test.html'
dir_path = os.path.dirname(os.path.realpath(__file__))
plot_view.load(QUrl('file:///%s/ptty.html' % dir_path))
plot_view_settings = plot_view.settings()
plot_view_settings.setAttribute(QWebSettings.WebGLEnabled, True)
plot_view_settings.setAttribute(QWebSettings.DeveloperExtrasEnabled, True)
hbox.addWidget(plot_view)
self.setLayout(hbox)
self.setWindowTitle('WTerminal')
self.show()
#############################################################
# internal Usefull method
#############################################################
def chgPrompt(self, prompt):
self.page.currentFrame().evaluateJavaScript("setPrompt('%s')" % prompt.replace("\\","/"));
def addTextConsole(self, elm):
self.page.currentFrame().evaluateJavaScript("addtext('%s')" % elm.replace("\\","/"))
def addImageConsole(self, elm):
#self.page.currentFrame().evaluateJavaScript("addtext('%s : <img src=\"%s\" width=\"100px\"/>')" % (os.path.basename(elm), elm.replace("\\","/")))
self.page.currentFrame().evaluateJavaScript("addtext('<div class=\"gallery\"><a href=\"file:///%s\"><img src=\"file:///%s\" alt=\"%s\" width=100px />%s</a></div>')" % (elm.replace("\\","/"), elm.replace("\\","/"),os.path.basename(elm),os.path.basename(elm)))
def isImage(self, filename):
return os.path.splitext(filename)[1].lower() in [".jpg",".png",".gif",".jpeg"]
def registerCmd(self):
pm = pluginManager(os.path.join(os.path.dirname(__file__),"cmds"))
for obj in pm.classes():
self._command.append(obj)
#################################################################
# SLOT
#################################################################
def helpNeeded(self, cmd):
print("Help Needed")
lstcmd = cmd.split(" ")
if len(lstcmd)>1:
for c in self._command:
if c.match(lstcmd[1]):
self.addTextConsole(c.gethelp())
else:
for c in self._command:
self.addTextConsole(c.getshorthelp())
def tabPressed(self,cmd):
print(cmd)
def commandRecieved(self, cmd):
#print(cmd)
lstcmd = cmd.split(" ")
print(lstcmd)
print(self._command)
for c in self._command:
if c.match(lstcmd[0]):
c.execute(lstcmd,self)
self.chgPrompt(os.getcwd())
"""
if lstcmd[0] in self._command.keys():
self._command[lstcmd[0]]
getattr(self, self._command[lstcmd[0]])(lstcmd)
else:
self.page.currentFrame().evaluateJavaScript("addtext('command not found')")
"""
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()