|
1 | 1 | from abc import ABC |
| 2 | +from collections.abc import MutableMapping |
| 3 | + |
| 4 | +from xattree import xattree |
2 | 5 |
|
3 | 6 | COMPONENTS = {} |
4 | 7 | """MF6 component registry.""" |
5 | 8 |
|
6 | 9 |
|
7 | | -class Component(ABC): |
| 10 | +@xattree |
| 11 | +class Component(ABC, MutableMapping): |
8 | 12 | @classmethod |
9 | 13 | def __attrs_init_subclass__(cls): |
10 | 14 | COMPONENTS[cls.__name__.lower()] = cls |
| 15 | + |
| 16 | + def __attrs_post_init__(self): |
| 17 | + self._where = type(self).__xattree__["where"] |
| 18 | + |
| 19 | + def __getitem__(self, key): |
| 20 | + data = getattr(self, self._where) |
| 21 | + return data.children[key] |
| 22 | + |
| 23 | + def __setitem__(self, key, value): |
| 24 | + data = getattr(self, self._where) |
| 25 | + if key in data.children: |
| 26 | + data.update({key: value}) |
| 27 | + else: |
| 28 | + data = data.assign({key: value}) |
| 29 | + setattr(self, self._where, data) |
| 30 | + |
| 31 | + def __delitem__(self, key): |
| 32 | + data = getattr(self, self._where) |
| 33 | + data = data.drop_nodes(key) |
| 34 | + setattr(self, self._where, data) |
| 35 | + |
| 36 | + def __iter__(self): |
| 37 | + data = getattr(self, self._where) |
| 38 | + return iter(data.children) |
| 39 | + |
| 40 | + def __len__(self): |
| 41 | + data = getattr(self, self._where) |
| 42 | + return len(data.children) |
0 commit comments