選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 

148 行
3.9 KiB

  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. return f'{self.__class__.__name__!s}(tag={self.tag!r}, value={value!s})'
  48. def dump(self, indent: int = 0) -> None:
  49. xx = self.__class__.__name__
  50. xi = ' ' * indent
  51. if isinstance(self.value, str):
  52. sys.stdout.write(f'{xi}{xx}(tag={self.tag!r}, value={self.value!r})\n')
  53. if self.comment:
  54. sys.stdout.write(f' {xi}comment: {self.comment})\n')
  55. return
  56. sys.stdout.write(f'{xi}{xx}(tag={self.tag!r})\n')
  57. if self.comment:
  58. sys.stdout.write(f' {xi}comment: {self.comment})\n')
  59. for v in self.value:
  60. if isinstance(v, tuple):
  61. for v1 in v:
  62. v1.dump(indent + 1)
  63. elif isinstance(v, Node):
  64. v.dump(indent + 1)
  65. else:
  66. sys.stdout.write(f'Node value type? {type(v)}\n')
  67. class ScalarNode(Node):
  68. """
  69. styles:
  70. ? -> set() ? key, no value
  71. - -> suppressable null value in set
  72. " -> double quoted
  73. ' -> single quoted
  74. | -> literal style
  75. > -> folding style
  76. """
  77. __slots__ = ('style',)
  78. id = 'scalar'
  79. def __init__(
  80. self,
  81. tag: Any,
  82. value: Any,
  83. start_mark: Any = None,
  84. end_mark: Any = None,
  85. style: Any = None,
  86. comment: Any = None,
  87. anchor: Any = None,
  88. ) -> None:
  89. Node.__init__(self, tag, value, start_mark, end_mark, comment=comment, anchor=anchor)
  90. self.style = style
  91. class CollectionNode(Node):
  92. __slots__ = ('flow_style',)
  93. def __init__(
  94. self,
  95. tag: Any,
  96. value: Any,
  97. start_mark: Any = None,
  98. end_mark: Any = None,
  99. flow_style: Any = None,
  100. comment: Any = None,
  101. anchor: Any = None,
  102. ) -> None:
  103. Node.__init__(self, tag, value, start_mark, end_mark, comment=comment)
  104. self.flow_style = flow_style
  105. self.anchor = anchor
  106. class SequenceNode(CollectionNode):
  107. __slots__ = ()
  108. id = 'sequence'
  109. class MappingNode(CollectionNode):
  110. __slots__ = ('merge',)
  111. id = 'mapping'
  112. def __init__(
  113. self,
  114. tag: Any,
  115. value: Any,
  116. start_mark: Any = None,
  117. end_mark: Any = None,
  118. flow_style: Any = None,
  119. comment: Any = None,
  120. anchor: Any = None,
  121. ) -> None:
  122. CollectionNode.__init__(
  123. self, tag, value, start_mark, end_mark, flow_style, comment, anchor,
  124. )
  125. self.merge = None