| 1 |
# coding: utf-8 |
| 2 |
|
| 3 |
from __future__ import absolute_import, print_function |
| 4 |
|
| 5 |
""" |
| 6 |
stuff to deal with comments and formatting on dict/list/ordereddict/set |
| 7 |
these are not really related, formatting could be factored out as |
| 8 |
a separate base |
| 9 |
""" |
| 10 |
|
| 11 |
import sys |
| 12 |
import copy |
| 13 |
|
| 14 |
|
| 15 |
from strictyaml.ruamel.compat import ordereddict # type: ignore |
| 16 |
from strictyaml.ruamel.compat import PY2, string_types, MutableSliceableSequence |
| 17 |
from strictyaml.ruamel.scalarstring import ScalarString |
| 18 |
from strictyaml.ruamel.anchor import Anchor |
| 19 |
|
| 20 |
if PY2: |
| 21 |
from collections import MutableSet, Sized, Set, Mapping |
| 22 |
else: |
| 23 |
from collections.abc import MutableSet, Sized, Set, Mapping |
| 24 |
|
| 25 |
if False: # MYPY |
| 26 |
from typing import Any, Dict, Optional, List, Union, Optional, Iterator # NOQA |
| 27 |
|
| 28 |
# fmt: off |
| 29 |
__all__ = ['CommentedSeq', 'CommentedKeySeq', |
| 30 |
'CommentedMap', 'CommentedOrderedMap', |
| 31 |
'CommentedSet', 'comment_attrib', 'merge_attrib'] |
| 32 |
# fmt: on |
| 33 |
|
| 34 |
comment_attrib = "_yaml_comment" |
| 35 |
format_attrib = "_yaml_format" |
| 36 |
line_col_attrib = "_yaml_line_col" |
| 37 |
merge_attrib = "_yaml_merge" |
| 38 |
tag_attrib = "_yaml_tag" |
| 39 |
|
| 40 |
|
| 41 |
class Comment(object): |
| 42 |
# sys.getsize tested the Comment objects, __slots__ makes them bigger |
| 43 |
# and adding self.end did not matter |
| 44 |
__slots__ = "comment", "_items", "_end", "_start" |
| 45 |
attrib = comment_attrib |
| 46 |
|
| 47 |
def __init__(self): |
| 48 |
# type: () -> None |
| 49 |
self.comment = None # [post, [pre]] |
| 50 |
# map key (mapping/omap/dict) or index (sequence/list) to a list of |
| 51 |
# dict: post_key, pre_key, post_value, pre_value |
| 52 |
# list: pre item, post item |
| 53 |
self._items = {} # type: Dict[Any, Any] |
| 54 |
# self._start = [] # should not put these on first item |
| 55 |
self._end = [] # type: List[Any] # end of document comments |
| 56 |
|
| 57 |
def __str__(self): |
| 58 |
# type: () -> str |
| 59 |
if bool(self._end): |
| 60 |
end = ",\n end=" + str(self._end) |
| 61 |
else: |
| 62 |
end = "" |
| 63 |
return "Comment(comment={0},\n items={1}{2})".format( |
| 64 |
self.comment, self._items, end |
| 65 |
) |
| 66 |
|
| 67 |
@property |
| 68 |
def items(self): |
| 69 |
# type: () -> Any |
| 70 |
return self._items |
| 71 |
|
| 72 |
@property |
| 73 |
def end(self): |
| 74 |
# type: () -> Any |
| 75 |
return self._end |
| 76 |
|
| 77 |
@end.setter |
| 78 |
def end(self, value): |
| 79 |
# type: (Any) -> None |
| 80 |
self._end = value |
| 81 |
|
| 82 |
@property |
| 83 |
def start(self): |
| 84 |
# type: () -> Any |
| 85 |
return self._start |
| 86 |
|
| 87 |
@start.setter |
| 88 |
def start(self, value): |
| 89 |
# type: (Any) -> None |
| 90 |
self._start = value |
| 91 |
|
| 92 |
|
| 93 |
# to distinguish key from None |
| 94 |
def NoComment(): |
| 95 |
# type: () -> None |
| 96 |
pass |
| 97 |
|
| 98 |
|
| 99 |
class Format(object): |
| 100 |
__slots__ = ("_flow_style",) |
| 101 |
attrib = format_attrib |
| 102 |
|
| 103 |
def __init__(self): |
| 104 |
# type: () -> None |
| 105 |
self._flow_style = None # type: Any |
| 106 |
|
| 107 |
def set_flow_style(self): |
| 108 |
# type: () -> None |
| 109 |
self._flow_style = True |
| 110 |
|
| 111 |
def set_block_style(self): |
| 112 |
# type: () -> None |
| 113 |
self._flow_style = False |
| 114 |
|
| 115 |
def flow_style(self, default=None): |
| 116 |
# type: (Optional[Any]) -> Any |
| 117 |
"""if default (the flow_style) is None, the flow style tacked on to |
| 118 |
the object explicitly will be taken. If that is None as well the |
| 119 |
default flow style rules the format down the line, or the type |
| 120 |
of the constituent values (simple -> flow, map/list -> block)""" |
| 121 |
if self._flow_style is None: |
| 122 |
return default |
| 123 |
return self._flow_style |
| 124 |
|
| 125 |
|
| 126 |
class LineCol(object): |
| 127 |
attrib = line_col_attrib |
| 128 |
|
| 129 |
def __init__(self): |
| 130 |
# type: () -> None |
| 131 |
self.line = None |
| 132 |
self.col = None |
| 133 |
self.data = None # type: Optional[Dict[Any, Any]] |
| 134 |
|
| 135 |
def add_kv_line_col(self, key, data): |
| 136 |
# type: (Any, Any) -> None |
| 137 |
if self.data is None: |
| 138 |
self.data = {} |
| 139 |
self.data[key] = data |
| 140 |
|
| 141 |
def key(self, k): |
| 142 |
# type: (Any) -> Any |
| 143 |
return self._kv(k, 0, 1) |
| 144 |
|
| 145 |
def value(self, k): |
| 146 |
# type: (Any) -> Any |
| 147 |
return self._kv(k, 2, 3) |
| 148 |
|
| 149 |
def _kv(self, k, x0, x1): |
| 150 |
# type: (Any, Any, Any) -> Any |
| 151 |
if self.data is None: |
| 152 |
return None |
| 153 |
data = self.data[k] |
| 154 |
return data[x0], data[x1] |
| 155 |
|
| 156 |
def item(self, idx): |
| 157 |
# type: (Any) -> Any |
| 158 |
if self.data is None: |
| 159 |
return None |
| 160 |
return self.data[idx][0], self.data[idx][1] |
| 161 |
|
| 162 |
def add_idx_line_col(self, key, data): |
| 163 |
# type: (Any, Any) -> None |
| 164 |
if self.data is None: |
| 165 |
self.data = {} |
| 166 |
self.data[key] = data |
| 167 |
|
| 168 |
|
| 169 |
class Tag(object): |
| 170 |
"""store tag information for roundtripping""" |
| 171 |
|
| 172 |
__slots__ = ("value",) |
| 173 |
attrib = tag_attrib |
| 174 |
|
| 175 |
def __init__(self): |
| 176 |
# type: () -> None |
| 177 |
self.value = None |
| 178 |
|
| 179 |
def __repr__(self): |
| 180 |
# type: () -> Any |
| 181 |
return "{0.__class__.__name__}({0.value!r})".format(self) |
| 182 |
|
| 183 |
|
| 184 |
class CommentedBase(object): |
| 185 |
@property |
| 186 |
def ca(self): |
| 187 |
# type: () -> Any |
| 188 |
if not hasattr(self, Comment.attrib): |
| 189 |
setattr(self, Comment.attrib, Comment()) |
| 190 |
return getattr(self, Comment.attrib) |
| 191 |
|
| 192 |
def yaml_end_comment_extend(self, comment, clear=False): |
| 193 |
# type: (Any, bool) -> None |
| 194 |
if comment is None: |
| 195 |
return |
| 196 |
if clear or self.ca.end is None: |
| 197 |
self.ca.end = [] |
| 198 |
self.ca.end.extend(comment) |
| 199 |
|
| 200 |
def yaml_key_comment_extend(self, key, comment, clear=False): |
| 201 |
# type: (Any, Any, bool) -> None |
| 202 |
r = self.ca._items.setdefault(key, [None, None, None, None]) |
| 203 |
if clear or r[1] is None: |
| 204 |
if comment[1] is not None: |
| 205 |
assert isinstance(comment[1], list) |
| 206 |
r[1] = comment[1] |
| 207 |
else: |
| 208 |
r[1].extend(comment[0]) |
| 209 |
r[0] = comment[0] |
| 210 |
|
| 211 |
def yaml_value_comment_extend(self, key, comment, clear=False): |
| 212 |
# type: (Any, Any, bool) -> None |
| 213 |
r = self.ca._items.setdefault(key, [None, None, None, None]) |
| 214 |
if clear or r[3] is None: |
| 215 |
if comment[1] is not None: |
| 216 |
assert isinstance(comment[1], list) |
| 217 |
r[3] = comment[1] |
| 218 |
else: |
| 219 |
r[3].extend(comment[0]) |
| 220 |
r[2] = comment[0] |
| 221 |
|
| 222 |
def yaml_set_start_comment(self, comment, indent=0): |
| 223 |
# type: (Any, Any) -> None |
| 224 |
"""overwrites any preceding comment lines on an object |
| 225 |
expects comment to be without `#` and possible have multiple lines |
| 226 |
""" |
| 227 |
from .error import CommentMark |
| 228 |
from .tokens import CommentToken |
| 229 |
|
| 230 |
pre_comments = self._yaml_get_pre_comment() |
| 231 |
if comment[-1] == "\n": |
| 232 |
comment = comment[:-1] # strip final newline if there |
| 233 |
start_mark = CommentMark(indent) |
| 234 |
for com in comment.split("\n"): |
| 235 |
c = com.strip() |
| 236 |
if len(c) > 0 and c[0] != "#": |
| 237 |
com = "# " + com |
| 238 |
pre_comments.append(CommentToken(com + "\n", start_mark, None)) |
| 239 |
|
| 240 |
def yaml_set_comment_before_after_key( |
| 241 |
self, key, before=None, indent=0, after=None, after_indent=None |
| 242 |
): |
| 243 |
# type: (Any, Any, Any, Any, Any) -> None |
| 244 |
""" |
| 245 |
expects comment (before/after) to be without `#` and possible have multiple lines |
| 246 |
""" |
| 247 |
from strictyaml.ruamel.error import CommentMark |
| 248 |
from strictyaml.ruamel.tokens import CommentToken |
| 249 |
|
| 250 |
def comment_token(s, mark): |
| 251 |
# type: (Any, Any) -> Any |
| 252 |
# handle empty lines as having no comment |
| 253 |
return CommentToken(("# " if s else "") + s + "\n", mark, None) |
| 254 |
|
| 255 |
if after_indent is None: |
| 256 |
after_indent = indent + 2 |
| 257 |
if before and (len(before) > 1) and before[-1] == "\n": |
| 258 |
before = before[:-1] # strip final newline if there |
| 259 |
if after and after[-1] == "\n": |
| 260 |
after = after[:-1] # strip final newline if there |
| 261 |
start_mark = CommentMark(indent) |
| 262 |
c = self.ca.items.setdefault(key, [None, [], None, None]) |
| 263 |
if before == "\n": |
| 264 |
c[1].append(comment_token("", start_mark)) |
| 265 |
elif before: |
| 266 |
for com in before.split("\n"): |
| 267 |
c[1].append(comment_token(com, start_mark)) |
| 268 |
if after: |
| 269 |
start_mark = CommentMark(after_indent) |
| 270 |
if c[3] is None: |
| 271 |
c[3] = [] |
| 272 |
for com in after.split("\n"): |
| 273 |
c[3].append(comment_token(com, start_mark)) # type: ignore |
| 274 |
|
| 275 |
@property |
| 276 |
def fa(self): |
| 277 |
# type: () -> Any |
| 278 |
"""format attribute |
| 279 |
|
| 280 |
set_flow_style()/set_block_style()""" |
| 281 |
if not hasattr(self, Format.attrib): |
| 282 |
setattr(self, Format.attrib, Format()) |
| 283 |
return getattr(self, Format.attrib) |
| 284 |
|
| 285 |
def yaml_add_eol_comment(self, comment, key=NoComment, column=None): |
| 286 |
# type: (Any, Optional[Any], Optional[Any]) -> None |
| 287 |
""" |
| 288 |
there is a problem as eol comments should start with ' #' |
| 289 |
(but at the beginning of the line the space doesn't have to be before |
| 290 |
the #. The column index is for the # mark |
| 291 |
""" |
| 292 |
from .tokens import CommentToken |
| 293 |
from .error import CommentMark |
| 294 |
|
| 295 |
if column is None: |
| 296 |
try: |
| 297 |
column = self._yaml_get_column(key) |
| 298 |
except AttributeError: |
| 299 |
column = 0 |
| 300 |
if comment[0] != "#": |
| 301 |
comment = "# " + comment |
| 302 |
if column is None: |
| 303 |
if comment[0] == "#": |
| 304 |
comment = " " + comment |
| 305 |
column = 0 |
| 306 |
start_mark = CommentMark(column) |
| 307 |
ct = [CommentToken(comment, start_mark, None), None] |
| 308 |
self._yaml_add_eol_comment(ct, key=key) |
| 309 |
|
| 310 |
@property |
| 311 |
def lc(self): |
| 312 |
# type: () -> Any |
| 313 |
if not hasattr(self, LineCol.attrib): |
| 314 |
setattr(self, LineCol.attrib, LineCol()) |
| 315 |
return getattr(self, LineCol.attrib) |
| 316 |
|
| 317 |
def _yaml_set_line_col(self, line, col): |
| 318 |
# type: (Any, Any) -> None |
| 319 |
self.lc.line = line |
| 320 |
self.lc.col = col |
| 321 |
|
| 322 |
def _yaml_set_kv_line_col(self, key, data): |
| 323 |
# type: (Any, Any) -> None |
| 324 |
self.lc.add_kv_line_col(key, data) |
| 325 |
|
| 326 |
def _yaml_set_idx_line_col(self, key, data): |
| 327 |
# type: (Any, Any) -> None |
| 328 |
self.lc.add_idx_line_col(key, data) |
| 329 |
|
| 330 |
@property |
| 331 |
def anchor(self): |
| 332 |
# type: () -> Any |
| 333 |
if not hasattr(self, Anchor.attrib): |
| 334 |
setattr(self, Anchor.attrib, Anchor()) |
| 335 |
return getattr(self, Anchor.attrib) |
| 336 |
|
| 337 |
def yaml_anchor(self): |
| 338 |
# type: () -> Any |
| 339 |
if not hasattr(self, Anchor.attrib): |
| 340 |
return None |
| 341 |
return self.anchor |
| 342 |
|
| 343 |
def yaml_set_anchor(self, value, always_dump=False): |
| 344 |
# type: (Any, bool) -> None |
| 345 |
self.anchor.value = value |
| 346 |
self.anchor.always_dump = always_dump |
| 347 |
|
| 348 |
@property |
| 349 |
def tag(self): |
| 350 |
# type: () -> Any |
| 351 |
if not hasattr(self, Tag.attrib): |
| 352 |
setattr(self, Tag.attrib, Tag()) |
| 353 |
return getattr(self, Tag.attrib) |
| 354 |
|
| 355 |
def yaml_set_tag(self, value): |
| 356 |
# type: (Any) -> None |
| 357 |
self.tag.value = value |
| 358 |
|
| 359 |
def copy_attributes(self, t, memo=None): |
| 360 |
# type: (Any, Any) -> None |
| 361 |
# fmt: off |
| 362 |
for a in [Comment.attrib, Format.attrib, LineCol.attrib, Anchor.attrib, |
| 363 |
Tag.attrib, merge_attrib]: |
| 364 |
if hasattr(self, a): |
| 365 |
if memo is not None: |
| 366 |
setattr(t, a, copy.deepcopy(getattr(self, a, memo))) |
| 367 |
else: |
| 368 |
setattr(t, a, getattr(self, a)) |
| 369 |
# fmt: on |
| 370 |
|
| 371 |
def _yaml_add_eol_comment(self, comment, key): |
| 372 |
# type: (Any, Any) -> None |
| 373 |
raise NotImplementedError |
| 374 |
|
| 375 |
def _yaml_get_pre_comment(self): |
| 376 |
# type: () -> Any |
| 377 |
raise NotImplementedError |
| 378 |
|
| 379 |
def _yaml_get_column(self, key): |
| 380 |
# type: (Any) -> Any |
| 381 |
raise NotImplementedError |
| 382 |
|
| 383 |
|
| 384 |
class CommentedSeq(MutableSliceableSequence, list, CommentedBase): # type: ignore |
| 385 |
__slots__ = (Comment.attrib, "_lst") |
| 386 |
|
| 387 |
def __init__(self, *args, **kw): |
| 388 |
# type: (Any, Any) -> None |
| 389 |
list.__init__(self, *args, **kw) |
| 390 |
|
| 391 |
def __getsingleitem__(self, idx): |
| 392 |
# type: (Any) -> Any |
| 393 |
return list.__getitem__(self, idx) |
| 394 |
|
| 395 |
def __setsingleitem__(self, idx, value): |
| 396 |
# type: (Any, Any) -> None |
| 397 |
# try to preserve the scalarstring type if setting an existing key to a new value |
| 398 |
if idx < len(self): |
| 399 |
if ( |
| 400 |
isinstance(value, string_types) |
| 401 |
and not isinstance(value, ScalarString) |
| 402 |
and isinstance(self[idx], ScalarString) |
| 403 |
): |
| 404 |
value = type(self[idx])(value) |
| 405 |
list.__setitem__(self, idx, value) |
| 406 |
|
| 407 |
def __delsingleitem__(self, idx=None): |
| 408 |
# type: (Any) -> Any |
| 409 |
list.__delitem__(self, idx) |
| 410 |
self.ca.items.pop(idx, None) # might not be there -> default value |
| 411 |
for list_index in sorted(self.ca.items): |
| 412 |
if list_index < idx: |
| 413 |
continue |
| 414 |
self.ca.items[list_index - 1] = self.ca.items.pop(list_index) |
| 415 |
|
| 416 |
def __len__(self): |
| 417 |
# type: () -> int |
| 418 |
return list.__len__(self) |
| 419 |
|
| 420 |
def insert(self, idx, val): |
| 421 |
# type: (Any, Any) -> None |
| 422 |
"""the comments after the insertion have to move forward""" |
| 423 |
list.insert(self, idx, val) |
| 424 |
for list_index in sorted(self.ca.items, reverse=True): |
| 425 |
if list_index < idx: |
| 426 |
break |
| 427 |
self.ca.items[list_index + 1] = self.ca.items.pop(list_index) |
| 428 |
|
| 429 |
def extend(self, val): |
| 430 |
# type: (Any) -> None |
| 431 |
list.extend(self, val) |
| 432 |
|
| 433 |
def __eq__(self, other): |
| 434 |
# type: (Any) -> bool |
| 435 |
return list.__eq__(self, other) |
| 436 |
|
| 437 |
def _yaml_add_comment(self, comment, key=NoComment): |
| 438 |
# type: (Any, Optional[Any]) -> None |
| 439 |
if key is not NoComment: |
| 440 |
self.yaml_key_comment_extend(key, comment) |
| 441 |
else: |
| 442 |
self.ca.comment = comment |
| 443 |
|
| 444 |
def _yaml_add_eol_comment(self, comment, key): |
| 445 |
# type: (Any, Any) -> None |
| 446 |
self._yaml_add_comment(comment, key=key) |
| 447 |
|
| 448 |
def _yaml_get_columnX(self, key): |
| 449 |
# type: (Any) -> Any |
| 450 |
return self.ca.items[key][0].start_mark.column |
| 451 |
|
| 452 |
def _yaml_get_column(self, key): |
| 453 |
# type: (Any) -> Any |
| 454 |
column = None |
| 455 |
sel_idx = None |
| 456 |
pre, post = key - 1, key + 1 |
| 457 |
if pre in self.ca.items: |
| 458 |
sel_idx = pre |
| 459 |
elif post in self.ca.items: |
| 460 |
sel_idx = post |
| 461 |
else: |
| 462 |
# self.ca.items is not ordered |
| 463 |
for row_idx, _k1 in enumerate(self): |
| 464 |
if row_idx >= key: |
| 465 |
break |
| 466 |
if row_idx not in self.ca.items: |
| 467 |
continue |
| 468 |
sel_idx = row_idx |
| 469 |
if sel_idx is not None: |
| 470 |
column = self._yaml_get_columnX(sel_idx) |
| 471 |
return column |
| 472 |
|
| 473 |
def _yaml_get_pre_comment(self): |
| 474 |
# type: () -> Any |
| 475 |
pre_comments = [] # type: List[Any] |
| 476 |
if self.ca.comment is None: |
| 477 |
self.ca.comment = [None, pre_comments] |
| 478 |
else: |
| 479 |
self.ca.comment[1] = pre_comments |
| 480 |
return pre_comments |
| 481 |
|
| 482 |
def __deepcopy__(self, memo): |
| 483 |
# type: (Any) -> Any |
| 484 |
res = self.__class__() |
| 485 |
memo[id(self)] = res |
| 486 |
for k in self: |
| 487 |
res.append(copy.deepcopy(k, memo)) |
| 488 |
self.copy_attributes(res, memo=memo) |
| 489 |
return res |
| 490 |
|
| 491 |
def __add__(self, other): |
| 492 |
# type: (Any) -> Any |
| 493 |
return list.__add__(self, other) |
| 494 |
|
| 495 |
def sort(self, key=None, reverse=False): # type: ignore |
| 496 |
# type: (Any, bool) -> None |
| 497 |
if key is None: |
| 498 |
tmp_lst = sorted(zip(self, range(len(self))), reverse=reverse) |
| 499 |
list.__init__(self, [x[0] for x in tmp_lst]) |
| 500 |
else: |
| 501 |
tmp_lst = sorted( |
| 502 |
zip(map(key, list.__iter__(self)), range(len(self))), reverse=reverse |
| 503 |
) |
| 504 |
list.__init__(self, [list.__getitem__(self, x[1]) for x in tmp_lst]) |
| 505 |
itm = self.ca.items |
| 506 |
self.ca._items = {} |
| 507 |
for idx, x in enumerate(tmp_lst): |
| 508 |
old_index = x[1] |
| 509 |
if old_index in itm: |
| 510 |
self.ca.items[idx] = itm[old_index] |
| 511 |
|
| 512 |
def __repr__(self): |
| 513 |
# type: () -> Any |
| 514 |
return list.__repr__(self) |
| 515 |
|
| 516 |
|
| 517 |
class CommentedKeySeq(tuple, CommentedBase): # type: ignore |
| 518 |
"""This primarily exists to be able to roundtrip keys that are sequences""" |
| 519 |
|
| 520 |
def _yaml_add_comment(self, comment, key=NoComment): |
| 521 |
# type: (Any, Optional[Any]) -> None |
| 522 |
if key is not NoComment: |
| 523 |
self.yaml_key_comment_extend(key, comment) |
| 524 |
else: |
| 525 |
self.ca.comment = comment |
| 526 |
|
| 527 |
def _yaml_add_eol_comment(self, comment, key): |
| 528 |
# type: (Any, Any) -> None |
| 529 |
self._yaml_add_comment(comment, key=key) |
| 530 |
|
| 531 |
def _yaml_get_columnX(self, key): |
| 532 |
# type: (Any) -> Any |
| 533 |
return self.ca.items[key][0].start_mark.column |
| 534 |
|
| 535 |
def _yaml_get_column(self, key): |
| 536 |
# type: (Any) -> Any |
| 537 |
column = None |
| 538 |
sel_idx = None |
| 539 |
pre, post = key - 1, key + 1 |
| 540 |
if pre in self.ca.items: |
| 541 |
sel_idx = pre |
| 542 |
elif post in self.ca.items: |
| 543 |
sel_idx = post |
| 544 |
else: |
| 545 |
# self.ca.items is not ordered |
| 546 |
for row_idx, _k1 in enumerate(self): |
| 547 |
if row_idx >= key: |
| 548 |
break |
| 549 |
if row_idx not in self.ca.items: |
| 550 |
continue |
| 551 |
sel_idx = row_idx |
| 552 |
if sel_idx is not None: |
| 553 |
column = self._yaml_get_columnX(sel_idx) |
| 554 |
return column |
| 555 |
|
| 556 |
def _yaml_get_pre_comment(self): |
| 557 |
# type: () -> Any |
| 558 |
pre_comments = [] # type: List[Any] |
| 559 |
if self.ca.comment is None: |
| 560 |
self.ca.comment = [None, pre_comments] |
| 561 |
else: |
| 562 |
self.ca.comment[1] = pre_comments |
| 563 |
return pre_comments |
| 564 |
|
| 565 |
|
| 566 |
class CommentedMapView(Sized): |
| 567 |
__slots__ = ("_mapping",) |
| 568 |
|
| 569 |
def __init__(self, mapping): |
| 570 |
# type: (Any) -> None |
| 571 |
self._mapping = mapping |
| 572 |
|
| 573 |
def __len__(self): |
| 574 |
# type: () -> int |
| 575 |
count = len(self._mapping) |
| 576 |
return count |
| 577 |
|
| 578 |
|
| 579 |
class CommentedMapKeysView(CommentedMapView, Set): # type: ignore |
| 580 |
__slots__ = () |
| 581 |
|
| 582 |
@classmethod |
| 583 |
def _from_iterable(self, it): |
| 584 |
# type: (Any) -> Any |
| 585 |
return set(it) |
| 586 |
|
| 587 |
def __contains__(self, key): |
| 588 |
# type: (Any) -> Any |
| 589 |
return key in self._mapping |
| 590 |
|
| 591 |
def __iter__(self): |
| 592 |
# type: () -> Any # yield from self._mapping # not in py27, pypy |
| 593 |
# for x in self._mapping._keys(): |
| 594 |
for x in self._mapping: |
| 595 |
yield x |
| 596 |
|
| 597 |
|
| 598 |
class CommentedMapItemsView(CommentedMapView, Set): # type: ignore |
| 599 |
__slots__ = () |
| 600 |
|
| 601 |
@classmethod |
| 602 |
def _from_iterable(self, it): |
| 603 |
# type: (Any) -> Any |
| 604 |
return set(it) |
| 605 |
|
| 606 |
def __contains__(self, item): |
| 607 |
# type: (Any) -> Any |
| 608 |
key, value = item |
| 609 |
try: |
| 610 |
v = self._mapping[key] |
| 611 |
except KeyError: |
| 612 |
return False |
| 613 |
else: |
| 614 |
return v == value |
| 615 |
|
| 616 |
def __iter__(self): |
| 617 |
# type: () -> Any |
| 618 |
for key in self._mapping._keys(): |
| 619 |
yield (key, self._mapping[key]) |
| 620 |
|
| 621 |
|
| 622 |
class CommentedMapValuesView(CommentedMapView): |
| 623 |
__slots__ = () |
| 624 |
|
| 625 |
def __contains__(self, value): |
| 626 |
# type: (Any) -> Any |
| 627 |
for key in self._mapping: |
| 628 |
if value == self._mapping[key]: |
| 629 |
return True |
| 630 |
return False |
| 631 |
|
| 632 |
def __iter__(self): |
| 633 |
# type: () -> Any |
| 634 |
for key in self._mapping._keys(): |
| 635 |
yield self._mapping[key] |
| 636 |
|
| 637 |
|
| 638 |
class CommentedMap(ordereddict, CommentedBase): # type: ignore |
| 639 |
__slots__ = (Comment.attrib, "_ok", "_ref") |
| 640 |
|
| 641 |
def __init__(self, *args, **kw): |
| 642 |
# type: (Any, Any) -> None |
| 643 |
self._ok = set() # type: MutableSet[Any] # own keys |
| 644 |
self._ref = [] # type: List[CommentedMap] |
| 645 |
ordereddict.__init__(self, *args, **kw) |
| 646 |
|
| 647 |
def _yaml_add_comment(self, comment, key=NoComment, value=NoComment): |
| 648 |
# type: (Any, Optional[Any], Optional[Any]) -> None |
| 649 |
"""values is set to key to indicate a value attachment of comment""" |
| 650 |
if key is not NoComment: |
| 651 |
self.yaml_key_comment_extend(key, comment) |
| 652 |
return |
| 653 |
if value is not NoComment: |
| 654 |
self.yaml_value_comment_extend(value, comment) |
| 655 |
else: |
| 656 |
self.ca.comment = comment |
| 657 |
|
| 658 |
def _yaml_add_eol_comment(self, comment, key): |
| 659 |
# type: (Any, Any) -> None |
| 660 |
"""add on the value line, with value specified by the key""" |
| 661 |
self._yaml_add_comment(comment, value=key) |
| 662 |
|
| 663 |
def _yaml_get_columnX(self, key): |
| 664 |
# type: (Any) -> Any |
| 665 |
return self.ca.items[key][2].start_mark.column |
| 666 |
|
| 667 |
def _yaml_get_column(self, key): |
| 668 |
# type: (Any) -> Any |
| 669 |
column = None |
| 670 |
sel_idx = None |
| 671 |
pre, post, last = None, None, None |
| 672 |
for x in self: |
| 673 |
if pre is not None and x != key: |
| 674 |
post = x |
| 675 |
break |
| 676 |
if x == key: |
| 677 |
pre = last |
| 678 |
last = x |
| 679 |
if pre in self.ca.items: |
| 680 |
sel_idx = pre |
| 681 |
elif post in self.ca.items: |
| 682 |
sel_idx = post |
| 683 |
else: |
| 684 |
# self.ca.items is not ordered |
| 685 |
for k1 in self: |
| 686 |
if k1 >= key: |
| 687 |
break |
| 688 |
if k1 not in self.ca.items: |
| 689 |
continue |
| 690 |
sel_idx = k1 |
| 691 |
if sel_idx is not None: |
| 692 |
column = self._yaml_get_columnX(sel_idx) |
| 693 |
return column |
| 694 |
|
| 695 |
def _yaml_get_pre_comment(self): |
| 696 |
# type: () -> Any |
| 697 |
pre_comments = [] # type: List[Any] |
| 698 |
if self.ca.comment is None: |
| 699 |
self.ca.comment = [None, pre_comments] |
| 700 |
else: |
| 701 |
self.ca.comment[1] = pre_comments |
| 702 |
return pre_comments |
| 703 |
|
| 704 |
def update(self, *vals, **kw): |
| 705 |
# type: (Any, Any) -> None |
| 706 |
try: |
| 707 |
ordereddict.update(self, *vals, **kw) |
| 708 |
except TypeError: |
| 709 |
# probably a dict that is used |
| 710 |
for x in vals[0]: |
| 711 |
self[x] = vals[0][x] |
| 712 |
try: |
| 713 |
self._ok.update(vals.keys()) # type: ignore |
| 714 |
except AttributeError: |
| 715 |
# assume one argument that is a list/tuple of two element lists/tuples |
| 716 |
for x in vals[0]: |
| 717 |
self._ok.add(x[0]) |
| 718 |
if kw: |
| 719 |
self._ok.add(*kw.keys()) |
| 720 |
|
| 721 |
def insert(self, pos, key, value, comment=None): |
| 722 |
# type: (Any, Any, Any, Optional[Any]) -> None |
| 723 |
"""insert key value into given position |
| 724 |
attach comment if provided |
| 725 |
""" |
| 726 |
ordereddict.insert(self, pos, key, value) |
| 727 |
self._ok.add(key) |
| 728 |
if comment is not None: |
| 729 |
self.yaml_add_eol_comment(comment, key=key) |
| 730 |
|
| 731 |
def mlget(self, key, default=None, list_ok=False): |
| 732 |
# type: (Any, Any, Any) -> Any |
| 733 |
"""multi-level get that expects dicts within dicts""" |
| 734 |
if not isinstance(key, list): |
| 735 |
return self.get(key, default) |
| 736 |
# assume that the key is a list of recursively accessible dicts |
| 737 |
|
| 738 |
def get_one_level(key_list, level, d): |
| 739 |
# type: (Any, Any, Any) -> Any |
| 740 |
if not list_ok: |
| 741 |
assert isinstance(d, dict) |
| 742 |
if level >= len(key_list): |
| 743 |
if level > len(key_list): |
| 744 |
raise IndexError |
| 745 |
return d[key_list[level - 1]] |
| 746 |
return get_one_level(key_list, level + 1, d[key_list[level - 1]]) |
| 747 |
|
| 748 |
try: |
| 749 |
return get_one_level(key, 1, self) |
| 750 |
except KeyError: |
| 751 |
return default |
| 752 |
except (TypeError, IndexError): |
| 753 |
if not list_ok: |
| 754 |
raise |
| 755 |
return default |
| 756 |
|
| 757 |
def __getitem__(self, key): |
| 758 |
# type: (Any) -> Any |
| 759 |
try: |
| 760 |
return ordereddict.__getitem__(self, key) |
| 761 |
except KeyError: |
| 762 |
for merged in getattr(self, merge_attrib, []): |
| 763 |
if key in merged[1]: |
| 764 |
return merged[1][key] |
| 765 |
raise |
| 766 |
|
| 767 |
def __setitem__(self, key, value): |
| 768 |
# type: (Any, Any) -> None |
| 769 |
# try to preserve the scalarstring type if setting an existing key to a new value |
| 770 |
if key in self: |
| 771 |
if ( |
| 772 |
isinstance(value, string_types) |
| 773 |
and not isinstance(value, ScalarString) |
| 774 |
and isinstance(self[key], ScalarString) |
| 775 |
): |
| 776 |
value = type(self[key])(value) |
| 777 |
ordereddict.__setitem__(self, key, value) |
| 778 |
self._ok.add(key) |
| 779 |
|
| 780 |
def _unmerged_contains(self, key): |
| 781 |
# type: (Any) -> Any |
| 782 |
if key in self._ok: |
| 783 |
return True |
| 784 |
return None |
| 785 |
|
| 786 |
def __contains__(self, key): |
| 787 |
# type: (Any) -> bool |
| 788 |
return bool(ordereddict.__contains__(self, key)) |
| 789 |
|
| 790 |
def get(self, key, default=None): |
| 791 |
# type: (Any, Any) -> Any |
| 792 |
try: |
| 793 |
return self.__getitem__(key) |
| 794 |
except: # NOQA |
| 795 |
return default |
| 796 |
|
| 797 |
def __repr__(self): |
| 798 |
# type: () -> Any |
| 799 |
return ordereddict.__repr__(self).replace("CommentedMap", "ordereddict") |
| 800 |
|
| 801 |
def non_merged_items(self): |
| 802 |
# type: () -> Any |
| 803 |
for x in ordereddict.__iter__(self): |
| 804 |
if x in self._ok: |
| 805 |
yield x, ordereddict.__getitem__(self, x) |
| 806 |
|
| 807 |
def __delitem__(self, key): |
| 808 |
# type: (Any) -> None |
| 809 |
# for merged in getattr(self, merge_attrib, []): |
| 810 |
# if key in merged[1]: |
| 811 |
# value = merged[1][key] |
| 812 |
# break |
| 813 |
# else: |
| 814 |
# # not found in merged in stuff |
| 815 |
# ordereddict.__delitem__(self, key) |
| 816 |
# for referer in self._ref: |
| 817 |
# referer.update_key_value(key) |
| 818 |
# return |
| 819 |
# |
| 820 |
# ordereddict.__setitem__(self, key, value) # merge might have different value |
| 821 |
# self._ok.discard(key) |
| 822 |
self._ok.discard(key) |
| 823 |
ordereddict.__delitem__(self, key) |
| 824 |
for referer in self._ref: |
| 825 |
referer.update_key_value(key) |
| 826 |
|
| 827 |
def __iter__(self): |
| 828 |
# type: () -> Any |
| 829 |
for x in ordereddict.__iter__(self): |
| 830 |
yield x |
| 831 |
|
| 832 |
def _keys(self): |
| 833 |
# type: () -> Any |
| 834 |
for x in ordereddict.__iter__(self): |
| 835 |
yield x |
| 836 |
|
| 837 |
def __len__(self): |
| 838 |
# type: () -> int |
| 839 |
return int(ordereddict.__len__(self)) |
| 840 |
|
| 841 |
def __eq__(self, other): |
| 842 |
# type: (Any) -> bool |
| 843 |
return bool(dict(self) == other) |
| 844 |
|
| 845 |
if PY2: |
| 846 |
|
| 847 |
def keys(self): |
| 848 |
# type: () -> Any |
| 849 |
return list(self._keys()) |
| 850 |
|
| 851 |
def iterkeys(self): |
| 852 |
# type: () -> Any |
| 853 |
return self._keys() |
| 854 |
|
| 855 |
def viewkeys(self): |
| 856 |
# type: () -> Any |
| 857 |
return CommentedMapKeysView(self) |
| 858 |
|
| 859 |
else: |
| 860 |
|
| 861 |
def keys(self): |
| 862 |
# type: () -> Any |
| 863 |
return CommentedMapKeysView(self) |
| 864 |
|
| 865 |
if PY2: |
| 866 |
|
| 867 |
def _values(self): |
| 868 |
# type: () -> Any |
| 869 |
for x in ordereddict.__iter__(self): |
| 870 |
yield ordereddict.__getitem__(self, x) |
| 871 |
|
| 872 |
def values(self): |
| 873 |
# type: () -> Any |
| 874 |
return list(self._values()) |
| 875 |
|
| 876 |
def itervalues(self): |
| 877 |
# type: () -> Any |
| 878 |
return self._values() |
| 879 |
|
| 880 |
def viewvalues(self): |
| 881 |
# type: () -> Any |
| 882 |
return CommentedMapValuesView(self) |
| 883 |
|
| 884 |
else: |
| 885 |
|
| 886 |
def values(self): |
| 887 |
# type: () -> Any |
| 888 |
return CommentedMapValuesView(self) |
| 889 |
|
| 890 |
def _items(self): |
| 891 |
# type: () -> Any |
| 892 |
for x in ordereddict.__iter__(self): |
| 893 |
yield x, ordereddict.__getitem__(self, x) |
| 894 |
|
| 895 |
if PY2: |
| 896 |
|
| 897 |
def items(self): |
| 898 |
# type: () -> Any |
| 899 |
return list(self._items()) |
| 900 |
|
| 901 |
def iteritems(self): |
| 902 |
# type: () -> Any |
| 903 |
return self._items() |
| 904 |
|
| 905 |
def viewitems(self): |
| 906 |
# type: () -> Any |
| 907 |
return CommentedMapItemsView(self) |
| 908 |
|
| 909 |
else: |
| 910 |
|
| 911 |
def items(self): |
| 912 |
# type: () -> Any |
| 913 |
return CommentedMapItemsView(self) |
| 914 |
|
| 915 |
@property |
| 916 |
def merge(self): |
| 917 |
# type: () -> Any |
| 918 |
if not hasattr(self, merge_attrib): |
| 919 |
setattr(self, merge_attrib, []) |
| 920 |
return getattr(self, merge_attrib) |
| 921 |
|
| 922 |
def copy(self): |
| 923 |
# type: () -> Any |
| 924 |
x = type(self)() # update doesn't work |
| 925 |
for k, v in self._items(): |
| 926 |
x[k] = v |
| 927 |
self.copy_attributes(x) |
| 928 |
return x |
| 929 |
|
| 930 |
def add_referent(self, cm): |
| 931 |
# type: (Any) -> None |
| 932 |
if cm not in self._ref: |
| 933 |
self._ref.append(cm) |
| 934 |
|
| 935 |
def add_yaml_merge(self, value): |
| 936 |
# type: (Any) -> None |
| 937 |
for v in value: |
| 938 |
v[1].add_referent(self) |
| 939 |
for k, v in v[1].items(): |
| 940 |
if ordereddict.__contains__(self, k): |
| 941 |
continue |
| 942 |
ordereddict.__setitem__(self, k, v) |
| 943 |
self.merge.extend(value) |
| 944 |
|
| 945 |
def update_key_value(self, key): |
| 946 |
# type: (Any) -> None |
| 947 |
if key in self._ok: |
| 948 |
return |
| 949 |
for v in self.merge: |
| 950 |
if key in v[1]: |
| 951 |
ordereddict.__setitem__(self, key, v[1][key]) |
| 952 |
return |
| 953 |
ordereddict.__delitem__(self, key) |
| 954 |
|
| 955 |
def __deepcopy__(self, memo): |
| 956 |
# type: (Any) -> Any |
| 957 |
res = self.__class__() |
| 958 |
memo[id(self)] = res |
| 959 |
for k in self: |
| 960 |
res[k] = copy.deepcopy(self[k], memo) |
| 961 |
self.copy_attributes(res, memo=memo) |
| 962 |
return res |
| 963 |
|
| 964 |
|
| 965 |
# based on brownie mappings |
| 966 |
@classmethod # type: ignore |
| 967 |
def raise_immutable(cls, *args, **kwargs): |
| 968 |
# type: (Any, *Any, **Any) -> None |
| 969 |
raise TypeError("{} objects are immutable".format(cls.__name__)) |
| 970 |
|
| 971 |
|
| 972 |
class CommentedKeyMap(CommentedBase, Mapping): # type: ignore |
| 973 |
__slots__ = Comment.attrib, "_od" |
| 974 |
"""This primarily exists to be able to roundtrip keys that are mappings""" |
| 975 |
|
| 976 |
def __init__(self, *args, **kw): |
| 977 |
# type: (Any, Any) -> None |
| 978 |
if hasattr(self, "_od"): |
| 979 |
raise_immutable(self) |
| 980 |
try: |
| 981 |
self._od = ordereddict(*args, **kw) |
| 982 |
except TypeError: |
| 983 |
if PY2: |
| 984 |
self._od = ordereddict(args[0].items()) |
| 985 |
else: |
| 986 |
raise |
| 987 |
|
| 988 |
__delitem__ = ( |
| 989 |
__setitem__ |
| 990 |
) = clear = pop = popitem = setdefault = update = raise_immutable |
| 991 |
|
| 992 |
# need to implement __getitem__, __iter__ and __len__ |
| 993 |
def __getitem__(self, index): |
| 994 |
# type: (Any) -> Any |
| 995 |
return self._od[index] |
| 996 |
|
| 997 |
def __iter__(self): |
| 998 |
# type: () -> Iterator[Any] |
| 999 |
for x in self._od.__iter__(): |
| 1000 |
yield x |
| 1001 |
|
| 1002 |
def __len__(self): |
| 1003 |
# type: () -> int |
| 1004 |
return len(self._od) |
| 1005 |
|
| 1006 |
def __hash__(self): |
| 1007 |
# type: () -> Any |
| 1008 |
return hash(tuple(self.items())) |
| 1009 |
|
| 1010 |
def __repr__(self): |
| 1011 |
# type: () -> Any |
| 1012 |
if not hasattr(self, merge_attrib): |
| 1013 |
return self._od.__repr__() |
| 1014 |
return "ordereddict(" + repr(list(self._od.items())) + ")" |
| 1015 |
|
| 1016 |
@classmethod |
| 1017 |
def fromkeys(keys, v=None): |
| 1018 |
# type: (Any, Any) -> Any |
| 1019 |
return CommentedKeyMap(dict.fromkeys(keys, v)) |
| 1020 |
|
| 1021 |
def _yaml_add_comment(self, comment, key=NoComment): |
| 1022 |
# type: (Any, Optional[Any]) -> None |
| 1023 |
if key is not NoComment: |
| 1024 |
self.yaml_key_comment_extend(key, comment) |
| 1025 |
else: |
| 1026 |
self.ca.comment = comment |
| 1027 |
|
| 1028 |
def _yaml_add_eol_comment(self, comment, key): |
| 1029 |
# type: (Any, Any) -> None |
| 1030 |
self._yaml_add_comment(comment, key=key) |
| 1031 |
|
| 1032 |
def _yaml_get_columnX(self, key): |
| 1033 |
# type: (Any) -> Any |
| 1034 |
return self.ca.items[key][0].start_mark.column |
| 1035 |
|
| 1036 |
def _yaml_get_column(self, key): |
| 1037 |
# type: (Any) -> Any |
| 1038 |
column = None |
| 1039 |
sel_idx = None |
| 1040 |
pre, post = key - 1, key + 1 |
| 1041 |
if pre in self.ca.items: |
| 1042 |
sel_idx = pre |
| 1043 |
elif post in self.ca.items: |
| 1044 |
sel_idx = post |
| 1045 |
else: |
| 1046 |
# self.ca.items is not ordered |
| 1047 |
for row_idx, _k1 in enumerate(self): |
| 1048 |
if row_idx >= key: |
| 1049 |
break |
| 1050 |
if row_idx not in self.ca.items: |
| 1051 |
continue |
| 1052 |
sel_idx = row_idx |
| 1053 |
if sel_idx is not None: |
| 1054 |
column = self._yaml_get_columnX(sel_idx) |
| 1055 |
return column |
| 1056 |
|
| 1057 |
def _yaml_get_pre_comment(self): |
| 1058 |
# type: () -> Any |
| 1059 |
pre_comments = [] # type: List[Any] |
| 1060 |
if self.ca.comment is None: |
| 1061 |
self.ca.comment = [None, pre_comments] |
| 1062 |
else: |
| 1063 |
self.ca.comment[1] = pre_comments |
| 1064 |
return pre_comments |
| 1065 |
|
| 1066 |
|
| 1067 |
class CommentedOrderedMap(CommentedMap): |
| 1068 |
__slots__ = (Comment.attrib,) |
| 1069 |
|
| 1070 |
|
| 1071 |
class CommentedSet(MutableSet, CommentedBase): # type: ignore # NOQA |
| 1072 |
__slots__ = Comment.attrib, "odict" |
| 1073 |
|
| 1074 |
def __init__(self, values=None): |
| 1075 |
# type: (Any) -> None |
| 1076 |
self.odict = ordereddict() |
| 1077 |
MutableSet.__init__(self) |
| 1078 |
if values is not None: |
| 1079 |
self |= values # type: ignore |
| 1080 |
|
| 1081 |
def _yaml_add_comment(self, comment, key=NoComment, value=NoComment): |
| 1082 |
# type: (Any, Optional[Any], Optional[Any]) -> None |
| 1083 |
"""values is set to key to indicate a value attachment of comment""" |
| 1084 |
if key is not NoComment: |
| 1085 |
self.yaml_key_comment_extend(key, comment) |
| 1086 |
return |
| 1087 |
if value is not NoComment: |
| 1088 |
self.yaml_value_comment_extend(value, comment) |
| 1089 |
else: |
| 1090 |
self.ca.comment = comment |
| 1091 |
|
| 1092 |
def _yaml_add_eol_comment(self, comment, key): |
| 1093 |
# type: (Any, Any) -> None |
| 1094 |
"""add on the value line, with value specified by the key""" |
| 1095 |
self._yaml_add_comment(comment, value=key) |
| 1096 |
|
| 1097 |
def add(self, value): |
| 1098 |
# type: (Any) -> None |
| 1099 |
"""Add an element.""" |
| 1100 |
self.odict[value] = None |
| 1101 |
|
| 1102 |
def discard(self, value): |
| 1103 |
# type: (Any) -> None |
| 1104 |
"""Remove an element. Do not raise an exception if absent.""" |
| 1105 |
del self.odict[value] |
| 1106 |
|
| 1107 |
def __contains__(self, x): |
| 1108 |
# type: (Any) -> Any |
| 1109 |
return x in self.odict |
| 1110 |
|
| 1111 |
def __iter__(self): |
| 1112 |
# type: () -> Any |
| 1113 |
for x in self.odict: |
| 1114 |
yield x |
| 1115 |
|
| 1116 |
def __len__(self): |
| 1117 |
# type: () -> int |
| 1118 |
return len(self.odict) |
| 1119 |
|
| 1120 |
def __repr__(self): |
| 1121 |
# type: () -> str |
| 1122 |
return "set({0!r})".format(self.odict.keys()) |
| 1123 |
|
| 1124 |
|
| 1125 |
class TaggedScalar(CommentedBase): |
| 1126 |
# the value and style attributes are set during roundtrip construction |
| 1127 |
def __init__(self, value=None, style=None, tag=None): |
| 1128 |
# type: (Any, Any, Any) -> None |
| 1129 |
self.value = value |
| 1130 |
self.style = style |
| 1131 |
if tag is not None: |
| 1132 |
self.yaml_set_tag(tag) |
| 1133 |
|
| 1134 |
def __str__(self): |
| 1135 |
# type: () -> Any |
| 1136 |
return self.value |
| 1137 |
|
| 1138 |
|
| 1139 |
def dump_comments(d, name="", sep=".", out=sys.stdout): |
| 1140 |
# type: (Any, str, str, Any) -> None |
| 1141 |
""" |
| 1142 |
recursively dump comments, all but the toplevel preceded by the path |
| 1143 |
in dotted form x.0.a |
| 1144 |
""" |
| 1145 |
if isinstance(d, dict) and hasattr(d, "ca"): |
| 1146 |
if name: |
| 1147 |
sys.stdout.write("{}\n".format(name)) |
| 1148 |
out.write("{}\n".format(d.ca)) # type: ignore |
| 1149 |
for k in d: |
| 1150 |
dump_comments(d[k], name=(name + sep + k) if name else k, sep=sep, out=out) |
| 1151 |
elif isinstance(d, list) and hasattr(d, "ca"): |
| 1152 |
if name: |
| 1153 |
sys.stdout.write("{}\n".format(name)) |
| 1154 |
out.write("{}\n".format(d.ca)) # type: ignore |
| 1155 |
for idx, k in enumerate(d): |
| 1156 |
dump_comments( |
| 1157 |
k, name=(name + sep + str(idx)) if name else str(idx), sep=sep, out=out |
| 1158 |
) |