comparison data_schema/__init__.py @ 44:ea8c2d01a9d9

Begin pickling support for ValidatenProblems, _Schema and Context
author Franz Glasner <fzglas.hg@dom66.de>
date Wed, 02 Aug 2023 09:31:49 +0200
parents 2376224a9717
children 8a560e0c3180
comparison
equal deleted inserted replaced
43:4ca530618303 44:ea8c2d01a9d9
30 import ast 30 import ast
31 import collections 31 import collections
32 import copy 32 import copy
33 import datetime 33 import datetime
34 import enum 34 import enum
35 import pickle
35 import re 36 import re
36 import urllib.parse 37 import urllib.parse
37 38
38 import rfc3986 39 import rfc3986
39 40
53 @enum.unique 54 @enum.unique
54 class SEVERITY(enum.IntEnum): 55 class SEVERITY(enum.IntEnum):
55 INFO = 20 56 INFO = 20
56 WARNING = 30 57 WARNING = 30
57 ERROR = 40 58 ERROR = 40
59
60 def __reduce_ex__(self, protocol):
61 return (getattr, (self.__class__, self._name_))
58 62
59 63
60 @enum.unique 64 @enum.unique
61 class ERRORS(enum.Enum): 65 class ERRORS(enum.Enum):
62 E10000 = NC_("schema-msg", "dict expected") 66 E10000 = NC_("schema-msg", "dict expected")
118 E10056 = NC_("schema-msg", "failing `any-of' item") 122 E10056 = NC_("schema-msg", "failing `any-of' item")
119 E10057 = NC_("schema-msg", "`all-of' failed") 123 E10057 = NC_("schema-msg", "`all-of' failed")
120 E10058 = NC_("schema-msg", "failing `all-of' item") 124 E10058 = NC_("schema-msg", "failing `all-of' item")
121 E10059 = NC_("schema-msg", "forbidden key detected") 125 E10059 = NC_("schema-msg", "forbidden key detected")
122 126
127 def __reduce_ex__(self, protocol):
128 return (getattr, (self.__class__, self._name_))
129
123 130
124 @enum.unique 131 @enum.unique
125 class WARNINGS(enum.Enum): 132 class WARNINGS(enum.Enum):
126 W80000 = NC_("schema-msg", "duplicate dict key") 133 W80000 = NC_("schema-msg", "duplicate dict key")
134
135 def __reduce_ex__(self, protocol):
136 return (getattr, (self.__class__, self._name_))
127 137
128 138
129 # Check some invariants at import time 139 # Check some invariants at import time
130 for e in ERRORS.__members__: 140 for e in ERRORS.__members__:
131 assert e.startswith('E'), "ERROR code `{}' shall start with letter `E'".format(e) 141 assert e.startswith('E'), "ERROR code `{}' shall start with letter `E'".format(e)
215 raise SchemaError( 225 raise SchemaError(
216 "can only nest other `ValidationProblem' instances") 226 "can only nest other `ValidationProblem' instances")
217 self.cause = cause 227 self.cause = cause
218 self.index = index 228 self.index = index
219 229
230 def __eq__(self, other):
231 if not isinstance(other, ValidationProblem):
232 return NotImplemented
233 return ((self.code == other.code)
234 and (self.severity == other.severity)
235 and (self.hint == other.hint)
236 and (self.context == other.context)
237 and (self.cause == other.cause)
238 and (self.index == other.index))
239
240 def __getstate__(self):
241 return (1, self.code, self.severity, self.hint, self.context,
242 self.cause, self.index)
243
244 def __setstate__(self, state):
245 ver = state[0]
246 if ver == 1:
247 _dummy, self.code, self.severity, self.hint, self.context, self.cause, self.index = state
248 else:
249 raise pickle.UnpicklingError(
250 "Unsupported pickle version for ValidationProblem: %d" % (ver,))
251
220 def __repr__(self): 252 def __repr__(self):
221 try: 253 try:
222 msg = " (" + problem_message(self) + ")" 254 msg = " (" + problem_message(self) + ")"
223 except LookupError: 255 except LookupError:
224 msg = "" 256 msg = ""
262 raise ValueError( 294 raise ValueError(
263 "the root schmema must be a sub-root (aka `$self') also") 295 "the root schmema must be a sub-root (aka `$self') also")
264 self.is_sub_root = True 296 self.is_sub_root = True
265 else: 297 else:
266 self.is_sub_root = is_sub_root 298 self.is_sub_root = is_sub_root
299
300 def __reduce_ex__(self, proto):
301 rv = super().__reduce_ex__(proto)
302 #assert False, repr(rv)
303 print("RRRRRRR\n\n", repr(rv))
304 assert False, repr(rv)
305 return rv
306
307 def __getstate__(self):
308 return (1, self.parent, self.is_sub_root)
309
310 def __setstate__(self, state):
311 ver = state[0]
312 if ver == 1:
313 _dummy, self.parent, self.is_sub_root = state
314 if self.parent is None:
315 self._schema_cache = {}
316 else:
317 raise pickle.UnpicklingError(
318 "Unsupported pickle version for _Schema: %d" % (ver,))
267 319
268 @property 320 @property
269 def ROOT(self): 321 def ROOT(self):
270 """Get the root schema""" 322 """Get the root schema"""
271 r = self 323 r = self
394 self._key = key 446 self._key = key
395 self._key_index = key_index 447 self._key_index = key_index
396 self._index = index 448 self._index = index
397 self._current_object = current_object 449 self._current_object = current_object
398 self._settings = settings 450 self._settings = settings
451
452 def __getstate__(self):
453 return (1, self._parent, self._key, self._key_index, self._index,
454 self.root_object, self.root_schema,
455 self._current_object, self._settings)
456
457 def __setstate__(self, state):
458 ver = state[0]
459 if ver == 1:
460 _dummy, self._parent, self._key, self._key_index, self._index, self.root_object, self.root_schema, self._current_object, self._settings = state
461 else:
462 raise pickle.UnpicklingError(
463 "Unsupported pickle version for _Context: %d" % (ver,))
399 464
400 @property 465 @property
401 def parent(self): 466 def parent(self):
402 return self._parent 467 return self._parent
403 468