-
Notifications
You must be signed in to change notification settings - Fork 4
/
manager_gui.py
executable file
·128 lines (108 loc) · 4.59 KB
/
manager_gui.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
#!/usr/bin/python
import sys
import os
#if __name__ == "__main__":
# if os.name == "posix":
# sys.path.insert(0, os.path.join(os.environ['HOME'], 'src/pylabrad-0.93.#1/'))
## elif os.name == "nt":
# base_path = "U://"
from PyQt4 import QtCore, QtGui, uic
import qt4reactor
app = QtGui.QApplication(sys.argv)
qt4reactor.install()
import pymanager
from twisted.internet.task import LoopingCall
import manager_config
import blacklist
import config_gui
import registry
class ManagerMainWindow(QtGui.QMainWindow):
def __init__(self, reactor):
super(ManagerMainWindow, self).__init__()
uic.loadUi('manager_gui.ui', self)
self.connection_list.setColumnWidth(1, 300)
self.connection_list.sortByColumn(0, QtCore.Qt.AscendingOrder)
self.reactor = reactor
self.cfg = manager_config.ManagerConfig()
self.blacklist = blacklist.Blacklist()
self.actionRun.toggled.connect(self.on_run)
self.actionDrop.triggered.connect(self.on_drop)
self.actionConfig.triggered.connect(self.on_config)
self.actionWhitelist.triggered.connect(self.on_whitelist)
if self.cfg.autorun:
print "Autorun enabled"
self.actionRun.setChecked(True)
#self.actionConfig.triggered.connect(self.on_config)
#self.actionWhitelist.triggered.connect(self.on_whitelist)
#self.actionSave.triggered.connect(self.on_save)
def on_run(self, state):
if state:
print "starting manager"
self.factory = pymanager.LabradManagerFactory(self.cfg, self.blacklist)
self.listener = self.reactor.listenTCP(self.cfg.port, self.factory)
print "starting registry"
reg = registry.Registry(self.cfg.registry)
reg.password = self.cfg.password
self.reactor.connectTCP('localhost', self.cfg.port, reg)
self.cb = LoopingCall(self.update_connections)
self.cb.start(interval=0.25, now=True)
else:
print "stopping manager"
self.cb.stop()
self.listener.stopListening()
del self.factory
while self.connection_list.rowCount():
self.connection_list.removeRow(0)
print "self.run_action: %s" %(state)
def update_connections(self):
connections = dict(self.factory.manager.server_list.items() + self.factory.manager.client_list.items())
self.connection_list.setSortingEnabled(False)
existing_connections = set()
for idx in reversed(range(self.connection_list.rowCount())):
ID = int(self.connection_list.item(idx, 0).text())
existing_connections.add(ID)
try:
self.connection_list.setItem(idx, 2, QtGui.QTableWidgetItem(str(connections[ID].sent_pkts)))
self.connection_list.setItem(idx, 3, QtGui.QTableWidgetItem(str(connections[ID].recv_pkts)))
except KeyError:
self.connection_list.removeRow(idx)
for k, cxn in connections.items():
if k not in existing_connections:
self.connection_list.insertRow(0)
self.connection_list.setItem(0, 0, QtGui.QTableWidgetItem(str(cxn.ID)))
self.connection_list.setItem(0, 1, QtGui.QTableWidgetItem(cxn.name))
self.connection_list.setItem(0, 2, QtGui.QTableWidgetItem(str(cxn.sent_pkts)))
self.connection_list.setItem(0, 3, QtGui.QTableWidgetItem(str(cxn.recv_pkts)))
self.connection_list.setSortingEnabled(True)
def showEvent(self, ev):
print "Show event"
def closeEvent(self, ev):
print "close event, shutting down reactor"
self.reactor.stop()
def on_drop(self):
print "Dropping connection"
ID = int(self.connection_list.item(self.connection_list.currentRow(), 0).text())
if ID <= 1:
return # Can't drop the manager
self.factory.connections[ID].transport.loseConnection()
def on_config(self):
dlg = config_gui.ManagerConfigDialog(self.cfg)
rv = dlg.exec_()
print "on config"
print "return value: ", rv
def on_whitelist(self):
dlg = config_gui.ManagerBlacklistDialog(self.blacklist)
rv = dlg.exec_()
print "on whitelist"
print "return value: ", rv
def on_save(self):
print "on save"
def main():
#app = QtGui.QApplication(sys.argv)
from twisted.internet import reactor
w = ManagerMainWindow(reactor)
w.show()
rv = reactor.run()
sys.exit(rv)
if __name__ == "__main__":
main()