Store the comments of an extracted model on the Box / BoxList tree built from the same document.
(root, model: dict | None)
| 209 | |
| 210 | |
| 211 | def apply_comments(root, model: dict | None) -> None: |
| 212 | """Store the comments of an extracted model on the Box / BoxList tree built from the same document.""" |
| 213 | if not model: |
| 214 | return |
| 215 | for position in DOCUMENT_POSITIONS: |
| 216 | if model.get(position): |
| 217 | root._comment_document()[position] = list(model[position]) |
| 218 | for path, comment in model["keys"].items(): |
| 219 | node = root |
| 220 | for segment in path[:-1]: |
| 221 | found = _resolve_key(node, segment) |
| 222 | if found is _MISSING: |
| 223 | node = None |
| 224 | break |
| 225 | if isinstance(node, list): |
| 226 | node = list.__getitem__(node, found) |
| 227 | elif isinstance(node, tuple): |
| 228 | node = tuple.__getitem__(node, found) |
| 229 | else: |
| 230 | node = dict.__getitem__(node, found) |
| 231 | if not hasattr(node, "_comment_store"): |
| 232 | node = None |
| 233 | break |
| 234 | if node is None: |
| 235 | continue # the key moved or disappeared when the Box was built (box_dots, camel_killer, ...) |
| 236 | key = _resolve_key(node, path[-1]) |
| 237 | if key is _MISSING: |
| 238 | continue |
| 239 | stored = {} |
| 240 | if comment["before"]: |
| 241 | stored["before"] = list(comment["before"]) |
| 242 | if comment["eol"]: |
| 243 | stored["eol"] = comment["eol"] |
| 244 | if stored: |
| 245 | node._comment_store()[key] = stored |
| 246 | |
| 247 | |
| 248 | def has_comments(obj, _seen=None) -> bool: |
nothing calls this directly
no test coverage detected