Debian Codegraph @

strictyaml/ruamel/nodes.py

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