| 1 |
from .entity import Entity |
| 2 |
try: |
| 3 |
from collections.abc import Iterable |
| 4 |
except ImportError: |
| 5 |
from collections import Iterable |
| 6 |
from . import util |
| 7 |
|
| 8 |
|
| 9 |
class Container: |
| 10 |
""" |
| 11 |
Container acts as an interface to container groups in the backend. In the |
| 12 |
case of HDF5, this is a group that is used as a container for other groups. |
| 13 |
|
| 14 |
Examples of Containers: |
| 15 |
Block.data_arrays |
| 16 |
Block.tags |
| 17 |
Block.multi_tags |
| 18 |
Source.sources |
| 19 |
|
| 20 |
:param name: Name of the container |
| 21 |
:param parent: Parent NIX object where this container will be created |
| 22 |
:param itemclass: The class of the objects this container holds (for |
| 23 |
checking and instantiations) |
| 24 |
""" |
| 25 |
|
| 26 |
def __init__(self, name, nixfile, parent, itemclass): |
| 27 |
self._backend = parent._h5group.open_group(name) |
| 28 |
self._itemclass = itemclass |
| 29 |
self._file = nixfile |
| 30 |
self._parent = parent |
| 31 |
self._name = name |
| 32 |
|
| 33 |
def _inst_item(self, item): |
| 34 |
return self._itemclass(self._file, self._parent, item) |
| 35 |
|
| 36 |
def __len__(self): |
| 37 |
return len(self._backend) |
| 38 |
|
| 39 |
def __getitem__(self, item): |
| 40 |
if isinstance(item, int): |
| 41 |
if item < 0: |
| 42 |
item = len(self) + item |
| 43 |
if item < 0 or item >= len(self): |
| 44 |
raise IndexError("Index out of bounds: {}".format(item)) |
| 45 |
item = self._backend.get_by_pos(item) |
| 46 |
else: |
| 47 |
item = self._backend.get_by_id_or_name(item) |
| 48 |
return self._inst_item(item) |
| 49 |
|
| 50 |
def __delitem__(self, item): |
| 51 |
if not isinstance(item, Entity): |
| 52 |
item = self[item] |
| 53 |
|
| 54 |
if not isinstance(item, self._itemclass): |
| 55 |
raise TypeError( |
| 56 |
"Wrong item type: {} required or the name or ID of one".format( |
| 57 |
self._itemclass.__name__) |
| 58 |
) |
| 59 |
|
| 60 |
self._file._h5group.delete_all([item.id]) |
| 61 |
|
| 62 |
def __iter__(self): |
| 63 |
for group in self._backend: |
| 64 |
yield self._inst_item(group) |
| 65 |
|
| 66 |
def __contains__(self, item): |
| 67 |
if hasattr(item, "id"): |
| 68 |
if isinstance(item, self._itemclass): |
| 69 |
return item.name in self._backend |
| 70 |
|
| 71 |
raise TypeError( |
| 72 |
"Wrong item type: {} required or the name or ID of one".format( |
| 73 |
self._itemclass.__name__) |
| 74 |
) |
| 75 |
if util.is_uuid(item): |
| 76 |
try: |
| 77 |
self._backend.get_by_id(item) |
| 78 |
return True |
| 79 |
except KeyError: |
| 80 |
return False |
| 81 |
else: |
| 82 |
return item in self._backend |
| 83 |
|
| 84 |
def __str__(self): |
| 85 |
return "[{}]".format( |
| 86 |
", ".join(str(item) for item in self) |
| 87 |
) |
| 88 |
|
| 89 |
def __repr__(self): |
| 90 |
return str(self) |
| 91 |
|
| 92 |
@staticmethod |
| 93 |
def _item_key(item): |
| 94 |
return item.name |
| 95 |
|
| 96 |
def items(self): |
| 97 |
for group in self._backend: |
| 98 |
item = self._inst_item(group) |
| 99 |
yield item.id, item |
| 100 |
|
| 101 |
|
| 102 |
class SectionContainer(Container): |
| 103 |
""" |
| 104 |
SectionContainer extends Container with a new __delitem__ method. |
| 105 |
When a Section is deleted, all child sections need to be deleted |
| 106 |
individually to make sure all their references are removed. |
| 107 |
""" |
| 108 |
def __delitem__(self, item): |
| 109 |
if not isinstance(item, Entity): |
| 110 |
item = self[item] |
| 111 |
|
| 112 |
if not isinstance(item, self._itemclass): |
| 113 |
raise TypeError( |
| 114 |
"Wrong item type: {} required or the name or ID of one".format( |
| 115 |
self._itemclass.__name__) |
| 116 |
) |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
secids = [s.id for s in item.find_sections()] |
| 121 |
|
| 122 |
self._file._h5group.delete_all(secids) |
| 123 |
|
| 124 |
|
| 125 |
class SourceContainer(Container): |
| 126 |
""" |
| 127 |
SourceContainer extends Container with a new __delitem__ method. |
| 128 |
When a Source is deleted, all child sources need to be deleted individually |
| 129 |
to make sure all their references are removed. |
| 130 |
""" |
| 131 |
def __delitem__(self, item): |
| 132 |
if not isinstance(item, Entity): |
| 133 |
item = self[item] |
| 134 |
|
| 135 |
if not isinstance(item, self._itemclass): |
| 136 |
raise TypeError( |
| 137 |
"Wrong item type: {} required or the name or ID of one".format( |
| 138 |
self._itemclass.__name__) |
| 139 |
) |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
srcids = [s.id for s in item.find_sources()] |
| 144 |
srcids.append(item.id) |
| 145 |
self._file._h5group.delete_all(srcids) |
| 146 |
|
| 147 |
|
| 148 |
class LinkContainer(Container): |
| 149 |
""" |
| 150 |
A LinkContainer acts as an interface to container groups in the backend |
| 151 |
that hold links to objects already contained in a Container. |
| 152 |
|
| 153 |
Objects are added to a LinkContainer using the 'append' method, as |
| 154 |
opposed to Containers which get populated by the parent object 'create' |
| 155 |
methods. |
| 156 |
|
| 157 |
An important difference between a LinkContainer and a Container is that |
| 158 |
links to objects are indexed by their 'id' whereas objects in Containers |
| 159 |
are indexed by 'name'. |
| 160 |
|
| 161 |
Examples of LinkContainers: |
| 162 |
Group.data_arrays |
| 163 |
Group.tags |
| 164 |
Group.multi_tags |
| 165 |
|
| 166 |
:param name: Name of the container |
| 167 |
|
| 168 |
:param parent: Parent H5Group where this container will be created |
| 169 |
|
| 170 |
:param itemclass: The class of the objects this container holds (for |
| 171 |
checking and instantiations) |
| 172 |
|
| 173 |
:param itemstore: The location (Container) where the original objects |
| 174 |
are stored and linked to. |
| 175 |
""" |
| 176 |
|
| 177 |
def __init__(self, name, parent, itemclass, itemstore): |
| 178 |
super(LinkContainer, self).__init__(name, parent.file, |
| 179 |
parent, itemclass) |
| 180 |
self._itemstore = itemstore |
| 181 |
|
| 182 |
def __delitem__(self, item): |
| 183 |
if not isinstance(item, Entity): |
| 184 |
item = self[item] |
| 185 |
|
| 186 |
if not isinstance(item, self._itemclass): |
| 187 |
raise TypeError( |
| 188 |
"Wrong item type: {} required or the name or ID of one".format( |
| 189 |
self._itemclass.__name__) |
| 190 |
) |
| 191 |
|
| 192 |
self._backend.delete(item.id) |
| 193 |
|
| 194 |
def append(self, item): |
| 195 |
if util.is_uuid(item): |
| 196 |
item = self._inst_item(self._backend.get_by_id(item)) |
| 197 |
|
| 198 |
if not hasattr(item, "id"): |
| 199 |
raise TypeError("NIX entity or id string required for append") |
| 200 |
|
| 201 |
if item not in self._itemstore: |
| 202 |
raise RuntimeError("This item cannot be appended here.") |
| 203 |
|
| 204 |
self._backend.create_link(item, item.id) |
| 205 |
|
| 206 |
def extend(self, items): |
| 207 |
if not isinstance(items, Iterable): |
| 208 |
raise TypeError("{} object is not iterable".format(type(items))) |
| 209 |
for item in items: |
| 210 |
self.append(item) |
| 211 |
|
| 212 |
def __getitem__(self, identifier): |
| 213 |
if isinstance(identifier, int): |
| 214 |
return super(LinkContainer, self).__getitem__(identifier) |
| 215 |
else: |
| 216 |
if util.is_uuid(identifier): |
| 217 |
|
| 218 |
item = self._backend.get_by_name(identifier) |
| 219 |
return self._inst_item(item) |
| 220 |
else: |
| 221 |
for grp in self._backend: |
| 222 |
if identifier == grp.get_attr("name"): |
| 223 |
return self._inst_item(grp) |
| 224 |
|
| 225 |
raise KeyError("Item not found '{}'".format(identifier)) |
| 226 |
|
| 227 |
def __contains__(self, item): |
| 228 |
|
| 229 |
if hasattr(item, "id"): |
| 230 |
if isinstance(item, self._itemclass): |
| 231 |
return item.id in self._backend |
| 232 |
|
| 233 |
raise TypeError( |
| 234 |
"Wrong item type: {} required or the name or ID of one".format( |
| 235 |
self._itemclass.__name__) |
| 236 |
) |
| 237 |
|
| 238 |
if util.is_uuid(item): |
| 239 |
return item in self._backend |
| 240 |
|
| 241 |
|
| 242 |
for grp in self._backend: |
| 243 |
if item == grp.get_attr("name"): |
| 244 |
return True |
| 245 |
return False |
| 246 |
|
| 247 |
def _inst_item(self, item): |
| 248 |
return self._itemclass(self._file, self._itemstore._parent, item) |
| 249 |
|
| 250 |
@staticmethod |
| 251 |
def _item_key(item): |
| 252 |
return item.id |