5 from PyQt4.QtCore import *
6 from PyQt4.QtGui import *
8 from sfa.util.record import SfaRecord, SliceRecord, AuthorityRecord, UserRecord
9 from sface.config import config
10 from sface.sfiprocess import SfiProcess
11 from sface.screens.sfascreen import SfaScreen
15 MEMBERSHIP_STATUS_COLUMN = 1
16 SERVER_MEMBERSHIP_STATUS_COLUMN = 2
18 user_status = { "in": "Already Selected",
19 "out": "Not Selected",
21 "remove": "To be Removed"}
23 class UserView(QTableView):
24 def __init__(self, parent=None):
25 QTableView.__init__(self, parent)
27 self.setSelectionBehavior(QAbstractItemView.SelectRows)
28 self.setSelectionMode(QAbstractItemView.SingleSelection)
29 self.setAlternatingRowColors(True)
30 self.setAttribute(Qt.WA_MacShowFocusRect, 0)
31 self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
32 self.setToolTip("Double click on a row to change its status.")
34 def mouseDoubleClickEvent(self, event):
35 index = self.currentIndex()
37 status_index = model.index(index.row(), MEMBERSHIP_STATUS_COLUMN, index.parent())
38 status_data = status_index.data().toString()
39 server_status_data = model.index(index.row(), SERVER_MEMBERSHIP_STATUS_COLUMN, index.parent()).data().toString()
40 node_index = model.index(index.row(), NAME_COLUMN, index.parent())
41 node_data = node_index.data().toString()
44 if status_data == user_status['in']:
45 model.setData(status_index, QString(user_status['remove']))
46 elif status_data == user_status['out']:
47 model.setData(status_index, QString(user_status['add']))
48 elif status_data in (user_status['add'], user_status['remove']):
49 if server_status_data == user_status["in"]:
50 model.setData(status_index, QString(user_status['in']))
52 model.setData(status_index, QString(user_status['out']))
54 model.emit(SIGNAL("dataChanged(QModelIndex, QModelIndex)"), node_index, node_index)
56 def currentChanged(self, current, previous):
57 model = current.model()
58 node_index = model.index(current.row(), 0, current.parent())
59 node_data = node_index.data().toString()
60 self.emit(SIGNAL('hostnameClicked(QString)'), node_data)
63 class UsersWidget(QWidget):
64 def __init__(self, parent):
65 QWidget.__init__(self, parent)
67 self.process = SfiProcess(self)
69 self.slicename = QLabel("", self)
70 self.updateSliceName()
71 self.slicename.setScaledContents(False)
72 searchlabel = QLabel ("Search: ", self)
73 searchlabel.setScaledContents(False)
74 searchbox = QLineEdit(self)
75 searchbox.setAttribute(Qt.WA_MacShowFocusRect, 0)
77 toplayout = QHBoxLayout()
78 toplayout.addWidget(self.slicename, 0, Qt.AlignLeft)
79 toplayout.addStretch()
80 toplayout.addWidget(searchlabel, 0, Qt.AlignRight)
81 toplayout.addWidget(searchbox, 0, Qt.AlignRight)
83 self.userView = UserView()
84 #self.userView.setSelectionBehavior(QAbstractItemView.SelectRows)
85 #self.userView.setSelectionMode(QAbstractItemView.SingleSelection)
87 refresh = QPushButton("Refresh", self)
88 refresh.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)
89 submit = QPushButton("Submit", self)
90 submit.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)
92 bottomlayout = QHBoxLayout()
93 bottomlayout.addWidget(refresh, 0, Qt.AlignLeft)
94 bottomlayout.addStretch()
95 bottomlayout.addWidget(submit, 0, Qt.AlignRight)
97 layout = QVBoxLayout()
98 layout.addLayout(toplayout)
99 layout.addWidget(self.userView)
100 layout.addLayout(bottomlayout)
101 self.setLayout(layout)
102 self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
104 self.userModel = QStandardItemModel(0, 4, self)
106 self.connect(refresh, SIGNAL('clicked()'), self.refresh)
107 self.connect(submit, SIGNAL('clicked()'), self.submit)
111 def submitFinished(self):
112 self.setStatus("<font color='green'>Slice data submitted.</font>")
113 QTimer.singleShot(1000, self.refresh)
115 def getSliceRecordFinished(self):
116 self.setStatus("<font color='green'>Authority data refreshed.</font>", timeout=5000)
117 self.refreshAuthority()
119 def getAuthorityRecordFinished(self):
120 self.setStatus("<font color='green'>Slice data refreshed.</font>", timeout=5000)
122 #self.parent().signalAll("usersUpdated")
124 def readSliceRecord(self):
125 rec_file = config.getSliceRecordFile()
126 if os.path.exists(rec_file):
127 xml = open(rec_file).read()
129 rec.load_from_string(xml)
133 def readAuthorityRecord(self):
134 rec_file = config.getAuthorityRecordFile()
135 if os.path.exists(rec_file):
136 xml = open(rec_file).read()
137 rec = AuthorityRecord()
138 rec.load_from_string(xml)
142 def readUserRecord(self, i):
143 rec_file = config.getAuthorityListFile(i)
144 if os.path.exists(rec_file):
145 xml = open(rec_file).read()
147 rec.load_from_string(xml)
151 def setStatus(self, msg, timeout=None):
152 self.parent().setStatus(msg, timeout)
154 def checkRunningProcess(self):
155 if self.process.isRunning():
156 self.setStatus("<font color='red'>There is already a process running. Please wait.</font>")
161 if self.checkRunningProcess():
164 rec = self.readSliceRecord()
165 change = self.process_table(rec)
168 self.setStatus("<font color=red>No change in slice data. Not submitting!</font>", timeout=3000)
171 rec_file = config.getSliceRecordFile()
172 file(rec_file, "w").write(rec.save_to_string())
174 self.disconnect(self.process, SIGNAL('finished()'), self.getAuthorityRecordFinished)
175 self.connect(self.process, SIGNAL('finished()'), self.submitFinished)
177 self.process.updateRecord(rec_file)
178 self.setStatus("Sending slice record. This will take some time...")
181 if not config.getSlice():
182 self.setStatus("<font color='red'>Slice not set yet!</font>")
185 if self.process.isRunning():
186 self.setStatus("<font color='red'>There is already a process running. Please wait.</font>")
189 self.disconnect(self.process, SIGNAL('finished()'), self.submitFinished)
190 self.connect(self.process, SIGNAL('finished()'), self.getSliceRecordFinished)
192 self.process.getSliceRecord()
193 self.setStatus("Refreshing slice record. This will take some time...")
195 def refreshAuthority(self):
196 self.disconnect(self.process, SIGNAL('finished()'), self.getSliceRecordFinished)
197 self.connect(self.process, SIGNAL('finished()'), self.getAuthorityRecordFinished)
199 self.process.listRecords(config.getAuthority(), "user", config.getAuthorityListFile())
200 self.setStatus("Refreshing user records. This will take some time...")
202 def addTableItem(self, table, row, col, val, data=None, readonly=True):
203 item = QTableWidgetItem(str(val))
205 item.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled)
207 if not isinstance(data, str):
208 data = pickle.dumps(data)
209 item.setData(Qt.UserRole, QVariant(data))
210 table.setItem(row, col, item)
212 def updateModel(self):
213 self.userModel.clear()
215 sliceRec = self.readSliceRecord()
222 #for pi in sliceRec.get_field("PI", default=[]):
224 # if not name in added_persons:
225 # slice_persons.append({"name": name, "role": "PI", "member": user_status["in"]})
226 # added_persons.append(name)
228 for researcher in sliceRec.get_field("researcher", default=[]):
229 name = str(researcher)
230 if not name in added_persons:
231 slice_persons.append({"name": name, "role": "researcher", "member": user_status["in"]})
232 added_persons.append(name)
235 while (os.path.exists(config.getAuthorityListFile(i))):
236 rec = self.readUserRecord(i)
238 name = str(rec.get_name())
239 if not name in added_persons:
240 slice_persons.append({"name": name, "role": "", "member": user_status["out"]})
241 added_persons.append(name)
244 rootItem = self.userModel.invisibleRootItem()
246 for person in slice_persons:
247 rootItem.appendRow([QStandardItem(QString(person["name"])),
248 #QStandardItem(QString(person["role"])),
249 QStandardItem(QString(person["member"])),
250 QStandardItem(QString(person["member"]))])
252 headers = QStringList() << "User Name" << "Status" << "ServerStatus"
253 self.userModel.setHorizontalHeaderLabels(headers)
255 def updateView(self):
258 self.userView.setModel(self.userModel)
259 self.userView.hideColumn(SERVER_MEMBERSHIP_STATUS_COLUMN)
260 self.userView.resizeColumnToContents(0)
262 def updateSliceName(self):
263 self.slicename.setText("Slice : %s" % (config.getSlice() or "None"))
265 def nodeSelectionChanged(self, hostname):
266 self.parent().nodeSelectionChanged(hostname)
268 def process_table(self, slicerec):
271 item = self.userModel.invisibleRootItem()
272 children = item.rowCount()
273 for row in range(0, children):
274 childName = str(item.child(row, NAME_COLUMN).data(Qt.DisplayRole).toString())
275 childStatus = str(item.child(row, MEMBERSHIP_STATUS_COLUMN).data(Qt.DisplayRole).toString())
277 if (childStatus == user_status['add']):
278 researcher = slicerec.get_field("researcher", [])
279 researcher.append(childName)
280 slicerec["researcher"] = researcher
282 elif (childStatus == user_status['remove']):
283 if childName in slicerec.get_field("PI"):
284 slicerec.get_field("PI").remove(childName)
285 if childName in slicerec.get_field("researcher"):
286 slicerec.get_field("researcher").remove(childName)
292 class UserScreen(SfaScreen):
293 def __init__(self, parent):
294 SfaScreen.__init__(self, parent)
296 slice = UsersWidget(self)
297 self.init(slice, "Users", "OneLab SFA crawler")
299 def configurationChanged(self):
300 self.widget.updateSliceName()
301 self.widget.updateView()