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

Function commented_tree

box/comments.py:283–323  ·  view source on GitHub ↗

Turn `plain` (the to_dict / to_list output of `obj`) into ruamel CommentedMap / CommentedSeq objects carrying the comments stored on `obj` and its nested boxes. `column` is where this container's keys (or list dashes) start with ruamel's default layout: nested mappings are indented

(obj, plain, column=0, _seen=None)

Source from the content-addressed store, hash-verified

281
282
283def commented_tree(obj, plain, column=0, _seen=None):
284 """
285 Turn `plain` (the to_dict / to_list output of `obj`) into ruamel CommentedMap / CommentedSeq
286 objects carrying the comments stored on `obj` and its nested boxes.
287
288 `column` is where this container's keys (or list dashes) start with ruamel's default layout:
289 nested mappings are indented by 2, sequences start at their parent key's column.
290 """
291 from ruamel.yaml.comments import CommentedMap, CommentedSeq
292
293 seen = _seen if _seen is not None else set()
294 if isinstance(plain, tuple) and hasattr(obj, "_comment_store"):
295 plain = list(plain) # a frozen list: a tuple that can carry comments
296 if isinstance(plain, (dict, list)):
297 if id(plain) in seen:
298 return plain # self references are written as they are
299 seen = seen | {id(plain)}
300 if isinstance(plain, dict):
301 out = CommentedMap()
302 for key, value in plain.items():
303 child = dict.get(obj, key) if isinstance(obj, dict) else None
304 out[key] = commented_tree(child, value, column if isinstance(value, list) else column + 2, seen)
305 keys = list(out)
306 elif isinstance(plain, list):
307 out = CommentedSeq()
308 for index, value in enumerate(plain):
309 child = (
310 (list if isinstance(obj, list) else tuple).__getitem__(obj, index)
311 if isinstance(obj, (list, tuple)) and index < len(obj)
312 else None
313 )
314 out.append(commented_tree(child, value, column + 2, seen))
315 keys = list(range(len(out)))
316 else:
317 return plain
318 store = obj._comment_store(create=False) if hasattr(obj, "_comment_store") else None
319 if store:
320 for key in keys:
321 if key in store:
322 _set_ruamel_comment(out, key, store[key], column)
323 return out
324
325
326def _document_lines(obj, position) -> str:

Callers 1

dump_yamlFunction · 0.85

Calls 6

_set_ruamel_commentFunction · 0.85
getMethod · 0.80
appendMethod · 0.80
itemsMethod · 0.45
__getitem__Method · 0.45
_comment_storeMethod · 0.45

Tested by

no test coverage detected