PowCon_MLVDS_register_map_to_floatporter.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 = 'ICD_PowCon_MLVDS_Oct_28_2013.csv'
00028     xmlFile = 'FloatPorter_PowConPorter.cpf'
00029 
00030     try:
00031         opts, args = getopt.getopt(argv, "hi:o:",["input=","output="])
00032     except getopt.GetoptError:
00033         print("PowCon_MLVDS_register_map_to_floatporter.py -i <inputfile> -o <outputfile>")
00034         sys.exit(2)
00035     for opt, arg in opts:
00036         if opt == '-h':
00037             print 'PowCon_MLVDS_register_map_to_floatporter.py -i <inputfile> -o <outputfile>'
00038             sys.exit()
00039         elif opt in ("-i", "--input"):
00040             csvFile = arg
00041         elif opt in ("-o", "--output"):
00042             xmlFile = arg
00043 
00044     print("Opening " + csvFile)
00045     data = list( csv.reader(open(csvFile)) )
00046 
00047     print("Writing output to " + xmlFile)
00048     xmlData = open(xmlFile, 'w')
00049 
00050     xmlData.write('<?xml version="1.0" encoding="UTF-8"?>' + "\n")
00051     xmlData.write('<!DOCTYPE properties SYSTEM "cpf.dtd">' + "\n")
00052     xmlData.write('<properties>' + "\n")
00053     xmlData.write('    <struct name="EventsToActOn" type="string[]">' + "\n")
00054     xmlData.write('             <description>The names of the RobonetDataElement groups that can trigger this FloatPorter to read data.</description>' + "\n")
00055     xmlData.write('     </struct>' + "\n")
00056 
00057     if(len(data)>0):
00058         tags = data[0]
00059         # replace spaces w/ underscores in tag names
00060         for i in range(len(tags)):
00061             tags[i] = tags[i].strip().replace(' ', '_')
00062         if("name" in tags):
00063             iName = tags.index("name")
00064             print("found name tag at index: " + str(iName))
00065         else:
00066             print("can not find name tag")
00067             return    
00068         if("ROBONET_type" in tags):
00069             iDirection = tags.index("ROBONET_type")
00070             print("found ROBONET_type tag at index: " + str(iDirection))
00071         else:
00072             print("can not find ROBONET_type tag")
00073             return
00074         if("registers" in tags):
00075             iRegisters = tags.index("registers")
00076             print("found registers tag at index: " + str(iRegisters))
00077         else:
00078             print("can not find registers tag")
00079             return
00080     else:
00081         print("can not find header!")
00082         return
00083 
00084     # do read elements
00085     xmlData.write('     <struct name="ReadElementNames" type="string[]">' + "\n")
00086     xmlData.write('             <description>The names of the RobonetDataElements this Porter will read and publish.</description>' + "\n")
00087     rowNum = 0
00088     elementNum = 0
00089     for row in data:
00090         if rowNum == 0:
00091             elementNum = 0
00092         else:
00093             if(row[iName].strip().replace(' ', '_')):
00094                 #registers = parseRegisterDescription(row[iRegisters])
00095                 if(row[iDirection].lower() == "status"):
00096                     xmlData.write('             <simple name="Element' + str(elementNum) +'"   type="string"><value>r2/bat_pack/powcon/' + str(row[iName].strip().replace(' ', '_')) + '</value> </simple>\n')
00097                     elementNum += 1
00098         rowNum +=1
00099     xmlData.write('     </struct>' + "\n")
00100 
00101     # do write elements
00102     xmlData.write('    <struct name="WriteElementNames" type="string[]">' + "\n")
00103     xmlData.write('             <description>The names of the RobonetDataElements this Porter will write subscribed data to.</description>' + "\n")
00104     rowNum = 0
00105     elementNum = 0
00106     for row in data:
00107         if rowNum == 0:
00108             elementNum = 0
00109         else:
00110             if(row[iName].strip().replace(' ', '_')):
00111                 #registers = parseRegisterDescription(row[iRegisters])
00112                 if(row[iDirection].lower() == "control"):
00113                     xmlData.write('             <simple name="Element' + str(elementNum) +'"   type="string"><value>r2/bat_pack/powcon/' + str(row[iName].strip().replace(' ', '_')) + '</value> </simple>\n')
00114                     elementNum += 1
00115         rowNum +=1
00116     xmlData.write('     </struct>' + "\n")
00117 
00118     #finish the file
00119     xmlData.write('</properties>')
00120     xmlData.close()
00121 
00122 if __name__ == "__main__":
00123    main(sys.argv[1:])


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