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