blob: 24b1ca22d3467aea53c2e377a09723163d6965e6 [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
49 except IndexError,e:
50 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):
61 #print statementsList[index]
62 m= re.match('def\s+CASE(\d+)',statementsList[index],0)
63 self.caseBlock = []
64 if m:
65 #print m.group(1)
66 index = index + 1
67 try :
68 while not re.match('\s*def\s+CASE(\d+)',statementsList[index],0) :
69 self.caseBlock.append(statementsList[index])
70 if index < len(statementsList)-1:
71 index = index + 1
72 else :
73 break
74 index = index - 1
75 except IndexError,e:
76 #print 'IndexError'
77 print ''
adminbae64d82013-08-01 10:50:15 -070078 self.caseCode [str(m.group(1))] = self.caseBlock
79 #print "Case CODE "+self.caseCode [str(m.group(1))]
80 index = index + 1
Jon Hallfeff3082015-05-19 10:23:26 -070081 return self.caseCode
82
adminbae64d82013-08-01 10:50:15 -070083 def step_code(self,caseStatements):
84 index = 0
Jon Hallfeff3082015-05-19 10:23:26 -070085 step = 0
86 stepCode = {}
87 step_flag = False
adminbae64d82013-08-01 10:50:15 -070088 while index < len(caseStatements):
89 m= re.match('main\.step',caseStatements[index],0)
90 stepBlock = ''
91 if m:
92 step_flag = True
93 if step == 0 :
94 i = 0
95 block = ''
Jon Hall9910c2b2015-01-21 15:55:56 -080096 while i < index :
adminbae64d82013-08-01 10:50:15 -070097 block += caseStatements[i]
98 i = i + 1
Jon Hallfeff3082015-05-19 10:23:26 -070099 stepCode[step] = block
adminbae64d82013-08-01 10:50:15 -0700100 step = step + 1
Jon Hallfeff3082015-05-19 10:23:26 -0700101 stepBlock = stepBlock + caseStatements[index]
adminbae64d82013-08-01 10:50:15 -0700102 index = index + 1
103 try :
104 while not re.match('main\.step',caseStatements[index],0) :
Jon Hallfeff3082015-05-19 10:23:26 -0700105 stepBlock = stepBlock + caseStatements[index]
adminbae64d82013-08-01 10:50:15 -0700106 if index < len(caseStatements)-1:
107 index = index + 1
108 else :
109 break
110 index = index - 1
111 except IndexError,e:
112 #print 'IndexError'
113 print ''
114 stepCode[step] = stepBlock
115 step = step + 1
116 index = index + 1
117 # If there is no step defined !!
118 if not step_flag :
119 stepCode[step] = "".join(caseStatements)
120 return stepCode
Jon Hallfeff3082015-05-19 10:23:26 -0700121
adminbae64d82013-08-01 10:50:15 -0700122 def getStepCode(self):
123 case_step_code = {}
124 case_block = self.case_code()
adminbae64d82013-08-01 10:50:15 -0700125 for case in case_block :
126 case_step_code[case] = {}
127 step_block = self.step_code(case_block[case])
128 for step in step_block :
129 case_step_code[case][step] = step_block[step]
130 return case_step_code