Robot Framework
itemlist.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 collections.abc import MutableSequence
17 from functools import total_ordering
18 
19 from robot.utils import type_name
20 
21 
22 @total_ordering
23 class ItemList(MutableSequence):
24  __slots__ = ['_item_class', '_common_attrs', '_items']
25 
26  def __init__(self, item_class, common_attrs=None, items=None):
27  self._item_class_item_class = item_class
28  self._common_attrs_common_attrs = common_attrs
29  self._items_items = []
30  if items:
31  self.extendextend(items)
32 
33  def create(self, *args, **kwargs):
34  return self.appendappend(self._item_class_item_class(*args, **kwargs))
35 
36  def append(self, item):
37  self._check_type_and_set_attrs_check_type_and_set_attrs(item)
38  self._items_items.append(item)
39  return item
40 
41  def _check_type_and_set_attrs(self, *items):
42  common_attrs = self._common_attrs_common_attrs or {}
43  for item in items:
44  if not isinstance(item, self._item_class_item_class):
45  raise TypeError(f'Only {type_name(self._item_class)} objects '
46  f'accepted, got {type_name(item)}.')
47  for attr in common_attrs:
48  setattr(item, attr, common_attrs[attr])
49  return items
50 
51  def extend(self, items):
52  self._items_items.extend(self._check_type_and_set_attrs_check_type_and_set_attrs(*items))
53 
54  def insert(self, index, item):
55  self._check_type_and_set_attrs_check_type_and_set_attrs(item)
56  self._items_items.insert(index, item)
57 
58  def index(self, item, *start_and_end):
59  return self._items_items.index(item, *start_and_end)
60 
61  def clear(self):
62  self._items_items = []
63 
64  def visit(self, visitor):
65  for item in self:
66  item.visit(visitor)
67 
68  def __iter__(self):
69  index = 0
70  while index < len(self._items_items):
71  yield self._items_items[index]
72  index += 1
73 
74  def __getitem__(self, index):
75  if isinstance(index, slice):
76  return self._create_new_from_create_new_from(self._items_items[index])
77  return self._items_items[index]
78 
79  def _create_new_from(self, items):
80  # Cannot pass common_attrs directly to new object because all
81  # subclasses don't have compatible __init__.
82  new = type(self)(self._item_class_item_class)
83  new._common_attrs = self._common_attrs_common_attrs
84  new.extend(items)
85  return new
86 
87  def __setitem__(self, index, item):
88  if isinstance(index, slice):
89  self._check_type_and_set_attrs_check_type_and_set_attrs(*item)
90  else:
91  self._check_type_and_set_attrs_check_type_and_set_attrs(item)
92  self._items_items[index] = item
93 
94  def __delitem__(self, index):
95  del self._items_items[index]
96 
97  def __contains__(self, item):
98  return item in self._items_items
99 
100  def __len__(self):
101  return len(self._items_items)
102 
103  def __str__(self):
104  return str(list(self))
105 
106  def __repr__(self):
107  class_name = type(self).__name__
108  item_name = self._item_class_item_class.__name__
109  return f'{class_name}(item_class={item_name}, items={self._items})'
110 
111  def count(self, item):
112  return self._items_items.count(item)
113 
114  def sort(self):
115  self._items_items.sort()
116 
117  def reverse(self):
118  self._items_items.reverse()
119 
120  def __reversed__(self):
121  index = 0
122  while index < len(self._items_items):
123  yield self._items_items[len(self._items_items) - index - 1]
124  index += 1
125 
126  def __eq__(self, other):
127  return (isinstance(other, ItemList)
128  and self._is_compatible_is_compatible(other)
129  and self._items_items == other._items)
130 
131  def _is_compatible(self, other):
132  return (self._item_class_item_class is other._item_class
133  and self._common_attrs_common_attrs == other._common_attrs)
134 
135  def __lt__(self, other):
136  if not isinstance(other, ItemList):
137  raise TypeError(f'Cannot order ItemList and {type_name(other)}.')
138  if not self._is_compatible_is_compatible(other):
139  raise TypeError('Cannot order incompatible ItemLists.')
140  return self._items_items < other._items
141 
142  def __add__(self, other):
143  if not isinstance(other, ItemList):
144  raise TypeError(f'Cannot add ItemList and {type_name(other)}.')
145  if not self._is_compatible_is_compatible(other):
146  raise TypeError('Cannot add incompatible ItemLists.')
147  return self._create_new_from_create_new_from(self._items_items + other._items)
148 
149  def __iadd__(self, other):
150  if isinstance(other, ItemList) and not self._is_compatible_is_compatible(other):
151  raise TypeError('Cannot add incompatible ItemLists.')
152  self.extendextend(other)
153  return self
154 
155  def __mul__(self, other):
156  return self._create_new_from_create_new_from(self._items_items * other)
157 
158  def __imul__(self, other):
159  self._items_items *= other
160  return self
161 
162  def __rmul__(self, other):
163  return self * other
def __mul__(self, other)
Definition: itemlist.py:155
def _is_compatible(self, other)
Definition: itemlist.py:131
def __eq__(self, other)
Definition: itemlist.py:126
def __imul__(self, other)
Definition: itemlist.py:158
def __delitem__(self, index)
Definition: itemlist.py:94
def extend(self, items)
Definition: itemlist.py:51
def create(self, *args, **kwargs)
Definition: itemlist.py:33
def index(self, item, *start_and_end)
Definition: itemlist.py:58
def __iadd__(self, other)
Definition: itemlist.py:149
def __setitem__(self, index, item)
Definition: itemlist.py:87
def __add__(self, other)
Definition: itemlist.py:142
def _create_new_from(self, items)
Definition: itemlist.py:79
def __rmul__(self, other)
Definition: itemlist.py:162
def append(self, item)
Definition: itemlist.py:36
def insert(self, index, item)
Definition: itemlist.py:54
def __getitem__(self, index)
Definition: itemlist.py:74
def _check_type_and_set_attrs(self, *items)
Definition: itemlist.py:41
def __lt__(self, other)
Definition: itemlist.py:135
def visit(self, visitor)
Definition: itemlist.py:64
def __init__(self, item_class, common_attrs=None, items=None)
Definition: itemlist.py:26
def __contains__(self, item)
Definition: itemlist.py:97