plugins/canfestival/config_utils.py
changeset 35 4d7861fc34d4
parent 34 2721e6910f5a
equal deleted inserted replaced
34:2721e6910f5a 35:4d7861fc34d4
   319         @param locations: list of locations to be mapped
   319         @param locations: list of locations to be mapped
   320         @param current_location: tuple of the located prefixes not to be considered
   320         @param current_location: tuple of the located prefixes not to be considered
   321         @param sync_TPDOs: indicate if TPDO must be synchronous
   321         @param sync_TPDOs: indicate if TPDO must be synchronous
   322         """
   322         """
   323         
   323         
       
   324         #-------------------------------------------------------------------------------
       
   325         #               Verify that locations correspond to real slave variables
       
   326         #-------------------------------------------------------------------------------
       
   327         
   324         # Get list of locations check if exists and mappables -> put them in IECLocations
   328         # Get list of locations check if exists and mappables -> put them in IECLocations
   325         for location in locations:
   329         for location in locations:
   326             COlocationtype = IECToCOType[location["IEC_TYPE"]]
   330             COlocationtype = IECToCOType[location["IEC_TYPE"]]
   327             name = location["NAME"]
   331             name = location["NAME"]
   328             if name in self.IECLocations:
   332             if name in self.IECLocations:
   351                 
   355                 
   352                 # Extract and check index and subindex
   356                 # Extract and check index and subindex
   353                 if not node.IsEntry(index, subindex):
   357                 if not node.IsEntry(index, subindex):
   354                     raise ValueError, "No such index/subindex (%x,%x) in ID : %d (variable %s)" % (index,subindex,nodeid,name)
   358                     raise ValueError, "No such index/subindex (%x,%x) in ID : %d (variable %s)" % (index,subindex,nodeid,name)
   355                 
   359                 
   356                 #Get the entry info
   360                 # Get the entry info
   357                 subentry_infos = node.GetSubentryInfos(index, subindex)
   361                 subentry_infos = node.GetSubentryInfos(index, subindex)
   358                 
   362                 
   359                 # If a PDO mappable
   363                 # If a PDO mappable
   360                 if subentry_infos and subentry_infos["pdo"]:
   364                 if subentry_infos and subentry_infos["pdo"]:
   361                     if sizelocation == "X" and len(loc) > 3:
   365                     if sizelocation == "X" and len(loc) > 3:
   569     dcfgenerator = ConciseDCFGenerator(nodelist)
   573     dcfgenerator = ConciseDCFGenerator(nodelist)
   570     dcfgenerator.GenerateDCF(locations, current_location, sync_TPDOs)
   574     dcfgenerator.GenerateDCF(locations, current_location, sync_TPDOs)
   571     return dcfgenerator.GetMasterNode()
   575     return dcfgenerator.GetMasterNode()
   572 
   576 
   573 if __name__ == "__main__":
   577 if __name__ == "__main__":
   574     import os, sys
   578     import os, sys, getopt
   575 
   579 
       
   580     def usage():
       
   581         print """
       
   582 Usage of config_utils.py test :
       
   583 
       
   584     %s [options]
       
   585 
       
   586 Options:
       
   587     --help  (-h)
       
   588             Displays help informations for config_utils
       
   589 
       
   590     --reset (-r)
       
   591             Reset the reference result of config_utils test.
       
   592             Use with caution. Be sure that config_utils
       
   593             is currently working properly.
       
   594 """%sys.argv[0]
       
   595     
       
   596     # Boolean that indicate if reference result must be redefined
       
   597     reset = False
       
   598 
       
   599     # Extract command options
       
   600     try:
       
   601         opts, args = getopt.getopt(sys.argv[1:], "hr", ["help","reset"])
       
   602     except getopt.GetoptError:
       
   603         # print help information and exit:
       
   604         usage()
       
   605         sys.exit(2)
       
   606 
       
   607     # Test each option
       
   608     for o, a in opts:
       
   609         if o in ("-h", "--help"):
       
   610             usage()
       
   611             sys.exit()
       
   612         elif o in ("-r", "--reset"):
       
   613             reset = True
       
   614 
       
   615     # Extract workspace base folder
   576     base_folder = sys.path[0]
   616     base_folder = sys.path[0]
   577     for i in xrange(3):
   617     for i in xrange(3):
   578         base_folder = os.path.split(base_folder)[0]
   618         base_folder = os.path.split(base_folder)[0]
       
   619     # Add CanFestival folder to search pathes
   579     sys.path.append(os.path.join(base_folder, "CanFestival-3", "objdictgen"))
   620     sys.path.append(os.path.join(base_folder, "CanFestival-3", "objdictgen"))
   580     
   621     
   581     from nodemanager import *
   622     from nodemanager import *
   582     from nodelist import *
   623     from nodelist import *
   583     
   624     
       
   625     # Open the test nodelist contained into test_config folder
   584     manager = NodeManager()
   626     manager = NodeManager()
   585     nodelist = NodeList(manager)
   627     nodelist = NodeList(manager)
   586     result = nodelist.LoadProject("test_config")
   628     result = nodelist.LoadProject("test_config")
   587     
   629     
       
   630     # List of locations, we try to map for test
   588     locations = [{"IEC_TYPE":"BYTE","NAME":"__IB0_1_64_24576_1","DIR":"I","SIZE":"B","LOC":(0,1,64,24576,1)},
   631     locations = [{"IEC_TYPE":"BYTE","NAME":"__IB0_1_64_24576_1","DIR":"I","SIZE":"B","LOC":(0,1,64,24576,1)},
   589                  {"IEC_TYPE":"INT","NAME":"__IW0_1_64_25601_2","DIR":"I","SIZE":"W","LOC":(0,1,64,25601,2)},
   632                  {"IEC_TYPE":"INT","NAME":"__IW0_1_64_25601_2","DIR":"I","SIZE":"W","LOC":(0,1,64,25601,2)},
   590                  {"IEC_TYPE":"INT","NAME":"__IW0_1_64_25601_3","DIR":"I","SIZE":"W","LOC":(0,1,64,25601,3)},
   633                  {"IEC_TYPE":"INT","NAME":"__IW0_1_64_25601_3","DIR":"I","SIZE":"W","LOC":(0,1,64,25601,3)},
   591                  {"IEC_TYPE":"INT","NAME":"__QW0_1_64_25617_2","DIR":"Q","SIZE":"W","LOC":(0,1,64,25617,1)},
   634                  {"IEC_TYPE":"INT","NAME":"__QW0_1_64_25617_2","DIR":"Q","SIZE":"W","LOC":(0,1,64,25617,1)},
   592                  {"IEC_TYPE":"BYTE","NAME":"__IB0_1_64_24578_1","DIR":"I","SIZE":"B","LOC":(0,1,64,24578,1)},
   635                  {"IEC_TYPE":"BYTE","NAME":"__IB0_1_64_24578_1","DIR":"I","SIZE":"B","LOC":(0,1,64,24578,1)},
   593                  {"IEC_TYPE":"UDINT","NAME":"__ID0_1_64_25638_1","DIR":"I","SIZE":"D","LOC":(0,1,64,25638,1)},
   636                  {"IEC_TYPE":"UDINT","NAME":"__ID0_1_64_25638_1","DIR":"I","SIZE":"D","LOC":(0,1,64,25638,1)},
   594                  {"IEC_TYPE":"UDINT","NAME":"__ID0_1_64_25638_2","DIR":"I","SIZE":"D","LOC":(0,1,64,25638,2)},
   637                  {"IEC_TYPE":"UDINT","NAME":"__ID0_1_64_25638_2","DIR":"I","SIZE":"D","LOC":(0,1,64,25638,2)},
   595                  {"IEC_TYPE":"UDINT","NAME":"__ID0_1_64_25638_3","DIR":"I","SIZE":"D","LOC":(0,1,64,25638,3)},
   638                  {"IEC_TYPE":"UDINT","NAME":"__ID0_1_64_25638_3","DIR":"I","SIZE":"D","LOC":(0,1,64,25638,3)},
   596                  {"IEC_TYPE":"UDINT","NAME":"__ID0_1_64_25638_4","DIR":"I","SIZE":"D","LOC":(0,1,64,25638,4)}]
   639                  {"IEC_TYPE":"UDINT","NAME":"__ID0_1_64_25638_4","DIR":"I","SIZE":"D","LOC":(0,1,64,25638,4)}]
   597     
   640     
   598     masternode = GenerateConciseDCF(locations, (0, 1), nodelist, True)
   641     # Generate MasterNode configuration
   599     #masternode.Print()
   642     try:
   600     result = [line.rstrip() for line in masternode.PrintString().splitlines()]
   643         masternode = GenerateConciseDCF(locations, (0, 1), nodelist, True)
   601     
   644     except ValueError, message:
   602     file = open("test_config/result.txt", "r")
   645         print "%s\nTest Failed!"%message
   603     model = [line.rstrip() for line in file.readlines()]
   646         sys.exit()
   604     file.close()
   647     
   605     
   648     # Get Text corresponding to MasterNode 
   606     errors = 0
   649     result = masternode.PrintString()
   607     for i, line in enumerate(model):
   650     
   608         if i >= len(result):
   651     # If reset has been choosen
       
   652     if reset:
       
   653         # Write Text into reference result file
       
   654         file = open("test_config/result.txt", "w")
       
   655         file.write(result)
       
   656         file.close()
       
   657         
       
   658         print "Reset Successful!"
       
   659     else:
       
   660         # Test each line of the result with the reference result
       
   661         test = [line.rstrip() for line in result.splitlines()]
       
   662         
       
   663         file = open("test_config/result.txt", "r")
       
   664         model = [line.rstrip() for line in file.readlines() if line.rstrip()]
       
   665         file.close()
       
   666         
       
   667         errors = 0
       
   668         for i, line in enumerate(model):
       
   669             if i >= len(test):
       
   670                 errors += 1
       
   671                 print "Line %d disappear :\n%s\n"%(i + 1, line)
       
   672             elif line != test[i]:
       
   673                 errors += 1
       
   674                 print "Error on line %d :\n%s\nInstead of :\n%s\n"%(i + 1, test[i], line)
       
   675         for i in xrange(len(model), len(test)):
   609             errors += 1
   676             errors += 1
   610             print "Line %d disappear :"%(i + 1)
   677             print "Line %d appear :\n%s\n"%(i + 1, test[i])
   611             print line
   678         
   612         elif line != result[i]:
   679         if errors > 0:
   613             errors += 1
   680             print "%d errors found.\nTest Failed!"%errors
   614             print "Error on line %d :"%(i + 1)
   681         else:
   615             print "\t%s"%result[i]
   682             print "Test Successful!"
   616             print "Instead of :\n\t%s"%line
       
   617     for i in xrange(len(model), len(result)):
       
   618         errors += 1
       
   619         print "Line %d appear :"%(i + 1)
       
   620         print result[i]
       
   621     
       
   622     if errors > 0:
       
   623         print "Test Failed!"
       
   624     else:
       
   625         print "Test Successful!"