00001 from qt import *
00002 import string
00003
00004 from cdbVisCore import *
00005 from objectclasses import *
00006 import helper
00007 from GUIcreatedevices import *
00008 from CreatePorts import *
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 class CreateDeviceWindow(GUIcreateDevices):
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 def __init__(self,parent,selectedsystem,creationmode=True,deviceobj=None,devicetypes=[],devtype="",location="",functionlist=[]):
00035 GUIcreateDevices.__init__(self,parent,"Create_Devices",0,0)
00036 self._creationmode = creationmode
00037 in_db = False
00038 self.main = parent
00039 close_window = False
00040 self._deletedports = []
00041
00042 if creationmode == True:
00043 title = "Create Device(s)"
00044 self._deviceports = []
00045 if devtype == None:
00046 devtype = ""
00047 else:
00048 self._deviceports = deviceobj.GetPorts()
00049
00050 if self._deviceports == False:
00051 self.main.ShowError("A database error occured while fetching information about the ports for the current device: " + str(deviceobj.GetErrorMessage()) + ". We are unable to continue.",ERR_ERROR,True)
00052 close_window = True
00053
00054 title = "Modify Device " + str(deviceobj.GetName())
00055 selectedsystem = deviceobj.GetSystem()
00056 self.renameobj = None
00057
00058
00059
00060 self.oldname = deviceobj.GetName()
00061 self.oldnode = deviceobj.GetNode()
00062 self.oldsystem = deviceobj.GetSystem()
00063
00064 self.deviceid = deviceobj.GetID()
00065 self.nodechanged = deviceobj.IsNodeChanged()
00066 self.systemchanged = deviceobj.IsSystemChanged()
00067
00068
00069 test_device = Device(deviceobj.GetSystem(),deviceobj.GetName(),True)
00070 res = test_device.Update(True)
00071 if res == {}:
00072 in_db = False
00073 elif res == False:
00074 in_db = False
00075 close_window = True
00076 self.main.ShowError("A database error occured while trying to fetch information about the current device: " + str(test_device.GetErrorMessage()) + ". We are unable to continue.",ERR_ERROR,True)
00077 else:
00078 in_db = True
00079
00080
00081
00082 if True:
00083 pass
00084 '''Create_DevicesLayout = QGridLayout(self,1,1,11,6,"Create_DevicesLayout")
00085
00086 self._serialtxtbox = QLineEdit(self,"__serialtxtbox")
00087
00088 Create_DevicesLayout.addMultiCellWidget(self._serialtxtbox,1,1,8,10)
00089
00090 self._hwtypetxtbox = QLineEdit(self,"__hwtypetxtbox")
00091
00092 Create_DevicesLayout.addMultiCellWidget(self._hwtypetxtbox,3,3,8,10)
00093
00094 self._responsibletxtbox = QLineEdit(self,"__responsibletxtbox")
00095
00096 Create_DevicesLayout.addMultiCellWidget(self._responsibletxtbox,5,5,8,10)
00097
00098 self.textLabel2_4_3 = QLabel(self,"textLabel2_4_3")
00099
00100 Create_DevicesLayout.addMultiCellWidget(self.textLabel2_4_3,0,0,8,9)
00101
00102 self.textLabel2_4_3_2 = QLabel(self,"textLabel2_4_3_2")
00103
00104 Create_DevicesLayout.addMultiCellWidget(self.textLabel2_4_3_2,2,2,8,9)
00105
00106 self.textLabel2_4_3_2_2_2 = QLabel(self,"textLabel2_4_3_2_2_2")
00107
00108 Create_DevicesLayout.addMultiCellWidget(self.textLabel2_4_3_2_2_2,6,6,8,9)
00109
00110 self.textLabel2_4_3_2_2 = QLabel(self,"textLabel2_4_3_2_2")
00111
00112 Create_DevicesLayout.addMultiCellWidget(self.textLabel2_4_3_2_2,4,4,8,9)
00113
00114 self.textLabel2_4_3_2_2_2_2 = QLabel(self,"textLabel2_4_3_2_2_2_2")
00115
00116 Create_DevicesLayout.addMultiCellWidget(self.textLabel2_4_3_2_2_2_2,9,9,8,9)
00117
00118 layout6 = QVBoxLayout(None,0,6,"layout6")
00119
00120 self.textLabel2_3_2 = QLabel(self,"textLabel2_3_2")
00121 layout6.addWidget(self.textLabel2_3_2)
00122
00123 self._functiontxtbox = QComboBox(0,self,"__functiontxtbox")
00124 layout6.addWidget(self._functiontxtbox)
00125
00126 Create_DevicesLayout.addMultiCellLayout(layout6,13,13,0,6)
00127
00128 layout11 = QVBoxLayout(None,0,6,"layout11")
00129
00130 self.textLabel2_2 = QLabel(self,"textLabel2_2")
00131 layout11.addWidget(self.textLabel2_2)
00132
00133 self._sysnamescmbbox = QComboBox(0,self,"__sysnamescmbbox")
00134 layout11.addWidget(self._sysnamescmbbox)
00135
00136 Create_DevicesLayout.addMultiCellLayout(layout11,0,1,2,4)
00137
00138 self._delbutton = QPushButton(self,"__delbutton")
00139 self._delbutton.setMaximumSize(QSize(21,21))
00140
00141 Create_DevicesLayout.addWidget(self._delbutton,1,6)
00142
00143 self._addbutton = QPushButton(self,"__addbutton")
00144 self._addbutton.setMaximumSize(QSize(21,21))
00145
00146 Create_DevicesLayout.addWidget(self._addbutton,1,5)
00147
00148 self._chk_prommode = QCheckBox(self,"__chk_prommode")
00149
00150 Create_DevicesLayout.addMultiCellWidget(self._chk_prommode,11,11,0,1)
00151
00152 layout5 = QVBoxLayout(None,0,6,"layout5")
00153
00154 self.textLabel2_4_2_2 = QLabel(self,"textLabel2_4_2_2")
00155 layout5.addWidget(self.textLabel2_4_2_2)
00156
00157 self._startspinbox = QSpinBox(self,"__startspinbox")
00158 layout5.addWidget(self._startspinbox)
00159
00160 Create_DevicesLayout.addMultiCellLayout(layout5,10,10,0,1)
00161
00162 layout4 = QVBoxLayout(None,0,6,"layout4")
00163
00164 self.textLabel2_4_2_2_2 = QLabel(self,"textLabel2_4_2_2_2")
00165 layout4.addWidget(self.textLabel2_4_2_2_2)
00166
00167 self._endspinbox = QSpinBox(self,"__endspinbox")
00168 layout4.addWidget(self._endspinbox)
00169
00170 Create_DevicesLayout.addMultiCellLayout(layout4,10,10,2,6)
00171
00172 self._paddingchkbox = QCheckBox(self,"__paddingchkbox")
00173
00174 Create_DevicesLayout.addWidget(self._paddingchkbox,8,0)
00175
00176 self._paddingspinbox = QSpinBox(self,"__paddingspinbox")
00177 self._paddingspinbox.setEnabled(0)
00178
00179 Create_DevicesLayout.addMultiCellWidget(self._paddingspinbox,8,8,1,3)
00180
00181 self.textLabel2_4_2 = QLabel(self,"textLabel2_4_2")
00182
00183 Create_DevicesLayout.addMultiCellWidget(self.textLabel2_4_2,8,8,4,5)
00184 spacer1 = QSpacerItem(310,20,QSizePolicy.Expanding,QSizePolicy.Minimum)
00185 Create_DevicesLayout.addMultiCell(spacer1,14,14,0,8)
00186
00187 self._okbutton = QPushButton(self,"__okbutton")
00188
00189 Create_DevicesLayout.addWidget(self._okbutton,14,9)
00190
00191 self._cancelbutton = QPushButton(self,"__cancelbutton")
00192
00193 Create_DevicesLayout.addWidget(self._cancelbutton,14,10)
00194
00195 self._portbutton = QPushButton(self,"__portbutton")
00196
00197 Create_DevicesLayout.addMultiCellWidget(self._portbutton,13,13,8,10)
00198
00199 self._commentstxtbox = QTextEdit(self,"__commentstxtbox")
00200
00201 Create_DevicesLayout.addMultiCellWidget(self._commentstxtbox,10,12,8,10)
00202 spacer4 = QSpacerItem(20,20,QSizePolicy.Fixed,QSizePolicy.Minimum)
00203 Create_DevicesLayout.addItem(spacer4,8,7)
00204
00205 self._locationtxtbox = QLineEdit(self,"__locationtxtbox")
00206
00207 Create_DevicesLayout.addMultiCellWidget(self._locationtxtbox,7,7,8,10)
00208
00209 layout9 = QVBoxLayout(None,0,6,"layout9")
00210
00211 self.textLabel2 = QLabel(self,"textLabel2")
00212 layout9.addWidget(self.textLabel2)
00213
00214 self._systemnametxtbox = QLineEdit(self,"__systemnametxtbox")
00215 self._systemnametxtbox.setEnabled(0)
00216 layout9.addWidget(self._systemnametxtbox)
00217
00218 Create_DevicesLayout.addMultiCellLayout(layout9,0,1,0,0)
00219
00220 layout7 = QVBoxLayout(None,0,6,"layout7")
00221
00222 self.textLabel2_4 = QLabel(self,"textLabel2_4")
00223 layout7.addWidget(self.textLabel2_4)
00224
00225 self._prefixtxtbox = QLineEdit(self,"__prefixtxtbox")
00226 layout7.addWidget(self._prefixtxtbox)
00227
00228 Create_DevicesLayout.addMultiCellLayout(layout7,6,7,0,6)
00229
00230 self._chk_node = QCheckBox(self,"__chk_node")
00231
00232 Create_DevicesLayout.addMultiCellWidget(self._chk_node,12,12,0,4)
00233
00234 layout8 = QVBoxLayout(None,0,6,"layout8")
00235
00236 self.textLabel2_3 = QLabel(self,"textLabel2_3")
00237 layout8.addWidget(self.textLabel2_3)
00238
00239 self._devicetypetxtbox = QComboBox(0,self,"__devicetypetxtbox")
00240 layout8.addWidget(self._devicetypetxtbox)
00241
00242 Create_DevicesLayout.addMultiCellLayout(layout8,4,5,0,6)
00243
00244 layout7_2_2 = QVBoxLayout(None,0,6,"layout7_2_2")
00245
00246 self.textLabel2_4_4_2 = QLabel(self,"textLabel2_4_4_2")
00247 layout7_2_2.addWidget(self.textLabel2_4_4_2)
00248
00249 self._prefixtxtbox = QLineEdit(self,"__prefixtxtbox")
00250 self._prefixtxtbox.setEnabled(False)
00251 layout7_2_2.addWidget(self._prefixtxtbox)
00252
00253 Create_DevicesLayout.addMultiCellLayout(layout7_2_2,2,3,0,2)
00254
00255 layout7_2 = QVBoxLayout(None,0,6,"layout7_2")
00256
00257 self.textLabel2_4_4 = QLabel(self,"textLabel2_4_4")
00258 layout7_2.addWidget(self.textLabel2_4_4)
00259
00260 self._deviceidtxtbox = QLineEdit(self,"__deviceidtxtbox")
00261 self._deviceidtxtbox.setEnabled(False)
00262 layout7_2.addWidget(self._deviceidtxtbox)
00263
00264 Create_DevicesLayout.addMultiCellLayout(layout7_2,2,3,3,6)
00265
00266 self.languageChange()
00267
00268 self.resize(QSize(579,461).expandedTo(self.minimumSizeHint()))
00269 self.clearWState(Qt.WState_Polished)
00270 '''
00271
00272 self.system = selectedsystem
00273 self.deviceobj = deviceobj
00274 self.devs = []
00275 self.deviceobjs = []
00276
00277
00278
00279 if self._creationmode:
00280 mysystems = selectedsystem + ","
00281 else:
00282 mysystems = deviceobj.GetSystem() + ","
00283
00284 self._systemnametxtbox.setText(mysystems)
00285 tmpsystems = self.main.GetCdb().GetSubSystems()
00286 subsystems = []
00287 for tmp in tmpsystems:
00288 subsystems.append(tmp.GetName())
00289 for system in subsystems:
00290 self._sysnamescmbbox.insertItem(system)
00291 self.connect(self._addbutton,SIGNAL("released()"),self.OnAdd)
00292 self.connect(self._delbutton,SIGNAL("released()"),self.OnRemove)
00293
00294 if not self._creationmode:
00295 self._deviceurl.setText(deviceobj.GetURL())
00296
00297
00298 print "devicetypes=%s " % (devicetypes);
00299
00300 for type in devicetypes:
00301 self._devicetypetxtbox.insertItem(type)
00302
00303 if not self._creationmode:
00304 devtype = deviceobj.GetType()
00305 self._devicetypetxtbox.setCurrentText(devtype)
00306 self._devicetypetxtbox.setEnabled(False)
00307 if in_db:
00308 self._deviceidtxtbox.setText(str(self.deviceobj.GetID()))
00309
00310
00311
00312 if not self._creationmode:
00313 self._prefixtxtbox.setEnabled(True)
00314 self._prefixtxtbox.setText(self.deviceobj.GetName())
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325 if not self._creationmode:
00326 self._chk_prommode.setChecked(self.deviceobj.GetPromMode())
00327 self._chk_node.setChecked(self.deviceobj.GetNode())
00328 self._serialtxtbox.setText(self.deviceobj.GetSerial())
00329 self._hwtypetxtbox.setText(self.deviceobj.GetHWType())
00330 self._responsibletxtbox.setText(self.deviceobj.GetResponsible())
00331 self._locationtxtbox.setText(self.deviceobj.GetLocation())
00332 self._commentstxtbox.setText(self.deviceobj.GetComments())
00333
00334
00335
00336
00337 if in_db:
00338 self._serialtxtbox.setEnabled(False)
00339 self._hwtypetxtbox.setEnabled(False)
00340 self._responsibletxtbox.setEnabled(False)
00341 self._commentstxtbox.setEnabled(False)
00342
00343 else:
00344 self._chk_prommode.setChecked(True)
00345 self._locationtxtbox.setText(location)
00346
00347
00348 if self._creationmode:
00349 self._prefixtxtbox.setEnabled(True)
00350 self._paddingchkbox.setEnabled(True)
00351 self._paddingspinbox.setEnabled(True)
00352 self._startspinbox.setEnabled(True)
00353 self._endspinbox.setEnabled(True)
00354
00355
00356 if devtype != "":
00357 self.OnDeviceTypeChanged(None)
00358 self._endspinbox.setValue(1)
00359 else:
00360 self._prefixtxtbox.setEnabled(False)
00361 self._paddingchkbox.setEnabled(False)
00362 self._paddingspinbox.setEnabled(False)
00363 self._paddingspinbox.setEnabled(True)
00364 self._startspinbox.setEnabled(False)
00365 self._endspinbox.setEnabled(False)
00366
00367 if self._creationmode:
00368 text = "Create/Modify ports..."
00369 else:
00370 text = "Modify ports..."
00371 self._portbutton.setText(text)
00372 self.connect(self._portbutton,SIGNAL("released()"),self.OnCreatePorts)
00373
00374
00375 self.connect(self._okbutton,SIGNAL("released()"),self.OkButtonClicked)
00376 self.connect(self._cancelbutton,SIGNAL("released()"),self.reject)
00377
00378
00379
00380
00381
00382
00383
00384 def SaveObjects(self):
00385 try:
00386
00387 if self._creationmode:
00388
00389 i = self._startspinbox.value()
00390
00391 while i < (int(str(self._endspinbox.text())) + int(str(self._startspinbox.text()))):
00392 padding = int(str(self._paddingspinbox.text()))
00393 nrlength = len(str(i))
00394
00395 if self._paddingchkbox.isChecked():
00396 padstr = "".zfill(padding-nrlength)
00397 else:
00398 padstr = ""
00399
00400
00401 devname = str(self._prefixtxtbox.text()).replace("%d",str(padstr)+str(i))
00402 serial = str(self._serialtxtbox.text()).replace("%d",str(padstr)+str(i))
00403 deviceobj = Device(str(self._systemnametxtbox.text())[:-1],devname,True)
00404 deviceobj.SetName(devname)
00405 sysname = str(self._systemnametxtbox.text())[:-1]
00406 deviceobj.SetSystem(sysname,False)
00407 deviceobj.SetNode(int(self._chk_node.isChecked()),False)
00408 deviceobj.SetType(str(self._devicetypetxtbox.currentText()))
00409 deviceobj.SetSerial(serial)
00410 deviceobj.SetHWType(str(self._hwtypetxtbox.text()))
00411 deviceobj.SetResponsible(str(self._responsibletxtbox.text()))
00412 deviceobj.SetLocation(str(self._locationtxtbox.text()))
00413 deviceobj.SetComments(str(self._commentstxtbox.text()))
00414 deviceobj.SetPromMode(int(self._chk_prommode.isChecked()))
00415 deviceobj.SetURL(str(self._deviceurl.text()))
00416
00417
00418 self.deviceobjs.append(deviceobj)
00419
00420 i += 1
00421 else:
00422 devname = str(self._prefixtxtbox.text())
00423 if devname != self.oldname:
00424 ans = QMessageBox.question(self,"Rename ?","You have renamed the device, was that your intention? Remember that you can not undo renaming, \nbut you can rename the object as many times as you want.","Yes","No","",0,1)
00425 if ans == 1:
00426 self._prefixtxtbox.setText(self.oldname)
00427 return False
00428 devtypename = str(self._devicetypetxtbox.currentText())
00429 devtype = DeviceType(str(self._systemnametxtbox.text())[:-1],devtypename,False)
00430 devs = devtype.GetDevices()
00431 if devs == False:
00432 self.main.ShowError("Failed to fetch data from ConfDB: " + str(devtype.GetErrorMessage()),ERR_ERROR,True)
00433 return False
00434 else:
00435 if devname in devs:
00436 self.main.ShowError("You cannot rename the device to this name because " + str(devname) + " is already in use by another device.",ERR_ERROR,True)
00437 self._prefixtxtbox.setText(self.oldname)
00438 return False
00439
00440 self.renameobj = Device(str(self._systemnametxtbox.text())[:-1],str(self._prefixtxtbox.text()),True)
00441 self.renameobj.SetOldName(self.oldname)
00442 self.renameobj.SetType(str(self._devicetypetxtbox.currentText()))
00443 self.renameobj.SetID(self.deviceid)
00444 self.renameobj.SetSaveStatus(RENAME)
00445
00446 self.deviceobj = Device(str(self._systemnametxtbox.text())[:-1],devname,False)
00447 self.deviceobj.SetID(self.deviceid)
00448
00449 self.deviceobj.SetName(str(self._prefixtxtbox.text()))
00450 self.deviceobj.SetType(str(self._devicetypetxtbox.currentText()))
00451
00452 self.deviceobj.SetSerial(str(self._serialtxtbox.text()))
00453 self.deviceobj.SetHWType(str(self._hwtypetxtbox.text()))
00454 self.deviceobj.SetResponsible(str(self._responsibletxtbox.text()))
00455 self.deviceobj.SetLocation(str(self._locationtxtbox.text()))
00456 self.deviceobj.SetComments(str(self._commentstxtbox.text()))
00457 self.deviceobj.SetURL(str(self._deviceurl.text()))
00458
00459
00460
00461 if int(self._chk_node.isChecked()) != self.oldnode or self.nodechanged:
00462 changed = True
00463 else:
00464 changed = False
00465 self.deviceobj.SetNode(int(self._chk_node.isChecked()),changed)
00466
00467 sysname = str(self._systemnametxtbox.text())[:-1]
00468 if sysname != self.oldsystem or self.systemchanged:
00469 changed = True
00470 else:
00471 changed = False
00472
00473 self.deviceobj.SetSystem(sysname,changed)
00474 self.deviceobj.SetPromMode(int(self._chk_prommode.isChecked()))
00475
00476 self.deviceobjs.append(self.deviceobj)
00477
00478 return True
00479
00480 except ValidationError,err:
00481 self.main.ShowError(str(err),ERR_ERROR,True)
00482 return False
00483 except ValueError,err:
00484 self.main.ShowError("A String to Integer conversion failed: " + str(err),ERR_ERROR,True)
00485 return False
00486
00487
00488
00489
00490
00491
00492
00493
00494 def OnCreatePorts(self):
00495
00496
00497 devicename = str(self._prefixtxtbox.text())
00498 devtype = DeviceType(str(self._systemnametxtbox.text())[:-1],str(self._devicetypetxtbox.currentText()),False)
00499 my_createportframe = createPortsWindow(self.main,devtype,devicename,self._deviceports,self)
00500 my_createportframe.show()
00501 ans = my_createportframe.exec_loop()
00502 if ans==1:
00503 self._deviceports = my_createportframe.GetPorts()
00504 self._deletedports = my_createportframe.GetDeletedPorts()
00505 return
00506 '''# We need device type and device name for the device(s) before we assign ports to them.
00507 if self._devicetypetxtbox.GetValue == "":
00508 self.main.ShowError("You have to choose a device type before you assign ports.",ERR_ERROR,True)
00509 return False
00510
00511 # If we create, the device name has to be set. Prefix with %d if more than one device to be created.
00512 if self._creationmode:
00513 if str(self._prefixtxtbox.text()) == "" or (str(self._prefixtxtbox.text()).find("%d",0,len(str(self._prefixtxtbox.text()))) == -1 and str(self._endspinbox.text()) > 1):
00514 self.main.ShowError("You have to set a proper device name, and include a %d if you create more than one device, before you assign ports.",ERR_ERROR,True)
00515 return False
00516
00517 # We set up the device names in a string like this: DEV_NAME_001 -> DEV_NAME_010
00518 # for the devices that the ports will be created for.
00519 padding = int(str(self._paddingspinbox.text()))
00520 nrlength = len(str(str(self._startspinbox.text())))
00521 if self._paddingchkbox.isChecked():
00522 padstr = "".zfill(padding-nrlength)
00523 else:
00524 padstr = ""
00525
00526
00527 devname = str(self._prefixtxtbox.text()).replace("%d",str(padstr)+str(str(self._startspinbox.text())))
00528 if str(self._endspinbox.text()) > 1:
00529 nrlength = len(str(str(self._endspinbox.text())))
00530 if self._paddingchkbox.isChecked():
00531 padstr = "".zfill(padding-nrlength)
00532 else:
00533 padstr = ""
00534 devname += " -> " + str(self._prefixtxtbox.text()).replace("%d",str(padstr)+str(str(self._startspinbox.text())+str(self._endspinbox.text())-1))
00535 else: # Modify
00536 if str(self._prefixtxtbox.text()) == "":
00537 self.main.ShowError("You have to choose a device name before you assign ports",ERR_ERROR,True)
00538 return False
00539 devname = str(self._prefixtxtbox.text())
00540
00541 # We also need to send the device type to the create ports window (to check that the number of created ports
00542 # and type is correct.
00543 devtypename = str(self._devicetypetxtbox.currentText())
00544 devtype = DeviceType(str(self._systemnametxtbox.text())[:-1],devtypename,False) #The [:-1] removes the last comma
00545
00546 # Open Create Ports Window
00547 win = CreatePortsWindow(self.main,-1,self._creationmode,devname,devtype,self._deviceports)
00548 if win.ShowModal() == wxID_OK:
00549 self._deviceports,deletedports = win.GetPortObjs() # Set the port objects created to the self._deviceports list
00550 self._deletedports.extend(deletedports)'''
00551
00552
00553
00554
00555
00556
00557 def OnSelectPadding(self,checked):
00558
00559 self._paddingspinbox.setEnabled(not self._paddingspinbox.isEnabled())
00560
00561
00562
00563
00564
00565 def OnDeviceTypeChanged(self,event):
00566
00567 devtypename = str(self._devicetypetxtbox.currentText())
00568 sysname = str(self._systemnametxtbox.text())[:-1]
00569 devtype = DeviceType(sysname,devtypename,False)
00570 devs = devtype.GetDevices()
00571 if devs == False:
00572 self.main.ShowError("Failed to retrieve devices of the given device type: " + str(devtype.GetErrorMessage()),ERR_ERROR,True)
00573 self._prefixtxtbox.setEnabled(True)
00574 self._paddingchkbox.setEnabled(True)
00575 self._startspinbox.setEnabled(True)
00576 self._endspinbox.setEnabled(True)
00577
00578 self._prefixtxtbox.setText("")
00579 self.devs = []
00580 elif devs == []:
00581 self._prefixtxtbox.setEnabled(True)
00582 self._paddingchkbox.setEnabled(True)
00583 self._startspinbox.setEnabled(True)
00584 self._endspinbox.setEnabled(True)
00585 self._prefixtxtbox.setText("")
00586 self.devs = []
00587 else:
00588
00589
00590
00591 devs.sort()
00592 self.devs = devs
00593 self._prefixtxtbox.setEnabled(True)
00594 self._paddingchkbox.setEnabled(True)
00595 self._startspinbox.setEnabled(True)
00596 self._endspinbox.setEnabled(True)
00597 self._prefixtxtbox.setText(devs[0])
00598 self.MakeEstimatedGuess(None)
00599
00600
00601
00602
00603
00604 def OnAdd(self):
00605
00606 val = str(self._sysnamescmbbox.currentText())
00607 if val != "":
00608 if string.find(str(self._systemnametxtbox.text()),val,0) != -1:
00609 self.main.ShowError("The subsystem is already added to the subsystem list.",ERR_ERROR)
00610 else:
00611 self._systemnametxtbox.setText(str(self._systemnametxtbox.text()) + val + ",")
00612
00613
00614
00615
00616
00617 def OnRemove(self):
00618
00619 val = str(self._sysnamescmbbox.currentText())
00620 if val != "":
00621 if string.find(str(self._systemnametxtbox.text()),val,0) == -1:
00622 self.main.ShowError("This subsystem is not a part of the subsystem list, and cannot be removed.",ERR_ERROR)
00623 else:
00624 myvals = str(self._systemnametxtbox.text())
00625 myvals = myvals.replace(val + ",","")
00626 self._systemnametxtbox.setText(myvals)
00627
00628
00629
00630
00631
00632
00633
00634 def MakeEstimatedGuess(self,event=None):
00635
00636
00637
00638 syntax = str(self._prefixtxtbox.text())
00639 if syntax == "":
00640 self.main.ShowError("We didn't find any devices of the given device type, the user will have to fill out all fields him/her self",ERR_ERROR)
00641 return
00642 if self.devs == []:
00643 self.main.ShowError("We didn't find any devices of the given device type, the user will have to fill out all fields him/her self",ERR_ERROR)
00644 return
00645
00646
00647 i = len(syntax)-1
00648 nrofindexint = []
00649 prevwasint = False
00650 while i >= 0:
00651 try:
00652 nr = int(syntax[i])
00653 nrofindexint.insert(0,i)
00654 prevwasint = True
00655 except ValueError,err:
00656 if prevwasint:
00657
00658
00659 break
00660 i -= 1
00661
00662
00663 if nrofindexint == []:
00664 return
00665
00666
00667 start = nrofindexint[0]
00668 end = nrofindexint[len(nrofindexint)-1]
00669 if start == end:
00670 substring = syntax[start]
00671 else:
00672 substring = syntax[start:end+1]
00673
00674
00675 if nrofindexint[0]-1 >= 0:
00676 startstr = syntax[0:start]
00677 else:
00678 startstr = ""
00679
00680 if len(syntax)-1 > end:
00681 endstr = syntax[end+1:len(syntax)]
00682 else:
00683 endstr = ""
00684
00685
00686 prefix = startstr + "%d" + endstr
00687 self._prefixtxtbox.setText(prefix)
00688
00689
00690
00691
00692
00693 if len(str(int(substring))) != len(str(substring)):
00694 nrofdigits = len(str(substring))
00695 self._paddingchkbox.setChecked(True)
00696 self._paddingspinbox.setValue(nrofdigits)
00697 self._paddingspinbox.setEnabled(True)
00698
00699 '''# 8. Find start number of next device (need to find the highest number used for a device for this type)
00700 nrs = []
00701 for dev in self.devs: # self.devs is alphabetically sorted
00702 # a. We replace string before and after number with "" (empty string), then we can easily
00703 # extract the number, convert it to integer, and check if we have found the biggest number
00704 # 2DO: a more efficient way to find the highest number without having to check every device name...
00705 # (Perhaps: first check string length, then compare numbers)
00706 if startstr != "":
00707 dev = dev.replace(startstr,"")
00708 if endstr != "":
00709 dev = dev.replace(endstr,"")
00710
00711 try:
00712 nrs.append(int(dev))
00713 except ValueError,err:
00714 self.main.ShowError("Some problem automatically finding the correct prefix, you will have to set the prefix yourself. \nStart number for new devices was not set.",ERR_ERROR,True)
00715 return
00716 # 9. Sort the numbers found and pick out the last one -> the biggest one -> add 1 to get the start number
00717 nrs.sort()
00718 self._startspinbox.setValue(nrs[len(nrs)-1]+1)'''
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733 def GenerateNames(self,event=None,showerror=False):
00734
00735 syntax = str(self._prefixtxtbox.text())
00736 index = syntax.find("%d",0,len(syntax))
00737
00738 if index == -1 and showerror:
00739 if self._endspinbox.value() == 1:
00740 return True
00741 else:
00742 self.main.ShowError("Couldn't find %d in the prefix, this HAS to be added because you are creating more than one device.",ERR_ERROR,True)
00743 self._startspinbox.setValue(0)
00744 return False
00745 elif index == -1:
00746 if str(self._endspinbox.text()) == 1:
00747 return True
00748 else:
00749 self._startspinbox.setValue(0)
00750 return False
00751 else:
00752 if index != 0:
00753 startstr = syntax[0:index]
00754 else:
00755 startstr = ""
00756 if index+1 != len(syntax)-1:
00757 endstr = syntax[index+2:len(syntax)]
00758 else:
00759 endstr = ""
00760
00761 return True
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771 def OkButtonClicked(self):
00772 if self._systemnametxtbox.text() == "":
00773 self.main.ShowError("No subsystems set!",ERR_ERROR,True)
00774 return False
00775
00776 if self._devicetypetxtbox.currentText() == "":
00777 self.main.ShowError("No device type set!",ERR_ERROR,True)
00778 return False
00779
00780 if not self._creationmode:
00781 if self._prefixtxtbox.text() == "":
00782 self.main.ShowError("No device name set!",ERR_ERROR,True)
00783 return False
00784 else:
00785 if self._endspinbox.value()>1 and self._prefixtxtbox.text() == "":
00786 self.main.ShowError("No prefix set.",ERR_ERROR,True)
00787 return False
00788
00789 if self._creationmode:
00790 success = self.GenerateNames(None,True)
00791 if not success:
00792 return False
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802 devtype = DeviceType(str(self._systemnametxtbox.text())[:-1],str(self._devicetypetxtbox.currentText()),False)
00803
00804 if len(self._deviceports) > (devtype.GetPortsIn()+devtype.GetPortsOut()):
00805 self.main.ShowError("The number of ports that a device of the given device type has (" + str(devtype.GetPortsIn()+devtype.GetPortsOut()) + "), does not correspond to the number of ports that you have created for this device (" + str(len(self._deviceports)) + "). Click the create/modify ports button to remove ports.",ERR_ERROR,True)
00806 return False
00807
00808
00809
00810
00811 devs = devtype.GetDevices()
00812 if devs == False:
00813 self.main.ShowError("Failed to fetch data from ConfDB: " + str(devtype.GetErrorMessage()),ERR_ERROR,True)
00814 return False
00815 i = int(str(self._startspinbox.text()))
00816 while i < (self._endspinbox.value() + self._startspinbox.value()):
00817 padding = int(str(self._paddingspinbox.text()))
00818 nrlength = len(str(i))
00819 if nrlength > padding and self._paddingchkbox.isChecked():
00820 self.main.ShowError("You have exceeded the maximum number of digits that you set as a maximum for the given device type.",ERR_ERROR,True)
00821 return False
00822 elif self._paddingchkbox.isChecked():
00823 padstr = "".zfill(padding-nrlength)
00824 else:
00825 padstr = ""
00826
00827 devname = str(self._prefixtxtbox.text()).replace("%d",str(padstr)+str(i))
00828 if devname in devs:
00829 self.main.ShowError("The device " + str(devname) + " already exists. You have to change the range or delete the device with the same name in order to save a new one with that name.",ERR_ERROR,True)
00830 return False
00831 i += 1
00832
00833
00834
00835 success = self.SaveObjects()
00836 if not success:
00837 return False
00838
00839 QDialog.accept(self)
00840
00841
00842
00843
00844
00845
00846 def IsCreationMode(self):
00847 return self._creationmode
00848
00849
00850
00851
00852
00853
00854
00855
00856 def GetDeviceObjs(self):
00857 if self._creationmode:
00858 return self.deviceobjs,self._deviceports
00859 else:
00860 return self.deviceobjs[0],self.renameobj,self._deviceports,self._deletedports
00861
00862