diff options
Diffstat (limited to 'comments.py')
-rw-r--r-- | comments.py | 527 |
1 files changed, 202 insertions, 325 deletions
diff --git a/comments.py b/comments.py index 892c868..c6a9703 100644 --- a/comments.py +++ b/comments.py @@ -11,14 +11,13 @@ import copy from ruamel.yaml.compat import ordereddict -from ruamel.yaml.compat import MutableSliceableSequence, _F, nprintf # NOQA +from ruamel.yaml.compat import MutableSliceableSequence, nprintf # NOQA from ruamel.yaml.scalarstring import ScalarString from ruamel.yaml.anchor import Anchor from collections.abc import MutableSet, Sized, Set, Mapping -if False: # MYPY - from typing import Any, Dict, Optional, List, Union, Optional, Iterator # NOQA +from typing import Any, Dict, Optional, List, Union, Optional, Iterator # NOQA # fmt: off __all__ = ['CommentedSeq', 'CommentedKeySeq', @@ -46,18 +45,15 @@ C_BLANK_LINE_PRESERVE_SPACE = 0b100 class IDX: # temporary auto increment, so rearranging is easier - def __init__(self): - # type: () -> None + def __init__(self) -> None: self._idx = 0 - def __call__(self): - # type: () -> Any + def __call__(self) -> Any: x = self._idx self._idx += 1 return x - def __str__(self): - # type: () -> Any + def __str__(self) -> Any: return str(self._idx) @@ -92,27 +88,24 @@ class Comment: __slots__ = 'comment', '_items', '_post', '_pre' attrib = comment_attrib - def __init__(self, old=True): - # type: (bool) -> None + def __init__(self, old: bool = True) -> None: self._pre = None if old else [] # type: ignore self.comment = None # [post, [pre]] # map key (mapping/omap/dict) or index (sequence/list) to a list of # dict: post_key, pre_key, post_value, pre_value # list: pre item, post item - self._items = {} # type: Dict[Any, Any] + self._items: Dict[Any, Any] = {} # self._start = [] # should not put these on first item - self._post = [] # type: List[Any] # end of document comments + self._post: List[Any] = [] # end of document comments - def __str__(self): - # type: () -> str + def __str__(self) -> str: if bool(self._post): end = ',\n end=' + str(self._post) else: end = "" - return 'Comment(comment={0},\n items={1}{2})'.format(self.comment, self._items, end) + return f'Comment(comment={self.comment},\n items={self._items}{end})' - def _old__repr__(self): - # type: () -> str + def _old__repr__(self) -> str: if bool(self._post): end = ',\n end=' + str(self._post) else: @@ -121,15 +114,12 @@ class Comment: ln = max([len(str(k)) for k in self._items]) + 1 except ValueError: ln = '' # type: ignore - it = ' '.join( - ['{:{}} {}\n'.format(str(k) + ':', ln, v) for k, v in self._items.items()] - ) + it = ' '.join([f'{str(k) + ":":{ln}} {v}\n' for k, v in self._items.items()]) if it: it = '\n ' + it + ' ' - return 'Comment(\n start={},\n items={{{}}}{})'.format(self.comment, it, end) + return f'Comment(\n start={self.comment},\n items={{{it}}}{end})' - def __repr__(self): - # type: () -> str + def __repr__(self) -> str: if self._pre is None: return self._old__repr__() if bool(self._post): @@ -140,47 +130,38 @@ class Comment: ln = max([len(str(k)) for k in self._items]) + 1 except ValueError: ln = '' # type: ignore - it = ' '.join( - ['{:{}} {}\n'.format(str(k) + ':', ln, v) for k, v in self._items.items()] - ) + it = ' '.join([f'{str(k) + ":":{ln}} {v}\n' for k, v in self._items.items()]) if it: it = '\n ' + it + ' ' - return 'Comment(\n pre={},\n items={{{}}}{})'.format(self.pre, it, end) + return f'Comment(\n pre={self.pre},\n items={{{it}}}{end})' @property - def items(self): - # type: () -> Any + def items(self) -> Any: return self._items @property - def end(self): - # type: () -> Any + def end(self) -> Any: return self._post @end.setter - def end(self, value): - # type: (Any) -> None + def end(self, value: Any) -> None: self._post = value @property - def pre(self): - # type: () -> Any + def pre(self) -> Any: return self._pre @pre.setter - def pre(self, value): - # type: (Any) -> None + def pre(self, value: Any) -> None: self._pre = value - def get(self, item, pos): - # type: (Any, Any) -> Any + def get(self, item: Any, pos: Any) -> Any: x = self._items.get(item) if x is None or len(x) < pos: return None return x[pos] # can be None - def set(self, item, pos, value): - # type: (Any, Any, Any) -> Any + def set(self, item: Any, pos: Any, value: Any) -> Any: x = self._items.get(item) if x is None: self._items[item] = x = [None] * (pos + 1) @@ -190,8 +171,7 @@ class Comment: assert x[pos] is None x[pos] = value - def __contains__(self, x): - # type: (Any) -> Any + def __contains__(self, x: Any) -> Any: # test if a substring is in any of the attached comments if self.comment: if self.comment[0] and x in self.comment[0].value: @@ -214,8 +194,7 @@ class Comment: # to distinguish key from None -def NoComment(): - # type: () -> None +def NoComment() -> None: pass @@ -223,20 +202,16 @@ class Format: __slots__ = ('_flow_style',) attrib = format_attrib - def __init__(self): - # type: () -> None - self._flow_style = None # type: Any + def __init__(self) -> None: + self._flow_style: Any = None - def set_flow_style(self): - # type: () -> None + def set_flow_style(self) -> None: self._flow_style = True - def set_block_style(self): - # type: () -> None + def set_block_style(self) -> None: self._flow_style = False - def flow_style(self, default=None): - # type: (Optional[Any]) -> Any + def flow_style(self, default: Optional[Any] = None) -> Any: """if default (the flow_style) is None, the flow style tacked on to the object explicitly will be taken. If that is None as well the default flow style rules the format down the line, or the type @@ -253,48 +228,40 @@ class LineCol: attrib = line_col_attrib - def __init__(self): - # type: () -> None + def __init__(self) -> None: self.line = None self.col = None - self.data = None # type: Optional[Dict[Any, Any]] + self.data: Optional[Dict[Any, Any]] = None - def add_kv_line_col(self, key, data): - # type: (Any, Any) -> None + def add_kv_line_col(self, key: Any, data: Any) -> None: if self.data is None: self.data = {} self.data[key] = data - def key(self, k): - # type: (Any) -> Any + def key(self, k: Any) -> Any: return self._kv(k, 0, 1) - def value(self, k): - # type: (Any) -> Any + def value(self, k: Any) -> Any: return self._kv(k, 2, 3) - def _kv(self, k, x0, x1): - # type: (Any, Any, Any) -> Any + def _kv(self, k: Any, x0: Any, x1: Any) -> Any: if self.data is None: return None data = self.data[k] return data[x0], data[x1] - def item(self, idx): - # type: (Any) -> Any + def item(self, idx: Any) -> Any: if self.data is None: return None return self.data[idx][0], self.data[idx][1] - def add_idx_line_col(self, key, data): - # type: (Any, Any) -> None + def add_idx_line_col(self, key: Any, data: Any) -> None: if self.data is None: self.data = {} self.data[key] = data - def __repr__(self): - # type: () -> str - return _F('LineCol({line}, {col})', line=self.line, col=self.col) # type: ignore + def __repr__(self) -> str: + return f'LineCol({self.line}, {self.col})' class Tag: @@ -303,13 +270,11 @@ class Tag: __slots__ = ('value',) attrib = tag_attrib - def __init__(self): - # type: () -> None + def __init__(self) -> None: self.value = None - def __repr__(self): - # type: () -> Any - return '{0.__class__.__name__}({0.value!r})'.format(self) + def __repr__(self) -> Any: + return f'{self.__class__.__name__}({self.value!r})' class CommentedBase: @@ -320,16 +285,14 @@ class CommentedBase: setattr(self, Comment.attrib, Comment()) return getattr(self, Comment.attrib) - def yaml_end_comment_extend(self, comment, clear=False): - # type: (Any, bool) -> None + def yaml_end_comment_extend(self, comment: Any, clear: bool = False) -> None: if comment is None: return if clear or self.ca.end is None: self.ca.end = [] self.ca.end.extend(comment) - def yaml_key_comment_extend(self, key, comment, clear=False): - # type: (Any, Any, bool) -> None + def yaml_key_comment_extend(self, key: Any, comment: Any, clear: bool = False) -> None: r = self.ca._items.setdefault(key, [None, None, None, None]) if clear or r[1] is None: if comment[1] is not None: @@ -339,8 +302,7 @@ class CommentedBase: r[1].extend(comment[0]) r[0] = comment[0] - def yaml_value_comment_extend(self, key, comment, clear=False): - # type: (Any, Any, bool) -> None + def yaml_value_comment_extend(self, key: Any, comment: Any, clear: bool = False) -> None: r = self.ca._items.setdefault(key, [None, None, None, None]) if clear or r[3] is None: if comment[1] is not None: @@ -350,8 +312,7 @@ class CommentedBase: r[3].extend(comment[0]) r[2] = comment[0] - def yaml_set_start_comment(self, comment, indent=0): - # type: (Any, Any) -> None + def yaml_set_start_comment(self, comment: Any, indent: Any = 0) -> None: """overwrites any preceding comment lines on an object expects comment to be without `#` and possible have multiple lines """ @@ -369,17 +330,20 @@ class CommentedBase: pre_comments.append(CommentToken(com + '\n', start_mark)) def yaml_set_comment_before_after_key( - self, key, before=None, indent=0, after=None, after_indent=None - ): - # type: (Any, Any, Any, Any, Any) -> None + self, + key: Any, + before: Any = None, + indent: Any = 0, + after: Any = None, + after_indent: Any = None, + ) -> None: """ expects comment (before/after) to be without `#` and possible have multiple lines """ from ruamel.yaml.error import CommentMark from ruamel.yaml.tokens import CommentToken - def comment_token(s, mark): - # type: (Any, Any) -> Any + def comment_token(s: Any, mark: Any) -> Any: # handle empty lines as having no comment return CommentToken(('# ' if s else "") + s + '\n', mark) @@ -407,8 +371,7 @@ class CommentedBase: c[3].append(comment_token(com, start_mark)) # type: ignore @property - def fa(self): - # type: () -> Any + def fa(self) -> Any: """format attribute set_flow_style()/set_block_style()""" @@ -416,8 +379,9 @@ class CommentedBase: setattr(self, Format.attrib, Format()) return getattr(self, Format.attrib) - def yaml_add_eol_comment(self, comment, key=NoComment, column=None): - # type: (Any, Optional[Any], Optional[Any]) -> None + def yaml_add_eol_comment( + self, comment: Any, key: Optional[Any] = NoComment, column: Optional[Any] = None + ) -> None: """ there is a problem as eol comments should start with ' #' (but at the beginning of the line the space doesn't have to be before @@ -442,56 +406,46 @@ class CommentedBase: self._yaml_add_eol_comment(ct, key=key) @property - def lc(self): - # type: () -> Any + def lc(self) -> Any: if not hasattr(self, LineCol.attrib): setattr(self, LineCol.attrib, LineCol()) return getattr(self, LineCol.attrib) - def _yaml_set_line_col(self, line, col): - # type: (Any, Any) -> None + def _yaml_set_line_col(self, line: Any, col: Any) -> None: self.lc.line = line self.lc.col = col - def _yaml_set_kv_line_col(self, key, data): - # type: (Any, Any) -> None + def _yaml_set_kv_line_col(self, key: Any, data: Any) -> None: self.lc.add_kv_line_col(key, data) - def _yaml_set_idx_line_col(self, key, data): - # type: (Any, Any) -> None + def _yaml_set_idx_line_col(self, key: Any, data: Any) -> None: self.lc.add_idx_line_col(key, data) @property - def anchor(self): - # type: () -> Any + def anchor(self) -> Any: if not hasattr(self, Anchor.attrib): setattr(self, Anchor.attrib, Anchor()) return getattr(self, Anchor.attrib) - def yaml_anchor(self): - # type: () -> Any + def yaml_anchor(self) -> Any: if not hasattr(self, Anchor.attrib): return None return self.anchor - def yaml_set_anchor(self, value, always_dump=False): - # type: (Any, bool) -> None + def yaml_set_anchor(self, value: Any, always_dump: bool = False) -> None: self.anchor.value = value self.anchor.always_dump = always_dump @property - def tag(self): - # type: () -> Any + def tag(self) -> Any: if not hasattr(self, Tag.attrib): setattr(self, Tag.attrib, Tag()) return getattr(self, Tag.attrib) - def yaml_set_tag(self, value): - # type: (Any) -> None + def yaml_set_tag(self, value: Any) -> None: self.tag.value = value - def copy_attributes(self, t, memo=None): - # type: (Any, Any) -> None + def copy_attributes(self, t: Any, memo: Any = None) -> None: # fmt: off for a in [Comment.attrib, Format.attrib, LineCol.attrib, Anchor.attrib, Tag.attrib, merge_attrib]: @@ -502,32 +456,26 @@ class CommentedBase: setattr(t, a, getattr(self, a)) # fmt: on - def _yaml_add_eol_comment(self, comment, key): - # type: (Any, Any) -> None + def _yaml_add_eol_comment(self, comment: Any, key: Any) -> None: raise NotImplementedError - def _yaml_get_pre_comment(self): - # type: () -> Any + def _yaml_get_pre_comment(self) -> Any: raise NotImplementedError - def _yaml_get_column(self, key): - # type: (Any) -> Any + def _yaml_get_column(self, key: Any) -> Any: raise NotImplementedError class CommentedSeq(MutableSliceableSequence, list, CommentedBase): # type: ignore __slots__ = (Comment.attrib, '_lst') - def __init__(self, *args, **kw): - # type: (Any, Any) -> None + def __init__(self, *args: Any, **kw: Any) -> None: list.__init__(self, *args, **kw) - def __getsingleitem__(self, idx): - # type: (Any) -> Any + def __getsingleitem__(self, idx: Any) -> Any: return list.__getitem__(self, idx) - def __setsingleitem__(self, idx, value): - # type: (Any, Any) -> None + def __setsingleitem__(self, idx: Any, value: Any) -> None: # try to preserve the scalarstring type if setting an existing key to a new value if idx < len(self): if ( @@ -538,8 +486,7 @@ class CommentedSeq(MutableSliceableSequence, list, CommentedBase): # type: igno value = type(self[idx])(value) list.__setitem__(self, idx, value) - def __delsingleitem__(self, idx=None): - # type: (Any) -> Any + def __delsingleitem__(self, idx: Any = None) -> Any: list.__delitem__(self, idx) self.ca.items.pop(idx, None) # might not be there -> default value for list_index in sorted(self.ca.items): @@ -547,12 +494,10 @@ class CommentedSeq(MutableSliceableSequence, list, CommentedBase): # type: igno continue self.ca.items[list_index - 1] = self.ca.items.pop(list_index) - def __len__(self): - # type: () -> int + def __len__(self) -> int: return list.__len__(self) - def insert(self, idx, val): - # type: (Any, Any) -> None + def insert(self, idx: Any, val: Any) -> None: """the comments after the insertion have to move forward""" list.insert(self, idx, val) for list_index in sorted(self.ca.items, reverse=True): @@ -560,31 +505,25 @@ class CommentedSeq(MutableSliceableSequence, list, CommentedBase): # type: igno break self.ca.items[list_index + 1] = self.ca.items.pop(list_index) - def extend(self, val): - # type: (Any) -> None + def extend(self, val: Any) -> None: list.extend(self, val) - def __eq__(self, other): - # type: (Any) -> bool + def __eq__(self, other: Any) -> bool: return list.__eq__(self, other) - def _yaml_add_comment(self, comment, key=NoComment): - # type: (Any, Optional[Any]) -> None + def _yaml_add_comment(self, comment: Any, key: Optional[Any] = NoComment) -> None: if key is not NoComment: self.yaml_key_comment_extend(key, comment) else: self.ca.comment = comment - def _yaml_add_eol_comment(self, comment, key): - # type: (Any, Any) -> None + def _yaml_add_eol_comment(self, comment: Any, key: Any) -> None: self._yaml_add_comment(comment, key=key) - def _yaml_get_columnX(self, key): - # type: (Any) -> Any + def _yaml_get_columnX(self, key: Any) -> Any: return self.ca.items[key][0].start_mark.column - def _yaml_get_column(self, key): - # type: (Any) -> Any + def _yaml_get_column(self, key: Any) -> Any: column = None sel_idx = None pre, post = key - 1, key + 1 @@ -604,26 +543,23 @@ class CommentedSeq(MutableSliceableSequence, list, CommentedBase): # type: igno column = self._yaml_get_columnX(sel_idx) return column - def _yaml_get_pre_comment(self): - # type: () -> Any - pre_comments = [] # type: List[Any] + def _yaml_get_pre_comment(self) -> Any: + pre_comments: List[Any] = [] if self.ca.comment is None: self.ca.comment = [None, pre_comments] else: pre_comments = self.ca.comment[1] return pre_comments - def _yaml_clear_pre_comment(self): - # type: () -> Any - pre_comments = [] # type: List[Any] + def _yaml_clear_pre_comment(self) -> Any: + pre_comments: List[Any] = [] if self.ca.comment is None: self.ca.comment = [None, pre_comments] else: self.ca.comment[1] = pre_comments return pre_comments - def __deepcopy__(self, memo): - # type: (Any) -> Any + def __deepcopy__(self, memo: Any) -> Any: res = self.__class__() memo[id(self)] = res for k in self: @@ -631,12 +567,10 @@ class CommentedSeq(MutableSliceableSequence, list, CommentedBase): # type: igno self.copy_attributes(res, memo=memo) return res - def __add__(self, other): - # type: (Any) -> Any + def __add__(self, other: Any) -> Any: return list.__add__(self, other) - def sort(self, key=None, reverse=False): - # type: (Any, bool) -> None + def sort(self, key: Any = None, reverse: bool = False) -> None: if key is None: tmp_lst = sorted(zip(self, range(len(self))), reverse=reverse) list.__init__(self, [x[0] for x in tmp_lst]) @@ -652,31 +586,26 @@ class CommentedSeq(MutableSliceableSequence, list, CommentedBase): # type: igno if old_index in itm: self.ca.items[idx] = itm[old_index] - def __repr__(self): - # type: () -> Any + def __repr__(self) -> Any: return list.__repr__(self) class CommentedKeySeq(tuple, CommentedBase): # type: ignore """This primarily exists to be able to roundtrip keys that are sequences""" - def _yaml_add_comment(self, comment, key=NoComment): - # type: (Any, Optional[Any]) -> None + def _yaml_add_comment(self, comment: Any, key: Optional[Any] = NoComment) -> None: if key is not NoComment: self.yaml_key_comment_extend(key, comment) else: self.ca.comment = comment - def _yaml_add_eol_comment(self, comment, key): - # type: (Any, Any) -> None + def _yaml_add_eol_comment(self, comment: Any, key: Any) -> None: self._yaml_add_comment(comment, key=key) - def _yaml_get_columnX(self, key): - # type: (Any) -> Any + def _yaml_get_columnX(self, key: Any) -> Any: return self.ca.items[key][0].start_mark.column - def _yaml_get_column(self, key): - # type: (Any) -> Any + def _yaml_get_column(self, key: Any) -> Any: column = None sel_idx = None pre, post = key - 1, key + 1 @@ -696,18 +625,16 @@ class CommentedKeySeq(tuple, CommentedBase): # type: ignore column = self._yaml_get_columnX(sel_idx) return column - def _yaml_get_pre_comment(self): - # type: () -> Any - pre_comments = [] # type: List[Any] + def _yaml_get_pre_comment(self) -> Any: + pre_comments: List[Any] = [] if self.ca.comment is None: self.ca.comment = [None, pre_comments] else: pre_comments = self.ca.comment[1] return pre_comments - def _yaml_clear_pre_comment(self): - # type: () -> Any - pre_comments = [] # type: List[Any] + def _yaml_clear_pre_comment(self) -> Any: + pre_comments: List[Any] = [] if self.ca.comment is None: self.ca.comment = [None, pre_comments] else: @@ -718,12 +645,10 @@ class CommentedKeySeq(tuple, CommentedBase): # type: ignore class CommentedMapView(Sized): __slots__ = ('_mapping',) - def __init__(self, mapping): - # type: (Any) -> None + def __init__(self, mapping: Any) -> None: self._mapping = mapping - def __len__(self): - # type: () -> int + def __len__(self) -> int: count = len(self._mapping) return count @@ -732,16 +657,14 @@ class CommentedMapKeysView(CommentedMapView, Set): # type: ignore __slots__ = () @classmethod - def _from_iterable(self, it): - # type: (Any) -> Any + def _from_iterable(self, it: Any) -> Any: return set(it) - def __contains__(self, key): - # type: (Any) -> Any + def __contains__(self, key: Any) -> Any: return key in self._mapping - def __iter__(self): - # type: () -> Any # yield from self._mapping # not in py27, pypy + def __iter__(self) -> Any: + # yield from self._mapping # not in py27, pypy # for x in self._mapping._keys(): for x in self._mapping: yield x @@ -751,12 +674,10 @@ class CommentedMapItemsView(CommentedMapView, Set): # type: ignore __slots__ = () @classmethod - def _from_iterable(self, it): - # type: (Any) -> Any + def _from_iterable(self, it: Any) -> Any: return set(it) - def __contains__(self, item): - # type: (Any) -> Any + def __contains__(self, item: Any) -> Any: key, value = item try: v = self._mapping[key] @@ -765,8 +686,7 @@ class CommentedMapItemsView(CommentedMapView, Set): # type: ignore else: return v == value - def __iter__(self): - # type: () -> Any + def __iter__(self) -> Any: for key in self._mapping._keys(): yield (key, self._mapping[key]) @@ -774,15 +694,13 @@ class CommentedMapItemsView(CommentedMapView, Set): # type: ignore class CommentedMapValuesView(CommentedMapView): __slots__ = () - def __contains__(self, value): - # type: (Any) -> Any + def __contains__(self, value: Any) -> Any: for key in self._mapping: if value == self._mapping[key]: return True return False - def __iter__(self): - # type: () -> Any + def __iter__(self) -> Any: for key in self._mapping._keys(): yield self._mapping[key] @@ -790,14 +708,14 @@ class CommentedMapValuesView(CommentedMapView): class CommentedMap(ordereddict, CommentedBase): __slots__ = (Comment.attrib, '_ok', '_ref') - def __init__(self, *args, **kw): - # type: (Any, Any) -> None - self._ok = set() # type: MutableSet[Any] # own keys - self._ref = [] # type: List[CommentedMap] + def __init__(self, *args: Any, **kw: Any) -> None: + self._ok: MutableSet[Any] = set() # own keys + self._ref: List[CommentedMap] = [] ordereddict.__init__(self, *args, **kw) - def _yaml_add_comment(self, comment, key=NoComment, value=NoComment): - # type: (Any, Optional[Any], Optional[Any]) -> None + def _yaml_add_comment( + self, comment: Any, key: Optional[Any] = NoComment, value: Optional[Any] = NoComment + ) -> None: """values is set to key to indicate a value attachment of comment""" if key is not NoComment: self.yaml_key_comment_extend(key, comment) @@ -807,17 +725,14 @@ class CommentedMap(ordereddict, CommentedBase): else: self.ca.comment = comment - def _yaml_add_eol_comment(self, comment, key): - # type: (Any, Any) -> None + def _yaml_add_eol_comment(self, comment: Any, key: Any) -> None: """add on the value line, with value specified by the key""" self._yaml_add_comment(comment, value=key) - def _yaml_get_columnX(self, key): - # type: (Any) -> Any + def _yaml_get_columnX(self, key: Any) -> Any: return self.ca.items[key][2].start_mark.column - def _yaml_get_column(self, key): - # type: (Any) -> Any + def _yaml_get_column(self, key: Any) -> Any: column = None sel_idx = None pre, post, last = None, None, None @@ -844,26 +759,23 @@ class CommentedMap(ordereddict, CommentedBase): column = self._yaml_get_columnX(sel_idx) return column - def _yaml_get_pre_comment(self): - # type: () -> Any - pre_comments = [] # type: List[Any] + def _yaml_get_pre_comment(self) -> Any: + pre_comments: List[Any] = [] if self.ca.comment is None: self.ca.comment = [None, pre_comments] else: pre_comments = self.ca.comment[1] return pre_comments - def _yaml_clear_pre_comment(self): - # type: () -> Any - pre_comments = [] # type: List[Any] + def _yaml_clear_pre_comment(self) -> Any: + pre_comments: List[Any] = [] if self.ca.comment is None: self.ca.comment = [None, pre_comments] else: self.ca.comment[1] = pre_comments return pre_comments - def update(self, *vals, **kw): - # type: (Any, Any) -> None + def update(self, *vals: Any, **kw: Any) -> None: try: ordereddict.update(self, *vals, **kw) except TypeError: @@ -880,8 +792,7 @@ class CommentedMap(ordereddict, CommentedBase): if kw: self._ok.add(*kw.keys()) - def insert(self, pos, key, value, comment=None): - # type: (Any, Any, Any, Optional[Any]) -> None + def insert(self, pos: Any, key: Any, value: Any, comment: Optional[Any] = None) -> None: """insert key value into given position attach comment if provided """ @@ -895,15 +806,13 @@ class CommentedMap(ordereddict, CommentedBase): if comment is not None: self.yaml_add_eol_comment(comment, key=key) - def mlget(self, key, default=None, list_ok=False): - # type: (Any, Any, Any) -> Any + def mlget(self, key: Any, default: Any = None, list_ok: Any = False) -> Any: """multi-level get that expects dicts within dicts""" if not isinstance(key, list): return self.get(key, default) # assume that the key is a list of recursively accessible dicts - def get_one_level(key_list, level, d): - # type: (Any, Any, Any) -> Any + def get_one_level(key_list: Any, level: Any, d: Any) -> Any: if not list_ok: assert isinstance(d, dict) if level >= len(key_list): @@ -921,8 +830,7 @@ class CommentedMap(ordereddict, CommentedBase): raise return default - def __getitem__(self, key): - # type: (Any) -> Any + def __getitem__(self, key: Any) -> Any: try: return ordereddict.__getitem__(self, key) except KeyError: @@ -931,8 +839,7 @@ class CommentedMap(ordereddict, CommentedBase): return merged[1][key] raise - def __setitem__(self, key, value): - # type: (Any, Any) -> None + def __setitem__(self, key: Any, value: Any) -> None: # try to preserve the scalarstring type if setting an existing key to a new value if key in self: if ( @@ -944,35 +851,36 @@ class CommentedMap(ordereddict, CommentedBase): ordereddict.__setitem__(self, key, value) self._ok.add(key) - def _unmerged_contains(self, key): - # type: (Any) -> Any + def _unmerged_contains(self, key: Any) -> Any: if key in self._ok: return True return None - def __contains__(self, key): - # type: (Any) -> bool + def __contains__(self, key: Any) -> bool: return bool(ordereddict.__contains__(self, key)) - def get(self, key, default=None): - # type: (Any, Any) -> Any + def get(self, key: Any, default: Any = None) -> Any: try: return self.__getitem__(key) except: # NOQA return default - def __repr__(self): - # type: () -> Any - return ordereddict.__repr__(self).replace('CommentedMap', 'ordereddict') + def __repr__(self) -> Any: + res = "ordereddict([" + sep = '' + for k, v in self.items(): + res += f'{sep}({k!r}, {v!r})' + if not sep: + sep = ', ' + res += '])' + return res - def non_merged_items(self): - # type: () -> Any + def non_merged_items(self) -> Any: for x in ordereddict.__iter__(self): if x in self._ok: yield x, ordereddict.__getitem__(self, x) - def __delitem__(self, key): - # type: (Any) -> None + def __delitem__(self, key: Any) -> None: # for merged in getattr(self, merge_attrib, []): # if key in merged[1]: # value = merged[1][key] @@ -991,73 +899,60 @@ class CommentedMap(ordereddict, CommentedBase): for referer in self._ref: referer.update_key_value(key) - def __iter__(self): - # type: () -> Any + def __iter__(self) -> Any: for x in ordereddict.__iter__(self): yield x - def _keys(self): - # type: () -> Any + def _keys(self) -> Any: for x in ordereddict.__iter__(self): yield x - def __len__(self): - # type: () -> int + def __len__(self) -> int: return int(ordereddict.__len__(self)) - def __eq__(self, other): - # type: (Any) -> bool + def __eq__(self, other: Any) -> bool: return bool(dict(self) == other) - def keys(self): - # type: () -> Any + def keys(self) -> Any: return CommentedMapKeysView(self) - def values(self): - # type: () -> Any + def values(self) -> Any: return CommentedMapValuesView(self) - def _items(self): - # type: () -> Any + def _items(self) -> Any: for x in ordereddict.__iter__(self): yield x, ordereddict.__getitem__(self, x) - def items(self): - # type: () -> Any + def items(self) -> Any: return CommentedMapItemsView(self) @property - def merge(self): - # type: () -> Any + def merge(self) -> Any: if not hasattr(self, merge_attrib): setattr(self, merge_attrib, []) return getattr(self, merge_attrib) - def copy(self): - # type: () -> Any + def copy(self) -> Any: x = type(self)() # update doesn't work for k, v in self._items(): x[k] = v self.copy_attributes(x) return x - def add_referent(self, cm): - # type: (Any) -> None + def add_referent(self, cm: Any) -> None: if cm not in self._ref: self._ref.append(cm) - def add_yaml_merge(self, value): - # type: (Any) -> None + def add_yaml_merge(self, value: Any) -> None: for v in value: v[1].add_referent(self) - for k, v in v[1].items(): - if ordereddict.__contains__(self, k): + for k1, v1 in v[1].items(): + if ordereddict.__contains__(self, k1): continue - ordereddict.__setitem__(self, k, v) + ordereddict.__setitem__(self, k1, v1) self.merge.extend(value) - def update_key_value(self, key): - # type: (Any) -> None + def update_key_value(self, key: Any) -> None: if key in self._ok: return for v in self.merge: @@ -1066,8 +961,7 @@ class CommentedMap(ordereddict, CommentedBase): return ordereddict.__delitem__(self, key) - def __deepcopy__(self, memo): - # type: (Any) -> Any + def __deepcopy__(self, memo: Any) -> Any: res = self.__class__() memo[id(self)] = res for k in self: @@ -1078,17 +972,15 @@ class CommentedMap(ordereddict, CommentedBase): # based on brownie mappings @classmethod # type: ignore -def raise_immutable(cls, *args, **kwargs): - # type: (Any, *Any, **Any) -> None - raise TypeError('{} objects are immutable'.format(cls.__name__)) +def raise_immutable(cls: Any, *args: Any, **kwargs: Any) -> None: + raise TypeError(f'{cls.__name__} objects are immutable') class CommentedKeyMap(CommentedBase, Mapping): # type: ignore __slots__ = Comment.attrib, '_od' """This primarily exists to be able to roundtrip keys that are mappings""" - def __init__(self, *args, **kw): - # type: (Any, Any) -> None + def __init__(self, *args: Any, **kw: Any) -> None: if hasattr(self, '_od'): raise_immutable(self) try: @@ -1099,51 +991,41 @@ class CommentedKeyMap(CommentedBase, Mapping): # type: ignore __delitem__ = __setitem__ = clear = pop = popitem = setdefault = update = raise_immutable # need to implement __getitem__, __iter__ and __len__ - def __getitem__(self, index): - # type: (Any) -> Any + def __getitem__(self, index: Any) -> Any: return self._od[index] - def __iter__(self): - # type: () -> Iterator[Any] + def __iter__(self) -> Iterator[Any]: for x in self._od.__iter__(): yield x - def __len__(self): - # type: () -> int + def __len__(self) -> int: return len(self._od) - def __hash__(self): - # type: () -> Any + def __hash__(self) -> Any: return hash(tuple(self.items())) - def __repr__(self): - # type: () -> Any + def __repr__(self) -> Any: if not hasattr(self, merge_attrib): return self._od.__repr__() return 'ordereddict(' + repr(list(self._od.items())) + ')' @classmethod - def fromkeys(keys, v=None): - # type: (Any, Any) -> Any + def fromkeys(keys: Any, v: Any = None) -> Any: return CommentedKeyMap(dict.fromkeys(keys, v)) - def _yaml_add_comment(self, comment, key=NoComment): - # type: (Any, Optional[Any]) -> None + def _yaml_add_comment(self, comment: Any, key: Optional[Any] = NoComment) -> None: if key is not NoComment: self.yaml_key_comment_extend(key, comment) else: self.ca.comment = comment - def _yaml_add_eol_comment(self, comment, key): - # type: (Any, Any) -> None + def _yaml_add_eol_comment(self, comment: Any, key: Any) -> None: self._yaml_add_comment(comment, key=key) - def _yaml_get_columnX(self, key): - # type: (Any) -> Any + def _yaml_get_columnX(self, key: Any) -> Any: return self.ca.items[key][0].start_mark.column - def _yaml_get_column(self, key): - # type: (Any) -> Any + def _yaml_get_column(self, key: Any) -> Any: column = None sel_idx = None pre, post = key - 1, key + 1 @@ -1163,9 +1045,8 @@ class CommentedKeyMap(CommentedBase, Mapping): # type: ignore column = self._yaml_get_columnX(sel_idx) return column - def _yaml_get_pre_comment(self): - # type: () -> Any - pre_comments = [] # type: List[Any] + def _yaml_get_pre_comment(self) -> Any: + pre_comments: List[Any] = [] if self.ca.comment is None: self.ca.comment = [None, pre_comments] else: @@ -1180,15 +1061,15 @@ class CommentedOrderedMap(CommentedMap): class CommentedSet(MutableSet, CommentedBase): # type: ignore # NOQA __slots__ = Comment.attrib, 'odict' - def __init__(self, values=None): - # type: (Any) -> None + def __init__(self, values: Any = None) -> None: self.odict = ordereddict() MutableSet.__init__(self) if values is not None: - self |= values # type: ignore + self |= values - def _yaml_add_comment(self, comment, key=NoComment, value=NoComment): - # type: (Any, Optional[Any], Optional[Any]) -> None + def _yaml_add_comment( + self, comment: Any, key: Optional[Any] = NoComment, value: Optional[Any] = NoComment + ) -> None: """values is set to key to indicate a value attachment of comment""" if key is not NoComment: self.yaml_key_comment_extend(key, comment) @@ -1198,69 +1079,65 @@ class CommentedSet(MutableSet, CommentedBase): # type: ignore # NOQA else: self.ca.comment = comment - def _yaml_add_eol_comment(self, comment, key): - # type: (Any, Any) -> None + def _yaml_add_eol_comment(self, comment: Any, key: Any) -> None: """add on the value line, with value specified by the key""" self._yaml_add_comment(comment, value=key) - def add(self, value): - # type: (Any) -> None + def add(self, value: Any) -> None: """Add an element.""" self.odict[value] = None - def discard(self, value): - # type: (Any) -> None + def discard(self, value: Any) -> None: """Remove an element. Do not raise an exception if absent.""" del self.odict[value] - def __contains__(self, x): - # type: (Any) -> Any + def __contains__(self, x: Any) -> Any: return x in self.odict - def __iter__(self): - # type: () -> Any + def __iter__(self) -> Any: for x in self.odict: yield x - def __len__(self): - # type: () -> int + def __len__(self) -> int: return len(self.odict) - def __repr__(self): - # type: () -> str - return 'set({0!r})'.format(self.odict.keys()) + def __repr__(self) -> str: + return f'set({self.odict.keys()!r})' class TaggedScalar(CommentedBase): # the value and style attributes are set during roundtrip construction - def __init__(self, value=None, style=None, tag=None): - # type: (Any, Any, Any) -> None + def __init__(self, value: Any = None, style: Any = None, tag: Any = None) -> None: self.value = value self.style = style if tag is not None: self.yaml_set_tag(tag) - def __str__(self): - # type: () -> Any + def __str__(self) -> Any: return self.value + def count(self, s: str, start: Optional[int] = None, end: Optional[int] = None) -> Any: + return self.value.count(s, start, end) + + def __getitem__(self, pos: int) -> Any: + return self.value[pos] + -def dump_comments(d, name="", sep='.', out=sys.stdout): - # type: (Any, str, str, Any) -> None +def dump_comments(d: Any, name: str = "", sep: str = '.', out: Any = sys.stdout) -> None: """ recursively dump comments, all but the toplevel preceded by the path in dotted form x.0.a """ if isinstance(d, dict) and hasattr(d, 'ca'): if name: - out.write('{} {}\n'.format(name, type(d))) - out.write('{!r}\n'.format(d.ca)) # type: ignore + out.write(f'{name} {type(d)}\n') + out.write(f'{d.ca!r}\n') # type: ignore for k in d: dump_comments(d[k], name=(name + sep + str(k)) if name else k, sep=sep, out=out) elif isinstance(d, list) and hasattr(d, 'ca'): if name: - out.write('{} {}\n'.format(name, type(d))) - out.write('{!r}\n'.format(d.ca)) # type: ignore + out.write(f'{name} {type(d)}\n') + out.write(f'{d.ca!r}\n') # type: ignore for idx, k in enumerate(d): dump_comments( k, name=(name + sep + str(idx)) if name else str(idx), sep=sep, out=out |