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