blob: d7af564a734cbd3c102942ac5b9dc94b73515957 [file] [log] [blame]
Jeremy Ronquillo696f4262017-10-17 10:56:26 -07001# /usr/bin/env python
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002'''
adminbae64d82013-08-01 10:50:15 -07003Created on 07-Jan-2013
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00004Copyright 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 Ronquillo4d5f1d02017-10-13 20:23:57 +000010@author: 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 Ronquillo4d5f1d02017-10-13 20:23:57 +000015 (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 Ronquillo4d5f1d02017-10-13 20:23:57 +000026'''
27
adminbae64d82013-08-01 10:50:15 -070028import xmldict
29import re
Jon Halld74d2952018-03-01 13:26:39 -080030import os.path
adminbae64d82013-08-01 10:50:15 -070031
Jeremy Ronquillo696f4262017-10-17 10:56:26 -070032class xmlparser:
Jon Hall4ba53f02015-07-29 13:07:41 -070033
Jeremy Ronquillo696f4262017-10-17 10:56:26 -070034 def __init__( self ):
adminbae64d82013-08-01 10:50:15 -070035 self.default = ''
36
Jeremy Ronquillo696f4262017-10-17 10:56:26 -070037 def parse( self, fileName ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +000038 '''
adminbae64d82013-08-01 10:50:15 -070039 This will parse the params or topo or cfg file and return content in the file as Dictionary
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +000040 '''
adminbae64d82013-08-01 10:50:15 -070041 self.fileName = fileName
Jeremy Ronquillo696f4262017-10-17 10:56:26 -070042 matchFileName = re.match( r'(.*)\.(params|topo|cfg)', self.fileName, re.M | re.I )
adminbae64d82013-08-01 10:50:15 -070043 if matchFileName:
Jeremy Ronquillo696f4262017-10-17 10:56:26 -070044 xml = open( fileName ).read()
45 try:
46 parsedInfo = xmldict.xml_to_dict( xml )
adminbae64d82013-08-01 10:50:15 -070047 return parsedInfo
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +000048 except StandardError as e:
Jon Hall0946f652015-08-13 11:44:38 -070049 print "Error parsing file " + fileName + ": " + e.message
Jeremy Ronquillo696f4262017-10-17 10:56:26 -070050 else:
Jon Hall0946f652015-08-13 11:44:38 -070051 print "File name is not correct"
adminbae64d82013-08-01 10:50:15 -070052
Jon Halld74d2952018-03-01 13:26:39 -080053 def parseFile( self, fileName ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +000054 '''
Jon Halld74d2952018-03-01 13:26:39 -080055 It will take a file path of an xml file and return the contents as a dictionary
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +000056 '''
Jon Halld74d2952018-03-01 13:26:39 -080057 contents = self.parse( fileName )
58 return eval( str( contents ) )
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +000059