Robot Framework Integrated Development Environment (RIDE)
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 robotide.lib.robot.utils import py2to3, unicode
17 
18 
19 @py2to3
20 class ItemList():
21  __slots__ = ['_item_class', '_common_attrs', '_items']
22 
23  def __init__(self, item_class, common_attrs=None, items=None):
24  self._item_class_item_class = item_class
25  self._common_attrs_common_attrs = common_attrs
26  self._items_items = ()
27  if items:
28  self.extendextend(items)
29 
30  def create(self, *args, **kwargs):
31  return self.appendappend(self._item_class_item_class(*args, **kwargs))
32 
33  def append(self, item):
34  self._check_type_and_set_attrs_check_type_and_set_attrs(item)
35  self._items_items += (item,)
36  return item
37 
38  def _check_type_and_set_attrs(self, *items):
39  common_attrs = self._common_attrs_common_attrs or {}
40  for item in items:
41  if not isinstance(item, self._item_class_item_class):
42  raise TypeError("Only %s objects accepted, got %s."
43  % (self._item_class_item_class.__name__,
44  item.__class__.__name__))
45  for attr in common_attrs:
46  setattr(item, attr, common_attrs[attr])
47  return items
48 
49  def extend(self, items):
50  self._items_items += self._check_type_and_set_attrs_check_type_and_set_attrs(*items)
51 
52  def insert(self, index, item):
53  self._check_type_and_set_attrs_check_type_and_set_attrs(item)
54  items = list(self._items_items)
55  items.insert(index, item)
56  self._items_items = tuple(items)
57 
58  def pop(self, *index):
59  items = list(self._items_items)
60  result = items.pop(*index)
61  self._items_items = tuple(items)
62  return result
63 
64  def index(self, item, *start_and_end):
65  return self._items_items.index(item, *start_and_end)
66 
67  def clear(self):
68  self._items_items = ()
69 
70  def visit(self, visitor):
71  for item in self._items_items:
72  item.visit(visitor)
73 
74  def __iter__(self):
75  return iter(self._items_items)
76 
77  def __getitem__(self, index):
78  if not isinstance(index, slice):
79  return self._items_items[index]
80  items = self.__class__(self._item_class_item_class)
81  items._common_attrs = self._common_attrs_common_attrs
82  items.extend(self._items_items[index])
83  return items
84 
85  def __setitem__(self, index, item):
86  if isinstance(index, slice):
87  self._check_type_and_set_attrs_check_type_and_set_attrs(*item)
88  else:
89  self._check_type_and_set_attrs_check_type_and_set_attrs(item)
90  items = list(self._items_items)
91  items[index] = item
92  self._items_items = tuple(items)
93 
94  def __len__(self):
95  return len(self._items_items)
96 
97  def __unicode__(self):
98  return u'[%s]' % ', '.join(unicode(item) for item in self)
def create(self, *args, **kwargs)
Definition: itemlist.py:30
def __init__(self, item_class, common_attrs=None, items=None)
Definition: itemlist.py:23
def __setitem__(self, index, item)
Definition: itemlist.py:85
def index(self, item, *start_and_end)
Definition: itemlist.py:64
def _check_type_and_set_attrs(self, *items)
Definition: itemlist.py:38
unicode
Exceptions and return codes used internally.
Definition: errors.py:24