1""" 2Convenience methods, used to get board and processor data from MEX files 3""" 4 5# MEX file has a default namespace, map it here 6NAMESPACES = {'mex' : 'http://mcuxpresso.nxp.com/XSD/mex_configuration_14'} 7 8import xml.etree.ElementTree as ET 9import logging 10 11 12class NxpMexUtil(): 13 14 def __init__(self, mexfile): 15 """ 16 Initialize instance of this utility class 17 @param mexfile: mex file to parse for board name 18 """ 19 self.mexfile = mexfile 20 21 22 def get_pins_version(self): 23 """ 24 Gets version of pins tool from MEX file. 25 @param self: instance of this class 26 @ret version as integer 27 """ 28 try: 29 config_tree = ET.parse(self.mexfile) 30 pins_version = config_tree.getroot().find('mex:tools/mex:pins', 31 NAMESPACES) 32 if pins_version is None: 33 return 0.0 34 return float(pins_version.get('version')) 35 except ET.ParseError: 36 logging.error("Malformed XML tree %s", self.mexfile) 37 return None 38 except IOError: 39 logging.error("File %s could not be opened", self.mexfile) 40 return None 41 42 43 def get_board_name(self): 44 """ 45 Extracts board name from a mex file 46 @param self: instance of this class 47 """ 48 try: 49 config_tree = ET.parse(self.mexfile) 50 if config_tree.getroot().find('mex:common/mex:board', NAMESPACES) is None: 51 return self.get_processor_name() + '-board' 52 return config_tree.getroot().find('mex:common/mex:board', 53 NAMESPACES).text 54 except ET.ParseError: 55 logging.error("Malformed XML tree %s", self.mexfile) 56 return None 57 except IOError: 58 logging.error("File %s could not be opened", self.mexfile) 59 return None 60 61 def get_processor_name(self): 62 """ 63 Extracts processor name from a mex file 64 @param self: instance of this class 65 """ 66 try: 67 config_tree = ET.parse(self.mexfile) 68 processor = config_tree.getroot().find('mex:common/mex:processor', 69 NAMESPACES) 70 if processor is None: 71 raise RuntimeError("Cannot locate processor name in MEX file") 72 return processor.text 73 except ET.ParseError: 74 logging.error("Malformed XML tree %s", self.mexfile) 75 return None 76 except IOError: 77 logging.error("File %s could not be opened", self.mexfile) 78 return None 79 80 def get_package_name(self): 81 """ 82 Extracts package name from a mex file 83 @param self.mexfile: mex file to parse for package name 84 @param self: instance of this class 85 """ 86 try: 87 config_tree = ET.parse(self.mexfile) 88 package = config_tree.getroot().find('mex:common/mex:package', 89 NAMESPACES) 90 if package is None: 91 raise RuntimeError("Cannot locate package name in MEX file") 92 return package.text 93 except ET.ParseError: 94 logging.error("Malformed XML tree %s", self.mexfile) 95 return None 96 except IOError: 97 logging.error("File %s could not be opened", self.mexfile) 98 return None 99