| 1 | |
|---|
| 2 | import yaml |
|---|
| 3 | import pprint |
|---|
| 4 | |
|---|
| 5 | import datetime |
|---|
| 6 | try: |
|---|
| 7 | set |
|---|
| 8 | except NameError: |
|---|
| 9 | from sets import Set as set |
|---|
| 10 | import yaml.tokens |
|---|
| 11 | |
|---|
| 12 | def execute(code): |
|---|
| 13 | exec code |
|---|
| 14 | return value |
|---|
| 15 | |
|---|
| 16 | def _make_objects(): |
|---|
| 17 | global MyLoader, MyDumper, MyTestClass1, MyTestClass2, MyTestClass3, YAMLObject1, YAMLObject2, \ |
|---|
| 18 | AnObject, AnInstance, AState, ACustomState, InitArgs, InitArgsWithState, \ |
|---|
| 19 | NewArgs, NewArgsWithState, Reduce, ReduceWithState, MyInt, MyList, MyDict, \ |
|---|
| 20 | FixedOffset, today, execute |
|---|
| 21 | |
|---|
| 22 | class MyLoader(yaml.Loader): |
|---|
| 23 | pass |
|---|
| 24 | class MyDumper(yaml.Dumper): |
|---|
| 25 | pass |
|---|
| 26 | |
|---|
| 27 | class MyTestClass1: |
|---|
| 28 | def __init__(self, x, y=0, z=0): |
|---|
| 29 | self.x = x |
|---|
| 30 | self.y = y |
|---|
| 31 | self.z = z |
|---|
| 32 | def __eq__(self, other): |
|---|
| 33 | if isinstance(other, MyTestClass1): |
|---|
| 34 | return self.__class__, self.__dict__ == other.__class__, other.__dict__ |
|---|
| 35 | else: |
|---|
| 36 | return False |
|---|
| 37 | |
|---|
| 38 | def construct1(constructor, node): |
|---|
| 39 | mapping = constructor.construct_mapping(node) |
|---|
| 40 | return MyTestClass1(**mapping) |
|---|
| 41 | def represent1(representer, native): |
|---|
| 42 | return representer.represent_mapping("!tag1", native.__dict__) |
|---|
| 43 | |
|---|
| 44 | yaml.add_constructor("!tag1", construct1, Loader=MyLoader) |
|---|
| 45 | yaml.add_representer(MyTestClass1, represent1, Dumper=MyDumper) |
|---|
| 46 | |
|---|
| 47 | class MyTestClass2(MyTestClass1, yaml.YAMLObject): |
|---|
| 48 | yaml_loader = MyLoader |
|---|
| 49 | yaml_dumper = MyDumper |
|---|
| 50 | yaml_tag = "!tag2" |
|---|
| 51 | def from_yaml(cls, constructor, node): |
|---|
| 52 | x = constructor.construct_yaml_int(node) |
|---|
| 53 | return cls(x=x) |
|---|
| 54 | from_yaml = classmethod(from_yaml) |
|---|
| 55 | def to_yaml(cls, representer, native): |
|---|
| 56 | return representer.represent_scalar(cls.yaml_tag, str(native.x)) |
|---|
| 57 | to_yaml = classmethod(to_yaml) |
|---|
| 58 | |
|---|
| 59 | class MyTestClass3(MyTestClass2): |
|---|
| 60 | yaml_tag = "!tag3" |
|---|
| 61 | def from_yaml(cls, constructor, node): |
|---|
| 62 | mapping = constructor.construct_mapping(node) |
|---|
| 63 | if '=' in mapping: |
|---|
| 64 | x = mapping['='] |
|---|
| 65 | del mapping['='] |
|---|
| 66 | mapping['x'] = x |
|---|
| 67 | return cls(**mapping) |
|---|
| 68 | from_yaml = classmethod(from_yaml) |
|---|
| 69 | def to_yaml(cls, representer, native): |
|---|
| 70 | return representer.represent_mapping(cls.yaml_tag, native.__dict__) |
|---|
| 71 | to_yaml = classmethod(to_yaml) |
|---|
| 72 | |
|---|
| 73 | class YAMLObject1(yaml.YAMLObject): |
|---|
| 74 | yaml_loader = MyLoader |
|---|
| 75 | yaml_dumper = MyDumper |
|---|
| 76 | yaml_tag = '!foo' |
|---|
| 77 | def __init__(self, my_parameter=None, my_another_parameter=None): |
|---|
| 78 | self.my_parameter = my_parameter |
|---|
| 79 | self.my_another_parameter = my_another_parameter |
|---|
| 80 | def __eq__(self, other): |
|---|
| 81 | if isinstance(other, YAMLObject1): |
|---|
| 82 | return self.__class__, self.__dict__ == other.__class__, other.__dict__ |
|---|
| 83 | else: |
|---|
| 84 | return False |
|---|
| 85 | |
|---|
| 86 | class YAMLObject2(yaml.YAMLObject): |
|---|
| 87 | yaml_loader = MyLoader |
|---|
| 88 | yaml_dumper = MyDumper |
|---|
| 89 | yaml_tag = '!bar' |
|---|
| 90 | def __init__(self, foo=1, bar=2, baz=3): |
|---|
| 91 | self.foo = foo |
|---|
| 92 | self.bar = bar |
|---|
| 93 | self.baz = baz |
|---|
| 94 | def __getstate__(self): |
|---|
| 95 | return {1: self.foo, 2: self.bar, 3: self.baz} |
|---|
| 96 | def __setstate__(self, state): |
|---|
| 97 | self.foo = state[1] |
|---|
| 98 | self.bar = state[2] |
|---|
| 99 | self.baz = state[3] |
|---|
| 100 | def __eq__(self, other): |
|---|
| 101 | if isinstance(other, YAMLObject2): |
|---|
| 102 | return self.__class__, self.__dict__ == other.__class__, other.__dict__ |
|---|
| 103 | else: |
|---|
| 104 | return False |
|---|
| 105 | |
|---|
| 106 | class AnObject(object): |
|---|
| 107 | def __new__(cls, foo=None, bar=None, baz=None): |
|---|
| 108 | self = object.__new__(cls) |
|---|
| 109 | self.foo = foo |
|---|
| 110 | self.bar = bar |
|---|
| 111 | self.baz = baz |
|---|
| 112 | return self |
|---|
| 113 | def __cmp__(self, other): |
|---|
| 114 | return cmp((type(self), self.foo, self.bar, self.baz), |
|---|
| 115 | (type(other), other.foo, other.bar, other.baz)) |
|---|
| 116 | def __eq__(self, other): |
|---|
| 117 | return type(self) is type(other) and \ |
|---|
| 118 | (self.foo, self.bar, self.baz) == (other.foo, other.bar, other.baz) |
|---|
| 119 | |
|---|
| 120 | class AnInstance: |
|---|
| 121 | def __init__(self, foo=None, bar=None, baz=None): |
|---|
| 122 | self.foo = foo |
|---|
| 123 | self.bar = bar |
|---|
| 124 | self.baz = baz |
|---|
| 125 | def __cmp__(self, other): |
|---|
| 126 | return cmp((type(self), self.foo, self.bar, self.baz), |
|---|
| 127 | (type(other), other.foo, other.bar, other.baz)) |
|---|
| 128 | def __eq__(self, other): |
|---|
| 129 | return type(self) is type(other) and \ |
|---|
| 130 | (self.foo, self.bar, self.baz) == (other.foo, other.bar, other.baz) |
|---|
| 131 | |
|---|
| 132 | class AState(AnInstance): |
|---|
| 133 | def __getstate__(self): |
|---|
| 134 | return { |
|---|
| 135 | '_foo': self.foo, |
|---|
| 136 | '_bar': self.bar, |
|---|
| 137 | '_baz': self.baz, |
|---|
| 138 | } |
|---|
| 139 | def __setstate__(self, state): |
|---|
| 140 | self.foo = state['_foo'] |
|---|
| 141 | self.bar = state['_bar'] |
|---|
| 142 | self.baz = state['_baz'] |
|---|
| 143 | |
|---|
| 144 | class ACustomState(AnInstance): |
|---|
| 145 | def __getstate__(self): |
|---|
| 146 | return (self.foo, self.bar, self.baz) |
|---|
| 147 | def __setstate__(self, state): |
|---|
| 148 | self.foo, self.bar, self.baz = state |
|---|
| 149 | |
|---|
| 150 | class InitArgs(AnInstance): |
|---|
| 151 | def __getinitargs__(self): |
|---|
| 152 | return (self.foo, self.bar, self.baz) |
|---|
| 153 | def __getstate__(self): |
|---|
| 154 | return {} |
|---|
| 155 | |
|---|
| 156 | class InitArgsWithState(AnInstance): |
|---|
| 157 | def __getinitargs__(self): |
|---|
| 158 | return (self.foo, self.bar) |
|---|
| 159 | def __getstate__(self): |
|---|
| 160 | return self.baz |
|---|
| 161 | def __setstate__(self, state): |
|---|
| 162 | self.baz = state |
|---|
| 163 | |
|---|
| 164 | class NewArgs(AnObject): |
|---|
| 165 | def __getnewargs__(self): |
|---|
| 166 | return (self.foo, self.bar, self.baz) |
|---|
| 167 | def __getstate__(self): |
|---|
| 168 | return {} |
|---|
| 169 | |
|---|
| 170 | class NewArgsWithState(AnObject): |
|---|
| 171 | def __getnewargs__(self): |
|---|
| 172 | return (self.foo, self.bar) |
|---|
| 173 | def __getstate__(self): |
|---|
| 174 | return self.baz |
|---|
| 175 | def __setstate__(self, state): |
|---|
| 176 | self.baz = state |
|---|
| 177 | |
|---|
| 178 | class Reduce(AnObject): |
|---|
| 179 | def __reduce__(self): |
|---|
| 180 | return self.__class__, (self.foo, self.bar, self.baz) |
|---|
| 181 | |
|---|
| 182 | class ReduceWithState(AnObject): |
|---|
| 183 | def __reduce__(self): |
|---|
| 184 | return self.__class__, (self.foo, self.bar), self.baz |
|---|
| 185 | def __setstate__(self, state): |
|---|
| 186 | self.baz = state |
|---|
| 187 | |
|---|
| 188 | class MyInt(int): |
|---|
| 189 | def __eq__(self, other): |
|---|
| 190 | return type(self) is type(other) and int(self) == int(other) |
|---|
| 191 | |
|---|
| 192 | class MyList(list): |
|---|
| 193 | def __init__(self, n=1): |
|---|
| 194 | self.extend([None]*n) |
|---|
| 195 | def __eq__(self, other): |
|---|
| 196 | return type(self) is type(other) and list(self) == list(other) |
|---|
| 197 | |
|---|
| 198 | class MyDict(dict): |
|---|
| 199 | def __init__(self, n=1): |
|---|
| 200 | for k in range(n): |
|---|
| 201 | self[k] = None |
|---|
| 202 | def __eq__(self, other): |
|---|
| 203 | return type(self) is type(other) and dict(self) == dict(other) |
|---|
| 204 | |
|---|
| 205 | class FixedOffset(datetime.tzinfo): |
|---|
| 206 | def __init__(self, offset, name): |
|---|
| 207 | self.__offset = datetime.timedelta(minutes=offset) |
|---|
| 208 | self.__name = name |
|---|
| 209 | def utcoffset(self, dt): |
|---|
| 210 | return self.__offset |
|---|
| 211 | def tzname(self, dt): |
|---|
| 212 | return self.__name |
|---|
| 213 | def dst(self, dt): |
|---|
| 214 | return datetime.timedelta(0) |
|---|
| 215 | |
|---|
| 216 | today = datetime.date.today() |
|---|
| 217 | |
|---|
| 218 | def _load_code(expression): |
|---|
| 219 | return eval(expression) |
|---|
| 220 | |
|---|
| 221 | def _serialize_value(data): |
|---|
| 222 | if isinstance(data, list): |
|---|
| 223 | return '[%s]' % ', '.join(map(_serialize_value, data)) |
|---|
| 224 | elif isinstance(data, dict): |
|---|
| 225 | items = [] |
|---|
| 226 | for key, value in data.items(): |
|---|
| 227 | key = _serialize_value(key) |
|---|
| 228 | value = _serialize_value(value) |
|---|
| 229 | items.append("%s: %s" % (key, value)) |
|---|
| 230 | items.sort() |
|---|
| 231 | return '{%s}' % ', '.join(items) |
|---|
| 232 | elif isinstance(data, datetime.datetime): |
|---|
| 233 | return repr(data.utctimetuple()) |
|---|
| 234 | elif isinstance(data, unicode): |
|---|
| 235 | return data.encode('utf-8') |
|---|
| 236 | elif isinstance(data, float) and data != data: |
|---|
| 237 | return '?' |
|---|
| 238 | else: |
|---|
| 239 | return str(data) |
|---|
| 240 | |
|---|
| 241 | def test_constructor_types(data_filename, code_filename, verbose=False): |
|---|
| 242 | _make_objects() |
|---|
| 243 | native1 = None |
|---|
| 244 | native2 = None |
|---|
| 245 | try: |
|---|
| 246 | native1 = list(yaml.load_all(open(data_filename, 'rb'), Loader=MyLoader)) |
|---|
| 247 | if len(native1) == 1: |
|---|
| 248 | native1 = native1[0] |
|---|
| 249 | native2 = _load_code(open(code_filename, 'rb').read()) |
|---|
| 250 | try: |
|---|
| 251 | if native1 == native2: |
|---|
| 252 | return |
|---|
| 253 | except TypeError: |
|---|
| 254 | pass |
|---|
| 255 | if verbose: |
|---|
| 256 | print "SERIALIZED NATIVE1:" |
|---|
| 257 | print _serialize_value(native1) |
|---|
| 258 | print "SERIALIZED NATIVE2:" |
|---|
| 259 | print _serialize_value(native2) |
|---|
| 260 | assert _serialize_value(native1) == _serialize_value(native2), (native1, native2) |
|---|
| 261 | finally: |
|---|
| 262 | if verbose: |
|---|
| 263 | print "NATIVE1:" |
|---|
| 264 | pprint.pprint(native1) |
|---|
| 265 | print "NATIVE2:" |
|---|
| 266 | pprint.pprint(native2) |
|---|
| 267 | |
|---|
| 268 | test_constructor_types.unittest = ['.data', '.code'] |
|---|
| 269 | |
|---|
| 270 | if __name__ == '__main__': |
|---|
| 271 | import sys, test_constructor |
|---|
| 272 | sys.modules['test_constructor'] = sys.modules['__main__'] |
|---|
| 273 | import test_appliance |
|---|
| 274 | test_appliance.run(globals()) |
|---|
| 275 | |
|---|