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