nodes.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # coding: utf-8
  2. from __future__ import print_function
  3. import sys
  4. from .compat import string_types
  5. if False: # MYPY
  6. from typing import Dict, Any, Text # NOQA
  7. class Node(object):
  8. __slots__ = 'tag', 'value', 'start_mark', 'end_mark', 'comment', 'anchor'
  9. def __init__(self, tag, value, start_mark, end_mark, comment=None, anchor=None):
  10. # type: (Any, Any, Any, Any, Any, Any) -> None
  11. self.tag = tag
  12. self.value = value
  13. self.start_mark = start_mark
  14. self.end_mark = end_mark
  15. self.comment = comment
  16. self.anchor = anchor
  17. def __repr__(self):
  18. # type: () -> str
  19. value = self.value
  20. # if isinstance(value, list):
  21. # if len(value) == 0:
  22. # value = '<empty>'
  23. # elif len(value) == 1:
  24. # value = '<1 item>'
  25. # else:
  26. # value = '<%d items>' % len(value)
  27. # else:
  28. # if len(value) > 75:
  29. # value = repr(value[:70]+u' ... ')
  30. # else:
  31. # value = repr(value)
  32. value = repr(value)
  33. return '%s(tag=%r, value=%s)' % (self.__class__.__name__, self.tag, value)
  34. def dump(self, indent=0):
  35. # type: (int) -> None
  36. if isinstance(self.value, string_types):
  37. sys.stdout.write(
  38. '{}{}(tag={!r}, value={!r})\n'.format(
  39. ' ' * indent, self.__class__.__name__, self.tag, self.value
  40. )
  41. )
  42. if self.comment:
  43. sys.stdout.write(' {}comment: {})\n'.format(' ' * indent, self.comment))
  44. return
  45. sys.stdout.write(
  46. '{}{}(tag={!r})\n'.format(' ' * indent, self.__class__.__name__, self.tag)
  47. )
  48. if self.comment:
  49. sys.stdout.write(' {}comment: {})\n'.format(' ' * indent, self.comment))
  50. for v in self.value:
  51. if isinstance(v, tuple):
  52. for v1 in v:
  53. v1.dump(indent + 1)
  54. elif isinstance(v, Node):
  55. v.dump(indent + 1)
  56. else:
  57. sys.stdout.write('Node value type? {}\n'.format(type(v)))
  58. class ScalarNode(Node):
  59. """
  60. styles:
  61. ? -> set() ? key, no value
  62. " -> double quoted
  63. ' -> single quoted
  64. | -> literal style
  65. > -> folding style
  66. """
  67. __slots__ = ('style',)
  68. id = 'scalar'
  69. def __init__(
  70. self, tag, value, start_mark=None, end_mark=None, style=None, comment=None, anchor=None
  71. ):
  72. # type: (Any, Any, Any, Any, Any, Any, Any) -> None
  73. Node.__init__(self, tag, value, start_mark, end_mark, comment=comment, anchor=anchor)
  74. self.style = style
  75. class CollectionNode(Node):
  76. __slots__ = ('flow_style',)
  77. def __init__(
  78. self,
  79. tag,
  80. value,
  81. start_mark=None,
  82. end_mark=None,
  83. flow_style=None,
  84. comment=None,
  85. anchor=None,
  86. ):
  87. # type: (Any, Any, Any, Any, Any, Any, Any) -> None
  88. Node.__init__(self, tag, value, start_mark, end_mark, comment=comment)
  89. self.flow_style = flow_style
  90. self.anchor = anchor
  91. class SequenceNode(CollectionNode):
  92. __slots__ = ()
  93. id = 'sequence'
  94. class MappingNode(CollectionNode):
  95. __slots__ = ('merge',)
  96. id = 'mapping'
  97. def __init__(
  98. self,
  99. tag,
  100. value,
  101. start_mark=None,
  102. end_mark=None,
  103. flow_style=None,
  104. comment=None,
  105. anchor=None,
  106. ):
  107. # type: (Any, Any, Any, Any, Any, Any, Any) -> None
  108. CollectionNode.__init__(
  109. self, tag, value, start_mark, end_mark, flow_style, comment, anchor
  110. )
  111. self.merge = None