robot tcs, test charts, robot container added
[ta/cloudtaf.git] / libraries / common / decorators_for_robot_functionalities.py
1 import inspect
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
7
8
9 BuiltIn().import_library('pabot.PabotLib')
10 PABOT = BuiltIn().get_library_instance('pabot.PabotLib')
11
12
13 # if both timeout and log decorator is used for one function, timeout decorator should be used first
14
15
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()))
21             timeout.start()
22             return timeout.run(func, args, kwargs)
23
24         return wrapper
25
26     return timeout_decorator
27
28
29 def robot_log(func):
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
36                 l = list(args)
37                 b = '{0}={1}'.format(key, kwargs[key])
38                 l.append(b)
39                 args = tuple(l)
40                 kwargs.pop(key)
41
42         argnames = func.func_code.co_varnames[:func.func_code.co_argcount]
43         parameters = ": "
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)
53         return result
54
55     return wrapper
56
57
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"""
61
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__)
66             result = None
67             try:
68                 result = func(*args, **kwargs)
69             finally:
70                 PABOT.release_lock(lock_name)
71                 logger.info(lock_name + " lock released from " + func.__name__)
72             return result
73
74         return wrapper
75
76     return pabot_lock_decorator