| [55] | 1 | |
|---|
| [322] | 2 | import yaml |
|---|
| 3 | import pprint |
|---|
| [55] | 4 | |
|---|
| [322] | 5 | def test_implicit_resolver(data_filename, detect_filename, verbose=False): |
|---|
| 6 | correct_tag = None |
|---|
| 7 | node = None |
|---|
| 8 | try: |
|---|
| [328] | 9 | correct_tag = open(detect_filename, 'r').read().strip() |
|---|
| [322] | 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: |
|---|
| [328] | 17 | print("CORRECT TAG:", correct_tag) |
|---|
| [322] | 18 | if hasattr(node, 'value'): |
|---|
| [328] | 19 | print("CHILDREN:") |
|---|
| [322] | 20 | pprint.pprint(node.value) |
|---|
| [55] | 21 | |
|---|
| [322] | 22 | test_implicit_resolver.unittest = ['.data', '.detect'] |
|---|
| [55] | 23 | |
|---|
| [322] | 24 | def _make_path_loader_and_dumper(): |
|---|
| 25 | global MyLoader, MyDumper |
|---|
| [137] | 26 | |
|---|
| [322] | 27 | class MyLoader(yaml.Loader): |
|---|
| 28 | pass |
|---|
| 29 | class MyDumper(yaml.Dumper): |
|---|
| 30 | pass |
|---|
| [137] | 31 | |
|---|
| [328] | 32 | yaml.add_path_resolver('!root', [], |
|---|
| [322] | 33 | Loader=MyLoader, Dumper=MyDumper) |
|---|
| [328] | 34 | yaml.add_path_resolver('!root/scalar', [], str, |
|---|
| [322] | 35 | Loader=MyLoader, Dumper=MyDumper) |
|---|
| [328] | 36 | yaml.add_path_resolver('!root/key11/key12/*', ['key11', 'key12'], |
|---|
| [322] | 37 | Loader=MyLoader, Dumper=MyDumper) |
|---|
| [328] | 38 | yaml.add_path_resolver('!root/key21/1/*', ['key21', 1], |
|---|
| [322] | 39 | Loader=MyLoader, Dumper=MyDumper) |
|---|
| [328] | 40 | yaml.add_path_resolver('!root/key31/*/*/key14/map', ['key31', None, None, 'key14'], dict, |
|---|
| [322] | 41 | Loader=MyLoader, Dumper=MyDumper) |
|---|
| [137] | 42 | |
|---|
| [322] | 43 | return MyLoader, MyDumper |
|---|
| [137] | 44 | |
|---|
| [322] | 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) |
|---|
| [137] | 58 | |
|---|
| [322] | 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: |
|---|
| [328] | 70 | print(yaml.serialize_all(nodes1)) |
|---|
| [137] | 71 | |
|---|
| [322] | 72 | test_path_resolver_loader.unittest = ['.data', '.path'] |
|---|
| [137] | 73 | |
|---|
| [322] | 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: |
|---|
| [328] | 79 | print(output) |
|---|
| [322] | 80 | nodes1 = yaml.compose_all(output) |
|---|
| 81 | nodes2 = yaml.compose_all(open(data_filename, 'rb')) |
|---|
| [137] | 82 | for node1, node2 in zip(nodes1, nodes2): |
|---|
| [322] | 83 | data1 = _convert_node(node1) |
|---|
| 84 | data2 = _convert_node(node2) |
|---|
| 85 | assert data1 == data2, (data1, data2) |
|---|
| [55] | 86 | |
|---|
| [322] | 87 | test_path_resolver_dumper.unittest = ['.data', '.path'] |
|---|
| [137] | 88 | |
|---|
| [322] | 89 | if __name__ == '__main__': |
|---|
| 90 | import test_appliance |
|---|
| 91 | test_appliance.run(globals()) |
|---|
| [137] | 92 | |
|---|