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