Package TestON :: Package bin :: Module updatedriver
[hide private]
[frames] | no frames]

Source Code for Module TestON.bin.updatedriver

  1  import inspect 
  2  import sys 
  3  import os 
  4  import re  
  5  sys.path.append("../") 
  6  from core import xmldict 
  7   
8 -class UpdateDriver:
9 - def __init__(self):
10 self.default = '' 11 self.configFile = "/home/openflow/TestON/config/ofadriver.cfg" 12 self.methodDict = {} 13 self.fileDict = {}
14 15
16 - def getmethods(self,modulePath,Class) :
17 ''' 18 This will get the list of methods in given module or class. 19 It accepts the module path and class name. If there is no 20 class name then it has be mentioned as None. 21 ''' 22 methodList = [] 23 moduleList = modulePath.split("/") 24 newModule = ".".join([moduleList[len(moduleList) - 2],moduleList[len(moduleList) - 1]]) 25 print "Message : Method list is being obatined , Please wait ..." 26 try : 27 if Class : 28 Module = __import__(moduleList[len(moduleList) - 1], globals(), locals(), [Class], -1) 29 ClassList = [x.__name__ for x in Module.__dict__.values() if inspect.isclass(x)] 30 self.ClassList = ClassList 31 Class = vars(Module)[Class] 32 methodList = [x.__name__ for x in Class.__dict__.values() if inspect.isfunction(x)] 33 else : 34 Module = __import__(moduleList[len(moduleList) - 1], globals(), locals(),[moduleList[len(moduleList) - 2]], -1) 35 methodList = [x.__name__ for x in Module.__dict__.values() if inspect.isfunction(x)] 36 ClassList = [x.__name__ for x in Module.__dict__.values() if inspect.isclass(x)] 37 self.ClassList = ClassList 38 except : 39 print "Error : " +str(sys.exc_info()[1]) 40 41 42 self.method = methodList 43 return self.method
44
45 - def echo(self) :
46 print "Echoing !!!!!!"
47
48 - def getargs(self,moduleName,className,method) :
49 ''' 50 This will return the list of arguments in a method of python module of class. 51 It accepts method list as an argument. 52 ''' 53 print "Message : Argument list is being obtained for each method" 54 methodArgsDict = {} 55 if className == None: 56 moduleList = moduleName.split(".") 57 for index,name in enumerate(method) : 58 Module = __import__(moduleList[len(moduleList) -1], globals(), locals(), [moduleList[len(moduleList) -2]], -1) 59 try : 60 names = vars(Module)[name] 61 except KeyError: 62 print "Message : method '" + name + "'does not exists,Continued with including it. " 63 return False 64 argumentList = inspect.getargspec(names) #inspect.getargvalues(name) 65 methodArgsDict[name] = argumentList[0] 66 else : 67 moduleList = moduleName.split(".") 68 for index,name in enumerate(method) : 69 Module = __import__(moduleList[len(moduleList) - 1], globals(), locals(), [className], -1) 70 Class = getattr(Module, className) 71 try : 72 names = vars(Class)[name] 73 except KeyError : 74 print "Message : method '" + name + "'does not exists,Continued with include it." 75 return False 76 77 argumentList = inspect.getargspec(names) #inspect.getargvalues(name) 78 methodArgsDict[name] = argumentList[0] 79 80 return methodArgsDict
81
82 - def configparser(self,fileName):
83 ''' 84 It will parse the config file (ofa.cfg) and return as dictionary 85 ''' 86 87 matchFileName = re.match(r'(.*)\.cfg', fileName, re.M | re.I) 88 if matchFileName: 89 self.configFile = fileName 90 try : 91 xml = open(fileName).read() 92 self.configDict = xmldict.xml_to_dict(xml) 93 return self.configDict 94 except : 95 print "Error : Config file " + self.configFile + " not defined properly or file path error"
96 97
98 - def getList(self):
99 ''' 100 This method will maintain the hash with module->class->methodList or 101 module -> methodList .It will return the same Hash. 102 ''' 103 classList = [] 104 try : 105 moduleList = self.configDict['config-driver']['importTypes'][self.driver]['modules'].keys() 106 except KeyError,e: 107 print "Error : Module Does not Exists" 108 print e 109 return False 110 111 for index,value in enumerate(moduleList): 112 modulePath = self.configDict['config-driver']['importTypes'][self.driver]['modules'][value]['path'] 113 moduleName = self.configDict['config-driver']['importTypes'][self.driver]['modules'][value]['name'] 114 115 try : 116 pathList = self.configDict['config-driver']['importTypes'][self.driver]['modules'][value]['set-path'].split(",") 117 sys.path.extend(pathList) 118 except KeyError : 119 print "Error : No System Path is given " 120 pass 121 try : 122 Class = self.configDict['config-driver']['importTypes'][self.driver]['modules'][value]['classes'] 123 except : 124 Class = None 125 if Class == None : 126 methodList = self.getmethods(modulePath,None) 127 self.methodDict[moduleName] = methodList 128 self.method_ignoreList(value,None) 129 self.getMethodArgsHash(moduleName,value,None) 130 else : 131 classList = self.configDict['config-driver']['importTypes'][self.driver]['modules'][value]['classes'].keys() 132 for indx,className in enumerate(classList): 133 if className == 'ignore-list' : 134 pass 135 else : 136 methodList = self.getmethods(modulePath,className) 137 self.methodDict[moduleName] = {className : methodList} 138 self.method_ignoreList(value,className) 139 self.class_ignoreList(value) 140 self.getMethodArgsHash(moduleName,value,className)
141
142 - def class_ignoreList(self,module) :
143 ''' 144 It removes the ignored classes for each module mention in ofadriver.cfg 145 ''' 146 class_ignoreList = [] 147 if self.configDict['config-driver']['importTypes'][self.driver]['modules'][module]['classes'] == None : 148 pass 149 else : 150 try : 151 class_ignoreList = str(self.configDict['config-driver']['importTypes'][self.driver]['modules'][module]['classes']['ignore-list']).split(",") 152 except KeyError : 153 print "Message : No Class Ignore List present" 154 return True 155 moduleName = self.configDict['config-driver']['importTypes'][self.driver]['modules'][module]['name'] 156 try : 157 for index,className in enumerate(class_ignoreList): 158 if className in self.methodDict[moduleName].keys(): 159 del self.methodDict[moduleName][className] 160 except AttributeError: 161 pass 162 return self.methodDict
163
164 - def method_ignoreList(self,module,className):
165 ''' 166 It removes the ignored methods of each module or class mentioned in ofadriver.cfg. 167 ''' 168 method_ignoreList = [] 169 170 try : 171 if className == None : 172 try : 173 method_ignoreList = str(self.configDict['config-driver']['importTypes'][self.driver]['modules'][module]['methods']['ignore-list']).split(",") 174 except TypeError : 175 pass 176 else : 177 try : 178 method_ignoreList = str(self.configDict['config-driver']['importTypes'][self.driver]['modules'][module]['classes'][className]['methods']['ignore-list']).split(",") 179 except TypeError : 180 pass 181 except KeyError : 182 print "Message : No Ignore-List Exists , proceeding for looking add method" 183 self.add_method(module,className) 184 return True 185 186 moduleName = self.configDict['config-driver']['importTypes'][self.driver]['modules'][module]['name'] 187 #import pprint 188 #pprint.pprint(self.methodDict[moduleName]) 189 for index, method in enumerate(method_ignoreList) : 190 if className == None : 191 try : 192 self.methodDict[moduleName].remove(method) 193 #pprint.pprint(self.methodDict) 194 except ValueError: 195 print "Message : Method " + method + "Does not exist in module " + moduleName + ", Continue to rest execution" 196 pass 197 198 else : 199 if method in self.methodDict[moduleName][className] : 200 self.methodDict[moduleName][className].remove(method) 201 self.add_method(module,className) 202 return self.methodDict
203
204 - def add_method(self,module,className) :
205 ''' 206 This will add the methods(mentioned in ofadriver.cfg file) into method list if it doesnot exists in list. 207 ''' 208 method_List = [] 209 try : 210 if className == None : 211 try : 212 method_List = str(self.configDict['config-driver']['importTypes'][self.driver]['modules'][module]['methods']['add-list']).split(",") 213 except TypeError : 214 pass 215 else : 216 try : 217 method_List = str(self.configDict['config-driver']['importTypes'][self.driver]['modules'][module]['classes'][className]['methods']['add-list']).split(",") 218 except TypeError : 219 pass 220 221 except KeyError : 222 print "Message : No Add-List Exists , Proceeding with all available methods" 223 return True 224 moduleName = self.configDict['config-driver']['importTypes'][self.driver]['modules'][module]['name'] 225 for index, method in enumerate(method_List) : 226 if className == None : 227 self.methodDict[moduleName] = [] 228 self.methodDict[moduleName].append(method) 229 else : 230 self.methodDict[moduleName][className] = [] 231 self.methodDict[moduleName][className].append(method)
232
233 - def getMethodArgsHash(self,moduleName,module,className):
234 ''' 235 This will maintain a Hash of class->method->argumentsList 236 ''' 237 modulePath = self.configDict['config-driver']['importTypes'][self.driver]['modules'][module]['path'] 238 moduleList = modulePath.split("/") 239 newModule = ".".join([moduleList[len(moduleList) - 2],moduleList[len(moduleList) - 1]]) 240 if className == None : 241 methodArgs = self.getargs(newModule,None,self.methodDict[moduleName]) 242 self.fileDict[moduleName] = methodArgs 243 else : 244 methodArgs = self.getargs(newModule,className,self.methodDict[moduleName][className]) 245 self.fileDict[className] = methodArgs 246 return self.fileDict
247
248 - def appendDriver(self,fileName):
249 ''' 250 This will append the given driver file with methods along with arguments. 251 ''' 252 matchFileName = re.match(r'(.*)\.py', fileName, re.M | re.I) 253 254 if matchFileName: 255 fileHandle = None 256 try : 257 print "Message : Writing Driver file at " + fileName 258 fileHandle = open(fileName,"a") 259 content = '' 260 261 for index, key in enumerate(self.fileDict.keys()): 262 try : 263 for ind, method in enumerate(self.fileDict[key].keys()): 264 if not method == "__init__" : 265 args = '' 266 args = ",".join(self.fileDict[key][method]) 267 content = content + "\n" + " " * 4 + "def " + method + "(self," + args + ") :" 268 content = content + "\n" + " " * 8 + "return " + key + "." + method + "(" + args + ")\n" 269 except AttributeError : 270 pass 271 fileHandle.write(content) 272 fileHandle.close() 273 return content 274 275 except : 276 print "Error : Driver file " + fileName + "does not exists" 277 else : 278 print "Error : File name " + fileName + "is not python module" 279 return False
280 281
282 - def writeDriver(self, driver) :
283 ''' 284 This will accept the List of driver name and write those drivers if no driver name is specified 285 then it will write all of the driver specified in the ofadriver.cfg. 286 ''' 287 self.printHeader(driver) 288 drivers = [] 289 commaMatch = re.search(",", driver, flags=0) 290 if commaMatch: 291 drivers = driver.split(",") 292 else : 293 drivers.append(driver) 294 self.driverList = [] 295 if len(drivers) == 0: 296 for index, driverName in enumerate(self.configDict['config-driver']['importTypes'].keys()): 297 self.driver = driverName 298 result = self.getList() 299 if result : 300 self.getDriverPath() 301 self.appendDriver(self.driverPath + self.driver + ".py") 302 self.driverList.append(self.driverPath + self.driver + ".py") 303 else : 304 return False 305 else : 306 for index, driverName in enumerate(drivers) : 307 308 self.driver = driverName 309 result = self.getList() 310 if result : 311 self.getDriverPath() 312 self.appendDriver(self.driverPath + self.driver + ".py") 313 self.driverList.append(self.driverPath + self.driver + ".py") 314 else : 315 return False 316 317 print "=" * 90 318 print " " * 30 + "Output Driver File :" 319 print ",\n".join(self.driverList) 320 print "=" * 90 321 return True
322 323 324
325 - def getDriverPath(self):
326 ''' 327 It will set the driver path and returns it.If driver path is not specified then it will take 328 default path (/lib/updatedriver/). 329 ''' 330 self.driverPath = '' 331 try : 332 self.driverPath = self.configDict['config-driver']['importTypes'][self.driver]['driver-path'] 333 334 except KeyError : 335 path = re.sub("(bin)$", "", os.getcwd()) 336 self.driverPath = path + "/lib/updatedriver/" 337 return self.driverPath
338 339
340 - def printHeader(self,driver):
341 content = '' 342 343 print " " * 10 +"=" * 90 + "\n" 344 content = content + " " * 30 + "*-- Welcome to Updated Driver --*\n" 345 content = content + "\n" + " " * 10 + " " * 10 + "Config File : " + "/home/openflow/TestON/config/ofadriver.py" 346 content = content + "\n" + " " * 10 + " " * 10 + "Drivers Name : " + driver 347 print content 348 print " " * 10 + "=" * 90
349