|
13 | 13 | T = TypeVar("T", bound=node.Node) |
14 | 14 |
|
15 | 15 |
|
| 16 | +def _assemble_attributes( |
| 17 | + _node: T, |
| 18 | + attr_dict: Optional[Dict[str, str]], |
| 19 | + all_attrs: bool, |
| 20 | + data_child: Dict[str, Any] = None, |
| 21 | +) -> Dict[str, Any]: |
| 22 | + """Assemble attributes of node into a dictionary. |
| 23 | +
|
| 24 | + Args: |
| 25 | + _node: node |
| 26 | + attr_dict: node attributes mapped to dictionary key, key: node attributes, value: corresponding dictionary key |
| 27 | + all_attrs: indicator whether to retrieve all ``Node`` attributes, overrides `attr_dict` |
| 28 | + data_child: existing attributes, if any |
| 29 | +
|
| 30 | + Returns: |
| 31 | + node attributes |
| 32 | + """ |
| 33 | + data_child = data_child or {} |
| 34 | + if all_attrs: |
| 35 | + data_child.update( |
| 36 | + dict(_node.describe(exclude_attributes=["name"], exclude_prefix="_")) |
| 37 | + ) |
| 38 | + elif attr_dict: |
| 39 | + for k, v in attr_dict.items(): |
| 40 | + data_child[v] = _node.get_attr(k) |
| 41 | + return data_child |
| 42 | + |
| 43 | + |
16 | 44 | def tree_to_dict( |
17 | 45 | tree: T, |
18 | 46 | name_key: Optional[str] = "name", |
@@ -79,17 +107,9 @@ def _recursive_append(_node: T) -> None: |
79 | 107 | if _node.parent: |
80 | 108 | parent_name = _node.parent.node_name |
81 | 109 | data_child[parent_key] = parent_name |
82 | | - if all_attrs: |
83 | | - data_child.update( |
84 | | - dict( |
85 | | - _node.describe( |
86 | | - exclude_attributes=["name"], exclude_prefix="_" |
87 | | - ) |
88 | | - ) |
89 | | - ) |
90 | | - elif attr_dict: |
91 | | - for k, v in attr_dict.items(): |
92 | | - data_child[v] = _node.get_attr(k) |
| 110 | + data_child = _assemble_attributes( |
| 111 | + _node, attr_dict, all_attrs, data_child |
| 112 | + ) |
93 | 113 | data_dict[_node.path_name] = data_child |
94 | 114 | for _child in _node.children: |
95 | 115 | _recursive_append(_child) |
@@ -145,17 +165,9 @@ def _recursive_append(_node: T, parent_dict: Dict[str, Any]) -> None: |
145 | 165 | if _node: |
146 | 166 | if not max_depth or _node.depth <= max_depth: |
147 | 167 | data_child = {name_key: _node.node_name} |
148 | | - if all_attrs: |
149 | | - data_child.update( |
150 | | - dict( |
151 | | - _node.describe( |
152 | | - exclude_attributes=["name"], exclude_prefix="_" |
153 | | - ) |
154 | | - ) |
155 | | - ) |
156 | | - elif attr_dict: |
157 | | - for k, v in attr_dict.items(): |
158 | | - data_child[v] = _node.get_attr(k) |
| 168 | + data_child = _assemble_attributes( |
| 169 | + _node, attr_dict, all_attrs, data_child |
| 170 | + ) |
159 | 171 | if child_key in parent_dict: |
160 | 172 | parent_dict[child_key].append(data_child) |
161 | 173 | else: |
@@ -212,18 +224,7 @@ def _recursive_append(_node: T, parent_dict: Dict[str, Any]) -> None: |
212 | 224 | """ |
213 | 225 | if _node: |
214 | 226 | if not max_depth or _node.depth <= max_depth: |
215 | | - data_child = {} |
216 | | - if all_attrs: |
217 | | - data_child.update( |
218 | | - dict( |
219 | | - _node.describe( |
220 | | - exclude_attributes=["name"], exclude_prefix="_" |
221 | | - ) |
222 | | - ) |
223 | | - ) |
224 | | - elif attr_dict: |
225 | | - for k, v in attr_dict.items(): |
226 | | - data_child[v] = _node.get_attr(k) |
| 227 | + data_child = _assemble_attributes(_node, attr_dict, all_attrs) |
227 | 228 | if child_key in parent_dict: |
228 | 229 | parent_dict[child_key][_node.node_name] = data_child |
229 | 230 | else: |
|
0 commit comments