admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1 | #/usr/bin/env python |
| 2 | ''' |
| 3 | Created on 20-Dec-2012 |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 4 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 5 | @author: Raghav Kashyap(raghavkashyap@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 Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 19 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 20 | |
| 21 | |
| 22 | ''' |
| 23 | import re |
| 24 | import inspect |
| 25 | |
| 26 | |
| 27 | class OpenSpeak: |
| 28 | |
| 29 | def __init__(self): |
| 30 | self.default = '' |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 31 | self.flag = 0 |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 32 | self.CurrentStep = 0 |
| 33 | self.grtrOrLssr = 0 |
| 34 | |
| 35 | def compiler(self,**compileParameters): |
| 36 | ''' |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 37 | This method will parse the openspeak file and will write to a python module with the equivalent translations. |
| 38 | It can accept OpenSpeak syntax in string or an OpenSpeak file as an input parameter. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 39 | Translated form can be written into python module if attribute "WRITETOFILE" is set to 1. |
| 40 | ''' |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 41 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 42 | args = self.parse_args(["OPENSPEAKFILE","TEXT","WRITETOFILE","FILEHANDLE"],**compileParameters) |
| 43 | resultString = '' |
| 44 | Test = "Mininet" |
| 45 | args["WRITETOFILE"] = args["WRITETOFILE"] if args["WRITETOFILE"] != None else 1 |
| 46 | self.CurrentStep = 0 |
| 47 | self.CurrentCase = '' |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 48 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 49 | ## here Open Speak file will be parsed by each line and translated. |
| 50 | if args["OPENSPEAKFILE"] !=None and args["TEXT"] ==None and args["FILEHANDLE"] == None: |
| 51 | self.openspeakfile = args["OPENSPEAKFILE"] |
| 52 | openSpeakFile = open(args["OPENSPEAKFILE"],"r").readlines() |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 53 | |
| 54 | elif args["OPENSPEAKFILE"] ==None and args["TEXT"] and args["FILEHANDLE"] == None: |
| 55 | openSpeakFile = args["TEXT"].split("\n") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 56 | elif args["FILEHANDLE"] and args["OPENSPEAKFILE"] ==None and args["TEXT"] ==None: |
| 57 | openSpeakFile = args["FILEHANDLE"].readlines() |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 58 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 59 | index = 0 |
| 60 | outputFile = [] |
| 61 | testName = re.search("\/(.*)\.ospk$",self.openspeakfile,0) |
| 62 | testName = testName.group(1) |
| 63 | testName = testName.split("/") |
| 64 | testName = testName[len(testName)-1] |
| 65 | outputFile.append("\nclass " + testName + " :" + "\n") |
| 66 | outputFile.append("\n" + " " * 4 + "def __init__(self) :") |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 67 | outputFile.append("\n" + " " * 8 + "self.default = \'\'" + "\n") |
| 68 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 69 | while index < len(openSpeakFile): |
| 70 | ifelseMatch = re.match("\s+IF|\s+ELSE|\s+ELIF",openSpeakFile[index],flags=0) |
| 71 | line = openSpeakFile[index] |
| 72 | repeatMatch = re.match("\s*REPEAT", openSpeakFile[index], flags=0) |
| 73 | if ifelseMatch : |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 74 | result = self.verify_and_translate(line) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 75 | initialSpaces = len(line) -len(line.lstrip()) |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 76 | self.outLoopSpace = initialSpaces |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 77 | nextLine = openSpeakFile[index+1] |
| 78 | nextinitialSpaces = len(nextLine) -len(nextLine.lstrip()) |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 79 | |
| 80 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 81 | while nextinitialSpaces > initialSpaces : |
| 82 | try : |
| 83 | elseMatch = re.match("\s*ELSE|\s*ELIF",nextLine,flags=0) |
| 84 | if elseMatch : |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 85 | self.flag = self.flag -1 |
| 86 | result = result + self.verify_and_translate(nextLine) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 87 | nextLine = openSpeakFile[index + 1] |
| 88 | nextinitialSpaces = len(nextLine) -len(nextLine.lstrip()) |
| 89 | except IndexError: |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 90 | pass |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 91 | index = index + 1 |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 92 | self.flag = 0 |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 93 | elif repeatMatch: |
| 94 | self.flag = 0 |
| 95 | result = self.verify_and_translate(line) |
| 96 | index = index + 1 |
| 97 | endMatch = re.match("\s*END",openSpeakFile[index],flags=0) |
| 98 | while not endMatch : |
| 99 | try : |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 100 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 101 | self.flag = self.flag + 1 |
| 102 | result = result + self.verify_and_translate(openSpeakFile[index]) |
| 103 | index = index + 1 |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 104 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 105 | except IndexError : |
| 106 | pass |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 107 | |
| 108 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 109 | else : |
| 110 | self.flag = 0 |
| 111 | result = self.verify_and_translate(line) |
| 112 | index = index + 1 |
| 113 | outputFile.append(result) |
| 114 | |
| 115 | if args["WRITETOFILE"] == 1 : |
| 116 | testscript = re.sub("ospk","py",self.openspeakfile,0) |
| 117 | testScript = open(testscript,"w") |
| 118 | for lines in outputFile : |
| 119 | testScript.write(lines) |
| 120 | testScript.close() |
| 121 | return resultString |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 122 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 123 | def verify_and_translate(self,line): |
| 124 | ''' |
| 125 | It will accept the each line and calls the suitable API to conver into pyton equivalent syntax . |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 126 | It will return the translated python syntax . |
| 127 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 128 | lineSpace = re.match("^\s+",line,flags=0) |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 129 | initialSpaces = len(line) -len(line.lstrip()) |
| 130 | line = re.sub("^\s+","",line) if lineSpace else line |
| 131 | |
| 132 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 133 | resultString = None |
| 134 | resultString = "\n" + " " * 4 if str(inspect.stack()[1][3]) == "compiler" else "\n" |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 135 | indent = " " *(4 + 4 * self.flag) if self.flag > 0 else " " * 4 |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 136 | caseMatch = re.search("^CASE\s+(\d+)",line,flags=0) |
| 137 | nameMatch = re.match("^NAME\s+\"(.*)\"",line,flags=0) |
| 138 | commentMatch = re.match("^COMMENT\s+\"(.*)\"",line,flags=0) |
| 139 | stepMatch = re.match("^STEP\s+\"(.*)\"",line,flags=0) |
| 140 | connectMatch = re.match("^CONNECT\s+(\w+)\s+USING\s+(.*)",line,flags=0) |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 141 | disconnectMatch = re.match("^DISCONNECT\s+(.*)",line,flags=0) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 142 | ondoMatch = re.match("^ON\s+(.*)\s+DO\s+(.*)",line,flags=0) |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 143 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 144 | storeMatch = re.match("^STORE\s+(.*)\s+IN\s+(.*)",line,flags=0) |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 145 | variableMatch = re.match("^(.*)\s+=\s+(.*)",line,flags=0) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 146 | assertMatch = re.match("^ASSERT\s+(\w+)\s+(.*)\s+(.*)\s+ONPASS\s+(.*)\s+ONFAIL\s+(.*)",line,flags=0) |
| 147 | logMatch = re.match("^(ERROR|INFO|DEBUG|CRITICAL|REPORT|EXACT|WARN)\s+(.*)",line,flags=0) |
| 148 | ifloop = re.match("IF\s+(\w+)\s*(..|\w+)\s*(.*)",line,flags=0) |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 149 | elseloopMatch = re.match("ELSE\s*$",line,flags=0) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 150 | elifloop = re.match("ELSE\sIF\s+(\w+)\s*(..|\w+)\s*(.*)",line,flags=0) |
| 151 | forloopMatch = re.match("\s*REPEAT\s+(/d+)\s+TIMES",line,flags=0) |
| 152 | experimentalMatch = re.match("EXPERIMENTAL\s+MODE\s+(\w+)",line,flags=0) |
| 153 | repeatMatch = re.match("\s*REPEAT\s+(\d+)\s+TIMES", line, flags=0) |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 154 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 155 | response_pasrse = re.match("\s*PARSE\s+(\w+)\s+AS\s+(\w+)\s+INTO\s+(\w+)", line, flags=0) |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 156 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 157 | if caseMatch : |
| 158 | self.CurrentStep = 0 |
| 159 | self.CurrentCase = "CASE" + caseMatch.group(1) |
| 160 | resultString = resultString + self.translate_case_block(casenumber=caseMatch.group(1)) |
| 161 | elif repeatMatch: |
| 162 | resultString = resultString + indent + self.translate_repeat(repeat=repeatMatch.group(1)) |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 163 | elif nameMatch : |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 164 | resultString = resultString + indent + self.translate_testcase_name(testname=nameMatch.group(1)) |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 165 | elif commentMatch : |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 166 | resultString = resultString + indent + self.translate_comment(comment=commentMatch.group(1)) |
| 167 | elif stepMatch : |
| 168 | self.CurrentStep = self.CurrentStep + 1 |
| 169 | resultString = resultString + indent + self.translate_step(step=stepMatch.group(1)) |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 170 | elif connectMatch : |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 171 | resultString = resultString + indent + self.translate_connect(component=connectMatch.group(1), |
| 172 | arguments=connectMatch.group(2) ) |
| 173 | elif disconnectMatch : |
| 174 | resultString = resultString + indent + self.translate_disconnect(component=disconnectMatch.group(1)) |
| 175 | elif ondoMatch : |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 176 | resultString = resultString + indent + self.translate_onDOAs(component=ondoMatch.group(1),action=ondoMatch.group(2)) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 177 | elif storeMatch : |
| 178 | resultString = resultString + indent + self.translate_store(variable=storeMatch.group(2), |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 179 | value=storeMatch.group(1)) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 180 | elif variableMatch : |
| 181 | resultString = resultString + indent + self.translate_store(variable=variableMatch.group(1), |
| 182 | value=variableMatch.group(2)) |
| 183 | elif assertMatch : |
| 184 | resultString = resultString + indent + self.translate_assertion(leftvalue=assertMatch.group(1), |
| 185 | operator=assertMatch.group(2), |
| 186 | rightvalue=assertMatch.group(3), |
| 187 | onpass=assertMatch.group(4), |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 188 | onfail=assertMatch.group(5)) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 189 | elif logMatch : |
| 190 | resultString = resultString + indent + self.translate_logs(loglevel=logMatch.group(1), |
| 191 | message=logMatch.group(2)) |
| 192 | elif ifloop : |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 193 | |
| 194 | self.initSpace = initialSpaces |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 195 | operand = ifloop.group(1) |
| 196 | operator = ifloop.group(2) |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 197 | value = ifloop.group(3) |
| 198 | resultString = resultString + indent + "if " + operand + self.translate_if_else_operator(conditionoperator=operator) + value + ":" |
| 199 | self.flag = self.flag + 1 |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 200 | elif experimentalMatch : |
| 201 | resultString = resultString + indent + self.translate_experimental_mode(mode=experimentalMatch.group(1)) |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 202 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 203 | elif elseloopMatch : |
| 204 | if initialSpaces == self.initSpace or initialSpaces == self.outLoopSpace: |
| 205 | resultString = resultString + indent + "else :" |
| 206 | self.flag = self.flag + 1 |
| 207 | else : |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 208 | indent = " " *(4 + 4 * (self.flag-1)) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 209 | resultString = resultString + indent + "else :" |
| 210 | self.flag = self.flag + 1 |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 211 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 212 | elif elifloop : |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 213 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 214 | operand = elifloop.group(1) |
| 215 | operator = elifloop.group(2) |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 216 | value = elifloop.group(3) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 217 | if initialSpaces == self.initSpace or initialSpaces == self.outLoopSpace: |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 218 | resultString = resultString + indent + "elif " + operand + self.translate_if_else_operator(conditionoperator=operator) + value + ":" |
| 219 | self.flag = self.flag + 1 |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 220 | else : |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 221 | indent = " " *(4 + 4 * (self.flag-1)) |
| 222 | resultString = resultString + indent + "elif " + operand + self.translate_if_else_operator(conditionoperator=operator) + value + ":" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 223 | self.flag = self.flag + 1 |
| 224 | elif response_pasrse : |
| 225 | output_string = response_pasrse.group(1) |
| 226 | req_format = response_pasrse.group(2) |
| 227 | store_in = response_pasrse.group(3) |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 228 | resultString = resultString + indent + store_in +'= main.response_parser('+output_string+",\""+req_format+"\")" |
| 229 | self.flag = self.flag + 1 |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 230 | |
| 231 | return resultString |
| 232 | |
| 233 | def translate_repeat(self,**repeatStatement): |
| 234 | ''' |
| 235 | this will transalte the repeat statement into a python equivalen while loop |
| 236 | ''' |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 237 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 238 | args = self.parse_args(["REPEAT"],**repeatStatement) |
| 239 | resultString = '' |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 240 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 241 | resultString = "i = 0" |
| 242 | resultString = resultString + "\n" + " " * 8 +"while i<" + args["REPEAT"] + " :" |
| 243 | return resultString |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 244 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 245 | def translate_if_else_operator(self,**loopBlock): |
| 246 | ''' |
| 247 | This method will translate if-else loop block into its equivalent python code. |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 248 | Whole loop block will be passed into loopBlock List. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 249 | It returns the transalted reuslt as a string. |
| 250 | ''' |
| 251 | args = self.parse_args(["CONDITIONOPERATOR"],**loopBlock) |
| 252 | resultString = '' |
| 253 | # process the loopBlock List translate all statements underlying the given loop block |
| 254 | equalsMatch = re.match("EQUALS$|==\s*$",args["CONDITIONOPERATOR"],flags=0) |
| 255 | greaterMatch = re.match("GREATER\s+THAN$|>\s*$",args["CONDITIONOPERATOR"],flags=0) |
| 256 | lesserMatch = re.match("LESSER\s+THAN$|<\s*$",args["CONDITIONOPERATOR"],flags=0) |
| 257 | greaterEqualMatch = re.match("GREATER\s+THAN\s+OR\s+EQUALS$|>=\s*$",args["CONDITIONOPERATOR"],flags=0) |
| 258 | lesserEqualMatch = re.match("LESSER\s+THAN\s+OR\s+EQUALS$|<=\s*$",args["CONDITIONOPERATOR"],flags=0) |
| 259 | if equalsMatch : |
| 260 | resultString = resultString + " == " |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 261 | elif greaterMatch : |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 262 | resultString = resultString + " > " |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 263 | elif lesserMatch : |
| 264 | resultString = resultString + " < " |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 265 | elif greaterEqualMatch: |
| 266 | resultString = resultString + " >= " |
| 267 | elif lesserEqualMatch : |
| 268 | resultString = resultString + " <= " |
| 269 | else : |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 270 | print "\n Error: Given Operator is not listed " |
| 271 | |
| 272 | return resultString |
| 273 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 274 | def translate_experimental_mode(self,**modeType): |
| 275 | ''' |
| 276 | This API will translate statment EXPERIMENTAL MODE ON/OFF into python equivalent. |
| 277 | It will return the transalted value. |
| 278 | ''' |
| 279 | args = self.parse_args(["MODE"],**modeType) |
| 280 | resultString = '' |
| 281 | ONmatch = re.match("\s*ON",args["MODE"],flags=0) |
| 282 | OFFmatch = re.match("\sOFF",args["MODE"],flags=0) |
| 283 | |
| 284 | if ONmatch : |
| 285 | resultString = "main.EXPERIMENTAL_MODE = main.TRUE" |
| 286 | elif OFFmatch : |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 287 | resultString = "main.EXPERIMENTAL_MODE = main.FALSE" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 288 | |
| 289 | return resultString |
| 290 | |
| 291 | def interpret(self,**interpetParameters): |
| 292 | ''' |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 293 | This method will accept the OpenSpeak syntax into a string and will return |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 294 | a python equivalent translations statement |
| 295 | ''' |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 296 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 297 | args = self.parse_args(["TEXT","WRITETOFILE"],**interpetParameters) |
| 298 | resultString = '' |
| 299 | ## here Open Speak syntax will be translated into python equivalent. |
| 300 | resultString = self.verify_and_translate(args["TEXT"]) |
| 301 | lineSpace = re.match("^\s+",resultString,flags=0) |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 302 | |
| 303 | resultString = re.sub("^\s+","",resultString) if lineSpace else resultString |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 304 | return resultString |
| 305 | |
| 306 | def translate_logs(self,**logStatement): |
| 307 | ''' |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 308 | This will translate the OpenSpeak log message statements into python equivalent |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 309 | to resultString and returns resultString |
| 310 | ''' |
| 311 | args = self.parse_args(["LOGLEVEL","MESSAGE"],**logStatement) |
| 312 | resultString = '' |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 313 | # convert the statement here |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 314 | message = self.translate_log_message(message=args["MESSAGE"]) |
| 315 | if args["LOGLEVEL"] == "INFO" : |
| 316 | resultString = resultString + "main.log.info(" + message + ")" |
| 317 | elif args["LOGLEVEL"] == "ERROR" : |
| 318 | resultString = resultString + "main.log.error(" + message + ")" |
| 319 | elif args["LOGLEVEL"] == "DEBUG" : |
| 320 | resultString = resultString + "main.log.debug(" + message + ")" |
| 321 | elif args["LOGLEVEL"] == "REPORT" : |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 322 | resultString = resultString + "main.log.report(" + message + ")" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 323 | elif args["LOGLEVEL"] == "CRITICAL" : |
| 324 | resultString = resultString + "main.log.critical(" + message + ")" |
| 325 | elif args["LOGLEVEL"] == "WARN" : |
| 326 | resultString = resultString + "main.log.warn(" + args["MESSAGE"] + ")" |
| 327 | elif args["LOGLEVEL"] == "EXACT" : |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 328 | resultString = resultString + "main.log.exact(" + args["MESSAGE"] + ")" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 329 | |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 330 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 331 | return resultString |
| 332 | |
| 333 | def translate_log_message(self,**messageStatement) : |
| 334 | ''' |
| 335 | This API will translate log messages if it is a string or Variable or combination |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 336 | of string and variable. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 337 | It will return the analysed and translate message. |
| 338 | ''' |
| 339 | args = self.parse_args(["MESSAGE"],**messageStatement) |
| 340 | resultString = '' |
| 341 | |
| 342 | paramsMatch = re.match("PARAMS\[(.*)\]|STEP\[(.*)\]|TOPO\[(.*)\]|CASE\[(.*)\]|LAST_RESULT|LAST_RESPONSE",args["MESSAGE"],flags=0) |
| 343 | stringMatch = re.match("\s*\"(.*)\"\s*$",args["MESSAGE"],flags=0) |
| 344 | stringWidVariableMatch = re.match("\"(.*)\"\s+\+\s+(.*)",args["MESSAGE"],flags=0) |
| 345 | varRefMatch = re.search("\<(\w+)\>",args["MESSAGE"],flags=0) |
| 346 | if paramsMatch : |
| 347 | resultString = resultString + self.translate_parameters(parameters=args["MESSAGE"]) |
| 348 | elif stringMatch : |
| 349 | resultString = resultString + args["MESSAGE"] |
| 350 | elif stringWidVariableMatch: |
| 351 | quoteWord = stringWidVariableMatch.group(1) |
| 352 | variableRef = stringWidVariableMatch.group(2) |
| 353 | varMatch = re.search("PARAMS\[(.*)\]|STEP\[(.*)\]|TOPO\[(.*)\]|CASE\[(.*)\]",variableRef,flags=0) |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 354 | varRefMatch = re.search("\<(\w+)\>",variableRef,flags=0) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 355 | if varMatch : |
| 356 | resultString = resultString + "\"" + quoteWord + "\"" + " + " + self.translate_parameters(parameters=variableRef) |
| 357 | elif varRefMatch : |
| 358 | resultString = resultString + "\"" + quoteWord + "\"" + " + " + varRefMatch.group(1) |
| 359 | elif varRefMatch: |
| 360 | resultString = resultString + varRefMatch.group(1) |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 361 | else : |
| 362 | print "\nError : Syntax error , Not defined way to give log message" + args["MESSAGE"] |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 363 | |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 364 | return resultString |
| 365 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 366 | def translate_assertion(self,**assertStatement): |
| 367 | ''' |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 368 | This will translate the ASSERT <value1> <COMPARISON OPERATOR> <value2> into python |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 369 | equivalent to resultString and returns resultString |
| 370 | ''' |
| 371 | args = self.parse_args(["LEFTVALUE","OPERATOR","RIGHTVALUE","ONPASS","ONFAIL"],**assertStatement) |
| 372 | resultString = '' |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 373 | # convert the statement here |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 374 | notOperatorMatch = re.search("NOT\s+(.*)",args["OPERATOR"],flags=0) |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 375 | notOperatorSymbMatch = re.search("\!(.*)",args["OPERATOR"],flags=0) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 376 | operator = '' |
| 377 | lastresultMatch = re.match("LAST_RESULT",args["RIGHTVALUE"],flags=0) |
| 378 | lastresponseMatch = re.match("LAST_RESPONSE",args["RIGHTVALUE"],flags=0) |
| 379 | if lastresultMatch : |
| 380 | operator = "main.last_result" |
| 381 | elif lastresponseMatch : |
| 382 | operator = "main.last_response" |
| 383 | else : |
| 384 | operator = args["RIGHTVALUE"] |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 385 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 386 | if args["OPERATOR"] == None or args["OPERATOR"] == "" : |
| 387 | print "\n Error : Operator has not been specified !!!" |
| 388 | elif notOperatorMatch or notOperatorSymbMatch: |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 389 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 390 | operators = notOperatorMatch.group(1) if notOperatorMatch else notOperatorSymbMatch.group(1) |
| 391 | operators = self.translate_operator(operator=operators) |
| 392 | if self.grtrOrLssr == 0 : |
| 393 | resultString = resultString + "utilities.assert_not_" + operators + "(expect=" +\ |
| 394 | self.translate_response_result(operator=args["RIGHTVALUE"]) + ",actual=" + self.translate_response_result(operator=args["LEFTVALUE"]) +\ |
| 395 | ",onpass=" + self.translate_assertMessage(message=args["ONPASS"]) +\ |
| 396 | ",onfail=" + self.translate_assertMessage(message=args["ONFAIL"]) + ")" |
| 397 | else : |
| 398 | resultString = resultString + "utilities.assert_not_" + operators + "(expect=" +\ |
| 399 | self.translate_response_result(operator=args["LEFTVALUE"]) + ",actual=" + self.translate_response_result(operator=args["RIGHTVALUE"]) +\ |
| 400 | ",onpass=" + self.translate_assertMessage(message=args["ONPASS"]) +\ |
| 401 | ",onfail=" + self.translate_assertMessage(message=args["ONFAIL"]) + ")" |
| 402 | |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 403 | else : |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 404 | operators = self.translate_operator(operator=args["OPERATOR"]) |
| 405 | if self.grtrOrLssr == 0 : |
| 406 | resultString = resultString + "utilities.assert_" + operators + "(expect=" +\ |
| 407 | self.translate_response_result(operator=args["RIGHTVALUE"]) +\ |
| 408 | ",actual=" + self.translate_response_result(operator=args["LEFTVALUE"]) +\ |
| 409 | ",onpass=" + self.translate_assertMessage(message=args["ONPASS"]) +\ |
| 410 | ",onfail=" + self.translate_assertMessage(message=args["ONFAIL"]) + ")" |
| 411 | else : |
| 412 | resultString = resultString + "utilities.assert_" + operators + "(expect=" +\ |
| 413 | self.translate_response_result(operator=args["LEFTVALUE"]) +\ |
| 414 | ",actual=" + self.translate_response_result(operator=args["RIGHTVALUE"]) +\ |
| 415 | ",onpass=" + self.translate_assertMessage(message=args["ONPASS"]) +\ |
| 416 | ",onfail=" + self.translate_assertMessage(message=args["ONFAIL"]) + ")" |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 417 | |
| 418 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 419 | return resultString |
| 420 | |
| 421 | def translate_response_result(self,**operatorStatement): |
| 422 | ''' |
| 423 | It will translate the LAST_RESPONSE or LAST_RESULT statement into its equivalent. |
| 424 | It returns the translate form in resulString. |
| 425 | ''' |
| 426 | args = self.parse_args(["OPERATOR"],**operatorStatement) |
| 427 | resultString = '' |
| 428 | lastResultMatch = re.match("LAST_RESULT",args["OPERATOR"],flags=0) |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 429 | lastResponseMatch = re.match("LAST_RESPONSE",args["OPERATOR"],flags=0) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 430 | if lastResultMatch : |
| 431 | resultString = resultString + "main.last_result" |
| 432 | elif lastResponseMatch: |
| 433 | resultString = resultString + "main.last_response" |
| 434 | else : |
| 435 | resultString = resultString + args["OPERATOR"] |
| 436 | return resultString |
| 437 | |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 438 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 439 | def translate_assertMessage(self,**messageStatement) : |
| 440 | ''' |
| 441 | This API will facilitate the translation of assert ONPASS or ONFAIL messages . The message can be |
| 442 | a string or calling another API in OpenSpeak syntax. |
| 443 | It will return the translated message |
| 444 | ''' |
| 445 | args = self.parse_args(["MESSAGE"],**messageStatement) |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 446 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 447 | connectMatch = re.search("CONNECT\s+(\w+)\s+USING\s+(.*)",args["MESSAGE"],flags=0) |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 448 | disconnectMatch = re.search("DISCONNECT\s+(.*)",args["MESSAGE"],flags=0) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 449 | ondoMatch = re.search("ON\s+(.*)\s+DO\s+(.*)",args["MESSAGE"],flags=0) |
| 450 | paramsMatch = re.search("PARAMS\[(.*)\]|STEP\[(.*)\]|TOPO\[(.*)\]|CASE\[(.*)\]",args["MESSAGE"],flags=0) |
| 451 | stringMatch = re.search("\"(.*)\"|\'(.*)\'",args["MESSAGE"],flags=0) |
| 452 | variableMatch = re.search("\<(.*)\>",args["MESSAGE"],flags=0) |
| 453 | |
| 454 | resultString = '' |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 455 | if connectMatch : |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 456 | resultString = resultString + self.translate_connect(component=connectMatch.group(1), |
| 457 | arguments=connectMatch.group(2) ) |
| 458 | elif disconnectMatch : |
| 459 | resultString = resultString + self.translate_disconnect(component=disconnectMatch.group(1)) |
| 460 | elif ondoMatch : |
| 461 | resultString = resultString + self.translate_onDOAs(component=ondoMatch.group(1), |
| 462 | action=ondoMatch.group(2)) |
| 463 | elif paramsMatch : |
| 464 | resultString = resultString + self.translate_parameters(parameters=args["MESSAGE"]) |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 465 | elif stringMatch : |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 466 | resultString = resultString + "\"" + stringMatch.group(1) + "\"" |
| 467 | elif variableMatch : |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 468 | resultString = resultString + variableMatch.group(1) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 469 | elif args["MESSAGE"] == None : |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 470 | print "\n Error : Please pass a message or action for assertion " |
| 471 | |
| 472 | return resultString |
| 473 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 474 | def translate_operator(self,**operatorStatement) : |
| 475 | ''' |
| 476 | It will translate the operator for assertion , by ensuring against given arguments. |
| 477 | It will return the translated assertion operator. |
| 478 | ''' |
| 479 | args = self.parse_args(["OPERATOR"],**operatorStatement) |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 480 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 481 | resultString = '' |
| 482 | equalsMatch = re.match("EQUALS$|==$",args["OPERATOR"],flags=0) |
| 483 | greaterMatch = re.match("GREATER\s+THAN$|>$",args["OPERATOR"],flags=0) |
| 484 | lesserMatch = re.match("LESSER\s+THAN$|<$",args["OPERATOR"],flags=0) |
| 485 | stringMatch = re.match("MATCHES|~$",args["OPERATOR"],flags=0) |
| 486 | greaterEqualMatch = re.match("GREATER\s+THAN\s+OR\s+EQUALS$|>=$",args["OPERATOR"],flags=0) |
| 487 | lesserEqualMatch = re.match("LESSER\s+THAN\s+OR\s+EQUALS$|<=$",args["OPERATOR"],flags=0) |
| 488 | if equalsMatch : |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 489 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 490 | resultString = resultString + "equals" |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 491 | elif greaterMatch : |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 492 | self.grtrOrLssr = self.grtrOrLssr + 1 |
| 493 | resultString = resultString + "greater" |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 494 | elif lesserMatch : |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 495 | self.grtrOrLssr = self.grtrOrLssr + 1 |
| 496 | resultString = resultString + "lesser" |
| 497 | elif stringMatch : |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 498 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 499 | resultString = resultString + "matches" |
| 500 | elif greaterEqualMatch: |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 501 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 502 | resultString = resultString + "greater_equals" |
| 503 | elif lesserEqualMatch : |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 504 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 505 | resultString = resultString + "lesser_equals" |
| 506 | else : |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 507 | print "\n Error: Given Operator is not listed for assertion" |
| 508 | return resultString |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 509 | |
| 510 | def translate_store(self,**storeStatement): |
| 511 | ''' |
| 512 | This will translate the STORE <variable> IN <value> or <variable> = <value> |
| 513 | into python equivalent to resultString and returns resultString |
| 514 | ''' |
| 515 | args = self.parse_args(["VARIABLE","VALUE"],**storeStatement) |
| 516 | resultString = '' |
| 517 | # convert the statement here |
| 518 | ondoMatch = re.match("^\s*ON\s+(.*)\s+DO\s+(.*)",args["VALUE"],flags=0) |
| 519 | paramsMatch = re.match("^\s*PARAMS\[(.*)\]|STEP\[(.*)\]|TOPO\[(.*)\]|CASE\[(.*)\]|LAST_RESULT|LAST_RESPONSE",args["VALUE"],flags=0) |
| 520 | if paramsMatch : |
| 521 | argString = self.translate_parameters(parameters=args["VALUE"]) |
| 522 | resultString = args["VARIABLE"] + " = " + argString |
| 523 | elif ondoMatch : |
| 524 | resultString = args["VARIABLE"] + " = " + self.translate_onDOAs(component=ondoMatch.group(1),action=ondoMatch.group(2)) |
| 525 | else : |
| 526 | resultString = args["VARIABLE"] + " = " + args["VALUE"] |
| 527 | |
| 528 | |
| 529 | return resultString |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 530 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 531 | def translate_disconnect(self,**disconnectStatement): |
| 532 | ''' |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 533 | This will translate the DISCONNECT <component_name> into python |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 534 | equivalent to resultString and returns resultString |
| 535 | ''' |
| 536 | args = self.parse_args(["COMPONENT"],**disconnectStatement) |
| 537 | resultString = '' |
| 538 | # convert the statement here |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 539 | resultString = "main." + args["COMPONENT"] + ".disconnect()" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 540 | return resultString |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 541 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 542 | def translate_onDOAs(self,**onDoStatement): |
| 543 | ''' |
| 544 | This will translate the ON <component> DO <action> USING <arg1> AS <value1>,<arg2> AS <value2> |
| 545 | into python equivalent to resultString and returns resultString |
| 546 | ''' |
| 547 | args = self.parse_args(["COMPONENT","ACTION","ARGUMENTS"],**onDoStatement) |
| 548 | subString = '' |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 549 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 550 | usingMatch = re.match("\s*(.*)\s+USING\s+(.*)",args["ACTION"],flags=0) |
| 551 | action = '' |
| 552 | if usingMatch : |
| 553 | action = usingMatch.group(1) |
| 554 | arguments = usingMatch.group(2) |
| 555 | subString = self.translate_usingas(arguments=arguments) |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 556 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 557 | else : |
| 558 | andCheck = re.search ("(.*)\s+AND\s+(.*)",args["ACTION"],flags=0) |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 559 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 560 | action = action + "()" |
| 561 | if andCheck: |
| 562 | action = andCheck.group(1) + "()" |
| 563 | subString = subString + self.handle_conjuction(statement=andCheck.group(2)) |
| 564 | else : |
| 565 | action = args["ACTION"] |
| 566 | action = action + "()" |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 567 | # convert the statement here |
| 568 | resultString = "main." + args["COMPONENT"] + "." + action + subString |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 569 | return resultString |
| 570 | |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 571 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 572 | def handle_conjuction(self,**conjuctStatement): |
| 573 | ''' |
| 574 | This will handle the conjuctions |
| 575 | ''' |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 576 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 577 | args = self.parse_args(["STATEMENT"],**conjuctStatement) |
| 578 | subSentence = '' |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 579 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 580 | storeMatch = re.match("\s*STORE\s+(.*)\s+IN\s+(.*)",args["STATEMENT"],flags=0) |
| 581 | assertMatch = re.match("\s*ASSERT\s+(\w+)\s+(.*)\s+(.*)\s+ONPASS\s+(.*)\s+ONFAIL\s+(.*)",args["STATEMENT"],flags=0) |
| 582 | if storeMatch : |
| 583 | subSentence = "\n" + " " * 8 + self.translate_store(variable=storeMatch.group(2), |
| 584 | value=storeMatch.group(1)) |
| 585 | elif assertMatch : |
| 586 | subSentence = "\n" + " " * 8 + self.translate_assertion(leftvalue=assertMatch.group(1), |
| 587 | operator=assertMatch.group(2), |
| 588 | rightvalue=assertMatch.group(3), |
| 589 | onpass=assertMatch.group(4), |
| 590 | onfail=assertMatch.group(5)) |
| 591 | return subSentence |
| 592 | |
| 593 | def translate_usingas(self,**argumentAS) : |
| 594 | ''' |
| 595 | This will tranlate USING argument AS value Statement into equivalent argument passing. |
| 596 | It will return translated form into resultString |
| 597 | ''' |
| 598 | args = self.parse_args(["ARGUMENTS"],**argumentAS) |
| 599 | resultString = '' |
| 600 | argsList = [] |
| 601 | subString = '' |
| 602 | subSentence = '' |
| 603 | line = '' |
| 604 | andCheck = re.search ("(.*)\s+AND\s+(.*)",args["ARGUMENTS"],flags=0) |
| 605 | if andCheck: |
| 606 | line = andCheck.group(1) |
| 607 | subSentence = self.handle_conjuction(statement=andCheck.group(2)) |
| 608 | else : |
| 609 | line = args["ARGUMENTS"] |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 610 | |
| 611 | |
| 612 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 613 | argsMatch = re.search("(.*),(.*)",line,flags=0) |
| 614 | |
| 615 | |
| 616 | if args["ARGUMENTS"] == None or args["ARGUMENTS"] == '' : |
| 617 | subString = '' |
| 618 | elif argsMatch : |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 619 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 620 | argsList = line.split(",") |
| 621 | for index, arguments in enumerate(argsList): |
| 622 | argMatch = re.search("(.*)\s+AS\s+(.*)",arguments,flags=0) |
| 623 | if argMatch: |
| 624 | argsKey = argMatch.group(1) |
| 625 | argsValue = argMatch.group(2) |
| 626 | paramsMatch = re.search("PARAMS\[(.*)\]|STEP\[(.*)\]|TOPO\[(.*)\]|CASE\[(.*)\]|LAST_RESPONSE|LAST_RESULT",argsValue,flags=0) |
| 627 | if not paramsMatch : |
| 628 | if index == len(argsList) - 1 : |
| 629 | subString = subString + argsKey + "=" + argsValue |
| 630 | else : |
| 631 | subString = subString + argsKey + "=" + argsValue + "," |
| 632 | else : |
| 633 | argString = self.translate_parameters(parameters=argsValue) |
| 634 | if index == len(argsList) - 1 : |
| 635 | subString = subString + argsKey + "=" + argString |
| 636 | else : |
| 637 | subString = subString + argsKey + "=" + argString + "," |
| 638 | else : |
| 639 | if index == len(argsList) - 1 : |
| 640 | subString = subString + arguments |
| 641 | else : |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 642 | subString = subString + arguments + "," |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 643 | else : |
| 644 | argMatch = re.search("(.*)\s+AS\s+(.*)",args["ARGUMENTS"],flags=0) |
| 645 | if argMatch: |
| 646 | argsKey = argMatch.group(1) |
| 647 | argsValue = argMatch.group(2) |
| 648 | paramsMatch = re.search("PARAMS\[(.*)\]|STEP\[(.*)\]|TOPO\[(.*)\]|CASE\[(.*)\]|LAST_RESPONSE|LAST_RESULT",argsValue,flags=0) |
| 649 | if not paramsMatch : |
| 650 | subString = subString + argsKey + "=" + argsValue |
| 651 | else : |
| 652 | argString = self.translate_parameters(parameters=argsValue) |
| 653 | subString = subString + argsKey + "=" + argString |
| 654 | else : |
| 655 | paramsMatch = re.match("PARAMS\[(.*)\]|STEP\[(.*)\]|TOPO\[(.*)\]|CASE\[(.*)\]|LAST_RESPONSE|LAST_RESULT",line,flags=0) |
| 656 | if paramsMatch : |
| 657 | subString = subString + self.translate_parameters(parameters=line) |
| 658 | else : |
| 659 | subString = subString + line |
| 660 | resultString = "(" + subString + ")"+ subSentence |
| 661 | return resultString |
| 662 | |
| 663 | |
| 664 | def translate_connect(self,**connectStatement): |
| 665 | ''' |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 666 | This will translate the CONNECT <component_name> USING1 <arg1> AS <value1>, <arg2> AS <value2> |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 667 | into python equivalent to resultString and returns resultString |
| 668 | ''' |
| 669 | args = self.parse_args(["COMPONENT","ARGUMENTS"],**connectStatement) |
| 670 | resultString = '' |
| 671 | subString = self.translate_usingas(arguments=args["ARGUMENTS"]) |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 672 | # convert the statement here |
| 673 | resultString = "main." + args["COMPONENT"] + ".connect(" + subString + ")" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 674 | return resultString |
| 675 | |
| 676 | |
| 677 | def translate_parameters(self,**parameterStatement): |
| 678 | ''' |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 679 | This will translate the OpenSpeak Case and Params parameters into python equivalent |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 680 | to resultString and returns resultString |
| 681 | ''' |
| 682 | args = self.parse_args(["PARAMETERS"],**parameterStatement) |
| 683 | argument = args["PARAMETERS"] |
| 684 | resultString = '' |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 685 | ### match arguments |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 686 | paramsMatch = re.search("PARAMS((\[(.*)\])*)",argument,flags=0) |
| 687 | stepsMatch = re.search("STEP((\[(.*)\])*)",argument,flags=0) |
| 688 | casesMatch = re.search("CASE((\[(.*)\])*)",argument,flags=0) |
| 689 | topoMatch = re.search("TOPO((\[(.*)\])*)",argument,flags=0) |
| 690 | lastResultMatch = re.match("LAST_RESULT",argument,flags=0) |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 691 | lastResponseMatch = re.match("LAST_RESPONSE",argument,flags=0) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 692 | # convert the statement here |
| 693 | if paramsMatch : |
| 694 | params = paramsMatch.group(1) |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 695 | resultString = resultString + "main.params" + self._argsCheck(checkvar=params) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 696 | elif stepsMatch : |
| 697 | resultString = resultString +"main.params[\'" + self.CurrentCase +\ |
| 698 | "\'][\'STEP" + str(self.CurrentStep) + "\']" +\ |
| 699 | self._argsCheck(checkvar=stepsMatch.group(1)) |
| 700 | elif casesMatch : |
| 701 | resultString = resultString + "main.params[\'" + self.CurrentCase + "\']" +\ |
| 702 | self._argsCheck(checkvar=casesMatch.group(1)) |
| 703 | elif topoMatch : |
| 704 | resultString = resultString + "main.componentDictionary" +\ |
| 705 | self._argsCheck(checkvar=topoMatch.group(1)) |
| 706 | elif lastResultMatch : |
| 707 | resultString = resultString + "main.last_result" |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 708 | elif lastResponseMatch : |
| 709 | resultString = resultString + "main.last_response" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 710 | return resultString |
| 711 | |
| 712 | def _argsCheck(self,**args): |
| 713 | ''' This API will check if given argument is varibale reference or String and will translate accordingly. |
| 714 | It will return the tanslate form in resultString. |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 715 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 716 | args = self.parse_args(["CHECKVAR"],**args) |
| 717 | params = args["CHECKVAR"] |
| 718 | argsList = params.split("]") |
| 719 | resultString = '' |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 720 | del argsList[len(argsList) - 1] |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 721 | for index,paramArgs in enumerate(argsList) : |
| 722 | argsWidVariable = re.search("(\"|\')\s*(\w+)\s*(\'|\")",paramArgs,flags=0) |
| 723 | if argsWidVariable : |
| 724 | resultString = resultString + "[\'" + argsWidVariable.group(2) + "\']" |
| 725 | else : |
| 726 | resultString = resultString + paramArgs + "]" |
| 727 | return resultString |
| 728 | |
| 729 | def translate_step(self,**stepStatement): |
| 730 | ''' |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 731 | This will translate the STEP "DO SOMETHING HERE" into python equivalent |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 732 | to resultString and returns resultString |
| 733 | ''' |
| 734 | args = self.parse_args(["STEP"],**stepStatement) |
| 735 | resultString = '' |
| 736 | resultString = "main.step(\"" + args["STEP"] + "\")" |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 737 | # convert the statement here |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 738 | return resultString |
| 739 | |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 740 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 741 | def translate_comment(self,**commentStatement): |
| 742 | ''' |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 743 | This will translate the COMMENT "DO SOMETHING HERE" into python equivalent |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 744 | to resultString and returns resultString |
| 745 | ''' |
| 746 | args = self.parse_args(["COMMENT"],**commentStatement) |
| 747 | resultString = '' |
| 748 | resultString = "#" + args["COMMENT"] |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 749 | # convert the statement here |
| 750 | return resultString |
| 751 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 752 | def translate_testcase_name(self,**nameStatement): |
| 753 | ''' |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 754 | This method will convert NAME "<Testcase_name>" into python equivalent statement |
| 755 | to resultString and returns resultString |
| 756 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 757 | args = self.parse_args(["TESTNAME"],**nameStatement) |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 758 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 759 | resultString = '' |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 760 | resultString = "main.case(\"" + args["TESTNAME"] + "\")" |
| 761 | # convert the statement here |
| 762 | return resultString |
| 763 | |
| 764 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 765 | def translate_case_block(self,**caseBlock): |
| 766 | ''' |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 767 | This method will translate the case block in test script . |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 768 | It returns the translated equivalent python code for test script |
| 769 | ''' |
| 770 | args = self.parse_args(["CASENUMBER"],**caseBlock) |
| 771 | resultString = "" |
| 772 | resultString = "def CASE" + str(args["CASENUMBER"]) + "(self,main) :\n" |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 773 | # process the caseBlock List translate all statements underlying the given case |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 774 | return resultString |
| 775 | |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 776 | |
| 777 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 778 | def translate_loop_block(self,*loopBlock): |
| 779 | ''' |
| 780 | This method will translate for loop block into its equivalent python code. |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 781 | Whole loop block will be passed into loopBlock List. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 782 | It returns the transalted reuslt as a string. |
| 783 | ''' |
| 784 | resultString = '' |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 785 | # process the loopBlock List translate all statements underlying the given loop block |
| 786 | return resultString |
| 787 | |
| 788 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 789 | def translate_conjuction(self,conjuctionStatement): |
| 790 | ''' |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 791 | This will translate the AND conjuction statements into python equivalent |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 792 | to resultString and returns resultString |
| 793 | ''' |
| 794 | resultString = '' |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 795 | # convert the statement here |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 796 | return resultString |
| 797 | |
| 798 | |
| 799 | def parse_args(self,args, **kwargs): |
| 800 | ''' |
| 801 | It will accept the (key,value) pair and will return the (key,value) pairs with keys in uppercase. |
| 802 | ''' |
| 803 | newArgs = {} |
| 804 | for key,value in kwargs.iteritems(): |
| 805 | #currentKey = str.upper(key) |
| 806 | if isinstance(args,list) and str.upper(key) in args: |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 807 | for each in args: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 808 | if each==str.upper(key): |
| 809 | newArgs [str(each)] = value |
| 810 | elif each != str.upper(key) and (newArgs.has_key(str(each)) == False ): |
| 811 | newArgs[str(each)] = None |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 812 | |
| 813 | |
| 814 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 815 | return newArgs |