Robot Framework
handlerstore.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 itertools import chain
17 
18 from robot.errors import DataError
19 from robot.utils import NormalizedDict, seq2str
20 
21 from .usererrorhandler import UserErrorHandler
22 
23 
25 
26  def __init__(self):
27  self._normal_normal = NormalizedDict(ignore='_')
28  self._embedded_embedded = []
29 
30  def add(self, handler, embedded=False):
31  if embedded:
32  self._embedded_embedded.append(handler)
33  elif handler.name not in self._normal_normal:
34  self._normal_normal[handler.name] = handler
35  else:
36  error = DataError('Keyword with same name defined multiple times.')
37  self._normal_normal[handler.name] = UserErrorHandler(error, handler.name,
38  handler.libname)
39  raise error
40 
41  def __iter__(self):
42  return chain(self._normal_normal.values(), self._embedded_embedded)
43 
44  def __len__(self):
45  return len(self._normal_normal) + len(self._embedded_embedded)
46 
47  def __contains__(self, name):
48  if name in self._normal_normal:
49  return True
50  return any(template.matches(name) for template in self._embedded_embedded)
51 
52  def __getitem__(self, name):
53  handlers = self.get_handlersget_handlers(name)
54  if len(handlers) == 1:
55  return handlers[0]
56  if not handlers:
57  raise ValueError(f"No handler with name '{name}' found.")
58  names = seq2str([handler.name for handler in handlers])
59  raise ValueError(f"Multiple handlers matching name '{name}' found: {names}")
60 
61  def get_handlers(self, name):
62  if name in self._normal_normal:
63  return [self._normal_normal[name]]
64  return [template for template in self._embedded_embedded if template.matches(name)]
def add(self, handler, embedded=False)
Definition: handlerstore.py:30
Created if creating handlers fail – running raises DataError.
def seq2str(sequence, quote="'", sep=', ', lastsep=' and ')
Returns sequence in format ‘'item 1’, 'item 2' and 'item 3'`.
Definition: misc.py:79