Package TestON :: Package core :: Module testparser
[hide private]
[frames] | no frames]

Source Code for Module TestON.core.testparser

  1  #!/usr/bin/env python 
  2  ''' 
  3  Created 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  ''' 
 23  import re 
 24  import sys 
25 -class 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 # skip empty lines and single line comments 53 elif not re.match('#|^\s*$',testFileList[index],0): 54 self.statementsList.append(testFileList[index]) 55 index = index + 1
56
57 - def case_code(self):
58 index = 0 59 statementsList = self.statementsList 60 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 '' 78 self.caseCode [str(m.group(1))] = self.caseBlock 79 #print "Case CODE "+self.caseCode [str(m.group(1))] 80 index = index + 1 81 return self.caseCode
82
83 - def step_code(self,caseStatements):
84 index = 0 85 step = 0 86 stepCode = {} 87 step_flag = False 88 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 = '' 96 while i < index : 97 block += caseStatements[i] 98 i = i + 1 99 stepCode[step] = block 100 step = step + 1 101 stepBlock = stepBlock + caseStatements[index] 102 index = index + 1 103 try : 104 while not re.match('main\.step',caseStatements[index],0) : 105 stepBlock = stepBlock + caseStatements[index] 106 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
121
122 - def getStepCode(self):
123 case_step_code = {} 124 case_block = self.case_code() 125 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
131