Robot Framework Integrated Development Environment (RIDE)
listenermethods.py
Go to the documentation of this file.
1 # Copyright 2008-2015 Nokia Networks
2 # Copyright 2016- Robot Framework Foundation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 from robotide.lib.robot.errors import TimeoutError
17 from robotide.lib.robot.utils import get_error_details, py2to3
18 
19 from .listenerarguments import ListenerArguments
20 from .logger import LOGGER
21 
22 
23 @py2to3
25 
26  def __init__(self, method_name, listeners):
27  self._methods_methods = []
28  self._method_name_method_name = method_name
29  if listeners:
30  self._register_methods_register_methods(method_name, listeners)
31 
32  def _register_methods(self, method_name, listeners):
33  for listener in listeners:
34  method = getattr(listener, method_name)
35  if method:
36  self._methods_methods.append(ListenerMethod(method, listener))
37 
38  def __call__(self, *args):
39  if self._methods_methods:
40  args = ListenerArguments.by_method_name(self._method_name_method_name, args)
41  for method in self._methods_methods:
42  method(args.get_arguments(method.version))
43 
44  def __nonzero__(self):
45  return bool(self._methods_methods)
46 
47 
49 
50  def __init__(self, method_name):
51  self._method_stack_method_stack = []
52  self._method_name_method_name = method_name
53 
54  def new_suite_scope(self):
55  self._method_stack_method_stack.append([])
56 
58  self._method_stack_method_stack.pop()
59 
60  def register(self, listeners, library):
61  methods = self._method_stack_method_stack[-1]
62  for listener in listeners:
63  method = getattr(listener, self._method_name_method_name)
64  if method:
65  info = ListenerMethod(method, listener, library)
66  methods.append(info)
67 
68  def unregister(self, library):
69  methods = [m for m in self._method_stack_method_stack[-1] if m.library is not library]
70  self._method_stack_method_stack[-1] = methods
71 
72  def __call__(self, *args, **conf):
73  methods = self._get_methods_get_methods(**conf)
74  if methods:
75  args = ListenerArguments.by_method_name(self._method_name_method_name, args)
76  for method in methods:
77  method(args.get_arguments(method.version))
78 
79  def _get_methods(self, library=None):
80  if not (self._method_stack_method_stack and self._method_stack_method_stack[-1]):
81  return []
82  methods = self._method_stack_method_stack[-1]
83  if library:
84  return [m for m in methods if m.library is library]
85  return methods
86 
87 
89  # Flag to avoid recursive listener calls.
90  called = False
91 
92  def __init__(self, method, listener, library=None):
93  self.methodmethod = method
94  self.listener_namelistener_name = listener.name
95  self.versionversion = listener.version
96  self.librarylibrary = library
97 
98  def __call__(self, args):
99  if self.calledcalled:
100  return
101  try:
102  ListenerMethod.called = True
103  self.methodmethod(*args)
104  except TimeoutError:
105  # Propagate possible timeouts:
106  # https://github.com/robotframework/robotframework/issues/2763
107  raise
108  except:
109  message, details = get_error_details()
110  LOGGER.error("Calling method '%s' of listener '%s' failed: %s"
111  % (self.methodmethod.__name__, self.listener_namelistener_name, message))
112  LOGGER.info("Details:\n%s" % details)
113  finally:
114  ListenerMethod.called = False
def __init__(self, method, listener, library=None)
def get_error_details(exclude_robot_traces=EXCLUDE_ROBOT_TRACES)
Returns error message and details of the last occurred exception.
Definition: error.py:46