blob: aab4388acd5489b9e6f496487facbb13f7198d5e [file] [log] [blame]
adminbae64d82013-08-01 10:50:15 -07001#!/usr/bin/env python
2'''
3Created on 26-Dec-2012
4
5@author: Anil Kumar (anilkumar.s@paxterrasolutions.com)
6
7
8 TestON is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 2 of the License, or
11 (at your option) any later version.
12
13 TestON is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
Jon Hallfeff3082015-05-19 10:23:26 -070019 along with TestON. If not, see <http://www.gnu.org/licenses/>.
adminbae64d82013-08-01 10:50:15 -070020
21
22'''
23import re
24import sys
25class TestParser:
26 def __init__(self,testFile):
27 try :
28 testFileHandler = open(testFile, 'r')
Jon Hallfeff3082015-05-19 10:23:26 -070029 except IOError:
adminbae64d82013-08-01 10:50:15 -070030 print "No such file "+testFile
31 sys.exit(0)
Jon Hallfeff3082015-05-19 10:23:26 -070032
adminbae64d82013-08-01 10:50:15 -070033 testFileList = testFileHandler.readlines()
Jon Hallfeff3082015-05-19 10:23:26 -070034 self.testscript = testFileList
adminbae64d82013-08-01 10:50:15 -070035 self.caseCode = {}
36 self.caseBlock = ''
37 self.statementsList = []
Jon Hallfeff3082015-05-19 10:23:26 -070038 index = 0
adminbae64d82013-08-01 10:50:15 -070039 self.statementsList = []
40 #initialSpaces = len(line) -len(line.lstrip())
41 while index < len(testFileList):
42 testFileList[index] = re.sub("^\s{8}|^\s{4}", "", testFileList[index])
Jon Hallfeff3082015-05-19 10:23:26 -070043 # Skip multiline comments
adminbae64d82013-08-01 10:50:15 -070044 if re.match('^(\'\'\')|^(\"\"\")',testFileList[index],0) :
45 index = index + 1
46 try :
47 while not re.match('^\s*(\'\'\')|^\s*(\"\"\")',testFileList[index],0) :
48 index = index + 1
Jon Hall1306a562015-09-04 11:21:24 -070049 except IndexError:
adminbae64d82013-08-01 10:50:15 -070050 print ''
adminbae64d82013-08-01 10:50:15 -070051
Jon Hallfeff3082015-05-19 10:23:26 -070052 # skip empty lines and single line comments
adminbae64d82013-08-01 10:50:15 -070053 elif not re.match('#|^\s*$',testFileList[index],0):
54 self.statementsList.append(testFileList[index])
55 index = index + 1
Jon Hallfeff3082015-05-19 10:23:26 -070056
adminbae64d82013-08-01 10:50:15 -070057 def case_code(self):
Jon Hallfeff3082015-05-19 10:23:26 -070058 index = 0
59 statementsList = self.statementsList
adminbae64d82013-08-01 10:50:15 -070060 while index < len(statementsList):
adminbae64d82013-08-01 10:50:15 -070061 m= re.match('def\s+CASE(\d+)',statementsList[index],0)
62 self.caseBlock = []
63 if m:
adminbae64d82013-08-01 10:50:15 -070064 index = index + 1
65 try :
66 while not re.match('\s*def\s+CASE(\d+)',statementsList[index],0) :
67 self.caseBlock.append(statementsList[index])
68 if index < len(statementsList)-1:
69 index = index + 1
70 else :
71 break
72 index = index - 1
Jon Hall1306a562015-09-04 11:21:24 -070073 except IndexError:
adminbae64d82013-08-01 10:50:15 -070074 print ''
adminbae64d82013-08-01 10:50:15 -070075 self.caseCode [str(m.group(1))] = self.caseBlock
adminbae64d82013-08-01 10:50:15 -070076 index = index + 1
Jon Hallfeff3082015-05-19 10:23:26 -070077 return self.caseCode
78
adminbae64d82013-08-01 10:50:15 -070079 def step_code(self,caseStatements):
80 index = 0
Jon Hallfeff3082015-05-19 10:23:26 -070081 step = 0
82 stepCode = {}
83 step_flag = False
adminbae64d82013-08-01 10:50:15 -070084 while index < len(caseStatements):
85 m= re.match('main\.step',caseStatements[index],0)
86 stepBlock = ''
87 if m:
88 step_flag = True
89 if step == 0 :
90 i = 0
91 block = ''
Jon Hall9910c2b2015-01-21 15:55:56 -080092 while i < index :
adminbae64d82013-08-01 10:50:15 -070093 block += caseStatements[i]
94 i = i + 1
Jon Hallfeff3082015-05-19 10:23:26 -070095 stepCode[step] = block
adminbae64d82013-08-01 10:50:15 -070096 step = step + 1
Jon Hallfeff3082015-05-19 10:23:26 -070097 stepBlock = stepBlock + caseStatements[index]
adminbae64d82013-08-01 10:50:15 -070098 index = index + 1
99 try :
100 while not re.match('main\.step',caseStatements[index],0) :
Jon Hallfeff3082015-05-19 10:23:26 -0700101 stepBlock = stepBlock + caseStatements[index]
adminbae64d82013-08-01 10:50:15 -0700102 if index < len(caseStatements)-1:
103 index = index + 1
104 else :
105 break
106 index = index - 1
Jon Hall1306a562015-09-04 11:21:24 -0700107 except IndexError:
adminbae64d82013-08-01 10:50:15 -0700108 print ''
109 stepCode[step] = stepBlock
110 step = step + 1
111 index = index + 1
112 # If there is no step defined !!
113 if not step_flag :
114 stepCode[step] = "".join(caseStatements)
115 return stepCode
Jon Hallfeff3082015-05-19 10:23:26 -0700116
adminbae64d82013-08-01 10:50:15 -0700117 def getStepCode(self):
118 case_step_code = {}
119 case_block = self.case_code()
adminbae64d82013-08-01 10:50:15 -0700120 for case in case_block :
121 case_step_code[case] = {}
122 step_block = self.step_code(case_block[case])
123 for step in step_block :
124 case_step_code[case][step] = step_block[step]
125 return case_step_code