Go to the documentation of this file.00001
00002
00003 import sys, getopt
00004 import csv
00005 import re
00006
00007 def parseRegisterDescription(desc):
00008
00009 regex = re.compile("([0-9a-zA-Z_]+)\(([0-9]+)\:([0-9]+)\)")
00010 registers = desc.split(' ')
00011
00012 output = []
00013 for reg in registers:
00014
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
00023 pass
00024 return output
00025
00026 def main(argv):
00027 csvFile = 'BMS_MLVDS_register_map_oct28.csv'
00028 xmlFile = 'FloatPorter_BmsPorter.cpf'
00029 try:
00030 opts, args = getopt.getopt(argv, "hi:o:",["input=","output="])
00031 except getopt.GetoptError:
00032 print("BMS_MLVDS_register_map_to_floatporter.py -i <inputfile> -o <outputfile>")
00033 sys.exit(2)
00034 for opt, arg in opts:
00035 if opt == '-h':
00036 print 'BMS_MLVDS_register_map_to_floatporter.py -i <inputfile> -o <outputfile>'
00037 sys.exit()
00038 elif opt in ("-i", "--input"):
00039 csvFile = arg
00040 elif opt in ("-o", "--output"):
00041 xmlFile = arg
00042
00043 print("Opening " + csvFile)
00044 data = list( csv.reader(open(csvFile)) )
00045
00046 print("Writing to " + xmlFile)
00047 xmlData = open(xmlFile, 'w')
00048
00049 xmlData.write('<?xml version="1.0" encoding="UTF-8"?>' + "\n")
00050 xmlData.write('<!DOCTYPE properties SYSTEM "cpf.dtd">' + "\n")
00051 xmlData.write('<properties>' + "\n")
00052 xmlData.write(' <struct name="EventsToActOn" type="string[]">' + "\n")
00053 xmlData.write(' <description>The names of the RobonetDataElement groups that can trigger this FloatPorter to read data.</description>' + "\n")
00054 xmlData.write(' </struct>' + "\n")
00055 rowNum = 0
00056 elementNum = 0
00057 if(len(data)>0):
00058 tags = data[0]
00059
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
00081
00082 xmlData.write(' <struct name="ReadElementNames" type="string[]">' + "\n")
00083 xmlData.write(' <description>The names of the RobonetDataElements this Porter will read and publish.</description>' + "\n")
00084 rowNum = 0
00085 for row in data:
00086 if rowNum == 0:
00087 elementNum = 0
00088 else:
00089 if(row[iName].strip().replace(' ', '_')):
00090 if(row[iDirection].lower() == "status"):
00091 xmlData.write(' <simple name="Element' + str(elementNum) +'" type="string"><value>r2/bat_pack/bms/' + str(row[iName].strip().replace(' ', '_')) + '</value> </simple>\n')
00092 elementNum += 1
00093 rowNum +=1
00094 xmlData.write(' </struct>' + "\n")
00095
00096
00097 xmlData.write(' <struct name="WriteElementNames" type="string[]">' + "\n")
00098 xmlData.write(' <description>The names of the RobonetDataElements this Porter will write subscribed data to.</description>' + "\n")
00099 rowNum = 0
00100 for row in data:
00101 if rowNum == 0:
00102 elementNum = 0
00103 else:
00104 if(row[iName].strip().replace(' ', '_')):
00105 if(row[iDirection].lower() == "control"):
00106 xmlData.write(' <simple name="Element' + str(elementNum) +'" type="string"><value>r2/bat_pack/bms/' + str(row[iName].strip().replace(' ', '_')) + '</value> </simple>\n')
00107 elementNum += 1
00108 rowNum +=1
00109 xmlData.write(' </struct>' + "\n")
00110
00111
00112 xmlData.write('</properties>')
00113 xmlData.close()
00114
00115 if __name__ == "__main__":
00116 main(sys.argv[1:])