nodes.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. from __future__ import annotations
  2. import sys
  3. if False: # MYPY
  4. from typing import Dict, Any, Text, Optional # NOQA
  5. from ruamel.yaml.tag import Tag
  6. class Node:
  7. __slots__ = 'ctag', 'value', 'start_mark', 'end_mark', 'comment', 'anchor'
  8. def __init__(
  9. self,
  10. tag: Any,
  11. value: Any,
  12. start_mark: Any,
  13. end_mark: Any,
  14. comment: Any = None,
  15. anchor: Any = None,
  16. ) -> None:
  17. # you can still get a string from the serializer
  18. self.ctag = tag if isinstance(tag, Tag) else Tag(suffix=tag)
  19. self.value = value
  20. self.start_mark = start_mark
  21. self.end_mark = end_mark
  22. self.comment = comment
  23. self.anchor = anchor
  24. @property
  25. def tag(self) -> Optional[str]:
  26. return None if self.ctag is None else str(self.ctag)
  27. @tag.setter
  28. def tag(self, val: Any) -> None:
  29. if isinstance(val, str):
  30. val = Tag(suffix=val)
  31. self.ctag = val
  32. def __repr__(self) -> Any:
  33. value = self.value
  34. # if isinstance(value, list):
  35. # if len(value) == 0:
  36. # value = '<empty>'
  37. # elif len(value) == 1:
  38. # value = '<1 item>'
  39. # else:
  40. # value = f'<{len(value)} items>'
  41. # else:
  42. # if len(value) > 75:
  43. # value = repr(value[:70]+' ... ')
  44. # else:
  45. # value = repr(value)
  46. value = repr(value)
  47. if self.anchor is not None:
  48. return f'{self.__class__.__name__!s}(tag={self.tag!r}, anchor={self.anchor!r}, value={value!s})' # NOQA
  49. return f'{self.__class__.__name__!s}(tag={self.tag!r}, value={value!s})'
  50. def dump(self, indent: int = 0) -> None:
  51. xx = self.__class__.__name__
  52. xi = ' ' * indent
  53. if isinstance(self.value, str):
  54. sys.stdout.write(f'{xi}{xx}(tag={self.tag!r}, value={self.value!r})\n')
  55. if self.comment:
  56. sys.stdout.write(f' {xi}comment: {self.comment})\n')
  57. return
  58. sys.stdout.write(f'{xi}{xx}(tag={self.tag!r})\n')
  59. if self.comment:
  60. sys.stdout.write(f' {xi}comment: {self.comment})\n')
  61. for v in self.value:
  62. if isinstance(v, tuple):
  63. for v1 in v:
  64. v1.dump(indent + 1)
  65. elif isinstance(v, Node):
  66. v.dump(indent + 1)
  67. else:
  68. sys.stdout.write(f'Node value type? {type(v)}\n')
  69. class ScalarNode(Node):
  70. """
  71. styles:
  72. ? -> set() ? key, no value
  73. - -> suppressable null value in set
  74. " -> double quoted
  75. ' -> single quoted
  76. | -> literal style
  77. > -> folding style
  78. """
  79. __slots__ = ('style',)
  80. id = 'scalar'
  81. def __init__(
  82. self,
  83. tag: Any,
  84. value: Any,
  85. start_mark: Any = None,
  86. end_mark: Any = None,
  87. style: Any = None,
  88. comment: Any = None,
  89. anchor: Any = None,
  90. ) -> None:
  91. Node.__init__(self, tag, value, start_mark, end_mark, comment=comment, anchor=anchor)
  92. self.style = style
  93. class CollectionNode(Node):
  94. __slots__ = ('flow_style',)
  95. def __init__(
  96. self,
  97. tag: Any,
  98. value: Any,
  99. start_mark: Any = None,
  100. end_mark: Any = None,
  101. flow_style: Any = None,
  102. comment: Any = None,
  103. anchor: Any = None,
  104. ) -> None:
  105. Node.__init__(self, tag, value, start_mark, end_mark, comment=comment)
  106. self.flow_style = flow_style
  107. self.anchor = anchor
  108. class SequenceNode(CollectionNode):
  109. __slots__ = ()
  110. id = 'sequence'
  111. class MappingNode(CollectionNode):
  112. __slots__ = ('merge',)
  113. id = 'mapping'
  114. def __init__(
  115. self,
  116. tag: Any,
  117. value: Any,
  118. start_mark: Any = None,
  119. end_mark: Any = None,
  120. flow_style: Any = None,
  121. comment: Any = None,
  122. anchor: Any = None,
  123. ) -> None:
  124. CollectionNode.__init__(
  125. self, tag, value, start_mark, end_mark, flow_style, comment, anchor,
  126. )
  127. self.merge = None