C:/mesDocs/QT_confDbVis_canvas/mass.py

Go to the documentation of this file.
00001 import csv, sys, pdb
00002 from objectclasses import *
00003 filename = "Velo_Connectivity_clean.csv"
00004 
00005 class mainApp:
00006     def ImportCSV(self,filename):
00007         def __addTo(elt,object_list):
00008                 if object_list==[] or (object_list and elt.GetName() not in [item.GetName() for item in object_list]):
00009                         object_list.append(elt)
00010                         return elt
00011                 else:
00012                         for item in object_list:
00013                                 if item.GetName() == elt.GetName():
00014                                         return item
00015         
00016         def __CreateLink(FromPort,ToPort):
00017                 if not FromPort or not ToPort:
00018                         pdb.set_trace()
00019                         raise 'No Port Given to the __CreateLink function'
00020                         return False
00021                 tmp_link = Link('')
00022                 tmp_link.SetNodeTo(ToPort.GetDevice())
00023                 tmp_link.SetNodeFrom(FromPort.GetDevice())
00024                 tmp_link.SetPortTo(str(ToPort.GetPortNbr()))
00025                 tmp_link.SetPortFrom(str(FromPort.GetPortNbr()))
00026                 tmp_link.SetType(LINK_TYPE)
00027                 tmp_link.SetName(tmp_link.GetNodeFrom()+":"+str(tmp_link.GetPortFrom())+":"+tmp_link.GetNodeTo()+":"+str(tmp_link.GetPortTo()))
00028                 return tmp_link
00029         def LookIn(list,current,steps):
00030                 length = len(list)
00031                 index = current
00032                 count = 0
00033                 if steps>0:
00034                         while index < length-1 and count < steps:
00035                                 if list[index]!=None:
00036                                         count += 1
00037                                 index += 1
00038                         if list[index] == None:
00039                                 return None
00040                         else:
00041                                 return list[index]
00042                 if steps<0:
00043                         while index >0 and count < abs(steps):
00044                                 if list[index]!=None:
00045                                         count += 1
00046                                 index -= 1
00047                         if list[index] == None:
00048                                 return None
00049                         else:
00050                                 return list[index]
00051                                 
00052                 
00053         filename = "Velo_Connectivity_clean.csv"
00054         reader = csv.reader(open(filename, "rb"))
00055         DEVICE, DEVPORT, PORT, LINK = 0, 1, 2, 3
00056         LINK_TYPE = 'hv'
00057         device_list = []
00058         port_list = []
00059         link_list = []
00060         types = [None, None, None, None, DEVICE, PORT, LINK, DEVPORT, DEVPORT, None, LINK, DEVPORT, DEVPORT, LINK, DEVICE]
00061         lastWay = 2
00062         lastPort = None
00063 
00064         try:
00065             # pdb.set_trace()
00066             for row in reader:
00067                 print row
00068                 index = 0
00069                 length = len(row)
00070                 while index < length:
00071                         cell = row[index]
00072                         tmp_port = None
00073                         if types[index]==DEVICE:
00074                                 # create device if does not exist and add it the the  object_list
00075                                 tmp_device = __addTo(Device("",cell),device_list)
00076                                 # Create a Link if  next cell is not a Port and previous cell is a LINK or a Port or a DEVPORT. 
00077                                 if index+1==length or (index+1<length and LookIn(types,index,1)!=PORT):  # if there is no port after our device we should create a port (with number = 1 and way= not lastWay) and a link.
00078                                         # creating the port
00079                                         lastWay = 3-lastWay
00080                                         tmp_port = Port('','1','',str(lastWay),cell)  # Port Nr = 1;   port way = Opposite of the last Port Way. (use of the function x -> 3-x) 
00081                                         tmp_port = __addTo(tmp_port, port_list)
00082                                         # ceating the link
00083                                         if index-1>0 and lastPort:
00084                                             if LookIn(types,index,-1) == DEVICE: # if there is a different device before we  create a link
00085                                                 tmp_link = __CreateLink(lastPort,tmp_port)
00086                                                 tmp_link = __addTo(tmp_link,link_list)
00087                                             elif LookIn(types,index,-1)== PORT:
00088                                                 pass # TO DO 
00089                                             elif LookIn(types,index,-1)== DEVPORT:
00090                                                 pass # TO DO    
00091                                 
00092                         elif types[index]==DEVPORT: # DEVPORT =   device_name[in/out][ _ ][integer]
00093                                 # 1 - extract data from the current column
00094                                 portWay = -1
00095                                 try:
00096                                         portNr = int(cell[-1:]) # if the last caractere is a number so we have to continue to find how many caracter is the port number made of.
00097                                         i = 2
00098                                         while(i<len(cell)):
00099                                                 try:
00100                                                         portNr = int(cell[-i:])
00101                                                         i += 1
00102                                                 except:
00103                                                         i -= 1
00104                                                         break;
00105                                 except:
00106                                         portNr = 1  # default : if no port number is indiquited
00107                                         if cell[-2:]=="in":
00108                                                 portWay = 1
00109                                                 cell = cell[:-2]
00110                                         elif cell[-3:] == "out":
00111                                                 portWay = 2
00112                                                 cell = cell[:-3]
00113                                         else:
00114                                                 raise "error : this is not a DEVICE PORT"
00115                                                 return
00116                                 
00117                                 if portWay == -1: # in the case have found a valid Port Number
00118                                         cell = cell[:(len(cell)-i)]
00119                                         if cell[-1:] in ['-','_']:
00120                                                 cell = cell[:-1] # we delete the underscore.
00121                                         if cell[-2:]=="in":
00122                                                 portWay = 1
00123                                                 cell = cell[:-2]
00124                                         elif cell[-3:] == "out":
00125                                                 portWay = 2
00126                                                 cell = cell[:-3]
00127                                         else:
00128                                                 print "Warinig: No port Way found for a DEVPORT"
00129                                                 portWay = 3 - lastWay
00130                                 # 2 - Now we have the devicename (cell), the port number (portNr) and the port Way (potWay).
00131                                 lastWay = portWay
00132                                 tmp_device = __addTo(Device('',cell),device_list) # adding the port to the list if not already done
00133                                 # update the ports number for our device
00134                                 if portWay == 1:        tmp_device.SetPortsIn(max(tmp_device.GetPortsIn(),portNr))
00135                                 else:   tmp_device.SetPortsOut(max(tmp_device.GetPortsOut(),portNr))
00136                                 
00137                                 # Create Port
00138                                 tmp_port = __addTo(Port('',portNr,"",portWay,cell), port_list)
00139                                 
00140                                 #Create The link:
00141                                 if index-1 >= 0 and lastPort:
00142                                    if LookIn(types,index,-1)== DEVICE: #types[index-1]==DEVICE:
00143                                         tmp_link = __CreateLink(lastPort,tmp_port)
00144                                         tmp_link = __addTo(tmp_link,link_list)
00145                                    if LookIn(types,index,-1)==DEVPORT: #types[index-1]==DEVPORT:
00146                                         tmp_link = __CreateLink(lastPort,tmp_port)
00147                                         tmp_link = __addTo(tmp_link,link_list)
00148                                    if LookIn(types,index,-1)== PORT:
00149                                         tmp_link = __CreateLink(lastPort,tmp_port)
00150                                         tmp_link = __addTo(tmp_link,link_list)
00151                                    if LookIn(types,index,-1)== LINK:
00152                                         tmp_link = __CreateLink(lastPort,tmp_port)
00153                                         tmp_link.SetLinkInfo(row[index-1])
00154                                         tmp_link = __addTo(tmp_link,link_list)
00155                                 
00156                         elif types[index]==PORT: #here we should create the Port then the Link if possible
00157                                 if LookIn(types,index,-1)!=DEVICE:  #types[index-1]!=DEVICE:
00158                                         print "error: No device correspond to a Port"
00159                                         return
00160                                 lastWay = 3 - lastWay
00161                                 tmp_port = Port('',cell,"",lastWay,row[index-1])
00162                                 tmp_port = __addTo(tmp_port, port_list)
00163                                 
00164                                 # Create the link
00165                                 if index-2>=0 and lastPort:
00166                                         if LookIn(types,index,-2)==LINK:
00167                                                 tmp_link = __CreateLink(lastPort,tmp_port)
00168                                                 tmp_link.SetLinkInfo(row[index-2])
00169                                                 tmp_link = __addTo(tmp_link,link_list)
00170                                         elif LookIn(types,index,-2)!=None:
00171                                                 tmp_link = __CreateLink(lastPort,tmp_port)
00172                                                 tmp_link = __addTo(tmp_link,link_list)                                  
00173                         elif types[index]==LINK:
00174                                 pass
00175 
00176                         if tmp_port:
00177                                 lastPort = tmp_port
00178                         index += 1
00179                 
00180         # except csv.Error, e:
00181             # sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e))
00182         finally:
00183                 print "########## DEVICE : ",device_list
00184                 print "########## PORT : ",port_list
00185                 print "########## LINK : ",link_list
00186         
00187         
00188 test = mainApp()
00189 test.ImportCSV(filename)
00190  
00191 

Generated on Fri Aug 31 11:11:15 2007 for CDBVis by  doxygen 1.5.3