4040import pytz
4141import re
4242import copy
43+ import traceback
4344
4445from . import common_functions as common , performance_action # Functions that are common to all modules
4546from Framework .Built_In_Automation .Shared_Resources import BuiltInFunctionSharedResources as sr
@@ -291,16 +292,24 @@ def If_else_action(step_data, data_set_no):
291292 statement = ""
292293 operator = ""
293294 operators = {" |==| " : 0 , " |!=| " : 0 , " |<=| " : 0 , " |>=| " : 0 , " |>| " : 0 , " |<| " : 0 , " |in| " : 0 }
294- statements = ("else if" , "else" , "if" )
295+ statements = ("elif" , " else if" , "else" , "if" )
295296 for i in statements :
296297 if left .lower ().find (i ) == 0 :
297298 statement = i
298299 break
299-
300+ if statement == 'else if' :
301+ left = 'elif' + left [len (statement ):]
302+ statement = 'elif'
303+ if CommonUtil .debug_status :
304+ CommonUtil .ExecLog (
305+ sModuleInfo ,
306+ "Syntax error. Convert 'else if' to 'elif'" ,
307+ 3 ,
308+ )
300309 if statement not in statements :
301310 CommonUtil .ExecLog (
302311 sModuleInfo ,
303- "Specify a statement among (if, else if , else) and add a <single space> after that" ,
312+ "Specify a statement among (if, elif , else) and add a <single space> after that" ,
304313 3 ,
305314 )
306315 return "zeuz_failed" , []
@@ -314,18 +323,7 @@ def If_else_action(step_data, data_set_no):
314323 for i in operators :
315324 if i .strip () in left :
316325 operators [i ] += 1
317- #TODO: Check these 2 lines, should not be needed anymore
318326
319- # operators[" |<| "] -= operators[" |<=| "]
320- # operators[" |>| "] -= operators[" |>=| "]
321-
322- if sum (operators .values ()) == 0 :
323- CommonUtil .ExecLog (
324- sModuleInfo ,
325- "Specify an operator among |==|, |!=|, |<|, |>|, |<=|, |>=|, |in| and add a <single space> before and after the operator" ,
326- 3 ,
327- )
328- return "zeuz_failed" , []
329327 if sum (operators .values ()) == 1 :
330328 for i in operators :
331329 if operators [i ] == 1 :
@@ -340,7 +338,7 @@ def If_else_action(step_data, data_set_no):
340338 Lvalue and Rvalue can have spaces at starting or ending so don't try stripping them. it can manipulate
341339 their actual values. Suppose, %|XY|% = "Hello " stripping will remove the last space"""
342340
343- if statement != "else" :
341+ if statement != "else" and operator :
344342 Lvalue , Rvalue = left [len (statement + " " ):].split (operator ) # remove "if "
345343 Lvalue = Lvalue [:- 1 ] if Lvalue [- 1 ] == " " else Lvalue # remove 1 space before the operator
346344 Rvalue = Rvalue [1 :] if Rvalue [0 ] == " " else Rvalue # remove 1 space after the operator
@@ -358,68 +356,94 @@ def check_operators():
358356 if statement == "else" :
359357 if not condition_matched :
360358 condition_matched = True
361- for i in get_data_set_nums (str ( right ). strip () ):
359+ for i in get_data_set_nums (right ):
362360 next_level_step_data .append (i )
363361 outer_skip += next_level_step_data
364362 else :
365- outer_skip += get_data_set_nums (str (right ).strip ())
363+ outer_skip += get_data_set_nums (right )
364+ return
365+
366+ try :
367+ if eval (left [len (statement ):], sr .shared_variables ):
368+ if not condition_matched :
369+ condition_matched = True
370+ for i in get_data_set_nums (right ):
371+ next_level_step_data .append (i )
372+ outer_skip += next_level_step_data
373+ else :
374+ outer_skip += get_data_set_nums (right )
375+ except SyntaxError as e :
376+ if CommonUtil .debug_status :
377+ m = traceback .format_exc ()
378+ m = m .split ('except SyntaxError as e:' )[- 1 ]
379+ m = m [m .find ('\n ' )+ 1 :]
380+ m = m .split ('SyntaxError: invalid syntax' )[0 ]
381+
382+ correct = left .replace (operator , operator [1 :- 1 ]) if operator else left
383+ correct = correct .replace ('%|' , '' ).replace ('|%' , '' )
384+ correct = statement + correct [len (statement ):]
366385
367- elif operator == "|==|" :
386+ CommonUtil .ExecLog (
387+ sModuleInfo ,
388+ m + f"Syntax error. Correct form would be >> { correct } \n Please migrate all if_else actions to this new dataset that is exactly similar to python syntax. Previous dataset is deprecated" ,
389+ 3 ,
390+ )
391+ if operator == "|==|" :
368392 if Lvalue == Rvalue and not condition_matched :
369393 condition_matched = True
370- for i in get_data_set_nums (str ( right ). strip () ):
394+ for i in get_data_set_nums (right ):
371395 next_level_step_data .append (i )
372396 outer_skip += next_level_step_data
373397 else :
374- outer_skip += get_data_set_nums (str ( right ). strip () )
398+ outer_skip += get_data_set_nums (right )
375399 elif operator == "|!=|" :
376400 if Lvalue != Rvalue and not condition_matched :
377401 condition_matched = True
378- for i in get_data_set_nums (str ( right ). strip () ):
402+ for i in get_data_set_nums (right ):
379403 next_level_step_data .append (i )
380404 outer_skip += next_level_step_data
381405 else :
382- outer_skip += get_data_set_nums (str ( right ). strip () )
406+ outer_skip += get_data_set_nums (right )
383407 elif operator == "|<=|" :
384408 if Lvalue <= Rvalue and not condition_matched :
385409 condition_matched = True
386- for i in get_data_set_nums (str ( right ). strip () ):
410+ for i in get_data_set_nums (right ):
387411 next_level_step_data .append (i )
388412 outer_skip += next_level_step_data
389413 else :
390- outer_skip += get_data_set_nums (str ( right ). strip () )
414+ outer_skip += get_data_set_nums (right )
391415 elif operator == "|>=|" :
392416 if Lvalue >= Rvalue and not condition_matched :
393417 condition_matched = True
394- for i in get_data_set_nums (str ( right ). strip () ):
418+ for i in get_data_set_nums (right ):
395419 next_level_step_data .append (i )
396420 outer_skip += next_level_step_data
397421 else :
398- outer_skip += get_data_set_nums (str ( right ). strip () )
422+ outer_skip += get_data_set_nums (right )
399423 elif operator == "|<|" :
400424 if Lvalue < Rvalue and not condition_matched :
401425 condition_matched = True
402- for i in get_data_set_nums (str ( right ). strip () ):
426+ for i in get_data_set_nums (right ):
403427 next_level_step_data .append (i )
404428 outer_skip += next_level_step_data
405429 else :
406- outer_skip += get_data_set_nums (str ( right ). strip () )
430+ outer_skip += get_data_set_nums (right )
407431 elif operator == "|>|" :
408432 if Lvalue > Rvalue and not condition_matched :
409433 condition_matched = True
410- for i in get_data_set_nums (str ( right ). strip () ):
434+ for i in get_data_set_nums (right ):
411435 next_level_step_data .append (i )
412436 outer_skip += next_level_step_data
413437 else :
414- outer_skip += get_data_set_nums (str ( right ). strip () )
438+ outer_skip += get_data_set_nums (right )
415439 elif operator == "|in|" :
416440 if Lvalue in Rvalue and not condition_matched :
417441 condition_matched = True
418- for i in get_data_set_nums (str ( right ). strip () ):
442+ for i in get_data_set_nums (right ):
419443 next_level_step_data .append (i )
420444 outer_skip += next_level_step_data
421445 else :
422- outer_skip += get_data_set_nums (str ( right ). strip () )
446+ outer_skip += get_data_set_nums (right )
423447
424448 if statement == "if" :
425449 if_exists = True
@@ -428,7 +452,7 @@ def check_operators():
428452 if condition_matched :
429453 log = if_else_log_for_actions (data_set_for_log [row_index ][0 ], next_level_step_data )
430454 CommonUtil .ExecLog (sModuleInfo , log , 1 )
431- elif statement == "else if " :
455+ elif statement == "elif " :
432456 if not if_exists :
433457 CommonUtil .ExecLog (sModuleInfo , "No 'if' statement found. Please define a 'if' statement first" , 3 )
434458 return "zeuz_failed" , []
@@ -2086,16 +2110,16 @@ def Conditional_Action_Handler(step_data, dataset_cnt):
20862110 left = left .lower ()
20872111 mid = mid .lower ()
20882112 if "true" in left and "conditional action" in mid :
2089- outer_skip += get_data_set_nums (str ( right ). strip () )
2113+ outer_skip += get_data_set_nums (right )
20902114 if logic_decision :
2091- for i in get_data_set_nums (str ( right ). strip () ):
2115+ for i in get_data_set_nums (right ):
20922116 next_level_step_data .append (i )
20932117 log_msg = if_else_log_for_actions (log_msg , next_level_step_data , "element" )
20942118
20952119 elif "false" in left and "conditional action" in mid :
2096- outer_skip += get_data_set_nums (str ( right ). strip () )
2120+ outer_skip += get_data_set_nums (right )
20972121 if not logic_decision :
2098- for i in get_data_set_nums (str ( right ). strip () ):
2122+ for i in get_data_set_nums (right ):
20992123 next_level_step_data .append (i )
21002124 log_msg = if_else_log_for_actions (log_msg , next_level_step_data , "element" )
21012125
0 commit comments