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  import re 
  8  import sys 
9 -class TestParser:
10 - def __init__(self,testFile):
11 try : 12 testFileHandler = open(testFile, 'r') 13 except IOError: 14 print "No such file "+testFile 15 sys.exit(0) 16 17 testFileList = testFileHandler.readlines() 18 self.testscript = testFileList 19 self.caseCode = {} 20 self.caseBlock = '' 21 self.statementsList = [] 22 index = 0 23 self.statementsList = [] 24 #initialSpaces = len(line) -len(line.lstrip()) 25 while index < len(testFileList): 26 testFileList[index] = re.sub("^\s{8}|^\s{4}", "", testFileList[index]) 27 # Skip multiline comments 28 if re.match('^(\'\'\')|^(\"\"\")',testFileList[index],0) : 29 index = index + 1 30 try : 31 while not re.match('^\s*(\'\'\')|^\s*(\"\"\")',testFileList[index],0) : 32 index = index + 1 33 except IndexError,e: 34 print '' 35 36 37 # skip empty lines and single line comments 38 elif not re.match('#|^\s*$',testFileList[index],0): 39 self.statementsList.append(testFileList[index]) 40 index = index + 1
41
42 - def case_code(self):
43 index = 0 44 statementsList = self.statementsList 45 while index < len(statementsList): 46 #print statementsList[index] 47 m= re.match('def\s+CASE(\d+)',statementsList[index],0) 48 self.caseBlock = [] 49 if m: 50 #print m.group(1) 51 index = index + 1 52 try : 53 while not re.match('\s*def\s+CASE(\d+)',statementsList[index],0) : 54 self.caseBlock.append(statementsList[index]) 55 if index < len(statementsList)-1: 56 index = index + 1 57 else : 58 break 59 index = index - 1 60 except IndexError,e: 61 #print 'IndexError' 62 print '' 63 64 self.caseCode [str(m.group(1))] = self.caseBlock 65 #print "Case CODE "+self.caseCode [str(m.group(1))] 66 index = index + 1 67 68 return self.caseCode
69
70 - def step_code(self,caseStatements):
71 index = 0 72 step = 0 73 stepCode = {} 74 step_flag = False 75 while index < len(caseStatements): 76 m= re.match('main\.step',caseStatements[index],0) 77 stepBlock = '' 78 if m: 79 step_flag = True 80 if step == 0 : 81 i = 0 82 block = '' 83 while i <= index : 84 block += caseStatements[i] 85 i = i + 1 86 stepCode[step] = block 87 step = step + 1 88 stepBlock= stepBlock + caseStatements[index] 89 index = index + 1 90 try : 91 while not re.match('main\.step',caseStatements[index],0) : 92 stepBlock= stepBlock + caseStatements[index] 93 if index < len(caseStatements)-1: 94 index = index + 1 95 else : 96 break 97 index = index - 1 98 except IndexError,e: 99 #print 'IndexError' 100 print '' 101 stepCode[step] = stepBlock 102 step = step + 1 103 index = index + 1 104 # If there is no step defined !! 105 if not step_flag : 106 stepCode[step] = "".join(caseStatements) 107 return stepCode
108
109 - def getStepCode(self):
110 case_step_code = {} 111 case_block = self.case_code() 112 113 for case in case_block : 114 case_step_code[case] = {} 115 step_block = self.step_code(case_block[case]) 116 for step in step_block : 117 case_step_code[case][step] = step_block[step] 118 return case_step_code
119