2 from robot.conf import RobotSettings
3 from robot.api import logger
4 from robot.variables import VariableScopes
5 from robot.running.timeouts import KeywordTimeout
6 from robot.libraries.BuiltIn import BuiltIn
9 BuiltIn().import_library('pabot.PabotLib')
10 PABOT = BuiltIn().get_library_instance('pabot.PabotLib')
13 # if both timeout and log decorator is used for one function, timeout decorator should be used first
16 def robot_timeout(timeoutinseconds):
17 def timeout_decorator(func):
18 def wrapper(*args, **kwargs):
19 timeout_msg = func.__name__ + " timed out !!"
20 timeout = KeywordTimeout(timeoutinseconds, timeout_msg, VariableScopes(RobotSettings()))
22 return timeout.run(func, args, kwargs)
26 return timeout_decorator
30 def wrapper(*args, **kwargs):
31 spec = inspect.getargspec(func)
32 for key in kwargs.iterkeys():
33 if key not in spec.args:
34 # if function is called from robot, and one of it's unnamed string parameters has '=' in it
35 # move this parameter from kwargs to args
37 b = '{0}={1}'.format(key, kwargs[key])
42 argnames = func.func_code.co_varnames[:func.func_code.co_argcount]
44 for entry in zip(argnames, args) + kwargs.items():
45 if 'self' not in entry:
46 parameters += ('%s=%r, ' % entry)
47 fname = func.func_name
48 logger.info("<span class='label pass'><span style='font-size: 1.25em'>ENTER: " + fname +
49 "</span></span>" + parameters, html=True)
50 result = func(*args, **kwargs)
51 logger.info("<span class='label warn'><span style='font-size: 1.25em'>EXIT: " + fname +
52 "</span></span>", html=True)
58 def pabot_lock(lock_name):
59 """Sets Pabot lock until the execution of the function
60 pabot_lock should be used after the robot_log if both function decorators are used at the same time"""
62 def pabot_lock_decorator(func):
63 def wrapper(*args, **kwargs):
64 PABOT.acquire_lock(lock_name)
65 logger.info(lock_name + " lock acquired on " + func.__name__)
68 result = func(*args, **kwargs)
70 PABOT.release_lock(lock_name)
71 logger.info(lock_name + " lock released from " + func.__name__)
76 return pabot_lock_decorator