blob: 181f64645af22d2a74c48cf847975db1e9678272 [file] [log] [blame]
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07001# /usr/bin/env python
2"""
adminbae64d82013-08-01 10:50:15 -07003Created on 07-Jan-2013
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07004Copyright 2013 Open Networking Foundation ( ONF )
Jeremy Songsterae01bba2016-07-11 15:39:17 -07005
6Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>,
7the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>,
8or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg>
Jon Hall4ba53f02015-07-29 13:07:41 -07009
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070010author:: Raghav Kashyap( raghavkashyap@paxterrasolutions.com )
adminbae64d82013-08-01 10:50:15 -070011
12 TestON is free software: you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation, either version 2 of the License, or
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070015 ( at your option ) any later version.
adminbae64d82013-08-01 10:50:15 -070016
17 TestON is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
Jon Hall4ba53f02015-07-29 13:07:41 -070023 along with TestON. If not, see <http://www.gnu.org/licenses/>.
adminbae64d82013-08-01 10:50:15 -070024
25
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070026"""
adminbae64d82013-08-01 10:50:15 -070027import xmldict
28import re
29
Jon Hall4ba53f02015-07-29 13:07:41 -070030
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070031class xmlparser:
32
33 def __init__( self ):
adminbae64d82013-08-01 10:50:15 -070034 self.default = ''
35
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070036 def parse( self, fileName ):
37 """
adminbae64d82013-08-01 10:50:15 -070038 This will parse the params or topo or cfg file and return content in the file as Dictionary
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070039 """
adminbae64d82013-08-01 10:50:15 -070040 self.fileName = fileName
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070041 matchFileName = re.match( r'(.*)\.(params|topo|cfg)', self.fileName, re.M | re.I )
adminbae64d82013-08-01 10:50:15 -070042 if matchFileName:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070043 xml = open( fileName ).read()
44 try:
45 parsedInfo = xmldict.xml_to_dict( xml )
adminbae64d82013-08-01 10:50:15 -070046 return parsedInfo
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070047 except Exception as e:
Jon Hall0946f652015-08-13 11:44:38 -070048 print "Error parsing file " + fileName + ": " + e.message
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070049 else:
Jon Hall0946f652015-08-13 11:44:38 -070050 print "File name is not correct"
adminbae64d82013-08-01 10:50:15 -070051
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070052 def parseParams( self, paramsPath ):
53 """
adminbae64d82013-08-01 10:50:15 -070054 It will take the params file path and will return the params dictionary
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070055 """
56 paramsPath = re.sub( "\.", "/", paramsPath )
57 paramsPath = re.sub( "tests|examples", "", paramsPath )
58 params = self.parse( main.tests_path + paramsPath + ".params" )
59 paramsAsString = str( params )
60 return eval( paramsAsString )
adminbae64d82013-08-01 10:50:15 -070061
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070062 def parseTopology( self, topologyPath ):
63 """
adminbae64d82013-08-01 10:50:15 -070064 It will take topology file path and will return topology dictionary
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070065 """
66 topologyPath = re.sub( "\.", "/", topologyPath )
67 topologyPath = re.sub( "tests|examples", "", topologyPath )
68 # topology = self.parse( main.tests_path+"/"+topologyPath+".topo" )
69 topology = self.parse( main.tests_path + topologyPath + ".topo" )
70 topoAsString = str( topology )
71 return eval( topoAsString )