BMS_MLVDS_register_map_to_token.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 
00006 def main(argv):
00007     csvFile = 'BMS_MLVDS_register_map_oct28.csv'
00008     xmlFile = 'BmsTokens.xml'
00009     try:
00010         opts, args = getopt.getopt(argv, "hi:o:",["input=", "output="])
00011     except getopt.GetoptError:
00012         print("BMS_MLVDS_register_map_to_token.py -i <inputfile> -o <outputfile>")
00013         sys.exit(2)
00014     for opt, arg in opts:
00015         if opt == '-h':
00016             print 'BMS_MLVDS_register_map_to_token.py -i <inputfile>'
00017             sys.exit()
00018         elif opt in ("-i", "--input"):
00019             csvFile = arg
00020         elif opt in ("-o", "--output"):
00021             xmlFile = arg
00022 
00023     print("Opening " + csvFile)
00024     data = csv.reader(open(csvFile))
00025 
00026     print("Writing to " + xmlFile)
00027     xmlData = open(xmlFile, 'w')
00028 
00029     xmlData.write('<?xml version="1.0" encoding="ISO-8859-1"?>' + "\n")
00030     xmlData.write('<TokenData>' + "\n")
00031     xmlData.write('    <Properties>' + "\n")
00032     xmlData.write('        <Node>BMS</Node>' + "\n")
00033     xmlData.write('        <Version>BatPack</Version>' + "\n")
00034     xmlData.write('    </Properties>' + "\n")
00035     xmlData.write('    <Tokens>' + "\n")
00036     rowNum = 0
00037     for row in data:
00038         if rowNum == 0:
00039             tags = row
00040             # replace spaces w/ underscores in tag names
00041             for i in range(len(tags)):
00042                 tags[i] = tags[i].strip().replace(' ', '_')
00043             if("addr_(hex)" in tags):
00044                 iHex = tags.index("addr_(hex)")
00045                 print("index of hex value: " + str(iHex))
00046             else:
00047                 break
00048             if("name" in tags):
00049                 iName = tags.index("name")
00050                 print("index of name: " + str(iName))
00051             else:
00052                 break            
00053         else:
00054             if(row[iName].strip().replace(' ', '_')):
00055                 xmlData.write('        <Token {0:50} {1:15} /> \n'.format('id="' + str(row[iName].strip().replace(' ', '_')) + '"', 'value="0x' + str(row[iHex]) + '"'))
00056         rowNum +=1
00057     xmlData.write('    </Tokens>' + "\n")
00058     xmlData.write('</TokenData>')
00059 
00060     xmlData.close()
00061 
00062 if __name__ == "__main__":
00063    main(sys.argv[1:])


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