Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 1 | # /usr/bin/env python |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 3 | Created on 07-Jan-2013 |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4 | Copyright 2013 Open Networking Foundation (ONF) |
Jeremy Songster | ae01bba | 2016-07-11 15:39:17 -0700 | [diff] [blame] | 5 | |
| 6 | Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>, |
| 7 | the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>, |
| 8 | or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg> |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 9 | |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 10 | @author: Raghav Kashyap(raghavkashyap@paxterrasolutions.com) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 11 | |
| 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 Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 15 | (at your option) any later version. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 16 | |
| 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 Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 23 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 24 | |
| 25 | |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 26 | ''' |
| 27 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 28 | import xmldict |
| 29 | import re |
Jon Hall | d74d295 | 2018-03-01 13:26:39 -0800 | [diff] [blame] | 30 | import os.path |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 31 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 32 | class xmlparser: |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 33 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 34 | def __init__( self ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 35 | self.default = '' |
| 36 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 37 | def parse( self, fileName ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 38 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 39 | This will parse the params or topo or cfg file and return content in the file as Dictionary |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 40 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 41 | self.fileName = fileName |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 42 | matchFileName = re.match( r'(.*)\.(params|topo|cfg)', self.fileName, re.M | re.I ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 43 | if matchFileName: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 44 | xml = open( fileName ).read() |
| 45 | try: |
| 46 | parsedInfo = xmldict.xml_to_dict( xml ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 47 | return parsedInfo |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 48 | except StandardError as e: |
Jon Hall | 0946f65 | 2015-08-13 11:44:38 -0700 | [diff] [blame] | 49 | print "Error parsing file " + fileName + ": " + e.message |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 50 | else: |
Jon Hall | 0946f65 | 2015-08-13 11:44:38 -0700 | [diff] [blame] | 51 | print "File name is not correct" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 52 | |
Jon Hall | d74d295 | 2018-03-01 13:26:39 -0800 | [diff] [blame] | 53 | def parseFile( self, fileName ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 54 | ''' |
Jon Hall | d74d295 | 2018-03-01 13:26:39 -0800 | [diff] [blame] | 55 | It will take a file path of an xml file and return the contents as a dictionary |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 56 | ''' |
Jon Hall | d74d295 | 2018-03-01 13:26:39 -0800 | [diff] [blame] | 57 | contents = self.parse( fileName ) |
| 58 | return eval( str( contents ) ) |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 59 | |