PowCon_MLVDS_register_map_to_numbergui.py
Go to the documentation of this file.
00001 # BMS_MLVDS_register_map_to_token.py
00002 # First row of the csv file must be header!
00003 import sys, getopt
00004 import csv
00005 import re
00006 
00007 def parseRegisterDescription(desc):
00008     #print('parsing ' + desc)
00009     regex = re.compile("([0-9a-zA-Z_]+)\(([0-9]+)\:([0-9]+)\)")
00010     registers = desc.split(' ')
00011     #print('splint into ' + str(len(registers)) + ' pieces')
00012     output = []
00013     for reg in registers:
00014         #print('finding regex match for ' + reg)
00015         try:
00016             match = regex.match(reg)
00017             mydict = {"id": match.group(1),
00018                       "size": int(match.group(2)) - int(match.group(3)) +1,
00019                       "index": int(match.group(3))}
00020             output.append(mydict)
00021         except AttributeError:
00022             #print('no match found')
00023             pass
00024     return output    
00025 
00026 def main(argv):
00027     csvFile = 'PowCon_MLVDS_register_map_oct28.csv'
00028     statusFile = 'NumberGuiConfigPowConStatus.xml'
00029     controlFile = 'NumberGuiConfigPowConControl.xml'
00030     try:
00031         opts, args = getopt.getopt(argv, "hi:c:s:",["input=", "control=", "status="])
00032     except getopt.GetoptError:
00033         print("PowCon_MLVDS_register_map_to_numbergui.py -i <inputfile> -c <controlfile> -s <statusfile>")
00034         sys.exit(2)
00035     for opt, arg in opts:
00036         if opt == '-h':
00037             print 'PowCon_MLVDS_register_map_to_numbergui.py -i <inputfile> -c <controlfile> -s <statusfile>'
00038             sys.exit()
00039         elif opt in ("-i", "--input"):
00040             csvFile = arg
00041         elif opt in ("-c", "--control"):
00042             controlFile = arg
00043         elif opt in ("-s", "--status"):
00044             statusFile = arg
00045 
00046     print("Opening " + csvFile)
00047     data = list( csv.reader(open(csvFile)) )
00048 
00049     if(len(data)>0):
00050         tags = data[0]
00051         # replace spaces w/ underscores in tag names
00052         for i in range(len(tags)):
00053             tags[i] = tags[i].strip().replace(' ', '_')
00054         if("name" in tags):
00055             iName = tags.index("name")
00056             print("found name tag at index: " + str(iName))
00057         else:
00058             print("can not find name tag")
00059             return    
00060         if("ROBONET_type" in tags):
00061             iDirection = tags.index("ROBONET_type")
00062             print("found ROBONET_type tag at index: " + str(iDirection))
00063         else:
00064             print("can not find ROBONET_type tag")
00065             return
00066         if("registers" in tags):
00067             iRegisters = tags.index("registers")
00068             print("found registers tag at index: " + str(iRegisters))
00069         else:
00070             print("can not find registers tag")
00071             return
00072     else:
00073         print("can not find header!")
00074         return
00075 
00076     # do status elements
00077     xmlFile = statusFile
00078     print("Writing output to " + xmlFile)
00079     xmlData = open(xmlFile, 'w')
00080 
00081     xmlData.write('<?xml version="1.0" encoding="ISO-8859-1"?>' + "\n")
00082     xmlData.write('<NumberGuiConfiguration>' + "\n")
00083     xmlData.write('    <Group id="r2/bat_pack/powcon">' + "\n")
00084     rowNum = 0
00085     for row in data:
00086         if rowNum == 0:
00087             pass
00088         else:
00089             if(row[iName].strip().replace(' ', '_')):
00090                 #registers = parseRegisterDescription(row[iRegisters])
00091                 if(row[iDirection].lower() == "status"):
00092                     xmlData.write('        <Element id="' + str(row[iName].strip().replace(' ', '_')) + '" type="float" />\n')
00093         rowNum +=1
00094     xmlData.write('    </Group>' + "\n")
00095     xmlData.write('</NumberGuiConfiguration>')
00096     xmlData.close()
00097 
00098     # do write elements
00099     xmlFile = controlFile
00100     print("Writing output to " + xmlFile)
00101     xmlData = open(xmlFile, 'w')
00102 
00103     xmlData.write('<?xml version="1.0" encoding="ISO-8859-1"?>' + "\n")
00104     xmlData.write('<NumberGuiConfiguration>' + "\n")
00105     xmlData.write('    <Group id="r2/bat_pack/powcon">' + "\n")
00106     rowNum = 0
00107     for row in data:
00108         if rowNum == 0:
00109             pass
00110         else:
00111             if(row[iName].strip().replace(' ', '_')):
00112                 #registers = parseRegisterDescription(row[iRegisters])
00113                 if(row[iDirection].lower() == "control"):
00114                     xmlData.write('        <Element id="' + str(row[iName].strip().replace(' ', '_')) + '" type="float" />\n')
00115         rowNum +=1
00116     xmlData.write('    </Group>' + "\n")
00117     xmlData.write('</NumberGuiConfiguration>')
00118     xmlData.close()
00119 
00120 if __name__ == "__main__":
00121    main(sys.argv[1:])


robot_instance
Author(s):
autogenerated on Sat Jun 8 2019 20:43:12