blob: f158259edc623e3b72ab1063ea9be84fc0a7a7e4 [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
19 along with TestON. If not, see <http://www.gnu.org/licenses/>.
20
21
22'''
23import re
24import sys
25class TestParser:
26 def __init__(self,testFile):
27 try :
28 testFileHandler = open(testFile, 'r')
29 except IOError:
30 print "No such file "+testFile
31 sys.exit(0)
32
33 testFileList = testFileHandler.readlines()
34 self.testscript = testFileList
35 self.caseCode = {}
36 self.caseBlock = ''
37 self.statementsList = []
38 index = 0
39 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])
43 # Skip multiline comments
44 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 ''
51
52
53 # skip empty lines and single line comments
54 elif not re.match('#|^\s*$',testFileList[index],0):
55 self.statementsList.append(testFileList[index])
56 index = index + 1
57
58 def case_code(self):
59 index = 0
60 statementsList = self.statementsList
61 while index < len(statementsList):
62 #print statementsList[index]
63 m= re.match('def\s+CASE(\d+)',statementsList[index],0)
64 self.caseBlock = []
65 if m:
66 #print m.group(1)
67 index = index + 1
68 try :
69 while not re.match('\s*def\s+CASE(\d+)',statementsList[index],0) :
70 self.caseBlock.append(statementsList[index])
71 if index < len(statementsList)-1:
72 index = index + 1
73 else :
74 break
75 index = index - 1
76 except IndexError,e:
77 #print 'IndexError'
78 print ''
79
80 self.caseCode [str(m.group(1))] = self.caseBlock
81 #print "Case CODE "+self.caseCode [str(m.group(1))]
82 index = index + 1
83
84 return self.caseCode
85
86 def step_code(self,caseStatements):
87 index = 0
88 step = 0
89 stepCode = {}
90 step_flag = False
91 while index < len(caseStatements):
92 m= re.match('main\.step',caseStatements[index],0)
93 stepBlock = ''
94 if m:
95 step_flag = True
96 if step == 0 :
97 i = 0
98 block = ''
99 while i <= index :
100 block += caseStatements[i]
101 i = i + 1
102 stepCode[step] = block
103 step = step + 1
104 stepBlock= stepBlock + caseStatements[index]
105 index = index + 1
106 try :
107 while not re.match('main\.step',caseStatements[index],0) :
108 stepBlock= stepBlock + caseStatements[index]
109 if index < len(caseStatements)-1:
110 index = index + 1
111 else :
112 break
113 index = index - 1
114 except IndexError,e:
115 #print 'IndexError'
116 print ''
117 stepCode[step] = stepBlock
118 step = step + 1
119 index = index + 1
120 # If there is no step defined !!
121 if not step_flag :
122 stepCode[step] = "".join(caseStatements)
123 return stepCode
124
125 def getStepCode(self):
126 case_step_code = {}
127 case_block = self.case_code()
128
129 for case in case_block :
130 case_step_code[case] = {}
131 step_block = self.step_code(case_block[case])
132 for step in step_block :
133 case_step_code[case][step] = step_block[step]
134 return case_step_code