A hybrid list/dict-like view to a group of repeated MJCF elements.
| 1324 | |
| 1325 | |
| 1326 | class _ElementListView: |
| 1327 | """A hybrid list/dict-like view to a group of repeated MJCF elements.""" |
| 1328 | |
| 1329 | def __init__(self, spec, parent): |
| 1330 | self._spec = spec |
| 1331 | self._parent = parent |
| 1332 | self._elements = self._parent._children # pylint: disable=protected-access |
| 1333 | self._scoped_elements = collections.OrderedDict( |
| 1334 | [(scope_namescope.name, getattr(scoped_parent, self._spec.name)) |
| 1335 | for scope_namescope, scoped_parent |
| 1336 | in self._parent._attachments.items()]) |
| 1337 | |
| 1338 | @property |
| 1339 | def spec(self): |
| 1340 | return self._spec |
| 1341 | |
| 1342 | @property |
| 1343 | def tag(self): |
| 1344 | return self._spec.name |
| 1345 | |
| 1346 | @property |
| 1347 | def namescope(self): |
| 1348 | return self._parent.namescope |
| 1349 | |
| 1350 | @property |
| 1351 | def parent(self): |
| 1352 | return self._parent |
| 1353 | |
| 1354 | def __len__(self): |
| 1355 | return len(self._full_list()) |
| 1356 | |
| 1357 | def __iter__(self): |
| 1358 | return iter(self._full_list()) |
| 1359 | |
| 1360 | def _identifier_not_found_error(self, index): |
| 1361 | return KeyError('An element <{}> with {}={!r} does not exist' |
| 1362 | .format(self._spec.name, self._spec.identifier, index)) |
| 1363 | |
| 1364 | def _find_index(self, index): |
| 1365 | """Locates an element given the index among siblings with the same tag.""" |
| 1366 | if isinstance(index, str) and self._spec.identifier: |
| 1367 | for i, element in enumerate(self._elements): |
| 1368 | if (element.tag == self._spec.name |
| 1369 | and getattr(element, self._spec.identifier) == index): |
| 1370 | return i |
| 1371 | raise self._identifier_not_found_error(index) |
| 1372 | else: |
| 1373 | count = 0 |
| 1374 | for i, element in enumerate(self._elements): |
| 1375 | if element.tag == self._spec.name: |
| 1376 | if index == count: |
| 1377 | return i |
| 1378 | else: |
| 1379 | count += 1 |
| 1380 | raise IndexError('list index out of range') |
| 1381 | |
| 1382 | def _full_list(self): |
| 1383 | out_list = [element for element in self._elements |
no outgoing calls
no test coverage detected
searching dependent graphs…