admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1 | #/usr/bin/env python |
| 2 | ''' |
| 3 | Created on 07-Jan-2013 |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 4 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 5 | @author: Raghav Kashyap(raghavkashyap@paxterrasolutions.com) |
| 6 | |
| 7 | TestON is free software: you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation, either version 2 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | TestON is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 18 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 19 | |
| 20 | |
| 21 | ''' |
| 22 | |
| 23 | import xmldict |
| 24 | import re |
| 25 | |
| 26 | class xmlparser : |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 27 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 28 | def __init__(self) : |
| 29 | self.default = '' |
| 30 | |
| 31 | def parse(self,fileName) : |
| 32 | ''' |
| 33 | This will parse the params or topo or cfg file and return content in the file as Dictionary |
| 34 | ''' |
| 35 | self.fileName = fileName |
| 36 | matchFileName = re.match(r'(.*)\.(params|topo|cfg)', self.fileName, re.M | re.I) |
| 37 | if matchFileName: |
| 38 | xml = open(fileName).read() |
| 39 | try : |
| 40 | parsedInfo = xmldict.xml_to_dict(xml) |
| 41 | return parsedInfo |
Jon Hall | 1de6c19 | 2015-09-02 13:50:16 -0700 | [diff] [blame^] | 42 | except StandardError as e: |
Jon Hall | 0946f65 | 2015-08-13 11:44:38 -0700 | [diff] [blame] | 43 | print "Error parsing file " + fileName + ": " + e.message |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 44 | else : |
Jon Hall | 0946f65 | 2015-08-13 11:44:38 -0700 | [diff] [blame] | 45 | print "File name is not correct" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 46 | |
| 47 | def parseParams(self,paramsPath): |
| 48 | ''' |
| 49 | It will take the params file path and will return the params dictionary |
| 50 | ''' |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 51 | paramsPath = re.sub("\.","/",paramsPath) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 52 | paramsPath = re.sub("tests|examples","",paramsPath) |
| 53 | params = self.parse(main.tests_path+paramsPath+".params") |
| 54 | paramsAsString = str(params) |
| 55 | return eval(paramsAsString) |
| 56 | |
| 57 | def parseTopology(self,topologyPath): |
| 58 | ''' |
| 59 | It will take topology file path and will return topology dictionary |
| 60 | ''' |
| 61 | topologyPath = re.sub("\.","/",topologyPath) |
| 62 | topologyPath = re.sub("tests|examples","",topologyPath) |
andrewonlab | 31d9f2d | 2014-10-09 13:25:13 -0400 | [diff] [blame] | 63 | #topology = self.parse(main.tests_path+"/"+topologyPath+".topo") |
| 64 | topology = self.parse(main.tests_path+topologyPath+".topo") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 65 | topoAsString = str(topology) |
| 66 | return eval(topoAsString) |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 67 | |