blob: 1b351f9eca0ed316f594dac350d63315ec478281 [file] [log] [blame]
Jeremy Ronquillo696f4262017-10-17 10:56:26 -07001# /usr/bin/env python
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002'''
adminbae64d82013-08-01 10:50:15 -07003Created on 20-Dec-2012
Jeremy Songsterae01bba2016-07-11 15:39:17 -07004Modified 2015 by ON.Lab
Jon Hall4ba53f02015-07-29 13:07:41 -07005
Jeremy Songsterae01bba2016-07-11 15:39:17 -07006Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>,
7the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>,
8or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg>
adminbae64d82013-08-01 10:50:15 -07009
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 Ronquillo4d5f1d02017-10-13 20:23:57 +000013 (at your option) any later version.
adminbae64d82013-08-01 10:50:15 -070014
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 Hall4ba53f02015-07-29 13:07:41 -070021 along with TestON. If not, see <http://www.gnu.org/licenses/>.
adminbae64d82013-08-01 10:50:15 -070022
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +000023'''
adminbae64d82013-08-01 10:50:15 -070024import re
25import inspect
26
adminbae64d82013-08-01 10:50:15 -070027class OpenSpeak:
28
Jeremy Ronquillo696f4262017-10-17 10:56:26 -070029 def __init__( self ):
adminbae64d82013-08-01 10:50:15 -070030 self.default = ''
Jon Hall4ba53f02015-07-29 13:07:41 -070031 self.flag = 0
adminbae64d82013-08-01 10:50:15 -070032 self.CurrentStep = 0
33 self.grtrOrLssr = 0
34
Jeremy Ronquillo696f4262017-10-17 10:56:26 -070035 def compiler( self, **compileParameters ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +000036 '''
Jon Hall4ba53f02015-07-29 13:07:41 -070037 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.
adminbae64d82013-08-01 10:50:15 -070039 Translated form can be written into python module if attribute "WRITETOFILE" is set to 1.
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +000040 '''
41
Jeremy Ronquillo696f4262017-10-17 10:56:26 -070042 args = self.parse_args( [ "OPENSPEAKFILE", "TEXT", "WRITETOFILE", "FILEHANDLE" ], **compileParameters )
adminbae64d82013-08-01 10:50:15 -070043 resultString = ''
44 Test = "Mininet"
Jeremy Ronquillo696f4262017-10-17 10:56:26 -070045 args[ "WRITETOFILE" ] = args[ "WRITETOFILE" ] if args[ "WRITETOFILE" ] is not None else 1
adminbae64d82013-08-01 10:50:15 -070046 self.CurrentStep = 0
47 self.CurrentCase = ''
Jon Hall4ba53f02015-07-29 13:07:41 -070048
Jeremy Ronquillo696f4262017-10-17 10:56:26 -070049 # 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 Hall4ba53f02015-07-29 13:07:41 -070053
Jeremy Ronquillo696f4262017-10-17 10:56:26 -070054 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 Hall4ba53f02015-07-29 13:07:41 -070058
adminbae64d82013-08-01 10:50:15 -070059 index = 0
60 outputFile = []
Jeremy Ronquillo696f4262017-10-17 10:56:26 -070061 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 Hall4ba53f02015-07-29 13:07:41 -070068
Jeremy Ronquillo696f4262017-10-17 10:56:26 -070069 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 Hall4ba53f02015-07-29 13:07:41 -070076 self.outLoopSpace = initialSpaces
Jeremy Ronquillo696f4262017-10-17 10:56:26 -070077 nextLine = openSpeakFile[ index + 1 ]
78 nextinitialSpaces = len( nextLine ) - len( nextLine.lstrip() )
Jon Hall4ba53f02015-07-29 13:07:41 -070079
Jeremy Ronquillo696f4262017-10-17 10:56:26 -070080 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() )
adminbae64d82013-08-01 10:50:15 -070088 except IndexError:
Jon Hall4ba53f02015-07-29 13:07:41 -070089 pass
adminbae64d82013-08-01 10:50:15 -070090 index = index + 1
Jon Hall4ba53f02015-07-29 13:07:41 -070091 self.flag = 0
adminbae64d82013-08-01 10:50:15 -070092 elif repeatMatch:
93 self.flag = 0
Jeremy Ronquillo696f4262017-10-17 10:56:26 -070094 result = self.verify_and_translate( line )
adminbae64d82013-08-01 10:50:15 -070095 index = index + 1
Jeremy Ronquillo696f4262017-10-17 10:56:26 -070096 endMatch = re.match( "\s*END", openSpeakFile[ index ], flags=0 )
97 while not endMatch:
98 try:
Jon Hall4ba53f02015-07-29 13:07:41 -070099
adminbae64d82013-08-01 10:50:15 -0700100 self.flag = self.flag + 1
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700101 result = result + self.verify_and_translate( openSpeakFile[ index ] )
adminbae64d82013-08-01 10:50:15 -0700102 index = index + 1
Jon Hall4ba53f02015-07-29 13:07:41 -0700103
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700104 except IndexError:
adminbae64d82013-08-01 10:50:15 -0700105 pass
Jon Hall4ba53f02015-07-29 13:07:41 -0700106
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700107 else:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000108 self.flag = 0
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700109 result = self.verify_and_translate( line )
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000110 index = index + 1
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700111 outputFile.append( result )
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000112
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700113 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 )
adminbae64d82013-08-01 10:50:15 -0700118 testScript.close()
119 return resultString
Jon Hall4ba53f02015-07-29 13:07:41 -0700120
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700121 def verify_and_translate( self, line ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000122 '''
adminbae64d82013-08-01 10:50:15 -0700123 It will accept the each line and calls the suitable API to conver into pyton equivalent syntax .
Jon Hall4ba53f02015-07-29 13:07:41 -0700124 It will return the translated python syntax .
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000125 '''
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700126 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 Hall4ba53f02015-07-29 13:07:41 -0700129
adminbae64d82013-08-01 10:50:15 -0700130 resultString = None
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700131 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 Hall4ba53f02015-07-29 13:07:41 -0700140
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700141 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 Hall4ba53f02015-07-29 13:07:41 -0700151
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700152 response_pasrse = re.match( "\s*PARSE\s+(\w+)\s+AS\s+(\w+)\s+INTO\s+(\w+)", line, flags=0 )
Jon Hall4ba53f02015-07-29 13:07:41 -0700153
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700154 if caseMatch:
adminbae64d82013-08-01 10:50:15 -0700155 self.CurrentStep = 0
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700156 self.CurrentCase = "CASE" + caseMatch.group( 1 )
157 resultString = resultString + self.translate_case_block( casenumber=caseMatch.group( 1 ) )
adminbae64d82013-08-01 10:50:15 -0700158 elif repeatMatch:
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700159 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:
adminbae64d82013-08-01 10:50:15 -0700165 self.CurrentStep = self.CurrentStep + 1
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700166 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 Hall4ba53f02015-07-29 13:07:41 -0700190
191 self.initSpace = initialSpaces
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700192 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 Hall4ba53f02015-07-29 13:07:41 -0700196 self.flag = self.flag + 1
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700197 elif experimentalMatch:
198 resultString = resultString + indent + self.translate_experimental_mode( mode=experimentalMatch.group( 1 ) )
Jon Hall4ba53f02015-07-29 13:07:41 -0700199
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700200 elif elseloopMatch:
adminbae64d82013-08-01 10:50:15 -0700201 if initialSpaces == self.initSpace or initialSpaces == self.outLoopSpace:
202 resultString = resultString + indent + "else :"
203 self.flag = self.flag + 1
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700204 else:
205 indent = " " * ( 4 + 4 * ( self.flag - 1 ) )
adminbae64d82013-08-01 10:50:15 -0700206 resultString = resultString + indent + "else :"
207 self.flag = self.flag + 1
Jon Hall4ba53f02015-07-29 13:07:41 -0700208
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700209 elif elifloop:
Jon Hall4ba53f02015-07-29 13:07:41 -0700210
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700211 operand = elifloop.group( 1 )
212 operator = elifloop.group( 2 )
213 value = elifloop.group( 3 )
adminbae64d82013-08-01 10:50:15 -0700214 if initialSpaces == self.initSpace or initialSpaces == self.outLoopSpace:
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700215 resultString = resultString + indent + "elif " + operand + self.translate_if_else_operator( conditionoperator=operator ) + value + ":"
Jon Hall4ba53f02015-07-29 13:07:41 -0700216 self.flag = self.flag + 1
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700217 else:
218 indent = " " * ( 4 + 4 * ( self.flag - 1 ) )
219 resultString = resultString + indent + "elif " + operand + self.translate_if_else_operator( conditionoperator=operator ) + value + ":"
adminbae64d82013-08-01 10:50:15 -0700220 self.flag = self.flag + 1
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700221 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 Hall4ba53f02015-07-29 13:07:41 -0700226 self.flag = self.flag + 1
adminbae64d82013-08-01 10:50:15 -0700227
228 return resultString
229
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700230 def translate_repeat( self, **repeatStatement ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000231 '''
adminbae64d82013-08-01 10:50:15 -0700232 this will transalte the repeat statement into a python equivalen while loop
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000233 '''
234
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700235 args = self.parse_args( [ "REPEAT" ], **repeatStatement )
adminbae64d82013-08-01 10:50:15 -0700236 resultString = ''
Jon Hall4ba53f02015-07-29 13:07:41 -0700237
adminbae64d82013-08-01 10:50:15 -0700238 resultString = "i = 0"
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700239 resultString = resultString + "\n" + " " * 8 + "while i<" + args[ "REPEAT" ] + " :"
adminbae64d82013-08-01 10:50:15 -0700240 return resultString
Jon Hall4ba53f02015-07-29 13:07:41 -0700241
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700242 def translate_if_else_operator( self, **loopBlock ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000243 '''
adminbae64d82013-08-01 10:50:15 -0700244 This method will translate if-else loop block into its equivalent python code.
Jon Hall4ba53f02015-07-29 13:07:41 -0700245 Whole loop block will be passed into loopBlock List.
adminbae64d82013-08-01 10:50:15 -0700246 It returns the transalted reuslt as a string.
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000247 '''
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700248 args = self.parse_args( [ "CONDITIONOPERATOR" ], **loopBlock )
adminbae64d82013-08-01 10:50:15 -0700249 resultString = ''
250 # process the loopBlock List translate all statements underlying the given loop block
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700251 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:
adminbae64d82013-08-01 10:50:15 -0700257 resultString = resultString + " == "
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700258 elif greaterMatch:
adminbae64d82013-08-01 10:50:15 -0700259 resultString = resultString + " > "
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700260 elif lesserMatch:
Jon Hall4ba53f02015-07-29 13:07:41 -0700261 resultString = resultString + " < "
adminbae64d82013-08-01 10:50:15 -0700262 elif greaterEqualMatch:
263 resultString = resultString + " >= "
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700264 elif lesserEqualMatch:
adminbae64d82013-08-01 10:50:15 -0700265 resultString = resultString + " <= "
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700266 else:
Jon Hall4ba53f02015-07-29 13:07:41 -0700267 print "\n Error: Given Operator is not listed "
268
269 return resultString
270
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700271 def translate_experimental_mode( self, **modeType ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000272 '''
adminbae64d82013-08-01 10:50:15 -0700273 This API will translate statment EXPERIMENTAL MODE ON/OFF into python equivalent.
274 It will return the transalted value.
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000275 '''
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700276 args = self.parse_args( [ "MODE" ], **modeType )
adminbae64d82013-08-01 10:50:15 -0700277 resultString = ''
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700278 ONmatch = re.match( "\s*ON", args[ "MODE" ], flags=0 )
279 OFFmatch = re.match( "\sOFF", args[ "MODE" ], flags=0 )
adminbae64d82013-08-01 10:50:15 -0700280
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700281 if ONmatch:
adminbae64d82013-08-01 10:50:15 -0700282 resultString = "main.EXPERIMENTAL_MODE = main.TRUE"
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700283 elif OFFmatch:
Jon Hall4ba53f02015-07-29 13:07:41 -0700284 resultString = "main.EXPERIMENTAL_MODE = main.FALSE"
adminbae64d82013-08-01 10:50:15 -0700285
286 return resultString
287
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700288 def interpret( self, **interpetParameters ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000289 '''
Jon Hall4ba53f02015-07-29 13:07:41 -0700290 This method will accept the OpenSpeak syntax into a string and will return
adminbae64d82013-08-01 10:50:15 -0700291 a python equivalent translations statement
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000292 '''
Jon Hall4ba53f02015-07-29 13:07:41 -0700293
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700294 args = self.parse_args( [ "TEXT", "WRITETOFILE" ], **interpetParameters )
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000295 resultString = ''
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700296 # 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 Ronquillo4d5f1d02017-10-13 20:23:57 +0000299
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700300 resultString = re.sub( "^\s+", "", resultString ) if lineSpace else resultString
adminbae64d82013-08-01 10:50:15 -0700301 return resultString
302
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700303 def translate_logs( self, **logStatement ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000304 '''
Jon Hall4ba53f02015-07-29 13:07:41 -0700305 This will translate the OpenSpeak log message statements into python equivalent
adminbae64d82013-08-01 10:50:15 -0700306 to resultString and returns resultString
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000307 '''
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700308 args = self.parse_args( [ "LOGLEVEL", "MESSAGE" ], **logStatement )
adminbae64d82013-08-01 10:50:15 -0700309 resultString = ''
Jon Hall4ba53f02015-07-29 13:07:41 -0700310 # convert the statement here
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700311 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 Hall4ba53f02015-07-29 13:07:41 -0700326
adminbae64d82013-08-01 10:50:15 -0700327 return resultString
328
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700329 def translate_log_message( self, **messageStatement ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000330 '''
adminbae64d82013-08-01 10:50:15 -0700331 This API will translate log messages if it is a string or Variable or combination
Jon Hall4ba53f02015-07-29 13:07:41 -0700332 of string and variable.
adminbae64d82013-08-01 10:50:15 -0700333 It will return the analysed and translate message.
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000334 '''
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700335 args = self.parse_args( [ "MESSAGE" ], **messageStatement )
adminbae64d82013-08-01 10:50:15 -0700336 resultString = ''
337
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700338 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" ]
adminbae64d82013-08-01 10:50:15 -0700346 elif stringWidVariableMatch:
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700347 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 )
adminbae64d82013-08-01 10:50:15 -0700355 elif varRefMatch:
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700356 resultString = resultString + varRefMatch.group( 1 )
357 else:
358 print "\nError : Syntax error , Not defined way to give log message" + args[ "MESSAGE" ]
adminbae64d82013-08-01 10:50:15 -0700359
Jon Hall4ba53f02015-07-29 13:07:41 -0700360 return resultString
361
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700362 def translate_assertion( self, **assertStatement ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000363 '''
Jon Hall4ba53f02015-07-29 13:07:41 -0700364 This will translate the ASSERT <value1> <COMPARISON OPERATOR> <value2> into python
adminbae64d82013-08-01 10:50:15 -0700365 equivalent to resultString and returns resultString
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000366 '''
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700367 args = self.parse_args( [ "LEFTVALUE", "OPERATOR", "RIGHTVALUE", "ONPASS", "ONFAIL" ], **assertStatement )
adminbae64d82013-08-01 10:50:15 -0700368 resultString = ''
Jon Hall4ba53f02015-07-29 13:07:41 -0700369 # convert the statement here
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700370 notOperatorMatch = re.search( "NOT\s+(.*)", args[ "OPERATOR" ], flags=0 )
371 notOperatorSymbMatch = re.search( "\!(.*)", args[ "OPERATOR" ], flags=0 )
adminbae64d82013-08-01 10:50:15 -0700372 operator = ''
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700373 lastresultMatch = re.match( "LAST_RESULT", args[ "RIGHTVALUE" ], flags=0 )
374 lastresponseMatch = re.match( "LAST_RESPONSE", args[ "RIGHTVALUE" ], flags=0 )
375 if lastresultMatch:
adminbae64d82013-08-01 10:50:15 -0700376 operator = "main.last_result"
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700377 elif lastresponseMatch:
adminbae64d82013-08-01 10:50:15 -0700378 operator = "main.last_response"
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700379 else:
380 operator = args[ "RIGHTVALUE" ]
Jon Hall4ba53f02015-07-29 13:07:41 -0700381
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700382 if args[ "OPERATOR" ] is None or args[ "OPERATOR" ] == "":
adminbae64d82013-08-01 10:50:15 -0700383 print "\n Error : Operator has not been specified !!!"
384 elif notOperatorMatch or notOperatorSymbMatch:
Jon Hall4ba53f02015-07-29 13:07:41 -0700385
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700386 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" ] ) + ")"
adminbae64d82013-08-01 10:50:15 -0700398
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700399 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 Hall4ba53f02015-07-29 13:07:41 -0700413
adminbae64d82013-08-01 10:50:15 -0700414 return resultString
415
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700416 def translate_response_result( self, **operatorStatement ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000417 '''
adminbae64d82013-08-01 10:50:15 -0700418 It will translate the LAST_RESPONSE or LAST_RESULT statement into its equivalent.
419 It returns the translate form in resulString.
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000420 '''
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700421 args = self.parse_args( [ "OPERATOR" ], **operatorStatement )
adminbae64d82013-08-01 10:50:15 -0700422 resultString = ''
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700423 lastResultMatch = re.match( "LAST_RESULT", args[ "OPERATOR" ], flags=0 )
424 lastResponseMatch = re.match( "LAST_RESPONSE", args[ "OPERATOR" ], flags=0 )
425 if lastResultMatch:
adminbae64d82013-08-01 10:50:15 -0700426 resultString = resultString + "main.last_result"
427 elif lastResponseMatch:
428 resultString = resultString + "main.last_response"
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700429 else:
430 resultString = resultString + args[ "OPERATOR" ]
adminbae64d82013-08-01 10:50:15 -0700431 return resultString
432
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700433 def translate_assertMessage( self, **messageStatement ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000434 '''
adminbae64d82013-08-01 10:50:15 -0700435 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 Ronquillo4d5f1d02017-10-13 20:23:57 +0000438 '''
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700439 args = self.parse_args( [ "MESSAGE" ], **messageStatement )
Jon Hall4ba53f02015-07-29 13:07:41 -0700440
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700441 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 )
adminbae64d82013-08-01 10:50:15 -0700447
448 resultString = ''
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700449 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 Hall4ba53f02015-07-29 13:07:41 -0700464 print "\n Error : Please pass a message or action for assertion "
465
466 return resultString
467
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700468 def translate_operator( self, **operatorStatement ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000469 '''
adminbae64d82013-08-01 10:50:15 -0700470 It will translate the operator for assertion , by ensuring against given arguments.
471 It will return the translated assertion operator.
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000472 '''
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700473 args = self.parse_args( [ "OPERATOR" ], **operatorStatement )
Jon Hall4ba53f02015-07-29 13:07:41 -0700474
adminbae64d82013-08-01 10:50:15 -0700475 resultString = ''
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700476 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 Hall4ba53f02015-07-29 13:07:41 -0700483
adminbae64d82013-08-01 10:50:15 -0700484 resultString = resultString + "equals"
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700485 elif greaterMatch:
adminbae64d82013-08-01 10:50:15 -0700486 self.grtrOrLssr = self.grtrOrLssr + 1
487 resultString = resultString + "greater"
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700488 elif lesserMatch:
adminbae64d82013-08-01 10:50:15 -0700489 self.grtrOrLssr = self.grtrOrLssr + 1
490 resultString = resultString + "lesser"
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700491 elif stringMatch:
Jon Hall4ba53f02015-07-29 13:07:41 -0700492
adminbae64d82013-08-01 10:50:15 -0700493 resultString = resultString + "matches"
494 elif greaterEqualMatch:
Jon Hall4ba53f02015-07-29 13:07:41 -0700495
adminbae64d82013-08-01 10:50:15 -0700496 resultString = resultString + "greater_equals"
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700497 elif lesserEqualMatch:
Jon Hall4ba53f02015-07-29 13:07:41 -0700498
adminbae64d82013-08-01 10:50:15 -0700499 resultString = resultString + "lesser_equals"
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700500 else:
Jon Hall4ba53f02015-07-29 13:07:41 -0700501 print "\n Error: Given Operator is not listed for assertion"
502 return resultString
adminbae64d82013-08-01 10:50:15 -0700503
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700504 def translate_store( self, **storeStatement ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000505 '''
adminbae64d82013-08-01 10:50:15 -0700506 This will translate the STORE <variable> IN <value> or <variable> = <value>
507 into python equivalent to resultString and returns resultString
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000508 '''
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700509 args = self.parse_args( [ "VARIABLE", "VALUE" ], **storeStatement )
adminbae64d82013-08-01 10:50:15 -0700510 resultString = ''
511 # convert the statement here
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700512 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" ]
adminbae64d82013-08-01 10:50:15 -0700521
522 return resultString
Jon Hall4ba53f02015-07-29 13:07:41 -0700523
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700524 def translate_disconnect( self, **disconnectStatement ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000525 '''
Jon Hall4ba53f02015-07-29 13:07:41 -0700526 This will translate the DISCONNECT <component_name> into python
adminbae64d82013-08-01 10:50:15 -0700527 equivalent to resultString and returns resultString
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000528 '''
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700529 args = self.parse_args( [ "COMPONENT" ], **disconnectStatement )
adminbae64d82013-08-01 10:50:15 -0700530 resultString = ''
531 # convert the statement here
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700532 resultString = "main." + args[ "COMPONENT" ] + ".disconnect()"
adminbae64d82013-08-01 10:50:15 -0700533 return resultString
Jon Hall4ba53f02015-07-29 13:07:41 -0700534
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700535 def translate_onDOAs( self, **onDoStatement ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000536 '''
adminbae64d82013-08-01 10:50:15 -0700537 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 Ronquillo4d5f1d02017-10-13 20:23:57 +0000539 '''
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700540 args = self.parse_args( [ "COMPONENT", "ACTION", "ARGUMENTS" ], **onDoStatement )
adminbae64d82013-08-01 10:50:15 -0700541 subString = ''
Jon Hall4ba53f02015-07-29 13:07:41 -0700542
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700543 usingMatch = re.match( "\s*(.*)\s+USING\s+(.*)", args[ "ACTION" ], flags=0 )
adminbae64d82013-08-01 10:50:15 -0700544 action = ''
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700545 if usingMatch:
546 action = usingMatch.group( 1 )
547 arguments = usingMatch.group( 2 )
548 subString = self.translate_usingas( arguments=arguments )
Jon Hall4ba53f02015-07-29 13:07:41 -0700549
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700550 else:
551 andCheck = re.search( "(.*)\s+AND\s+(.*)", args[ "ACTION" ], flags=0 )
Jon Hall4ba53f02015-07-29 13:07:41 -0700552
adminbae64d82013-08-01 10:50:15 -0700553 action = action + "()"
554 if andCheck:
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700555 action = andCheck.group( 1 ) + "()"
556 subString = subString + self.handle_conjuction( statement=andCheck.group( 2 ) )
557 else:
558 action = args[ "ACTION" ]
adminbae64d82013-08-01 10:50:15 -0700559 action = action + "()"
Jon Hall4ba53f02015-07-29 13:07:41 -0700560 # convert the statement here
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700561 resultString = "main." + args[ "COMPONENT" ] + "." + action + subString
adminbae64d82013-08-01 10:50:15 -0700562 return resultString
563
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700564 def handle_conjuction( self, **conjuctStatement ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000565 '''
adminbae64d82013-08-01 10:50:15 -0700566 This will handle the conjuctions
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000567 '''
568
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700569 args = self.parse_args( [ "STATEMENT" ], **conjuctStatement )
adminbae64d82013-08-01 10:50:15 -0700570 subSentence = ''
Jon Hall4ba53f02015-07-29 13:07:41 -0700571
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700572 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 ) )
adminbae64d82013-08-01 10:50:15 -0700583 return subSentence
584
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700585 def translate_usingas( self, **argumentAS ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000586 '''
adminbae64d82013-08-01 10:50:15 -0700587 This will tranlate USING argument AS value Statement into equivalent argument passing.
588 It will return translated form into resultString
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000589 '''
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700590 args = self.parse_args( [ "ARGUMENTS" ], **argumentAS )
adminbae64d82013-08-01 10:50:15 -0700591 resultString = ''
592 argsList = []
593 subString = ''
594 subSentence = ''
595 line = ''
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700596 andCheck = re.search( "(.*)\s+AND\s+(.*)", args[ "ARGUMENTS" ], flags=0 )
adminbae64d82013-08-01 10:50:15 -0700597 if andCheck:
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700598 line = andCheck.group( 1 )
599 subSentence = self.handle_conjuction( statement=andCheck.group( 2 ) )
600 else:
601 line = args[ "ARGUMENTS" ]
Jon Hall4ba53f02015-07-29 13:07:41 -0700602
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700603 argsMatch = re.search( "(.*),(.*)", line, flags=0 )
Jon Hall4ba53f02015-07-29 13:07:41 -0700604
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700605 if args[ "ARGUMENTS" ] is None or args[ "ARGUMENTS" ] == '':
adminbae64d82013-08-01 10:50:15 -0700606 subString = ''
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700607 elif argsMatch:
Jon Hall4ba53f02015-07-29 13:07:41 -0700608
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700609 argsList = line.split( "," )
610 for index, arguments in enumerate( argsList ):
611 argMatch = re.search( "(.*)\s+AS\s+(.*)", arguments, flags=0 )
adminbae64d82013-08-01 10:50:15 -0700612 if argMatch:
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700613 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 )
adminbae64d82013-08-01 10:50:15 -0700634 if argMatch:
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700635 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
adminbae64d82013-08-01 10:50:15 -0700650 return resultString
651
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700652 def translate_connect( self, **connectStatement ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000653 '''
Jon Hall4ba53f02015-07-29 13:07:41 -0700654 This will translate the CONNECT <component_name> USING1 <arg1> AS <value1>, <arg2> AS <value2>
adminbae64d82013-08-01 10:50:15 -0700655 into python equivalent to resultString and returns resultString
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000656 '''
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700657 args = self.parse_args( [ "COMPONENT", "ARGUMENTS" ], **connectStatement )
adminbae64d82013-08-01 10:50:15 -0700658 resultString = ''
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700659 subString = self.translate_usingas( arguments=args[ "ARGUMENTS" ] )
Jon Hall4ba53f02015-07-29 13:07:41 -0700660 # convert the statement here
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700661 resultString = "main." + args[ "COMPONENT" ] + ".connect( " + subString + " )"
adminbae64d82013-08-01 10:50:15 -0700662 return resultString
663
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700664 def translate_parameters( self, **parameterStatement ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000665 '''
Jon Hall4ba53f02015-07-29 13:07:41 -0700666 This will translate the OpenSpeak Case and Params parameters into python equivalent
adminbae64d82013-08-01 10:50:15 -0700667 to resultString and returns resultString
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000668 '''
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700669 args = self.parse_args( [ "PARAMETERS" ], **parameterStatement )
670 argument = args[ "PARAMETERS" ]
adminbae64d82013-08-01 10:50:15 -0700671 resultString = ''
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700672 # 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 )
adminbae64d82013-08-01 10:50:15 -0700679 # convert the statement here
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700680 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:
adminbae64d82013-08-01 10:50:15 -0700694 resultString = resultString + "main.last_result"
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700695 elif lastResponseMatch:
Jon Hall4ba53f02015-07-29 13:07:41 -0700696 resultString = resultString + "main.last_response"
adminbae64d82013-08-01 10:50:15 -0700697 return resultString
698
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700699 def _argsCheck( self, **args ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000700 ''' This API will check if given argument is varibale reference or String and will translate accordingly.
adminbae64d82013-08-01 10:50:15 -0700701 It will return the tanslate form in resultString.
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000702 '''
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700703 args = self.parse_args( [ "CHECKVAR" ], **args )
704 params = args[ "CHECKVAR" ]
705 argsList = params.split( "]" )
adminbae64d82013-08-01 10:50:15 -0700706 resultString = ''
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700707 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:
adminbae64d82013-08-01 10:50:15 -0700713 resultString = resultString + paramArgs + "]"
714 return resultString
715
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700716 def translate_step( self, **stepStatement ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000717 '''
Jon Hall4ba53f02015-07-29 13:07:41 -0700718 This will translate the STEP "DO SOMETHING HERE" into python equivalent
adminbae64d82013-08-01 10:50:15 -0700719 to resultString and returns resultString
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000720 '''
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700721 args = self.parse_args( [ "STEP" ], **stepStatement )
adminbae64d82013-08-01 10:50:15 -0700722 resultString = ''
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700723 resultString = "main.step(\"" + args[ "STEP" ] + "\")"
Jon Hall4ba53f02015-07-29 13:07:41 -0700724 # convert the statement here
adminbae64d82013-08-01 10:50:15 -0700725 return resultString
726
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700727 def translate_comment( self, **commentStatement ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000728 '''
Jon Hall4ba53f02015-07-29 13:07:41 -0700729 This will translate the COMMENT "DO SOMETHING HERE" into python equivalent
adminbae64d82013-08-01 10:50:15 -0700730 to resultString and returns resultString
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000731 '''
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700732 args = self.parse_args( [ "COMMENT" ], **commentStatement )
adminbae64d82013-08-01 10:50:15 -0700733 resultString = ''
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700734 resultString = "#" + args[ "COMMENT" ]
Jon Hall4ba53f02015-07-29 13:07:41 -0700735 # convert the statement here
736 return resultString
737
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700738 def translate_testcase_name( self, **nameStatement ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000739 '''
Jon Hall4ba53f02015-07-29 13:07:41 -0700740 This method will convert NAME "<Testcase_name>" into python equivalent statement
741 to resultString and returns resultString
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000742 '''
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700743 args = self.parse_args( [ "TESTNAME" ], **nameStatement )
Jon Hall4ba53f02015-07-29 13:07:41 -0700744
adminbae64d82013-08-01 10:50:15 -0700745 resultString = ''
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700746 resultString = "main.case(\"" + args[ "TESTNAME" ] + "\")"
Jon Hall4ba53f02015-07-29 13:07:41 -0700747 # convert the statement here
748 return resultString
749
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700750 def translate_case_block( self, **caseBlock ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000751 '''
Jon Hall4ba53f02015-07-29 13:07:41 -0700752 This method will translate the case block in test script .
adminbae64d82013-08-01 10:50:15 -0700753 It returns the translated equivalent python code for test script
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000754 '''
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700755 args = self.parse_args( [ "CASENUMBER" ], **caseBlock )
adminbae64d82013-08-01 10:50:15 -0700756 resultString = ""
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700757 resultString = "def CASE" + str( args[ "CASENUMBER" ] ) + "(self,main) :\n"
Jon Hall4ba53f02015-07-29 13:07:41 -0700758 # process the caseBlock List translate all statements underlying the given case
adminbae64d82013-08-01 10:50:15 -0700759 return resultString
760
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700761 def translate_loop_block( self, *loopBlock ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000762 '''
adminbae64d82013-08-01 10:50:15 -0700763 This method will translate for loop block into its equivalent python code.
Jon Hall4ba53f02015-07-29 13:07:41 -0700764 Whole loop block will be passed into loopBlock List.
adminbae64d82013-08-01 10:50:15 -0700765 It returns the transalted reuslt as a string.
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000766 '''
adminbae64d82013-08-01 10:50:15 -0700767 resultString = ''
Jon Hall4ba53f02015-07-29 13:07:41 -0700768 # process the loopBlock List translate all statements underlying the given loop block
769 return resultString
770
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700771 def translate_conjuction( self, conjuctionStatement ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000772 '''
Jon Hall4ba53f02015-07-29 13:07:41 -0700773 This will translate the AND conjuction statements into python equivalent
adminbae64d82013-08-01 10:50:15 -0700774 to resultString and returns resultString
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000775 '''
adminbae64d82013-08-01 10:50:15 -0700776 resultString = ''
Jon Hall4ba53f02015-07-29 13:07:41 -0700777 # convert the statement here
adminbae64d82013-08-01 10:50:15 -0700778 return resultString
779
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700780 def parse_args( self, args, **kwargs ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000781 '''
782 It will accept the (key,value) pair and will return the (key,value) pairs with keys in uppercase.
783 '''
adminbae64d82013-08-01 10:50:15 -0700784 newArgs = {}
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700785 for key, value in kwargs.iteritems():
786 if isinstance( args, list ) and str.upper( key ) in args:
Jon Hall4ba53f02015-07-29 13:07:41 -0700787 for each in args:
Jeremy Ronquillo696f4262017-10-17 10:56:26 -0700788 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
adminbae64d82013-08-01 10:50:15 -0700792 return newArgs