def importMsg( self, msgFile ): " import device msg format setting" _f = XmlParser() _f .loadXmlFile( msgFile ) #获得所有Msg节点 _msgNdoe = _f.getAllElementByName( self.importFileParser['msg']['id']['path'] ) for _mn in _msgNdoe: #获得msg节点的所有属性 _ma = _f.getAttrListOneNode( _mn, self.importFileParser['msg']['id']['attr'] ) #msg type(msg id)做为key self.__msg[int( _ma[1] )] = {} #消息类型 self.__msg[int( _ma[1] )]['name'] = _ma[0] #消息打包格式头 self.__msg[int( _ma[1] )]['format'] = _ma[2] #获得该msg下的所有Item的属性 _ia = [_f.getAttrListOneNode( _item, self.importFileParser['msg']['item']['attr'] ) \ for _item in _f.getNodeListInNode( _mn, self.importFileParser['msg']['item']['path'] )] for _ii in _ia: #消息格式 self.__msg[int( _ma[1] )]['format'] += _ii[1] #消息子项的偏移 self.__msg[int( _ma[1] )][_ii[2]] = int( _ii[0] ) #消息的长度 self.__msg[int( _ma[1] )]['len'] = struct.calcsize( self.__msg[int( _ma[1] )]['format'] )
def _test_xml_text_parents(self): print("_test_xml_text_parents start") parser = XmlParser(io.StringIO("<root>Hello, world!</root>")) parser.next_token() token = parser.next_token() self.assertEqual(len(token.get_parent_tags()), 1) self.assertEqual(token.get_parent_tags()[0], "root") print("_test_xml_text_parents end")
def _test_xml_pure_text(self): print("_test_xml_pure_text start") parser = XmlParser(io.StringIO("hello world")) had_error = False try: parser.next_token() except XmlParserInvalidSyntaxError: had_error = True self.assertTrue(had_error, msg="verify the parser has aborted " + 'parsing "hello world" as XML') print("_test_xml_pure_text end")
def _test_xml_attributes_unfinished(self): print("_test_xml_attributes_unfinished start") parser = XmlParser(io.StringIO("<root><child myattr=/></root>")) self.assertTrue(parser.next_token().get_tag() == "root") had_error = False try: parser.next_token() except XmlParserInvalidSyntaxError: had_error = True self.assertTrue( had_error, msg="verify the parser has aborted " + "due to unfinished attribute (= character but no value)" ) print("_test_xml_attributes_unfinished end")
def _test_xml_questionmark_invalid_followup(self): print("_test_xml_questionmark_invalid_followup start") parser = XmlParser(io.StringIO('<?xml version="1.0"?test><root></root>')) had_error = False try: parser.next_token() except XmlParserInvalidSyntaxError: had_error = True self.assertTrue( had_error, msg="verify the parser has aborted " + "after encountering invalid followup data after closing '?'", ) print("_test_xml_questionmark_invalid_followup end")
def _test_xml_unfinished(self): print("_test_xml_unfinished start") parser = XmlParser(io.StringIO("<root><child/>")) self.assertTrue(parser.next_token().get_tag() == "root") self.assertTrue(parser.next_token().get_tag() == "child") self.assertTrue(parser.next_token().get_tag() == "child") had_error = False try: token = parser.next_token() print("TOKEN: " + str(token)) except XmlParserInvalidSyntaxError: had_error = True self.assertTrue(had_error, msg="verify the parser has aborted " + "due to unfinished XML") print("_test_xml_unfinished end")
def _test_xml_selfclosing_invalid_followup(self): print("_test_xml_selfclosing_invalid_followup start") parser = XmlParser(io.StringIO("<root><child/test></root>")) parser.next_token() had_error = False try: parser.next_token() except XmlParserInvalidSyntaxError: had_error = True self.assertTrue( had_error, msg="verify the parser has aborted " + "after encountering invalid followup data after closing '/'", ) print("_test_xml_selfclosing_invalid_followup end")
def _test_xml_attributes_malformed(self): print("_test_xml_attributes_malformed start") parser = XmlParser(io.StringIO('<root><child ""/></root>')) had_error = False root_begin = parser.next_token() self.assertEqual(root_begin.get_type(), "begin_tag") self.assertEqual(root_begin.get_tag(), "root") try: token = parser.next_token() print("token: " + str(token)) except XmlParserInvalidSyntaxError: had_error = True self.assertTrue(had_error, msg="verify the parser has aborted " + "due to malformed attribute") print("_test_xml_attributes_malformed end")
def importSmartTramInfo(cls, path): "import smartTram infomation." _f = XmlParser() _f .loadXmlFile( path ) _Mode = _f.getAttrListOneElement( cls.smartTramParser['smartTramInfo']['Mode']['path'], \ cls.smartTramParser['smartTramInfo']['Mode']['attr'] )[0] _stop_time = int( _f.getAttrListOneElement( cls.smartTramParser['smartTramInfo']['Stop_time']['path'], \ cls.smartTramParser['smartTramInfo']['Stop_time']['attr'] )[0] ) _Cog_dir = float( _f.getAttrListOneElement( cls.smartTramParser['smartTramInfo']['Cog_dir']['path'], \ cls.smartTramParser['smartTramInfo']['Cog_dir']['attr'] )[0] ) _accel = _f.getAttrListOneElement( cls.smartTramParser['smartTramInfo']['accel']['path'], \ cls.smartTramParser['smartTramInfo']['accel']['attr'] ) _accelV = [int( _a ) for _a in _accel] _V_max = int(_f.getAttrListOneElement( cls.smartTramParser['smartTramInfo']['V_max']['path'], \ cls.smartTramParser['smartTramInfo']['V_max']['attr'] )[0]) _timer = _f.getAttrListOneElement( cls.smartTramParser['smartTramInfo']['timer']['path'], \ cls.smartTramParser['smartTramInfo']['timer']['attr'] ) _timerV = [float( _t ) for _t in _timer] _radio = _f.getAttrListOneElement( cls.smartTramParser['smartTramInfo']['radio']['path'], \ cls.smartTramParser['smartTramInfo']['radio']['attr'] ) _radioV = [float( _r) for _r in _radio] return _Mode,_stop_time,_Cog_dir,_accelV,_V_max,_timerV,_radioV
def drawTest(): try: externalFilesPath = os.path.join(os.path.realpath(os.getcwd()), '..', 'external-files') diagramFileName = 'file_output_test_pattern.xml' referenceFileName = 'reference_diagram.xml' if os.path.exists(os.path.join(externalFilesPath, diagramFileName)): os.remove(os.path.join(externalFilesPath, diagramFileName)) #Open draw.io, create a new diagram, put things in it, and save the file webdriver = openDrawIo(externalFilesPath) createDiagram(webdriver, diagramFileName) createTestImage(webdriver) downloadDiagram(webdriver) #Use a decoder to compare the contents of the saved diagram to a known-good reference xmlParser = XmlParser() compressedData = extractXmlFromDiagramFile(xmlParser, externalFilesPath, diagramFileName) decompressedData = inflateXml(webdriver, compressedData) compareDataToReference(xmlParser, decompressedData, externalFilesPath, referenceFileName) print('draw.io saved data test passed!') finally: webdriver.quit()
def temp_save_machine(self): full_path = os.path.join(self.temp_path, self.curr_machine_name) io_success = XmlParser.save_machine(full_path, self.tm_gui, self.tape_gui) if io_success: return full_path return None
def load_machine(self, file_path, file_name): if len(file_name) != 0: full_path = os.path.join(file_path, file_name[0]) io_success = XmlParser.load_machine(full_path, self.tm_gui, self.tape_gui) if io_success: self.curr_machine_name = file_name[0] self.close_menus(None, None) self._popup.dismiss() else: self.tm_gui.create_new_machine()
def save_machine(self, file_path, file_name): file_name = file_name.strip() if file_name != '': if file_name[-3:] != '.tm': file_name = file_name + '.tm' full_path = os.path.join(file_path, file_name) io_success = XmlParser.save_machine(full_path, self.tm_gui, self.tape_gui) if io_success: self.curr_machine_name = file_name self.close_menus(None, None) self._popup.dismiss()
def importDefSce( self, sceFile ): "import default format scenario" _f = XmlParser() _f .loadXmlFile( sceFile ) self.defScenario = [] self.TimeScenario = [] #获得所有Position节点 _posNode = _f.getAllElementByName( self.defSceParser['pos']['path'] ) for _pn in _posNode: #获Position得属性 _l = [] _pna = _f.getAttrListOneNode( _pn, self.defSceParser['pos']['attr'] ) #_l.append([_pna[0], int(_pna[1]), int(_pna[2])]) _l.append( _pna[0] ) _l.append( _pna[1] ) _l.append( _pna[2] ) #获得所有该Position下Set节点的属性 _seta = [_f.getAttrListOneNode( _set, self.defSceParser['set']['attr'] ) \ for _set in _f.getNodeListInNode( _pn, self.defSceParser['set']['path'] )] _l.append( _seta ) self.defScenario.append( _l ) #获得所有Time节点 _timeNode = _f.getAllElementByName( self.defSceParser['time']['path'] ) for _tn in _timeNode: #获Position得属性 _l = [] _pna = _f.getAttrListOneNode( _tn, self.defSceParser['time']['attr'] ) #将loophour以及delay转化为int _l.append( int( _pna[0] ) ) #获得所有该Position下Set节点的属性 _seta = [_f.getAttrListOneNode( _set, self.defSceParser['set']['attr'] ) \ for _set in _f.getNodeListInNode( _tn, self.defSceParser['set']['path'] )] _l.append( _seta ) self.TimeScenario.append( _l )
def importVarint( self, varintFile ): " import device varint" _f = XmlParser() _f.loadXmlFile( varintFile ) _var = _f.getAttrListManyElement( self.importFileParser['lineInfo']['path'], self.importFileParser['lineInfo']['attr'] ) for _v in _var: #添加到_data self._data[int(_v[0])] = _v[1:] # print self._data _f.closeXmlFile()
def importTrainRoute( cls, path ): "import train route." _f = XmlParser() _f .loadXmlFile( path ) #获得route节点的属性 _map = _f.getAttrListOneElement( cls.FileParser['train_route']['map']['path'], \ cls.FileParser['train_route']['map']['attr'] ) # print'-----_map-----',_map _mapList = [int( _s ) for _s in _map[0].strip().split( ',' )] # print'-----_mapList-----',_mapList _Cog_dir = float( _f.getAttrListOneElement( cls.FileParser['train_route']['Cog_dir']['path'], \ cls.FileParser['train_route']['Cog_dir']['attr'] )[0] ) _accel = _f.getAttrListOneElement( cls.FileParser['train_route']['accel']['path'], \ cls.FileParser['train_route']['accel']['attr'] ) _accelV = [int( _a ) for _a in _accel] _V_max = int(_f.getAttrListOneElement( cls.FileParser['train_route']['V_max']['path'], \ cls.FileParser['train_route']['V_max']['attr'] )[0]) _timer = _f.getAttrListOneElement( cls.FileParser['train_route']['timer']['path'], \ cls.FileParser['train_route']['timer']['attr'] ) _timerV = [float( _t ) for _t in _timer] _radio = _f.getAttrListOneElement( cls.FileParser['train_route']['radio']['path'], \ cls.FileParser['train_route']['radio']['attr'] ) _radioV = [float( _r) for _r in _radio] _lineInfo = _f.getAttrListOneElement(cls.FileParser['train_route']['lineInfo']['path'],\ cls.FileParser['train_route']['lineInfo']['attr']) _lineList = [int( _a ) for _a in _lineInfo[0].strip().split( ',' )] # _lineId= int(_lineInfo[0]) # _dir = int(_lineInfo[1]) # _ssaList = [int( _s ) for _s in _lineInfo[2].strip().split( ',' )] # if _dir == 1:#上行线解析 # _blockList = [int( _s ) for _s in _lineInfo[3].strip().split( ',' )] # else:#下行线 # _blockList = [int( _s ) for _s in _lineInfo[3].strip().split( ',' )[::-1]] # print '-------_lineInfo----------',_lineInfo # print '-----------_blockList-------',_blockList return _mapList,_Cog_dir,_accelV,_V_max,_timerV,_radioV,_lineList
def importVarint( self, varintFile ): " import device varint" _f = XmlParser() _f.loadXmlFile( varintFile ) _var = _f.getAttrListManyElement( self.importFileParser['var']['path'], self.importFileParser['var']['attr'] ) for _v in _var: #添加到__data #若变量的类型是基本类型,则赋初值,不是则赋值None if _v[1] in self.importFileParser['var']['type']: self.addDataKeyValue( _v[0], \ self.importFileParser['var']['type'][_v[1]]( _v[3] ) ) else: self.addDataKeyValue( _v[0], None ) #添加到__var self.__var[_v[0]] = _v[1:] _f.closeXmlFile()
def importConfig( self, xmlpath ): " import config file" self.__telnetHandle = {} self.__ftpHandle = {} _f = XmlParser() _f.loadXmlFile( xmlpath ) _telnet = _f.getAttrListManyElement( self.importFileParser['path'], self.importFileParser['attr'] ) for _t in _telnet: _host = _t[0] _hostName = _t[1] _ip = _t[2] _port = _t[3] _password = _t[4] _savePath = _t[5] _handle = None self.__telnetHandle[_host] = [_hostName, _ip, _port, _password, _savePath, _handle] self.__ftpHandle[_host] = [_hostName, _ip, _port, _password, _handle] # print self.__telnetHandle _f.closeXmlFile()
def _test_xml_parents(self): print("_test_xml_parents start") parser = XmlParser(io.StringIO("<root><child1><child2/></child1></root>")) root_begin = parser.next_token() assert len(root_begin.get_parent_tags()) == 0 child1_begin = parser.next_token() assert len(child1_begin.get_parent_tags()) == 1 assert child1_begin.get_parent_tags()[0] == "root" child2_begin = parser.next_token() assert len(child2_begin.get_parent_tags()) == 2 assert child2_begin.get_parent_tags()[0] == "root" assert child2_begin.get_parent_tags()[1] == "child1" child2_end = parser.next_token() assert child2_end.get_tag() == "child2" assert len(child2_begin.get_parent_tags()) == 2 assert child2_end.get_parent_tags()[0] == "root" assert child2_end.get_parent_tags()[1] == "child1" child1_end = parser.next_token() assert child1_end.get_tag() == "child1" assert len(child1_end.get_parent_tags()) == 1 assert child1_end.get_parent_tags()[0] == "root" root_end = parser.next_token() assert len(root_end.get_parent_tags()) == 0 print("_test_xml_parents end")
def loadTrainRout( routeFile ): " parser train route file" _f = XmlParser() _f .loadXmlFile( routeFile ) #获得route节点的属性 _route = _f.getAttrListOneElement( TRAIN_ROUTE['route']['path'], \ TRAIN_ROUTE['route']['attr'] ) _routeV = [int( _s ) for _s in _route[0].strip().split( ',' )] _start = _f.getAttrListOneElement( TRAIN_ROUTE['start']['path'], \ TRAIN_ROUTE['start']['attr'] ) _startV = [int( _s ) for _s in _start] _dire = _f.getAttrListOneElement( TRAIN_ROUTE['dire']['path'], \ TRAIN_ROUTE['dire']['attr'] ) _direV = int( _dire[0] ) _trainLen = int( _f.getAttrListOneElement( TRAIN_ROUTE['trainLen']['path'], \ TRAIN_ROUTE['trainLen']['attr'] )[0] ) _Cog_dir = int( _f.getAttrListOneElement( TRAIN_ROUTE['Cog_dir']['path'], \ TRAIN_ROUTE['Cog_dir']['attr'] )[0] ) return _routeV, _startV, _direV, _trainLen, _Cog_dir
from xmlparser import XmlParser from csvexport import CsvExport import os import sys data = [] header = [ 'Fecha', 'Serie', 'Folio', 'Metodo de Pago', '#cta', 'RFC', 'Razon Social', 'Concepto', 'Importe', 'IVA', 'IEPS', 'Total' ] data.append(header[:]) '''file_name = 'xmls/0BA84398-2706-4EF0-B733-E5CED270BE1D.xml' xml = XmlParser(file_name) for row in xml.get_data(): data.append(row)''' walk_dir = 'xmls' for root, subdirs, files in os.walk(walk_dir): for xml in files: xml = XmlParser(root + '/' + xml) for row in xml.get_data(): data.append(row) csv_writer = CsvExport(data, 'alpha.csv') csv_writer.create()
def setImage(self, item): start = time.time() text = str(item.text()) if text == "Original": image = TEMP + 'original.bmp' elif text == "OpenCV Cascade": if self.mode == 'detection': image = TEMP + 'opencv_cat.bmp' elif self.mode == 'points': if self.path == '': self.listCascade.clear() self.listCascade.addItem( QListWidgetItem('Изображение не было загружено')) return width, height, feature_matrices, stages_list = xml_parser.read_cascade( 'source\\haarcascade_frontalcatface.xml') xml_parser.show_points(self.path, width, height, feature_matrices, stages_list, int(self.Number.text())) image = TEMP + str(self.Number.text()) + '.jpg' elif self.mode == 'haar': width, height, feature_matrices, stages_list = xml_parser.read_cascade( 'source\\haarcascade_frontalcatface.xml') xml_parser.show_feature(feature_matrices, int(self.Number.text())) image = TEMP + 'feature.png' elif text == "OpenCV Cascade Extended": if self.mode == 'detection': image = TEMP + 'opencv_cat_ext.bmp' elif self.mode == 'points': if self.path == '': self.listCascade.clear() self.listCascade.addItem( QListWidgetItem('Изображение не было загружено')) return width, height, feature_matrices, stages_list = xml_parser.read_cascade( 'source\\haarcascade_frontalcatface_extended.xml') xml_parser.show_points(self.path, width, height, feature_matrices, stages_list, int(self.Number.text())) image = TEMP + str(self.Number.text()) + '.jpg' elif self.mode == 'haar': width, height, feature_matrices, stages_list = xml_parser.read_cascade( 'source\\haarcascade_frontalcatface_extended.xml') xml_parser.show_feature(feature_matrices, int(self.Number.text())) image = TEMP + 'feature.png' elif text == "My Cascade": if self.mode == 'detection': image = TEMP + 'my.bmp' elif self.mode == 'points': if self.path == '': self.listCascade.clear() self.listCascade.addItem( QListWidgetItem('Изображение не было загружено')) return width, height, feature_matrices, stages_list = xml_parser.read_cascade( 'source\\cascade_cat.xml') xml_parser.show_points(self.path, width, height, feature_matrices, stages_list, int(self.Number.text())) image = TEMP + str(self.Number.text()) + '.jpg' elif self.mode == 'haar': width, height, feature_matrices, stages_list = xml_parser.read_cascade( 'source\\cascade_cat.xml') xml_parser.show_feature(feature_matrices, int(self.Number.text())) image = TEMP + 'feature.png' elif text == "Glitch Cascade": if self.mode == 'detection': image = TEMP + 'glitch.bmp' elif self.mode == 'points': if self.path == '': self.listCascade.clear() self.listCascade.addItem( QListWidgetItem('Изображение не было загружено')) return width, height, feature_matrices, stages_list = xml_parser.read_cascade( 'source\\glitch.xml') xml_parser.show_points(self.path, width, height, feature_matrices, stages_list, int(self.Number.text())) image = TEMP + str(self.Number.text()) + '.jpg' elif text == "All in one picture": image = TEMP + 'full.bmp' else: if self.mode == 'detection': image = TEMP + 'original.bmp' end = time.time() print("Mode " + self.mode + ": ", end - start) self.drawImage(image) self.pic.update()
def load_machine_uri(self, full_path): io_success = XmlParser.load_machine(full_path, self.tm_gui, self.tape_gui) if not io_success: self.tm_gui.create_new_machine()
def __init__(self): XmlParser.__init__(self)
from xmlparser import XmlParser if __name__ == "__main__": tvparser = XmlParser() tvparser.parse()