diff 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
line wrap: on
line diff
--- a/data_schema/__init__.py	Thu Jul 20 09:37:22 2023 +0200
+++ b/data_schema/__init__.py	Wed Aug 02 09:31:49 2023 +0200
@@ -32,6 +32,7 @@
 import copy
 import datetime
 import enum
+import pickle
 import re
 import urllib.parse
 
@@ -56,6 +57,9 @@
     WARNING = 30
     ERROR = 40
 
+    def __reduce_ex__(self, protocol):
+        return (getattr, (self.__class__, self._name_))
+
 
 @enum.unique
 class ERRORS(enum.Enum):
@@ -120,11 +124,17 @@
     E10058 = NC_("schema-msg", "failing `all-of' item")
     E10059 = NC_("schema-msg", "forbidden key detected")
 
+    def __reduce_ex__(self, protocol):
+        return (getattr, (self.__class__, self._name_))
+
 
 @enum.unique
 class WARNINGS(enum.Enum):
     W80000 = NC_("schema-msg", "duplicate dict key")
 
+    def __reduce_ex__(self, protocol):
+        return (getattr, (self.__class__, self._name_))
+
 
 # Check some invariants at import time
 for e in ERRORS.__members__:
@@ -217,6 +227,28 @@
         self.cause = cause
         self.index = index
 
+    def __eq__(self, other):
+        if not isinstance(other, ValidationProblem):
+            return NotImplemented
+        return ((self.code == other.code)
+                and (self.severity == other.severity)
+                and (self.hint == other.hint)
+                and (self.context == other.context)
+                and (self.cause == other.cause)
+                and (self.index == other.index))
+
+    def __getstate__(self):
+        return (1, self.code, self.severity, self.hint, self.context,
+                self.cause, self.index)
+
+    def __setstate__(self, state):
+        ver = state[0]
+        if ver == 1:
+            _dummy, self.code, self.severity, self.hint, self.context, self.cause, self.index = state
+        else:
+            raise pickle.UnpicklingError(
+                "Unsupported pickle version for ValidationProblem: %d" % (ver,))
+
     def __repr__(self):
         try:
             msg = " (" + problem_message(self) + ")"
@@ -265,6 +297,26 @@
         else:
             self.is_sub_root = is_sub_root
 
+    def __reduce_ex__(self, proto):
+        rv = super().__reduce_ex__(proto)
+        #assert False, repr(rv)
+        print("RRRRRRR\n\n", repr(rv))
+        assert False, repr(rv)
+        return rv
+
+    def __getstate__(self):
+        return (1, self.parent, self.is_sub_root)
+
+    def __setstate__(self, state):
+        ver = state[0]
+        if ver == 1:
+            _dummy, self.parent, self.is_sub_root = state
+            if self.parent is None:
+                self._schema_cache = {}
+        else:
+            raise pickle.UnpicklingError(
+                "Unsupported pickle version for _Schema: %d" % (ver,))
+
     @property
     def ROOT(self):
         """Get the root schema"""
@@ -397,6 +449,19 @@
         self._current_object = current_object
         self._settings = settings
 
+    def __getstate__(self):
+        return (1, self._parent, self._key, self._key_index, self._index,
+                self.root_object, self.root_schema,
+                self._current_object, self._settings)
+
+    def __setstate__(self, state):
+        ver = state[0]
+        if ver == 1:
+            _dummy, self._parent, self._key, self._key_index, self._index, self.root_object, self.root_schema, self._current_object, self._settings  = state
+        else:
+            raise pickle.UnpicklingError(
+                "Unsupported pickle version for _Context: %d" % (ver,))
+
     @property
     def parent(self):
         return self._parent