RtmParser.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- coding: utf-8 -*- 
00003 #
00004 #  @file RtmParser.py
00005 #  @brief rtc-link XML connection list file parser
00006 #  @date $Date: 2005-05-12 09:06:19 $
00007 #  @author Tsuyoshi Tanabe, Noriaki Ando <n-ando@aist.go.jp>
00008 # 
00009 #  Copyright (C) 2004-2005
00010 #      Task-intelligence Research Group,
00011 #      Intelligent Systems Research Institute,
00012 #      National Institute of
00013 #          Advanced Industrial Science and Technology (AIST), Japan
00014 #      All rights reserved.
00015 # 
00016 #  $Id: RtmParser.py 775 2008-07-28 16:14:45Z n-ando $
00017 # 
00018 
00019 import sys,os
00020 import string
00021 from omniORB import CORBA
00022 from xml.dom.minidom import parse
00023 from xml.dom.minidom import getDOMImplementation
00024 
00025 import RtmDtdValidator
00026 
00027 #
00028 # 
00029 #
00030 class RtmParser :
00031 #  def serializeDOM(self, target, node) :
00032 
00033 # XMLファイルの出力: DOMツリー → XML
00034   def dump2file(self, outfilename=None) :
00035     if outfilename:
00036       self.outfilename = outfilename
00037     if self.outfilename :
00038       print "outfilename:",self.outfilename
00039       self.outfile = open(self.outfilename, "w")
00040       self.outfile.write(self.cur_node.toxml())
00041       self.outfile.write("\n")
00042       self.outfile.close()
00043     else :
00044       none
00045     return 1
00046 
00047   def dump2cout(self) :
00048     print(self.cur_node.toxml())
00049     return 1
00050 
00051 # root node の取得 ?
00052   def get_root(self) :
00053     return self.the_root_node
00054 
00055   def get_cur_node(self) :
00056     return self.cur_node
00057 
00058   def set_cur_node(self, nd) :
00059     self.cur_node = nd
00060     return 1
00061 
00062   def reset_node(self) :
00063     self.cur_node = self.the_root_node
00064     return 1
00065 
00066   def getChildNodes(self) :
00067     self.node_list = self.cur_node.childNodes
00068     return self.node_list
00069 
00070   def getTaggedNodes(self, tg) :
00071     self.node_list = self.cur_node.getElementsByTagName(tg)
00072     return self.node_list
00073 
00074   def getText(self, nodes=None) :
00075     if nodes == None:
00076       nodes = self.cur_node.childNodes
00077     rc = ''
00078     for node in nodes:
00079       if node.nodeType == node.TEXT_NODE:
00080         rc = rc + node.data
00081     return rc
00082 
00083   def replaceText(self, txt) :
00084     tmp_node = self.dom.createTextNode(txt)
00085     self.cur_node.childNodes = [tmp_node]
00086     return 1
00087 
00088   def getTaggedText(self, tg) :
00089     nodes = self.cur_node.getElementsByTagName(tg)[0]
00090 #    tmp_root = nodes[0]
00091 #    nodes = tmp_root.childNodes
00092     rc = self.getText(nodes.childNodes)
00093     return rc
00094   
00095 # child element の更新(replace)
00096   def replaceTaggedText(self, tg, txt) :
00097     nodes = self.cur_node.getElementsByTagName(tg)
00098     tmp_root = nodes[0]
00099     tmp_node = self.dom.createTextNode(txt)
00100     tmp_root.childNodes = [tmp_node]
00101     return 1
00102 
00103 # child node の削除(remove)
00104   def removeChildNode(self, nd):
00105     self.cur_node.removeChild(nd)
00106 
00107 # element,text を子ノードとして追加
00108   def appendChildTaggedText(self, tg, txt):
00109     tmp_elem = self.dom.createElement(tg)
00110     tmp_txt = self.dom.createTextNode(txt)
00111     tmp_elem.appendChild(tmp_txt)
00112     self.cur_node.appendChild(tmp_elem)
00113     return tmp_elem
00114     
00115 # elementを子ノードとして追加
00116   def appendChildTagged(self, tg):
00117     tmp_elem = self.dom.createElement(tg)
00118     self.cur_node.appendChild(tmp_elem)
00119     return tmp_elem
00120 
00121 #
00122 # for C access
00123 #
00124   def set_tmplist(self, lst) :
00125     self.tmp_node_list = lst
00126     return 1
00127 
00128   def elt_tmp(self, idx) :
00129     return self.tmp_node_list[idx];
00130 #
00131 # for C access (end)
00132 #
00133 
00134   def getComponentNode(self, comp_key):
00135     # search for compornent node list
00136     comp_list = self.the_root_node.getElementsByTagName("rt_component")
00137     tmp = ''
00138     tmp1 = ''
00139     tmp2 = ''
00140     tmp3 = ''
00141     tmp4 = ''
00142     tmp5 = ''
00143     ret = None
00144     for comp_node in comp_list:
00145 
00146       tmp1 = comp_node.getElementsByTagName("host_name")[0]
00147       tmp1 = self.getText(tmp.childNodes)
00148       tmp2 = comp_node.getElementsByTagName("manager_name")[0]
00149       tmp2 = self.getText(tmp.childNodes)
00150       tmp3 = comp_node.getElementsByTagName("category")[0]
00151       tmp3 = self.getText(tmp.childNodes)
00152       tmp4 = comp_node.getElementsByTagName("module_name")[0]
00153       tmp4 = self.getText(tmp.childNodes)
00154       tmp5 = comp_node.getElementsByTagName("component_name")[0]
00155       tmp5 = self.getText(tmp.childNodes)
00156 
00157       tmp = tmp1 + "/" + tmp2 + "/" + tmp3 + "/" + tmp4 + "/" + tmp5
00158 
00159       if tmp == comp_key:
00160         break
00161 
00162     if tmp1 == '' or tmp2 == '' or tmp3 == '' or tmp4 == '' or tmp5 == '':
00163 #       tmp1 == '.*' or tmp2 == '.*' or tmp3 == '.*' or tmp4 == '.*' or tmp5 == '.*' :
00164       print "Component Name is not an"
00165       print self.infilename
00166     else:
00167       self.cur_node = comp_node
00168       ret = self.cur_node
00169     return ret
00170 
00171   def getComponentNames(self):
00172     # search for compornent node list
00173     comp_list = self.the_root_node.getElementsByTagName("rt_component")
00174     ret = []
00175     save_node = self.get_cur_node()
00176     for comp_node in comp_list:
00177       self.set_cur_node(comp_node)
00178       tmp = self.getTaggedText("component_name")
00179       ret.append(tmp)
00180     self.set_cur_node(save_node)
00181     return ret
00182 
00183   def getComponentKeys(self):
00184     # search for compornent node list
00185     comp_list = self.the_root_node.getElementsByTagName("rt_component")
00186     ret = []
00187     save_node = self.get_cur_node()
00188     for comp_node in comp_list:
00189       self.set_cur_node(comp_node)
00190       tmp = self.getTaggedText("component_name")
00191 
00192       tmp1 = self.getTaggedText("host_name")
00193       tmp2 = self.getTaggedText("manager_name")
00194       tmp3 = self.getTaggedText("category")
00195       tmp4 = self.getTaggedText("module_name")
00196       tmp5 = self.getTaggedText("component_name")
00197       nam = tmp1 + "/" + tmp2 + "/" + tmp3 + "/" + tmp4 + "/" + tmp5
00198 
00199       ret.append(nam)
00200     self.set_cur_node(save_node)
00201     return ret
00202 
00203 
00204 # XMLファイルの読込み: XML → DOMツリー → ディクショナリ
00205   def readXML(self, in_name):
00206 
00207     self.infilename = in_name
00208 
00209     self.dom = parse(self.infilename)
00210     self.the_root_node = self.dom
00211     self.reset_node()
00212 
00213     dict = {}
00214     dict['rtc'] = {}
00215     dict['line'] = []
00216     rtc_list = self.getTaggedNodes('rtc_list')[0]
00217     self.set_cur_node(rtc_list)
00218     comp_list = self.getChildNodes()
00219     for comp_node in comp_list:
00220       self.set_cur_node(comp_node)
00221       name = self.getTaggedText('name')
00222       name_elem =  self.getTaggedNodes('name')[0]
00223       x = string.atof(name_elem.getAttribute('x'))
00224       y = string.atof(name_elem.getAttribute('y'))
00225       rot = name_elem.getAttribute('rot')
00226 #      print "Comp name:",name," x=",x," y=",y
00227       dict['rtc'][name] = {}
00228       dict['rtc'][name]['x'] = x
00229       dict['rtc'][name]['y'] = y
00230       dict['rtc'][name]['rot'] = rot
00231 
00232     self.reset_node()
00233     line_list = self.getTaggedNodes('line_list')[0]
00234     self.set_cur_node(line_list)
00235     lines = self.getChildNodes()
00236     for line_node in lines:
00237       self.set_cur_node(line_node)
00238       pos = self.getTaggedText('pos')
00239       inp_comp = self.getTaggedText('inp-comp')
00240       inp_name = self.getTaggedText('inp-name')
00241       outp_comp = self.getTaggedText('outp-comp')
00242       outp_name = self.getTaggedText('outp-name')
00243       tmp = {}
00244       tmp['pos'] = pos
00245       tmp['in-comp'] = inp_comp
00246       tmp['in-name'] = inp_name
00247       tmp['out-comp'] = outp_comp
00248       tmp['out-name'] = outp_name
00249       dict['line'].append(tmp)
00250 
00251     return dict
00252 
00253 
00254   def writeXML(self, filename, dict):
00255     #XML document create
00256     impl = getDOMImplementation()
00257     doc = impl.createDocument(None, "rt_system", None)
00258     top_elem = doc.documentElement
00259 #    LF = doc.createTextNode('\n')
00260 #    top_elem.appendChild(LF)
00261 
00262     #XML node append
00263     #component block
00264     comp_list = dict['rtc'].keys()
00265     elemRtcList = doc.createElement("rtc_list")
00266     top_elem.appendChild(elemRtcList)
00267     for comp_name in comp_list:
00268       elem = doc.createElement("rt_component")
00269       elemRtcList.appendChild(elem)
00270       elemName = doc.createElement("name")
00271       elem.appendChild(elemName)
00272       child = doc.createTextNode(comp_name)
00273       elemName.appendChild(child)
00274       elemName.setAttribute('x','%f'%dict['rtc'][comp_name]['x'])
00275       elemName.setAttribute('y','%f'%dict['rtc'][comp_name]['y'])
00276       elemName.setAttribute('rot',dict['rtc'][comp_name]['rot'])
00277 
00278     elemLineList = doc.createElement("line_list")
00279     top_elem.appendChild(elemLineList)
00280     line_list = dict['line']
00281     for line in line_list:
00282       elem = doc.createElement("line")
00283       elemLineList.appendChild(elem)
00284       str = ''
00285       for pos in line['pos']:
00286         str += '%f,%f,'%(pos[0],pos[1])
00287 #      str[-1] = ''
00288       elemPos = doc.createElement("pos")
00289       elem.appendChild(elemPos)
00290       child = doc.createTextNode(str)
00291       elemPos.appendChild(child)
00292 
00293       elemInComp = doc.createElement("inp-comp")
00294       elemInName = doc.createElement("inp-name")
00295       elem.appendChild(elemInComp)
00296       elem.appendChild(elemInName)
00297       child = doc.createTextNode(line['in-comp'])
00298       elemInComp.appendChild(child)
00299       child = doc.createTextNode(line['in-name'])
00300       elemInName.appendChild(child)
00301 
00302       elemOutComp = doc.createElement("outp-comp")
00303       elemOutName = doc.createElement("outp-name")
00304       elem.appendChild(elemOutComp)
00305       elem.appendChild(elemOutName)
00306       child = doc.createTextNode(line['out-comp'])
00307       elemOutComp.appendChild(child)
00308       child = doc.createTextNode(line['out-name'])
00309       elemOutName.appendChild(child)
00310 
00311 
00312     #dump to file
00313     outfile = open(filename, "w")
00314     doc.writexml(outfile)
00315     outfile.close()
00316 
00317     doc.unlink()
00318 
00319   def __init__(self) :
00320     self.infilename = None
00321     self.outfilename = None
00322 #----------------------------------------------------------------------


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Thu Aug 27 2015 14:16:38