def parseCluster(child): try: # retVal = {} # need to used OrderedDict retVal = OrderedDict() for node in child: tag = node.tag if tag == 'Name': name = node.text # retVal[name] = {} retVal[name] = OrderedDict() #need to use OrderedDict elif node.tag == 'NumElts': pass elif node.tag == 'String': strList = parseString(node) retVal[name][strList[0]] = strList[1] elif node.tag == 'Path': strList = parseString(node) retVal[name][strList[0]] = strList[1] elif node.tag == 'Array': arrayList = parseArray(node) retVal[name][arrayList[0]] = arrayList[1] elif node.tag == 'Cluster': # Nested Clusters for subName, subVal in parseCluster(node).iteritems(): retVal[name][subName] = subVal else: dataList = parseNumeric(node) retVal[name][dataList[0]] = dataList[1] return retVal except Exception as e: a = Lta_Error(e, sys.exc_info()) print a raise e
def Lta_Unparse(topDict): root = etree.Element("root") root_doc = etree.SubElement(root, "doc") try: for tag in topDict: uParse = __uParseType(type(topDict[tag])) uParse(root_doc, topDict, tag) except Exception as e: a = Lta_Error(e, sys.exc_info()) print a raise e retVal = '' for child in root_doc: retVal = retVal + (etree.tostring(child, pretty_print=True)) # parser = etree.XMLParser(remove_blank_text=True) # retVal = etree.tostring(etree.XML(retVal, parser=parser)) return retVal
def Lta_Parse(xml): topDict = {} try: root = getRootDoc(xml) # for each top level element, determine the data type and create # a container in the op level dictionary for child in root: tag = child.tag if tag == 'Cluster': val = parseCluster(child) topDict.update(val) elif tag == 'Array': val = parseArray(child) val = {val[0]: val[1]} else: # data that is not in a string or array val = parseData(child) topDict.update(val) return topDict except Exception as e: err = Lta_Error(e, sys.exc_info()) raise err
# -*- coding: utf-8 -*- from lta import Lta import sys from lta_err import Lta_Error #------------------- following code must be in all scripts-------------------- lta = Lta("127.0.0.1", 60100) # all scripts must create an Lta object try: lta.connect() # connect to the Labview Host #---------------------Script code goes here------------------------------------ EvtCfg = lta.__get__('bus1.event.config') EvtCfg['clEvtConfig']['Fs'] = 60 lta.__set__('bus1.event.config', EvtCfg) lta.__run__() EvtCfg = lta.__get__('bus1.event.config') EvtCfg['clEvtConfig']['Fs'] = 30 lta.__set__('bus1.event.config', EvtCfg) lta.__run__() EvtCfg = lta.__get__('bus1.event.config') EvtCfg['clEvtConfig']['Fs'] = 20 lta.__set__('bus1.event.config', EvtCfg) lta.__run__() #------------------all scripts should send their errors to labview------------ except Exception as ex: err = Lta_Error(ex, sys.exc_info()) #format a labview error lta.send_error(err, 3, 'Abort') #send the error to labview for display lta.close() print err
CommsData = Lta_Parse(CommsData) CommsData = Lta_Parse(CommsData['CommsData']['XMLData']) return CommsData except (IOError, Exception) as e: raise e def __run__(self): try: cmdDict = Lta_Command('run',"").cmdDict xml = Lta_Unparse(cmdDict); packet.SendPacket(self.s, xml) CommsData = packet.ReceivePacket(self.s) CommsData = Lta_Parse(CommsData); #print CommsData assert CommsData['CommsData']['Command']=='LtaRunComplete', "Run was not acknowledged as completed" except (IOError, Exception) as e: raise e if __name__=='__main__': try: execfile(sys.argv[1]) except Exception as ex: lta = Lta("127.0.0.1",60100) lta.connect() err = Lta_Error(ex,sys.exc_info()) lta.send_error(err,3,'Abort') lta.close()
else: # data that is not in a string or array val = parseData(child) topDict.update(val) return topDict except Exception as e: err = Lta_Error(e, sys.exc_info()) raise err if __name__ == '__main__': # Test the parser import os fileRelPath = 'nested.xml' # has 2 nested clusters # fileRelPath = 'err.xml' #LabVIEW formatted error messege # fileRelPath = 'clData.xml' # Cluster Data # fileRelPath = 'arData.xml' # Array Data # fileRelPath = 'stData.xml' # Just Data # fileRelPath = '..\..\..\Plugins\Plugin_B\Plugin.B.xml' try: thisDir = os.getcwd() fileDir = os.path.join(thisDir, fileRelPath) xml = open(fileDir).read() dataStruct = Lta_Parse(xml) print dataStruct except Exception as e: a = Lta_Error(e, sys.exc_info()) print a