| 1 |
Read-only Tree |
| 2 |
============== |
| 3 |
|
| 4 |
**Application**: A read-only tree data structure, which denies modifications. |
| 5 |
|
| 6 |
The `Node._pre_attach` and `Node._pre_detach` hookups can be used |
| 7 |
for blocking tree modifications. |
| 8 |
If they raise an `Exception`, the tree is not modified. |
| 9 |
|
| 10 |
>>> from anytree import NodeMixin, RenderTree |
| 11 |
|
| 12 |
The exception: |
| 13 |
|
| 14 |
>>> class ReadOnlyError(RuntimeError): |
| 15 |
... pass |
| 16 |
|
| 17 |
Permanent |
| 18 |
--------- |
| 19 |
|
| 20 |
The read-only attribute needs to be set after attaching to parent: |
| 21 |
|
| 22 |
>>> class ReadOnlyNode(NodeMixin): |
| 23 |
... |
| 24 |
... def __init__(self, foo, parent=None): |
| 25 |
... super(ReadOnlyNode, self).__init__() |
| 26 |
... self.foo = foo |
| 27 |
... self.__readonly = False |
| 28 |
... self.parent = parent |
| 29 |
... self.__readonly = True |
| 30 |
... |
| 31 |
... def _pre_attach(self, parent): |
| 32 |
... if self.__readonly: |
| 33 |
... raise ReadOnlyError() |
| 34 |
... |
| 35 |
... def _pre_detach(self, parent): |
| 36 |
... raise ReadOnlyError() |
| 37 |
|
| 38 |
An example tree: |
| 39 |
|
| 40 |
>>> a = ReadOnlyNode("a") |
| 41 |
>>> a0 = ReadOnlyNode("a0", parent=a) |
| 42 |
>>> a1 = ReadOnlyNode("a1", parent=a) |
| 43 |
>>> a1a = ReadOnlyNode("a1a", parent=a1) |
| 44 |
>>> a2 = ReadOnlyNode("a2", parent=a) |
| 45 |
>>> print(RenderTree(a).by_attr("foo")) |
| 46 |
a |
| 47 |
├── a0 |
| 48 |
├── a1 |
| 49 |
│ └── a1a |
| 50 |
└── a2 |
| 51 |
|
| 52 |
Modifications raise an `ReadOnlyError` |
| 53 |
|
| 54 |
>>> a0.parent = a2 |
| 55 |
Traceback (most recent call last): |
| 56 |
... |
| 57 |
ReadOnlyError |
| 58 |
>>> a.children = [a1] |
| 59 |
Traceback (most recent call last): |
| 60 |
... |
| 61 |
ReadOnlyError |
| 62 |
|
| 63 |
The tree structure is untouched: |
| 64 |
|
| 65 |
>>> print(RenderTree(a).by_attr("foo")) |
| 66 |
a |
| 67 |
├── a0 |
| 68 |
├── a1 |
| 69 |
│ └── a1a |
| 70 |
└── a2 |
| 71 |
|
| 72 |
.. note:: |
| 73 |
|
| 74 |
It is important to use the ``_pre_*`` and **not** the ``_post_*`` methods. |
| 75 |
An exception raised by `_pre_detach(parent)` and `_pre_attach(parent)` will **prevent** the tree structure to be updated. |
| 76 |
The node keeps the old state. |
| 77 |
An exception raised by `_post_detach(parent)` and `_post_attach(parent)` does **not rollback** the tree structure modification. |
| 78 |
|
| 79 |
|
| 80 |
Temporary |
| 81 |
--------- |
| 82 |
|
| 83 |
To select the read-only mode temporarily, the root node should provide |
| 84 |
an attribute for all child nodes, set *after* construction. |
| 85 |
|
| 86 |
>>> class ReadOnlyNode(NodeMixin): |
| 87 |
... def __init__(self, foo, parent=None): |
| 88 |
... super(ReadOnlyNode, self).__init__() |
| 89 |
... self.readonly = False |
| 90 |
... self.foo = foo |
| 91 |
... self.parent = parent |
| 92 |
... def _pre_attach(self, parent): |
| 93 |
... if self.root.readonly: |
| 94 |
... raise ReadOnlyError() |
| 95 |
... def _pre_detach(self, parent): |
| 96 |
... if self.root.readonly: |
| 97 |
... raise ReadOnlyError() |
| 98 |
|
| 99 |
An example tree: |
| 100 |
|
| 101 |
>>> a = ReadOnlyNode("a") |
| 102 |
>>> a0 = ReadOnlyNode("a0", parent=a) |
| 103 |
>>> a1 = ReadOnlyNode("a1", parent=a) |
| 104 |
>>> a1a = ReadOnlyNode("a1a", parent=a1) |
| 105 |
>>> a2 = ReadOnlyNode("a2", parent=a) |
| 106 |
>>> print(RenderTree(a).by_attr("foo")) |
| 107 |
a |
| 108 |
├── a0 |
| 109 |
├── a1 |
| 110 |
│ └── a1a |
| 111 |
└── a2 |
| 112 |
|
| 113 |
Switch to read-only mode: |
| 114 |
|
| 115 |
>>> a.readonly = True |
| 116 |
|
| 117 |
>>> a0.parent = a2 |
| 118 |
Traceback (most recent call last): |
| 119 |
... |
| 120 |
ReadOnlyError |
| 121 |
>>> a.children = [a1] |
| 122 |
Traceback (most recent call last): |
| 123 |
... |
| 124 |
ReadOnlyError |
| 125 |
|
| 126 |
Disable read-only mode: |
| 127 |
|
| 128 |
>>> a.readonly = False |
| 129 |
|
| 130 |
Modifications are allowed now: |
| 131 |
|
| 132 |
>>> a0.parent = a2 |
| 133 |
>>> print(RenderTree(a).by_attr("foo")) |
| 134 |
a |
| 135 |
├── a1 |
| 136 |
│ └── a1a |
| 137 |
└── a2 |
| 138 |
└── a0 |