summaryrefslogtreecommitdiff
path: root/nodes.py
blob: 7f1cf8869889900dc8508dd3b5d8ae45897d7eab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# coding: utf-8

from __future__ import print_function

import sys
if sys.version_info >= (3, 5, 2):
    from typing import Dict, Any, Text   # NOQA


class Node(object):
    __slots__ = 'tag', 'value', 'start_mark', 'end_mark', 'comment', 'anchor',

    def __init__(self, tag, value, start_mark, end_mark, comment=None):
        # type: (Any, Any, Any, Any, Any) -> None
        self.tag = tag
        self.value = value
        self.start_mark = start_mark
        self.end_mark = end_mark
        self.comment = comment
        self.anchor = None

    def __repr__(self):
        # type: () -> str
        value = self.value
        # if isinstance(value, list):
        #     if len(value) == 0:
        #         value = '<empty>'
        #     elif len(value) == 1:
        #         value = '<1 item>'
        #     else:
        #         value = '<%d items>' % len(value)
        # else:
        #     if len(value) > 75:
        #         value = repr(value[:70]+u' ... ')
        #     else:
        #         value = repr(value)
        value = repr(value)
        return '%s(tag=%r, value=%s)' % (self.__class__.__name__,
                                         self.tag, value)

    def dump(self, indent=0):
        # type: (int) -> None
        if isinstance(self.value, basestring):
            print('{}{}(tag={!r}, value={!r})'.format(
                '  ' * indent, self.__class__.__name__, self.tag, self.value))
            if self.comment:
                print('    {}comment: {})'.format(
                    '  ' * indent, self.comment))
            return
        print('{}{}(tag={!r})'.format(
            '  ' * indent, self.__class__.__name__, self.tag))
        if self.comment:
            print('    {}comment: {})'.format(
                '  ' * indent, self.comment))
        for v in self.value:
            if isinstance(v, tuple):
                for v1 in v:
                    v1.dump(indent+1)
            elif isinstance(v, Node):
                v.dump(indent+1)
            else:
                print('Node value type?', type(v))


class ScalarNode(Node):
    """
    styles:
      ? -> set() ? key, no value
      " -> double quoted
      ' -> single quoted
      | -> literal style
      > -> folding style
    """
    __slots__ = 'style',
    id = 'scalar'

    def __init__(self, tag, value, start_mark=None, end_mark=None, style=None,
                 comment=None):
        # type: (Any, Any, Any, Any, Any, Any) -> None
        Node.__init__(self, tag, value, start_mark, end_mark, comment=comment)
        self.style = style


class CollectionNode(Node):
    __slots__ = 'flow_style', 'anchor',

    def __init__(self, tag, value, start_mark=None, end_mark=None,
                 flow_style=None, comment=None, anchor=None):
        # type: (Any, Any, Any, Any, Any, Any, Any) -> None
        Node.__init__(self, tag, value, start_mark, end_mark, comment=comment)
        self.flow_style = flow_style
        self.anchor = anchor


class SequenceNode(CollectionNode):
    __slots__ = ()
    id = 'sequence'


class MappingNode(CollectionNode):
    __slots__ = ()
    id = 'mapping'