| [133] | 1 | |
|---|
| 2 | import test_appliance |
|---|
| 3 | |
|---|
| 4 | try: |
|---|
| 5 | import datetime |
|---|
| 6 | except ImportError: |
|---|
| 7 | pass |
|---|
| 8 | try: |
|---|
| 9 | set |
|---|
| 10 | except NameError: |
|---|
| 11 | from sets import Set as set |
|---|
| 12 | |
|---|
| 13 | from yaml import * |
|---|
| 14 | |
|---|
| [136] | 15 | class MyLoader(Loader): |
|---|
| [133] | 16 | pass |
|---|
| [136] | 17 | class MyDumper(Dumper): |
|---|
| [133] | 18 | pass |
|---|
| 19 | |
|---|
| 20 | class MyTestClass1(object): |
|---|
| 21 | |
|---|
| 22 | def __init__(self, x, y=0, z=0): |
|---|
| 23 | self.x = x |
|---|
| 24 | self.y = y |
|---|
| 25 | self.z = z |
|---|
| 26 | |
|---|
| 27 | def __eq__(self, other): |
|---|
| 28 | if isinstance(other, MyTestClass1): |
|---|
| 29 | return self.__class__, self.__dict__ == other.__class__, other.__dict__ |
|---|
| 30 | else: |
|---|
| 31 | return False |
|---|
| 32 | |
|---|
| 33 | def construct1(constructor, node): |
|---|
| 34 | mapping = constructor.construct_mapping(node) |
|---|
| 35 | return MyTestClass1(**mapping) |
|---|
| 36 | def represent1(representer, native): |
|---|
| 37 | return representer.represent_mapping("!tag1", native.__dict__) |
|---|
| 38 | |
|---|
| 39 | class MyTestClass2(MyTestClass1, YAMLObject): |
|---|
| 40 | |
|---|
| [136] | 41 | yaml_loader = MyLoader |
|---|
| 42 | yaml_dumper = MyDumper |
|---|
| [133] | 43 | yaml_tag = "!tag2" |
|---|
| 44 | |
|---|
| 45 | def from_yaml(cls, constructor, node): |
|---|
| 46 | x = constructor.construct_yaml_int(node) |
|---|
| 47 | return cls(x=x) |
|---|
| 48 | from_yaml = classmethod(from_yaml) |
|---|
| 49 | |
|---|
| 50 | def to_yaml(cls, representer, native): |
|---|
| 51 | return representer.represent_scalar(cls.yaml_tag, str(native.x)) |
|---|
| 52 | to_yaml = classmethod(to_yaml) |
|---|
| 53 | |
|---|
| 54 | class MyTestClass3(MyTestClass2): |
|---|
| 55 | |
|---|
| 56 | yaml_tag = "!tag3" |
|---|
| 57 | |
|---|
| 58 | def from_yaml(cls, constructor, node): |
|---|
| 59 | mapping = constructor.construct_mapping(node) |
|---|
| 60 | if '=' in mapping: |
|---|
| 61 | x = mapping['='] |
|---|
| 62 | del mapping['='] |
|---|
| 63 | mapping['x'] = x |
|---|
| 64 | return cls(**mapping) |
|---|
| 65 | from_yaml = classmethod(from_yaml) |
|---|
| 66 | |
|---|
| 67 | def to_yaml(cls, representer, native): |
|---|
| 68 | return representer.represent_mapping(cls.yaml_tag, native.__dict__) |
|---|
| 69 | to_yaml = classmethod(to_yaml) |
|---|
| 70 | |
|---|
| [136] | 71 | MyLoader.add_constructor("!tag1", construct1) |
|---|
| 72 | MyDumper.add_representer(MyTestClass1, represent1) |
|---|
| [133] | 73 | |
|---|
| [136] | 74 | class YAMLObject1(YAMLObject): |
|---|
| 75 | yaml_loader = MyLoader |
|---|
| 76 | yaml_dumper = MyDumper |
|---|
| 77 | yaml_tag = '!foo' |
|---|
| 78 | yaml_flow_style = True |
|---|
| 79 | |
|---|
| 80 | def __init__(self, my_parameter=None, my_another_parameter=None): |
|---|
| 81 | self.my_parameter = my_parameter |
|---|
| 82 | self.my_another_parameter = my_another_parameter |
|---|
| 83 | |
|---|
| 84 | def __eq__(self, other): |
|---|
| 85 | if isinstance(other, YAMLObject1): |
|---|
| 86 | return self.__class__, self.__dict__ == other.__class__, other.__dict__ |
|---|
| 87 | else: |
|---|
| 88 | return False |
|---|
| 89 | |
|---|
| [133] | 90 | class TestTypeRepresenter(test_appliance.TestAppliance): |
|---|
| 91 | |
|---|
| 92 | def _testTypes(self, test_name, data_filename, code_filename): |
|---|
| [136] | 93 | data1 = eval(file(code_filename, 'rb').read()) |
|---|
| 94 | data2 = None |
|---|
| [133] | 95 | output = None |
|---|
| 96 | try: |
|---|
| [136] | 97 | output = dump(data1, Dumper=MyDumper) |
|---|
| 98 | data2 = load(output, Loader=MyLoader) |
|---|
| [133] | 99 | try: |
|---|
| [136] | 100 | self.failUnlessEqual(data1, data2) |
|---|
| [133] | 101 | except AssertionError: |
|---|
| [136] | 102 | if isinstance(data1, dict): |
|---|
| 103 | data1 = data1.items() |
|---|
| 104 | data1.sort() |
|---|
| 105 | data1 = repr(data1) |
|---|
| 106 | data2 = data2.items() |
|---|
| 107 | data2.sort() |
|---|
| 108 | data2 = repr(data2) |
|---|
| 109 | if data1 != data2: |
|---|
| [133] | 110 | raise |
|---|
| 111 | except: |
|---|
| 112 | print |
|---|
| 113 | print "OUTPUT:" |
|---|
| 114 | print output |
|---|
| [136] | 115 | print "NATIVES1:", data1 |
|---|
| 116 | print "NATIVES2:", data2 |
|---|
| [133] | 117 | raise |
|---|
| 118 | |
|---|
| 119 | TestTypeRepresenter.add_tests('testTypes', '.data', '.code') |
|---|
| 120 | |
|---|