File: yaml_loader.py

package info (click to toggle)
python-murano-pkg-check 0.3.0-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 464 kB
  • sloc: python: 2,599; sh: 106; makefile: 21
file content (106 lines) | stat: -rw-r--r-- 2,594 bytes parent folder | download | duplicates (3)
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
103
104
105
106
#    Copyright (c) 2016 Mirantis, Inc.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import six
import yaml

__all__ = ['YamlLoader']


class YamlMetadata(object):
    def __init__(self, mark):
        self.mark = mark

    @property
    def line(self):
        return self.mark.line

    @property
    def column(self):
        return self.mark.column

    def get_snippet(self, indent=4, max_length=75):
        return self.mark.get_snippet(indent, max_length)


class YamlObject(object):
    def __init__(self, value=None):
        self.value = value


class YamlMapping(YamlObject, dict):
    pass


class YamlSequence(YamlObject, list):
    pass


class YamlString(YamlObject, six.text_type):
    pass


class YamlNull(YamlObject):
    def __str__(self):
        return 'null'

    def __bool__(self):
        return False
    __nonzero__ = __bool__


BaseLoader = getattr(yaml, 'CSafeLoader', yaml.SafeLoader)


class YamlLoader(BaseLoader):

    def construct_yaml_seq(self, node):
        data = YamlSequence()
        yield data
        data.extend(self.construct_sequence(node))
        data.__yaml_meta__ = node.start_mark

    def construct_yaml_str(self, node):
        value = super(YamlLoader, self).construct_yaml_str(node)
        value = YamlString(value)
        value.__yaml_meta__ = node.start_mark
        return value

    def construct_yaml_map(self, node):
        data = YamlMapping()
        yield data
        value = self.construct_mapping(node)
        data.update(value)
        data.__yaml_meta__ = node.start_mark

    def construct_yaml_null(self, node):
        value = YamlNull(node)
        value.__yaml_meta__ = node.start_mark
        return value

YamlLoader.add_constructor(
    u'tag:yaml.org,2002:seq',
    YamlLoader.construct_yaml_seq)

YamlLoader.add_constructor(
    u'tag:yaml.org,2002:str',
    YamlLoader.construct_yaml_str)

YamlLoader.add_constructor(
    u'tag:yaml.org,2002:map',
    YamlLoader.construct_yaml_map)

YamlLoader.add_constructor(
    u'tag:yaml.org,2002:null',
    YamlLoader.construct_yaml_null)