MCPcopy Create free account
hub / github.com/cdgriffith/Box / apply_comments

Function apply_comments

box/comments.py:211–245  ·  view source on GitHub ↗

Store the comments of an extracted model on the Box / BoxList tree built from the same document.

(root, model: dict | None)

Source from the content-addressed store, hash-verified

209
210
211def 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
248def has_comments(obj, _seen=None) -> bool:

Callers

nothing calls this directly

Calls 6

_resolve_keyFunction · 0.85
getMethod · 0.80
_comment_documentMethod · 0.45
itemsMethod · 0.45
__getitem__Method · 0.45
_comment_storeMethod · 0.45

Tested by

no test coverage detected