• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1
2import yaml
3import pprint
4
5def test_implicit_resolver(data_filename, detect_filename, verbose=False):
6    correct_tag = None
7    node = None
8    try:
9        with open(detect_filename, 'r') as file:
10            correct_tag = file.read().strip()
11        with open(data_filename, 'rb') as file:
12            node = yaml.compose(file)
13        assert isinstance(node, yaml.SequenceNode), node
14        for scalar in node.value:
15            assert isinstance(scalar, yaml.ScalarNode), scalar
16            assert scalar.tag == correct_tag, (scalar.tag, correct_tag)
17    finally:
18        if verbose:
19            print("CORRECT TAG:", correct_tag)
20            if hasattr(node, 'value'):
21                print("CHILDREN:")
22                pprint.pprint(node.value)
23
24test_implicit_resolver.unittest = ['.data', '.detect']
25
26def _make_path_loader_and_dumper():
27    global MyLoader, MyDumper
28
29    class MyLoader(yaml.Loader):
30        pass
31    class MyDumper(yaml.Dumper):
32        pass
33
34    yaml.add_path_resolver('!root', [],
35            Loader=MyLoader, Dumper=MyDumper)
36    yaml.add_path_resolver('!root/scalar', [], str,
37            Loader=MyLoader, Dumper=MyDumper)
38    yaml.add_path_resolver('!root/key11/key12/*', ['key11', 'key12'],
39            Loader=MyLoader, Dumper=MyDumper)
40    yaml.add_path_resolver('!root/key21/1/*', ['key21', 1],
41            Loader=MyLoader, Dumper=MyDumper)
42    yaml.add_path_resolver('!root/key31/*/*/key14/map', ['key31', None, None, 'key14'], dict,
43            Loader=MyLoader, Dumper=MyDumper)
44
45    return MyLoader, MyDumper
46
47def _convert_node(node):
48    if isinstance(node, yaml.ScalarNode):
49        return (node.tag, node.value)
50    elif isinstance(node, yaml.SequenceNode):
51        value = []
52        for item in node.value:
53            value.append(_convert_node(item))
54        return (node.tag, value)
55    elif isinstance(node, yaml.MappingNode):
56        value = []
57        for key, item in node.value:
58            value.append((_convert_node(key), _convert_node(item)))
59        return (node.tag, value)
60
61def test_path_resolver_loader(data_filename, path_filename, verbose=False):
62    _make_path_loader_and_dumper()
63    with open(data_filename, 'rb') as file:
64        nodes1 = list(yaml.compose_all(file.read(), Loader=MyLoader))
65    with open(path_filename, 'rb') as file:
66        nodes2 = list(yaml.compose_all(file.read()))
67    try:
68        for node1, node2 in zip(nodes1, nodes2):
69            data1 = _convert_node(node1)
70            data2 = _convert_node(node2)
71            assert data1 == data2, (data1, data2)
72    finally:
73        if verbose:
74            print(yaml.serialize_all(nodes1))
75
76test_path_resolver_loader.unittest = ['.data', '.path']
77
78def test_path_resolver_dumper(data_filename, path_filename, verbose=False):
79    _make_path_loader_and_dumper()
80    for filename in [data_filename, path_filename]:
81        with open(filename, 'rb') as file:
82            output = yaml.serialize_all(yaml.compose_all(file), Dumper=MyDumper)
83        if verbose:
84            print(output)
85        nodes1 = yaml.compose_all(output)
86        with open(data_filename, 'rb') as file:
87            nodes2 = yaml.compose_all(file)
88            for node1, node2 in zip(nodes1, nodes2):
89                data1 = _convert_node(node1)
90                data2 = _convert_node(node2)
91                assert data1 == data2, (data1, data2)
92
93test_path_resolver_dumper.unittest = ['.data', '.path']
94
95if __name__ == '__main__':
96    import test_appliance
97    test_appliance.run(globals())
98
99