X-Git-Url: https://gerrit.akraino.org/r/gitweb?p=ta%2Fcloudtaf.git;a=blobdiff_plain;f=libraries%2Fcommon%2Fdecorators_for_robot_functionalities.py;fp=libraries%2Fcommon%2Fdecorators_for_robot_functionalities.py;h=e5e16e76aa585f46700ef9814858eef0ac7f3890;hp=0000000000000000000000000000000000000000;hb=af5eb3ff36b92ab1d9c156ffa0391eadc73eb6ba;hpb=025a45508d009db84c34076fb4a668f712628d6d
diff --git a/libraries/common/decorators_for_robot_functionalities.py b/libraries/common/decorators_for_robot_functionalities.py
new file mode 100644
index 0000000..e5e16e7
--- /dev/null
+++ b/libraries/common/decorators_for_robot_functionalities.py
@@ -0,0 +1,76 @@
+import inspect
+from robot.conf import RobotSettings
+from robot.api import logger
+from robot.variables import VariableScopes
+from robot.running.timeouts import KeywordTimeout
+from robot.libraries.BuiltIn import BuiltIn
+
+
+BuiltIn().import_library('pabot.PabotLib')
+PABOT = BuiltIn().get_library_instance('pabot.PabotLib')
+
+
+# if both timeout and log decorator is used for one function, timeout decorator should be used first
+
+
+def robot_timeout(timeoutinseconds):
+ def timeout_decorator(func):
+ def wrapper(*args, **kwargs):
+ timeout_msg = func.__name__ + " timed out !!"
+ timeout = KeywordTimeout(timeoutinseconds, timeout_msg, VariableScopes(RobotSettings()))
+ timeout.start()
+ return timeout.run(func, args, kwargs)
+
+ return wrapper
+
+ return timeout_decorator
+
+
+def robot_log(func):
+ def wrapper(*args, **kwargs):
+ spec = inspect.getargspec(func)
+ for key in kwargs.iterkeys():
+ if key not in spec.args:
+ # if function is called from robot, and one of it's unnamed string parameters has '=' in it
+ # move this parameter from kwargs to args
+ l = list(args)
+ b = '{0}={1}'.format(key, kwargs[key])
+ l.append(b)
+ args = tuple(l)
+ kwargs.pop(key)
+
+ argnames = func.func_code.co_varnames[:func.func_code.co_argcount]
+ parameters = ": "
+ for entry in zip(argnames, args) + kwargs.items():
+ if 'self' not in entry:
+ parameters += ('%s=%r, ' % entry)
+ fname = func.func_name
+ logger.info("ENTER: " + fname +
+ "" + parameters, html=True)
+ result = func(*args, **kwargs)
+ logger.info("EXIT: " + fname +
+ "", html=True)
+ return result
+
+ return wrapper
+
+
+def pabot_lock(lock_name):
+ """Sets Pabot lock until the execution of the function
+ pabot_lock should be used after the robot_log if both function decorators are used at the same time"""
+
+ def pabot_lock_decorator(func):
+ def wrapper(*args, **kwargs):
+ PABOT.acquire_lock(lock_name)
+ logger.info(lock_name + " lock acquired on " + func.__name__)
+ result = None
+ try:
+ result = func(*args, **kwargs)
+ finally:
+ PABOT.release_lock(lock_name)
+ logger.info(lock_name + " lock released from " + func.__name__)
+ return result
+
+ return wrapper
+
+ return pabot_lock_decorator