annotate data_schema/__init__.py @ 45:8a560e0c3180

Implement __ne__ also where __eq__ is implemented
author Franz Glasner <f.glasner@feldmann-mg.com>
date Wed, 02 Aug 2023 13:58:25 +0200
parents ea8c2d01a9d9
children 92ae1e882cef
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
2 r"""
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
3 Object schema validation support.
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
4
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
5 Somewhat modelled after JSON schema.
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
6
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
7 .. seealso:: https://json-schema.org/understanding-json-schema/index.html
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
8
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
9 :Author: Franz Glasner <fzglas.hg@dom66.de>
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
10 :Copyright: \(c) 2023 Franz Glasner
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
11 :License: BSD 3-Clause "New" or "Revised" License.
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
12 See :ref:`LICENSE.txt <license>` for details.
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
13 :ID: @(#) $Header$
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
14
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
15 """
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
16
37
ef586378f79a Prepare for development of v0.3: v0.3.dev1
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 34
diff changeset
17 __version__ = "0.3.dev1"
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
18
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
19 __revision__ = "|VCSRevision|"
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
20
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
21 __date__ = "|VCSJustDate|"
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
22
16
2a9e7c4b717e Problem severity now is also an enum
Franz Glasner <fzglas.hg@dom66.de>
parents: 15
diff changeset
23 __all__ = ["SEVERITY", "ERRORS", "WARNINGS",
17
65f937fb8de7 Put the computation of the default problem severity into a new public function
Franz Glasner <fzglas.hg@dom66.de>
parents: 16
diff changeset
24 "problem_message", "problem_severity",
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
25 "ValidationProblem", "SchemaError",
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
26 "validate",
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
27 "log_problem_cause"]
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
28
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
29
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
30 import ast
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
31 import collections
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
32 import copy
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
33 import datetime
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
34 import enum
44
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
35 import pickle
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
36 import re
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
37 import urllib.parse
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
38
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
39 import rfc3986
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
40
28
db3491e1b590 Allow to customize the loading of the schema dict
Franz Glasner <fzglas.hg@dom66.de>
parents: 25
diff changeset
41 try:
db3491e1b590 Allow to customize the loading of the schema dict
Franz Glasner <fzglas.hg@dom66.de>
parents: 25
diff changeset
42 from configmix.yaml import load as default_schema_loader
db3491e1b590 Allow to customize the loading of the schema dict
Franz Glasner <fzglas.hg@dom66.de>
parents: 25
diff changeset
43 except ImportError:
db3491e1b590 Allow to customize the loading of the schema dict
Franz Glasner <fzglas.hg@dom66.de>
parents: 25
diff changeset
44 default_schema_loader = None
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
45
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
46 from .util import get_data_stream
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
47
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
48
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
49 def NC_(ctx, msg):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
50 """Mimimum dummy translation support"""
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
51 return msg
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
52
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
53
16
2a9e7c4b717e Problem severity now is also an enum
Franz Glasner <fzglas.hg@dom66.de>
parents: 15
diff changeset
54 @enum.unique
2a9e7c4b717e Problem severity now is also an enum
Franz Glasner <fzglas.hg@dom66.de>
parents: 15
diff changeset
55 class SEVERITY(enum.IntEnum):
2a9e7c4b717e Problem severity now is also an enum
Franz Glasner <fzglas.hg@dom66.de>
parents: 15
diff changeset
56 INFO = 20
2a9e7c4b717e Problem severity now is also an enum
Franz Glasner <fzglas.hg@dom66.de>
parents: 15
diff changeset
57 WARNING = 30
2a9e7c4b717e Problem severity now is also an enum
Franz Glasner <fzglas.hg@dom66.de>
parents: 15
diff changeset
58 ERROR = 40
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
59
44
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
60 def __reduce_ex__(self, protocol):
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
61 return (getattr, (self.__class__, self._name_))
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
62
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
63
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
64 @enum.unique
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
65 class ERRORS(enum.Enum):
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
66 E10000 = NC_("schema-msg", "dict expected")
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
67 E10001 = NC_("schema-msg", "list expected")
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
68 E10002 = NC_("schema-msg", "string expected")
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
69 E10003 = NC_("schema-msg", "dict key must be a string")
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
70 E10004 = NC_("schema-msg", "additional key encountered")
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
71 E10005 = NC_("schema-msg", "required key(s) missing")
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
72 E10006 = NC_("schema-msg", "min string length encountered")
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
73 E10007 = NC_("schema-msg", "max string length exceeded")
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
74 E10008 = NC_("schema-msg", "string value does not match the required RE pattern")
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
75 E10009 = NC_("schema-msg", "string value does not validate")
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
76 E10010 = NC_("schema-msg", "validation error")
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
77 E10011 = NC_("schema-msg", "None/Null object expected")
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
78 E10012 = NC_("schema-msg", "min list length encountered")
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
79 E10013 = NC_("schema-msg", "max list length exceeded")
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
80 E10014 = NC_("schema-msg", "tuple expected")
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
81 E10015 = NC_("schema-msg", "min tuple length encountered")
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
82 E10016 = NC_("schema-msg", "max tuple length exceeded")
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
83 E10017 = NC_("schema-msg", "additional items in tuple not allowed")
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
84 E10018 = NC_("schema-msg", "object is not empty")
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
85 E10019 = NC_("schema-msg", "more than one match in `one-of' detected")
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
86 E10020 = NC_("schema-msg", "int expected")
19
c3a0fe8d4587 Consistent casing of schema items: all lowercase with dash as separator
Franz Glasner <fzglas.hg@dom66.de>
parents: 17
diff changeset
87 E10021 = NC_("schema-msg", "int value lower than min-value")
c3a0fe8d4587 Consistent casing of schema items: all lowercase with dash as separator
Franz Glasner <fzglas.hg@dom66.de>
parents: 17
diff changeset
88 E10022 = NC_("schema-msg", "int value greater than max-value")
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
89 E10023 = NC_("schema-msg", "float expected")
19
c3a0fe8d4587 Consistent casing of schema items: all lowercase with dash as separator
Franz Glasner <fzglas.hg@dom66.de>
parents: 17
diff changeset
90 E10024 = NC_("schema-msg", "float value lower than min-value")
c3a0fe8d4587 Consistent casing of schema items: all lowercase with dash as separator
Franz Glasner <fzglas.hg@dom66.de>
parents: 17
diff changeset
91 E10025 = NC_("schema-msg", "float value greater than max-value")
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
92 E10026 = NC_("schema-msg", "boolean value expected")
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
93 E10027 = NC_("schema-msg", "boolean true expected")
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
94 E10028 = NC_("schema-msg", "boolean false expected")
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
95 E10029 = NC_("schema-msg", "`not' expected problems but got none")
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
96 E10030 = NC_("schema-msg", "numeric type (int or float) expected")
19
c3a0fe8d4587 Consistent casing of schema items: all lowercase with dash as separator
Franz Glasner <fzglas.hg@dom66.de>
parents: 17
diff changeset
97 E10031 = NC_("schema-msg", "numeric value lower than min-value")
c3a0fe8d4587 Consistent casing of schema items: all lowercase with dash as separator
Franz Glasner <fzglas.hg@dom66.de>
parents: 17
diff changeset
98 E10032 = NC_("schema-msg", "numeric value greater than max-value")
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
99 E10033 = NC_("schema-msg", "a plain scalar value expected")
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
100 E10034 = NC_("schema-msg", "dict key does not match required schema")
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
101 E10035 = NC_("schema-msg", "binary data expected")
19
c3a0fe8d4587 Consistent casing of schema items: all lowercase with dash as separator
Franz Glasner <fzglas.hg@dom66.de>
parents: 17
diff changeset
102 E10036 = NC_("schema-msg", "length of binary data lower than min-value")
c3a0fe8d4587 Consistent casing of schema items: all lowercase with dash as separator
Franz Glasner <fzglas.hg@dom66.de>
parents: 17
diff changeset
103 E10037 = NC_("schema-msg", "length of binary data exceeds max-value")
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
104 E10038 = NC_("schema-msg", "a set is expected")
19
c3a0fe8d4587 Consistent casing of schema items: all lowercase with dash as separator
Franz Glasner <fzglas.hg@dom66.de>
parents: 17
diff changeset
105 E10039 = NC_("schema-msg", "length of set lower than min-length")
c3a0fe8d4587 Consistent casing of schema items: all lowercase with dash as separator
Franz Glasner <fzglas.hg@dom66.de>
parents: 17
diff changeset
106 E10040 = NC_("schema-msg", "length of set greater than max-length")
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
107 E10041 = NC_("schema-msg", "timestamp expected")
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
108 E10042 = NC_("schema-msg", "value of timestamp does not validate")
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
109 E10043 = NC_("schema-msg", "enumerated string value expected but not found")
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
110 E10044 = NC_("schema-msg", "referenced object doest not exist")
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
111 E10045 = NC_("schema-msg", "key is not contained in referenced object")
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
112 E10046 = NC_("schema-msg", "referenced object is not a container")
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
113 E10047 = NC_("schema-msg", "binary data does not match the required RE pattern")
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
114 E10048 = NC_("schema-msg", "enumerated integer value expected but not found")
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
115 E10049 = NC_("schema-msg", "enumerated number value expected but not found")
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
116 E10050 = NC_("schema-msg", "min dict length encountered")
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
117 E10051 = NC_("schema-msg", "max dict length exceeded")
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
118 E10052 = NC_("schema-msg", "index constraint violated")
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
119 E10053 = NC_("schema-msg", "`one-of' failed")
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
120 E10054 = NC_("schema-msg", "failing `one-of' item")
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
121 E10055 = NC_("schema-msg", "`any-of' failed")
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
122 E10056 = NC_("schema-msg", "failing `any-of' item")
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
123 E10057 = NC_("schema-msg", "`all-of' failed")
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
124 E10058 = NC_("schema-msg", "failing `all-of' item")
38
5a2fba996773 Implement a "forbidden" schema field for dicts
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 37
diff changeset
125 E10059 = NC_("schema-msg", "forbidden key detected")
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
126
44
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
127 def __reduce_ex__(self, protocol):
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
128 return (getattr, (self.__class__, self._name_))
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
129
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
130
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
131 @enum.unique
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
132 class WARNINGS(enum.Enum):
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
133 W80000 = NC_("schema-msg", "duplicate dict key")
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
134
44
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
135 def __reduce_ex__(self, protocol):
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
136 return (getattr, (self.__class__, self._name_))
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
137
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
138
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
139 # Check some invariants at import time
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
140 for e in ERRORS.__members__:
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
141 assert e.startswith('E'), "ERROR code `{}' shall start with letter `E'".format(e)
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
142 assert 10000 <= int(e[1:], 10) < 80000, "Invalid ERROR code number in `{}'".format(e)
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
143 for w in WARNINGS.__members__:
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
144 assert w.startswith('W'), "WARNING code `{}' must start with letter `W'".format(w)
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
145 assert 80000 <= int(w[1:], 10), "Invalid WARNING code number in `{}'".format(w)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
146
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
147
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
148 TYPE_RE = type(re.compile(r"\A.+\Z"))
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
149
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
150 _SENTINEL = object()
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
151
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
152 SCHEMA_REF_KEY = "$ref"
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
153 """Key name for schema references (like a symlink within a schema)"""
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
154
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
155 SCHEMA_PATH_ROOT = "$root"
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
156 """URI path to the root schema"""
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
157
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
158 SCHEMA_PATH_SELF = "$self"
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
159 """URI path to the current schema"""
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
160
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
161
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
162 def problem_message(pr):
17
65f937fb8de7 Put the computation of the default problem severity into a new public function
Franz Glasner <fzglas.hg@dom66.de>
parents: 16
diff changeset
163 """
65f937fb8de7 Put the computation of the default problem severity into a new public function
Franz Glasner <fzglas.hg@dom66.de>
parents: 16
diff changeset
164
65f937fb8de7 Put the computation of the default problem severity into a new public function
Franz Glasner <fzglas.hg@dom66.de>
parents: 16
diff changeset
165 :raises KeyError: the code in `pr` does not refer to
65f937fb8de7 Put the computation of the default problem severity into a new public function
Franz Glasner <fzglas.hg@dom66.de>
parents: 16
diff changeset
166 :class:`.ERRORS` or `.WARNINGS`
65f937fb8de7 Put the computation of the default problem severity into a new public function
Franz Glasner <fzglas.hg@dom66.de>
parents: 16
diff changeset
167
65f937fb8de7 Put the computation of the default problem severity into a new public function
Franz Glasner <fzglas.hg@dom66.de>
parents: 16
diff changeset
168 """
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
169 if isinstance(pr, ValidationProblem):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
170 code = getattr(pr, "code", None)
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
171 elif isinstance(pr, (ERRORS, WARNINGS)):
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
172 code = pr
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
173 else:
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
174 if pr >= 80000:
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
175 code = WARNINGS["W" + str(pr)]
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
176 else:
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
177 code = ERRORS["E" + str(pr)]
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
178 return code.value
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
179
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
180
17
65f937fb8de7 Put the computation of the default problem severity into a new public function
Franz Glasner <fzglas.hg@dom66.de>
parents: 16
diff changeset
181 def problem_severity(pr):
65f937fb8de7 Put the computation of the default problem severity into a new public function
Franz Glasner <fzglas.hg@dom66.de>
parents: 16
diff changeset
182 """Get the default severity for error or warning code `pr`
65f937fb8de7 Put the computation of the default problem severity into a new public function
Franz Glasner <fzglas.hg@dom66.de>
parents: 16
diff changeset
183
65f937fb8de7 Put the computation of the default problem severity into a new public function
Franz Glasner <fzglas.hg@dom66.de>
parents: 16
diff changeset
184 :raises TypeError: if `pr` is not in :class:`.ERRORS` or :class:`.WARNINGS`
65f937fb8de7 Put the computation of the default problem severity into a new public function
Franz Glasner <fzglas.hg@dom66.de>
parents: 16
diff changeset
185
65f937fb8de7 Put the computation of the default problem severity into a new public function
Franz Glasner <fzglas.hg@dom66.de>
parents: 16
diff changeset
186 """
65f937fb8de7 Put the computation of the default problem severity into a new public function
Franz Glasner <fzglas.hg@dom66.de>
parents: 16
diff changeset
187 if pr in ERRORS:
65f937fb8de7 Put the computation of the default problem severity into a new public function
Franz Glasner <fzglas.hg@dom66.de>
parents: 16
diff changeset
188 return SEVERITY.ERROR
65f937fb8de7 Put the computation of the default problem severity into a new public function
Franz Glasner <fzglas.hg@dom66.de>
parents: 16
diff changeset
189 if pr in WARNINGS:
65f937fb8de7 Put the computation of the default problem severity into a new public function
Franz Glasner <fzglas.hg@dom66.de>
parents: 16
diff changeset
190 return SEVERITY.WARNING
65f937fb8de7 Put the computation of the default problem severity into a new public function
Franz Glasner <fzglas.hg@dom66.de>
parents: 16
diff changeset
191 raise TypeError("invalid error or warning code: %r" % (pr, ))
65f937fb8de7 Put the computation of the default problem severity into a new public function
Franz Glasner <fzglas.hg@dom66.de>
parents: 16
diff changeset
192
65f937fb8de7 Put the computation of the default problem severity into a new public function
Franz Glasner <fzglas.hg@dom66.de>
parents: 16
diff changeset
193
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
194 class ValidationProblem(object):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
195
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
196 __slots__ = ("code", "severity", "hint", "context", "cause", "index")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
197
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
198 def __init__(self,
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
199 code=None,
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
200 severity=None,
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
201 hint=None,
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
202 context=None,
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
203 cause=None,
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
204 index=None):
14
cfb97c7c9e5b Simplify ctor of ValidationProblem somewhat
Franz Glasner <fzglas.hg@dom66.de>
parents: 13
diff changeset
205 if code is None:
15
696b83f29363 Use enum props when printing validation problems
Franz Glasner <fzglas.hg@dom66.de>
parents: 14
diff changeset
206 raise TypeError("`code' must be given")
14
cfb97c7c9e5b Simplify ctor of ValidationProblem somewhat
Franz Glasner <fzglas.hg@dom66.de>
parents: 13
diff changeset
207 # check validity
cfb97c7c9e5b Simplify ctor of ValidationProblem somewhat
Franz Glasner <fzglas.hg@dom66.de>
parents: 13
diff changeset
208 if code not in ERRORS and code not in WARNINGS:
cfb97c7c9e5b Simplify ctor of ValidationProblem somewhat
Franz Glasner <fzglas.hg@dom66.de>
parents: 13
diff changeset
209 raise ValueError(
cfb97c7c9e5b Simplify ctor of ValidationProblem somewhat
Franz Glasner <fzglas.hg@dom66.de>
parents: 13
diff changeset
210 "unknown validation error code: {}".format(code))
cfb97c7c9e5b Simplify ctor of ValidationProblem somewhat
Franz Glasner <fzglas.hg@dom66.de>
parents: 13
diff changeset
211 self.code = code
cfb97c7c9e5b Simplify ctor of ValidationProblem somewhat
Franz Glasner <fzglas.hg@dom66.de>
parents: 13
diff changeset
212 if severity is None:
17
65f937fb8de7 Put the computation of the default problem severity into a new public function
Franz Glasner <fzglas.hg@dom66.de>
parents: 16
diff changeset
213 self.severity = problem_severity(code)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
214 else:
16
2a9e7c4b717e Problem severity now is also an enum
Franz Glasner <fzglas.hg@dom66.de>
parents: 15
diff changeset
215 if not isinstance(severity, SEVERITY):
2a9e7c4b717e Problem severity now is also an enum
Franz Glasner <fzglas.hg@dom66.de>
parents: 15
diff changeset
216 raise TypeError("invalid type for `severity'")
14
cfb97c7c9e5b Simplify ctor of ValidationProblem somewhat
Franz Glasner <fzglas.hg@dom66.de>
parents: 13
diff changeset
217 self.severity = severity
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
218 self.hint = hint
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
219 self.context = context
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
220 if cause:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
221 if not isinstance(cause, (list, tuple, set, frozenset)):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
222 cause = (cause, )
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
223 for c in cause:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
224 if not isinstance(c, ValidationProblem):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
225 raise SchemaError(
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
226 "can only nest other `ValidationProblem' instances")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
227 self.cause = cause
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
228 self.index = index
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
229
44
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
230 def __eq__(self, other):
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
231 if not isinstance(other, ValidationProblem):
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
232 return NotImplemented
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
233 return ((self.code == other.code)
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
234 and (self.severity == other.severity)
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
235 and (self.hint == other.hint)
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
236 and (self.context == other.context)
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
237 and (self.cause == other.cause)
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
238 and (self.index == other.index))
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
239
45
8a560e0c3180 Implement __ne__ also where __eq__ is implemented
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 44
diff changeset
240 def __ne__(self, other):
8a560e0c3180 Implement __ne__ also where __eq__ is implemented
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 44
diff changeset
241 #
8a560e0c3180 Implement __ne__ also where __eq__ is implemented
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 44
diff changeset
242 # While the default in Python3 is sensible implementing is recommended
8a560e0c3180 Implement __ne__ also where __eq__ is implemented
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 44
diff changeset
243 # when a built-in __eq__ is overwritten (Raymond Hettinger).
8a560e0c3180 Implement __ne__ also where __eq__ is implemented
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 44
diff changeset
244 #
8a560e0c3180 Implement __ne__ also where __eq__ is implemented
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 44
diff changeset
245 # Do not use not self == other because NotImplemented is not handled
8a560e0c3180 Implement __ne__ also where __eq__ is implemented
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 44
diff changeset
246 # properly in some early Python versions (including Py2).
8a560e0c3180 Implement __ne__ also where __eq__ is implemented
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 44
diff changeset
247 #
8a560e0c3180 Implement __ne__ also where __eq__ is implemented
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 44
diff changeset
248 equal = self.__eq__(other)
8a560e0c3180 Implement __ne__ also where __eq__ is implemented
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 44
diff changeset
249 return NotImplemented if equal is NotImplemented else not equal
8a560e0c3180 Implement __ne__ also where __eq__ is implemented
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 44
diff changeset
250
44
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
251 def __getstate__(self):
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
252 return (1, self.code, self.severity, self.hint, self.context,
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
253 self.cause, self.index)
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
254
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
255 def __setstate__(self, state):
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
256 ver = state[0]
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
257 if ver == 1:
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
258 _dummy, self.code, self.severity, self.hint, self.context, self.cause, self.index = state
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
259 else:
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
260 raise pickle.UnpicklingError(
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
261 "Unsupported pickle version for ValidationProblem: %d" % (ver,))
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
262
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
263 def __repr__(self):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
264 try:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
265 msg = " (" + problem_message(self) + ")"
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
266 except LookupError:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
267 msg = ""
30
2e7c08c356ee Test the the ValidationProblem's str() and repr()
Franz Glasner <fzglas.hg@dom66.de>
parents: 29
diff changeset
268 if self.context is not None:
2e7c08c356ee Test the the ValidationProblem's str() and repr()
Franz Glasner <fzglas.hg@dom66.de>
parents: 29
diff changeset
269 context_depth = self.context.depth
2e7c08c356ee Test the the ValidationProblem's str() and repr()
Franz Glasner <fzglas.hg@dom66.de>
parents: 29
diff changeset
270 else:
2e7c08c356ee Test the the ValidationProblem's str() and repr()
Franz Glasner <fzglas.hg@dom66.de>
parents: 29
diff changeset
271 context_depth = None
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
272 if self.index is None:
16
2a9e7c4b717e Problem severity now is also an enum
Franz Glasner <fzglas.hg@dom66.de>
parents: 15
diff changeset
273 return "ValidationProblem(code={}{}, severity={}, hint={}, context=[depth={}]{})".format(
30
2e7c08c356ee Test the the ValidationProblem's str() and repr()
Franz Glasner <fzglas.hg@dom66.de>
parents: 29
diff changeset
274 self.code.name, msg, self.severity.name, self.hint, context_depth, self.context)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
275 else:
16
2a9e7c4b717e Problem severity now is also an enum
Franz Glasner <fzglas.hg@dom66.de>
parents: 15
diff changeset
276 return "ValidationProblem(code={}{}, severity={}, hint={}, context=[depth={}]{}, index={})".format(
30
2e7c08c356ee Test the the ValidationProblem's str() and repr()
Franz Glasner <fzglas.hg@dom66.de>
parents: 29
diff changeset
277 self.code.name, msg, self.severity.name, self.hint, context_depth, self.context, self.index)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
278
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
279
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
280 class SchemaError(Exception):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
281 """An error within the schema itself"""
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
282 pass
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
283
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
284
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
285 ValidationSettings = collections.namedtuple(
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
286 "ValidationSettings",
29
68286d27f27d FIX: Allow customization of the data stream loader (get_data_stream())
Franz Glasner <fzglas.hg@dom66.de>
parents: 28
diff changeset
287 ["skip_keys", "break_on_keynames_problems",
68286d27f27d FIX: Allow customization of the data stream loader (get_data_stream())
Franz Glasner <fzglas.hg@dom66.de>
parents: 28
diff changeset
288 "data_stream_loader", "schema_loader"])
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
289
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
290
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
291 class _Schema(dict):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
292
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
293 __slots__ = ("parent", "is_sub_root", "_schema_cache")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
294
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
295 def __init__(self, parent, is_sub_root, *args, **kwds):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
296 super().__init__(*args, **kwds)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
297 if parent is None or isinstance(parent, _Schema):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
298 self.parent = parent
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
299 else:
25
fd2a40c3d87b Enhance exception message
Franz Glasner <fzglas.hg@dom66.de>
parents: 23
diff changeset
300 raise TypeError(
fd2a40c3d87b Enhance exception message
Franz Glasner <fzglas.hg@dom66.de>
parents: 23
diff changeset
301 "`_Schema' or `None' expected for `parent` argument")
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
302 if parent is None:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
303 self._schema_cache = {}
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
304 if not is_sub_root:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
305 raise ValueError(
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
306 "the root schmema must be a sub-root (aka `$self') also")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
307 self.is_sub_root = True
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
308 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
309 self.is_sub_root = is_sub_root
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
310
44
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
311 def __reduce_ex__(self, proto):
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
312 rv = super().__reduce_ex__(proto)
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
313 #assert False, repr(rv)
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
314 print("RRRRRRR\n\n", repr(rv))
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
315 assert False, repr(rv)
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
316 return rv
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
317
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
318 def __getstate__(self):
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
319 return (1, self.parent, self.is_sub_root)
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
320
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
321 def __setstate__(self, state):
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
322 ver = state[0]
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
323 if ver == 1:
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
324 _dummy, self.parent, self.is_sub_root = state
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
325 if self.parent is None:
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
326 self._schema_cache = {}
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
327 else:
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
328 raise pickle.UnpicklingError(
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
329 "Unsupported pickle version for _Schema: %d" % (ver,))
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
330
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
331 @property
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
332 def ROOT(self):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
333 """Get the root schema"""
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
334 r = self
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
335 while r.parent is not None:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
336 r = r.parent
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
337 return r
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
338
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
339 @property
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
340 def SELF(self):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
341 r = self
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
342 while not r.is_sub_root:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
343 r = r.parent
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
344 return r
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
345
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
346 def copy(self):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
347 return _Schema(self.parent, self.is_sub_root, self)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
348
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
349 def get_child(self, name, default=None):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
350 return self.ensure_child_schema(self.get(name, default))
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
351
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
352 def ensure_child_schema(self, v):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
353 if v is None:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
354 return None
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
355 elif isinstance(v, _Schema):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
356 return v
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
357 elif isinstance(v, dict):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
358 return _Schema(self, False, v)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
359 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
360 return v
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
361
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
362 def ensure_list_of_child_schemata(self, v):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
363 if isinstance(v, (list, tuple)):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
364 return [_Schema(self, False, i) for i in v]
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
365 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
366 return v
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
367
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
368 def __eq__(self, other):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
369 if not isinstance(other, _Schema):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
370 return NotImplemented
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
371 return (self.parent is other.parent
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
372 and bool(self.is_sub_root) == bool(other.is_sub_root)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
373 and dict(self) == dict(other))
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
374
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
375 def __ne__(self, other):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
376 #
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
377 # While the default in Python3 is sensible implementing is recommended
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
378 # when a built-in __eq__ is overwritten (Raymond Hettinger).
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
379 #
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
380 # Do not use not self == other because NotImplemented is not handled
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
381 # properly in some early Python versions (including Py2).
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
382 #
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
383 equal = self.__eq__(other)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
384 return NotImplemented if equal is NotImplemented else not equal
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
385
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
386 def __copy__(self):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
387 return _Schema(self.parent, self.is_sub_root, self)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
388
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
389 def __deepcopy__(self, memo):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
390 return _Schema(self.parent,
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
391 self.is_sub_root,
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
392 copy.deepcopy(dict(self), memo))
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
393
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
394 def __str__(self):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
395 return "<_Schema " + super().__str__() + ">"
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
396
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
397 def __repr__(self):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
398 return "<_Schema " + super().__repr__() + ">"
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
399
29
68286d27f27d FIX: Allow customization of the data stream loader (get_data_stream())
Franz Glasner <fzglas.hg@dom66.de>
parents: 28
diff changeset
400 def get_cached_schema(self, key, load_if_needed=True,
68286d27f27d FIX: Allow customization of the data stream loader (get_data_stream())
Franz Glasner <fzglas.hg@dom66.de>
parents: 28
diff changeset
401 data_stream_loader=None,
68286d27f27d FIX: Allow customization of the data stream loader (get_data_stream())
Franz Glasner <fzglas.hg@dom66.de>
parents: 28
diff changeset
402 schema_loader=None):
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
403 root = self.ROOT
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
404 s = root._schema_cache.get(key, None)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
405 if s is None and load_if_needed:
28
db3491e1b590 Allow to customize the loading of the schema dict
Franz Glasner <fzglas.hg@dom66.de>
parents: 25
diff changeset
406 if schema_loader is None:
db3491e1b590 Allow to customize the loading of the schema dict
Franz Glasner <fzglas.hg@dom66.de>
parents: 25
diff changeset
407 raise SchemaError("no schema loader available")
29
68286d27f27d FIX: Allow customization of the data stream loader (get_data_stream())
Franz Glasner <fzglas.hg@dom66.de>
parents: 28
diff changeset
408 dsl = data_stream_loader or get_data_stream
68286d27f27d FIX: Allow customization of the data stream loader (get_data_stream())
Franz Glasner <fzglas.hg@dom66.de>
parents: 28
diff changeset
409 with dsl(key) as schemastream:
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
410 # load schema a new `$self' (i.e. sub-root is True)
28
db3491e1b590 Allow to customize the loading of the schema dict
Franz Glasner <fzglas.hg@dom66.de>
parents: 25
diff changeset
411 s = _Schema(self, True, schema_loader(schemastream))
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
412 root._schema_cache[key] = s
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
413 return s
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
414
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
415 def add_cached_schema(self, key, schema):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
416 r = self.ROOT
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
417 assert isinstance(schema, _Schema)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
418 r._schema_cache[key] = schema
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
419
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
420
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
421 class Context(object):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
422
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
423 __slots__ = ("_parent", "_key", "_key_index",
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
424 "_index",
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
425 "root_object", "root_schema",
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
426 "_current_object",
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
427 "_settings")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
428
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
429 def __init__(self, parent, *, key=_SENTINEL, index=_SENTINEL,
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
430 root_object=_SENTINEL, root_schema=_SENTINEL,
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
431 current_object=_SENTINEL,
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
432 settings=_SENTINEL,
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
433 key_index=_SENTINEL):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
434 if parent is None:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
435 if key is not _SENTINEL:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
436 raise TypeError("the root context may not have a key")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
437 if index is not _SENTINEL:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
438 raise TypeError("the root context may not have an index")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
439 if settings is _SENTINEL:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
440 raise TypeError("the root context must have settings")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
441 self.root_object = root_object
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
442 if current_object is _SENTINEL:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
443 current_object = root_object
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
444 self.root_schema = root_schema
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
445 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
446 if key is _SENTINEL and index is _SENTINEL:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
447 raise TypeError("one of `key` and `index` must be given in a non-root context")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
448 if root_object is not _SENTINEL:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
449 raise TypeError("non-root context may not have a root object")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
450 if root_schema is not _SENTINEL:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
451 raise TypeError("non-root context may not have a root schema")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
452 if key is not _SENTINEL and index is not _SENTINEL:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
453 raise ValueError("only one of `key` and `index` may be given in a context")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
454 if key_index is not _SENTINEL and key is _SENTINEL:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
455 raise ValueError("when having a `key_index` a `key` also must be given")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
456 self._parent = parent
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
457 self._key = key
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
458 self._key_index = key_index
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
459 self._index = index
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
460 self._current_object = current_object
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
461 self._settings = settings
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
462
44
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
463 def __getstate__(self):
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
464 return (1, self._parent, self._key, self._key_index, self._index,
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
465 self.root_object, self.root_schema,
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
466 self._current_object, self._settings)
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
467
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
468 def __setstate__(self, state):
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
469 ver = state[0]
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
470 if ver == 1:
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
471 _dummy, self._parent, self._key, self._key_index, self._index, self.root_object, self.root_schema, self._current_object, self._settings = state
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
472 else:
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
473 raise pickle.UnpicklingError(
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
474 "Unsupported pickle version for _Context: %d" % (ver,))
ea8c2d01a9d9 Begin pickling support for ValidatenProblems, _Schema and Context
Franz Glasner <fzglas.hg@dom66.de>
parents: 40
diff changeset
475
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
476 @property
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
477 def parent(self):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
478 return self._parent
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
479
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
480 @property
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
481 def safe_parent(self):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
482 if self.is_root:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
483 raise TypeError("the root context has no parent")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
484 return self.parent
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
485
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
486 @property
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
487 def root(self):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
488 """Get the root context"""
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
489 ctx = self
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
490 while not ctx.is_root:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
491 ctx = ctx.parent
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
492 return ctx
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
493
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
494 @property
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
495 def is_root(self):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
496 return not bool(self.parent)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
497
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
498 @property
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
499 def key(self):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
500 if self._key is _SENTINEL:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
501 raise AttributeError("no `key' in Context")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
502 return self._key
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
503
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
504 @property
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
505 def index(self):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
506 if self._index is _SENTINEL:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
507 raise AttributeError("no `index' in Context")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
508 return self._index
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
509
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
510 @property
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
511 def key_index(self):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
512 if self._key_index is _SENTINEL:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
513 raise AttributeError("no `key_index' in Context")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
514 return self._key_index
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
515
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
516 @property
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
517 def current_object(self):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
518 if self._current_object is _SENTINEL:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
519 raise AttributeError("no `current_object' in Context")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
520 return self._current_object
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
521
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
522 @property
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
523 def settings(self):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
524 s = self._settings
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
525 return s if s is not _SENTINEL else self.parent.settings
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
526
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
527 @property
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
528 def depth(self):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
529 if self._key is _SENTINEL and self._index is _SENTINEL and self.is_root:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
530 return 0
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
531 n = 0
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
532 ctx = self
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
533 while not ctx.is_root:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
534 n += 1
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
535 ctx = ctx.parent
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
536 return n
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
537
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
538 def __str__(self):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
539 if self._key is _SENTINEL and self._index is _SENTINEL and self.is_root:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
540 return "<ROOT>"
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
541 chain = []
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
542 ctx = self
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
543 while not ctx.is_root:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
544 if ctx._key is not _SENTINEL:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
545 chain.append(str(ctx.key))
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
546 elif ctx._index is not _SENTINEL:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
547 chain.append("INDEX:{}".format(ctx.index))
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
548 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
549 chain.append("")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
550 ctx = ctx.parent
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
551 chain.reverse()
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
552 return " / ".join(chain)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
553
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
554 def __repr__(self):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
555 return "<Context path=`{}'>".format(str(self))
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
556
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
557
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
558 def _get_one_of(d, *keys, default=None, strict=True):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
559 """Get the first found key and its value of `keys` from dict `d`.
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
560
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
561 """
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
562 for k in keys:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
563 v = d.get(k, _SENTINEL)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
564 if v is not _SENTINEL:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
565 if strict:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
566 #
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
567 # check that all no other key of `keys` besides of `k` is
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
568 # in `d`
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
569 #
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
570 other_keys = set(keys)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
571 other_keys.remove(k)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
572 for k2 in other_keys:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
573 if k2 in d:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
574 raise SchemaError("ambiguous key from: {}".format(
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
575 ", ".join(keys)))
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
576 return k, v
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
577 return None, default
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
578
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
579
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
580 def validate(obj, schema, **kwds):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
581 """Validate object `obj` against the *specific* schema `schema`.
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
582
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
583 Yields errors and warnings
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
584
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
585 """
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
586 settings = {
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
587 "skip_keys": None,
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
588 "break_on_keynames_problems": True,
29
68286d27f27d FIX: Allow customization of the data stream loader (get_data_stream())
Franz Glasner <fzglas.hg@dom66.de>
parents: 28
diff changeset
589 "data_stream_loader": get_data_stream,
28
db3491e1b590 Allow to customize the loading of the schema dict
Franz Glasner <fzglas.hg@dom66.de>
parents: 25
diff changeset
590 "schema_loader": default_schema_loader
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
591 }
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
592 settings.update(kwds)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
593 if not isinstance(schema, _Schema):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
594 if not isinstance(schema, dict):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
595 raise SchemaError("Schema must be a dict-alike."
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
596 " Got: {!r}".format(schema))
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
597 schema = _Schema(None, True, schema)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
598 context = Context(None, root_object=obj, root_schema=schema,
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
599 settings=ValidationSettings(**settings))
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
600 yield from _validate(obj, schema, context, is_root=True)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
601
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
602
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
603 def _validate(obj, schema, context, is_root=False):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
604 """Validate object `obj` against the *specific* schema `schema`.
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
605
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
606 Yields errors and warnings
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
607
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
608 """
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
609 if not isinstance(schema, _Schema):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
610 raise SchemaError("Schema must be a `_Schema'."
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
611 " Got: {!r}. Context: {!s}".format(schema, context))
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
612 # 1. Process "cond" or "match"
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
613 schema = process_schema_conditionals(schema, context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
614 # 2. Process "$ref" schema references
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
615 schema = process_schema_references(
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
616 schema, context, check_single_ref_key=not is_root)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
617
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
618 # 3. Real validation
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
619
31
271ec3abdfa3 Use "$type" as the schema's type specifier instead of "type".
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 30
diff changeset
620 # check combinator shortcuts without "$type" indirection
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
621 combinator, combinator_schema = _get_one_of(
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
622 schema, "not", "all-of", "any-of", "one-of")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
623 if combinator is None:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
624 try:
31
271ec3abdfa3 Use "$type" as the schema's type specifier instead of "type".
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 30
diff changeset
625 t = schema["$type"]
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
626 except KeyError:
31
271ec3abdfa3 Use "$type" as the schema's type specifier instead of "type".
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 30
diff changeset
627 raise SchemaError("Schema has no `$type' key: {!r}."
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
628 " Context: {!s}".format(schema, context))
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
629 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
630 #
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
631 # Construct a temporary schema with the proper indirection for
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
632 # the check below
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
633 #
31
271ec3abdfa3 Use "$type" as the schema's type specifier instead of "type".
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 30
diff changeset
634 t = {"$type": {combinator: combinator_schema}}
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
635 if combinator_schema is None:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
636 raise SchemaError("a combinator requires a child")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
637 if callable(t):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
638 yield from t(obj, schema, context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
639 elif t is None:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
640 yield from validate_null(obj, schema, context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
641 elif isinstance(t, dict):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
642 if len(t) != 1:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
643 raise SchemaError("type dict must be of length 1")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
644 # Check whether a shortcut is already seen above
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
645 if combinator is None:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
646 combinator = list(t.keys())[0]
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
647 combinator_schema = t[combinator]
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
648 if combinator == "not":
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
649 yield from validate_not(
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
650 obj, schema.ensure_child_schema(combinator_schema), context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
651 elif combinator == "all-of":
23
413b344be2d1 Consistent naming of validation methods: all lowercase
Franz Glasner <fzglas.hg@dom66.de>
parents: 19
diff changeset
652 yield from validate_all_of(
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
653 obj,
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
654 schema.ensure_list_of_child_schemata(combinator_schema),
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
655 context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
656 elif combinator == "any-of":
23
413b344be2d1 Consistent naming of validation methods: all lowercase
Franz Glasner <fzglas.hg@dom66.de>
parents: 19
diff changeset
657 yield from validate_any_of(
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
658 obj,
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
659 schema.ensure_list_of_child_schemata(combinator_schema),
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
660 context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
661 elif combinator == "one-of":
23
413b344be2d1 Consistent naming of validation methods: all lowercase
Franz Glasner <fzglas.hg@dom66.de>
parents: 19
diff changeset
662 yield from validate_one_of(
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
663 obj,
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
664 schema.ensure_list_of_child_schemata(combinator_schema),
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
665 context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
666 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
667 raise SchemaError("unknown combinator: {}".format(combinator))
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
668 elif isinstance(t, (list, tuple)):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
669 # a simple list is "any-of"
23
413b344be2d1 Consistent naming of validation methods: all lowercase
Franz Glasner <fzglas.hg@dom66.de>
parents: 19
diff changeset
670 yield from validate_any_of(
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
671 obj, schema.ensure_list_of_child_schemata(t), context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
672 elif t in ("dict", "map", "object"):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
673 yield from validate_dict(obj, schema, context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
674 elif t in ("list", "array",):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
675 yield from validate_list(obj, schema, context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
676 elif t in ("tuple", "record"):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
677 yield from validate_tuple(obj, schema, context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
678 elif t in ("set", "frozenset"):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
679 yield from validate_set(obj, schema, context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
680 elif t in ("string", "str"):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
681 yield from validate_str(obj, schema, context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
682 elif t in ("deny", ):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
683 yield from validate_deny(obj, schema, context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
684 elif t in ("accept", ):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
685 yield from validate_accept(obj, schema, context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
686 elif t in ("none", "null", "nil"):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
687 yield from validate_null(obj, schema, context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
688 elif t in ("empty", ):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
689 yield from validate_empty(obj, schema, context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
690 elif t in ("integer", "int"):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
691 yield from validate_integer(obj, schema, context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
692 elif t in ("float", "real", "double"):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
693 yield from validate_float(obj, schema, context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
694 elif t in ("number", "num"):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
695 yield from validate_number(obj, schema, context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
696 elif t in ("bool", "boolean"):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
697 yield from validate_bool(obj, schema, context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
698 elif t in ("scalar", ):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
699 yield from validate_scalar(obj, schema, context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
700 elif t in ("binary", ):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
701 yield from validate_binary(obj, schema, context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
702 elif t in ("timestamp", "datetime"):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
703 yield from validate_timestamp(obj, schema, context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
704 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
705 raise SchemaError("unknown type in schema: {}".format(t))
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
706
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
707
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
708 def _is_in_skip_keys(key, skip_keys):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
709 if not skip_keys:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
710 return False
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
711 for sk in skip_keys:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
712 if isinstance(sk, str):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
713 if key == sk:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
714 return True
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
715 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
716 if sk.search(key):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
717 return True
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
718 return False
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
719
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
720
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
721 def _is_null_allowed_for_object(obj, schema, context):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
722 if obj is None and schema.get("nullable", False):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
723 return True
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
724 return False
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
725
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
726
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
727 def _validate_index_constraint(obj, schema, context):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
728 # No evaluation of index constraints for the root context
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
729 if context.is_root:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
730 return
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
731 try:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
732 index_constraints = schema["index-constraint"]
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
733 except KeyError:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
734 return # no constraints
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
735 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
736 if not isinstance(index_constraints, (list, tuple, set, frozenset)):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
737 index_constraints = [index_constraints]
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
738 if not index_constraints:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
739 return
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
740 parent = context.safe_parent
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
741 try:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
742 effective_index = context.index
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
743 except AttributeError:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
744 try:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
745 effective_index = context.key_index
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
746 except AttributeError:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
747 raise SchemaError("parent container has no usable index")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
748 for idx in index_constraints:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
749 if idx < 0:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
750 idx = len(parent.current_object) + idx
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
751 if idx == effective_index:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
752 break
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
753 else:
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
754 yield ValidationProblem(code=ERRORS.E10052, context=context)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
755
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
756
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
757 def validate_dict(obj, schema, context):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
758 if _is_null_allowed_for_object(obj, schema, context):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
759 return
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
760 if not isinstance(obj, dict):
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
761 yield ValidationProblem(code=ERRORS.E10000, hint="got: {}".format(type(obj).__name__), context=context)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
762 return
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
763 yield from _validate_index_constraint(obj, schema, context)
19
c3a0fe8d4587 Consistent casing of schema items: all lowercase with dash as separator
Franz Glasner <fzglas.hg@dom66.de>
parents: 17
diff changeset
764 minlen = schema.get("min-length", None)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
765 if minlen:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
766 if len(obj) < minlen:
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
767 yield ValidationProblem(code=ERRORS.E10050, hint=obj, context=context)
19
c3a0fe8d4587 Consistent casing of schema items: all lowercase with dash as separator
Franz Glasner <fzglas.hg@dom66.de>
parents: 17
diff changeset
768 maxlen = schema.get("max-length", None)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
769 if maxlen is not None:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
770 if len(obj) > maxlen:
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
771 yield ValidationProblem(code=ERRORS.E10051, hint=obj, context=context)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
772 schema_keys = schema.get("keys", {}) if schema else {}
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
773 seen_keys = set()
19
c3a0fe8d4587 Consistent casing of schema items: all lowercase with dash as separator
Franz Glasner <fzglas.hg@dom66.de>
parents: 17
diff changeset
774 schema_keynames = schema.get_child("key-names", None)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
775 idx = -1
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
776 for key, item in obj.items():
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
777 idx += 1
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
778 if schema_keynames is None:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
779 if not isinstance(key, str):
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
780 yield ValidationProblem(code=ERRORS.E10003, hint=repr(key), context=context)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
781 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
782 # validate the key against given schema
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
783 new_context = Context(context, key=key, key_index=idx, current_object=key)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
784 key_probs = list(_validate(key, schema_keynames, new_context))
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
785 if key_probs:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
786 yield ValidationProblem(
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
787 code=ERRORS.E10034, hint=key, context=context, cause=key_probs)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
788 if context.settings.break_on_keynames_problems:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
789 return
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
790 if key in seen_keys:
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
791 yield ValidationProblem(code=WARNINGS.W80000, hint=key, context=context)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
792 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
793 seen_keys.add(key)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
794 # XXX FIXME: context: new leaf context with new key for recursion
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
795 if key in schema_keys:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
796 new_context = Context(context, key=key, key_index=idx, current_object=item)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
797 yield from _validate(item, schema.ensure_child_schema(schema_keys[key]), new_context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
798 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
799 # check whether additional keys are allowed
19
c3a0fe8d4587 Consistent casing of schema items: all lowercase with dash as separator
Franz Glasner <fzglas.hg@dom66.de>
parents: 17
diff changeset
800 additional_keys = schema.get_child("additional-keys", False)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
801 if isinstance(additional_keys, bool):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
802 if not additional_keys:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
803 if not _is_in_skip_keys(key, context.settings.skip_keys):
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
804 yield ValidationProblem(code=ERRORS.E10004, hint=str(key), context=context)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
805 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
806 if not _is_in_skip_keys(key, context.settings.skip_keys):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
807 # try this as the common schema for all the additional keys
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
808 new_context = Context(context, key=key, key_index=idx, current_object=item)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
809 yield from _validate(item, additional_keys, new_context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
810 # check whether all required keys are seen
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
811 try:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
812 required_keys = set(schema.get("required", set()))
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
813 except (TypeError, ValueError):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
814 raise SchemaError("`required` must be an iterable")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
815 if not required_keys <= seen_keys:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
816 hs = [str(i) for i in required_keys - seen_keys]
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
817 yield ValidationProblem(code=ERRORS.E10005, hint=sorted(hs), context=context)
38
5a2fba996773 Implement a "forbidden" schema field for dicts
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 37
diff changeset
818 # check whether no forbidden keys are seen
5a2fba996773 Implement a "forbidden" schema field for dicts
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 37
diff changeset
819 try:
5a2fba996773 Implement a "forbidden" schema field for dicts
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 37
diff changeset
820 forbidden_keys = set(schema.get("forbidden", set()))
5a2fba996773 Implement a "forbidden" schema field for dicts
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 37
diff changeset
821 except (TypeError, ValueError):
5a2fba996773 Implement a "forbidden" schema field for dicts
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 37
diff changeset
822 raise SchemaError("`forbidden` must be an iterable")
5a2fba996773 Implement a "forbidden" schema field for dicts
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 37
diff changeset
823 if forbidden_keys & seen_keys:
5a2fba996773 Implement a "forbidden" schema field for dicts
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 37
diff changeset
824 hs = [str(i) for i in forbidden_keys & seen_keys]
5a2fba996773 Implement a "forbidden" schema field for dicts
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 37
diff changeset
825 yield ValidationProblem(code=ERRORS.E10059, hint=sorted(hs), context=context)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
826
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
827
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
828 def validate_list(obj, schema, context):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
829 if _is_null_allowed_for_object(obj, schema, context):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
830 return
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
831 if not isinstance(obj, (list, tuple)):
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
832 yield ValidationProblem(code=ERRORS.E10001, hint="got: {}".format(type(obj).__name__), context=context)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
833 return
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
834 yield from _validate_index_constraint(obj, schema, context)
19
c3a0fe8d4587 Consistent casing of schema items: all lowercase with dash as separator
Franz Glasner <fzglas.hg@dom66.de>
parents: 17
diff changeset
835 minlen = schema.get("min-length", None)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
836 if minlen:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
837 if len(obj) < minlen:
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
838 yield ValidationProblem(code=ERRORS.E10012, hint=obj, context=context)
19
c3a0fe8d4587 Consistent casing of schema items: all lowercase with dash as separator
Franz Glasner <fzglas.hg@dom66.de>
parents: 17
diff changeset
839 maxlen = schema.get("max-length", None)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
840 if maxlen is not None:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
841 if len(obj) > maxlen:
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
842 yield ValidationProblem(code=ERRORS.E10013, hint=obj, context=context)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
843 try:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
844 schema_items = schema.ensure_child_schema(schema["items"])
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
845 except KeyError:
31
271ec3abdfa3 Use "$type" as the schema's type specifier instead of "type".
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 30
diff changeset
846 schema_items = _Schema(schema, False, {"$type": validate_deny})
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
847 for idx, o in enumerate(obj):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
848 new_context = Context(parent=context, index=idx, current_object=o)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
849 yield from _validate(o, schema_items, new_context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
850
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
851
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
852 def validate_set(obj, schema, context):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
853 if _is_null_allowed_for_object(obj, schema, context):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
854 return
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
855 if not isinstance(obj, (set, frozenset)):
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
856 yield ValidationProblem(code=ERRORS.E10038, hint="got: {}".format(type(obj).__name__), context=context)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
857 return
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
858 yield from _validate_index_constraint(obj, schema, context)
19
c3a0fe8d4587 Consistent casing of schema items: all lowercase with dash as separator
Franz Glasner <fzglas.hg@dom66.de>
parents: 17
diff changeset
859 minlen = schema.get("min-length", None)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
860 if minlen:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
861 if len(obj) < minlen:
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
862 yield ValidationProblem(code=ERRORS.E10039, hint=obj, context=context)
19
c3a0fe8d4587 Consistent casing of schema items: all lowercase with dash as separator
Franz Glasner <fzglas.hg@dom66.de>
parents: 17
diff changeset
863 maxlen = schema.get("max-length", None)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
864 if maxlen is not None:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
865 if len(obj) > maxlen:
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
866 yield ValidationProblem(code=ERRORS.E10040, hint=obj, context=context)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
867 try:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
868 schema_items = schema.ensure_child_schema(schema["items"])
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
869 except KeyError:
31
271ec3abdfa3 Use "$type" as the schema's type specifier instead of "type".
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 30
diff changeset
870 schema_items = _Schema(schema, False, {"$type": validate_deny})
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
871 for o in obj:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
872 new_context = Context(parent=context, key=o, current_object=o)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
873 yield from _validate(o, schema_items, new_context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
874
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
875
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
876 def validate_tuple(obj, schema, context):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
877 if _is_null_allowed_for_object(obj, schema, context):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
878 return
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
879 if not isinstance(obj, (list, tuple)):
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
880 yield ValidationProblem(code=ERRORS.E10014, hint="got: {}".format(type(obj).__name__), context=context)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
881 return
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
882 yield from _validate_index_constraint(obj, schema, context)
19
c3a0fe8d4587 Consistent casing of schema items: all lowercase with dash as separator
Franz Glasner <fzglas.hg@dom66.de>
parents: 17
diff changeset
883 minlen = schema.get("min-length", None)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
884 if minlen:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
885 if len(obj) < minlen:
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
886 yield ValidationProblem(code=ERRORS.E10015, hint=obj, context=context)
19
c3a0fe8d4587 Consistent casing of schema items: all lowercase with dash as separator
Franz Glasner <fzglas.hg@dom66.de>
parents: 17
diff changeset
887 maxlen = schema.get("max-length", None)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
888 if maxlen is not None:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
889 if len(obj) > maxlen:
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
890 yield ValidationProblem(code=ERRORS.E10016, hint=obj, context=context)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
891 schema_items = schema.get("items", [])
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
892 if not isinstance(schema_items, (list, tuple)):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
893 raise SchemaError("tuple items require a list of schemata in items")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
894 for idx, o in enumerate(obj):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
895 # early exit at maxlen
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
896 if maxlen is not None and idx >= maxlen:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
897 break
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
898 new_context = Context(parent=context, index=idx, current_object=o)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
899 try:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
900 schema_index = schema.ensure_child_schema(schema_items[idx])
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
901 except IndexError:
19
c3a0fe8d4587 Consistent casing of schema items: all lowercase with dash as separator
Franz Glasner <fzglas.hg@dom66.de>
parents: 17
diff changeset
902 additional_items = schema.get_child("additional-items", False)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
903 if isinstance(additional_items, bool):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
904 if not additional_items:
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
905 yield ValidationProblem(code=ERRORS.E10017, context=new_context)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
906 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
907 yield from _validate(o, additional_items, new_context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
908 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
909 yield from _validate(o, schema_index, new_context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
910
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
911
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
912 def validate_str(obj, schema, context):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
913 if _is_null_allowed_for_object(obj, schema, context):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
914 return
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
915 if not isinstance(obj, str):
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
916 yield ValidationProblem(code=ERRORS.E10002, hint=obj, context=context)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
917 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
918 yield from _validate_index_constraint(obj, schema, context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
919 enumvalues = schema.get("enum", None)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
920 if enumvalues is not None:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
921 for ev in enumvalues:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
922 if ev == obj:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
923 break
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
924 else:
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
925 yield ValidationProblem(code=ERRORS.E10043, hint=obj, context=context)
19
c3a0fe8d4587 Consistent casing of schema items: all lowercase with dash as separator
Franz Glasner <fzglas.hg@dom66.de>
parents: 17
diff changeset
926 minlen = schema.get("min-length", None)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
927 if minlen:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
928 if len(obj) < minlen:
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
929 yield ValidationProblem(code=ERRORS.E10006, hint=obj, context=context)
19
c3a0fe8d4587 Consistent casing of schema items: all lowercase with dash as separator
Franz Glasner <fzglas.hg@dom66.de>
parents: 17
diff changeset
930 maxlen = schema.get("max-length", None)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
931 if maxlen is not None:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
932 if len(obj) > maxlen:
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
933 yield ValidationProblem(code=ERRORS.E10007, hint=obj, context=context)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
934 pattern = schema.get("pattern", None)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
935 if pattern is not None:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
936 if isinstance(pattern, str):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
937 mo = re.search(pattern, obj)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
938 if not mo:
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
939 yield ValidationProblem(code=ERRORS.E10008, context=context)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
940 elif isinstance(pattern, TYPE_RE):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
941 mo = pattern.search(obj)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
942 if not mo:
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
943 yield ValidationProblem(code=ERRORS.E10008, context=context)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
944 elif callable(pattern):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
945 yield from pattern(obj, schema, context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
946 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
947 raise SchemaError("unknown pattern type")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
948 is_contained = schema.get("is-contained-in-ref", None)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
949 if is_contained is not None:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
950 refobj = try_get_reference(is_contained,
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
951 context,
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
952 schema,
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
953 default=_SENTINEL)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
954 if refobj is _SENTINEL:
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
955 yield ValidationProblem(code=ERRORS.E10044, context=context)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
956 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
957 try:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
958 if obj not in refobj:
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
959 yield ValidationProblem(code=ERRORS.E10045, context=context)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
960 except TypeError:
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
961 yield ValidationProblem(code=ERRORS.E10046, context=context)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
962
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
963
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
964 def validate_binary(obj, schema, context):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
965 if not isinstance(obj, (bytes, bytearray)):
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
966 yield ValidationProblem(code=ERRORS.E10035, hint=obj, context=context)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
967 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
968 yield from _validate_index_constraint(obj, schema, context)
19
c3a0fe8d4587 Consistent casing of schema items: all lowercase with dash as separator
Franz Glasner <fzglas.hg@dom66.de>
parents: 17
diff changeset
969 minlen = schema.get("min-length", None)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
970 if minlen:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
971 if len(obj) < minlen:
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
972 yield ValidationProblem(code=ERRORS.E10036, hint=obj, context=context)
19
c3a0fe8d4587 Consistent casing of schema items: all lowercase with dash as separator
Franz Glasner <fzglas.hg@dom66.de>
parents: 17
diff changeset
973 maxlen = schema.get("max-length", None)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
974 if maxlen is not None:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
975 if len(obj) > maxlen:
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
976 yield ValidationProblem(code=ERRORS.E10037, hint=obj, context=context)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
977 pattern = schema.get("pattern", None)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
978 if pattern is not None:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
979 if isinstance(pattern, (str, bytes, bytearray)):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
980 if isinstance(pattern, str):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
981 if "'''" not in pattern:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
982 bytes_pattern = ast.literal_eval(
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
983 "b'''" + pattern + "'''")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
984 elif '"""' not in pattern:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
985 bytes_pattern = ast.literal_eval(
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
986 'b"""' + pattern + '"""')
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
987 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
988 raise SchemaError("incompatible bytes pattern")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
989 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
990 bytes_pattern = pattern
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
991 mo = re.search(bytes_pattern, obj)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
992 if not mo:
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
993 yield ValidationProblem(code=ERRORS.E10047, context=context)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
994 elif isinstance(pattern, TYPE_RE):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
995 mo = pattern.search(obj)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
996 if not mo:
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
997 yield ValidationProblem(code=ERRORS.E10047, context=context)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
998 elif callable(pattern):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
999 yield from pattern(obj, schema, context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1000 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1001 raise SchemaError("unknown pattern type")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1002
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1003
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1004 def validate_timestamp(obj, schema, context):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1005 if not isinstance(obj, datetime.datetime):
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
1006 yield ValidationProblem(code=ERRORS.E10041, hint=obj, context=context)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1007 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1008 yield from _validate_index_constraint(obj, schema, context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1009 value = schema.get("value", None)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1010 if value is not None:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1011 if callable(value):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1012 yield from value(obj, schema, context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1013 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1014 raise SchemaError("unknown value validator (only a callable allowed)")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1015
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1016
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1017 def validate_integer(obj, schema, context):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1018 if _is_null_allowed_for_object(obj, schema, context):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1019 return
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1020 if not isinstance(obj, int):
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
1021 yield ValidationProblem(code=ERRORS.E10020, hint=obj, context=context)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1022 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1023 yield from _validate_index_constraint(obj, schema, context)
19
c3a0fe8d4587 Consistent casing of schema items: all lowercase with dash as separator
Franz Glasner <fzglas.hg@dom66.de>
parents: 17
diff changeset
1024 min_value = schema.get("min-value", None)
c3a0fe8d4587 Consistent casing of schema items: all lowercase with dash as separator
Franz Glasner <fzglas.hg@dom66.de>
parents: 17
diff changeset
1025 if min_value is not None and obj < min_value:
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
1026 yield ValidationProblem(code=ERRORS.E10021, hint=obj, context=context)
19
c3a0fe8d4587 Consistent casing of schema items: all lowercase with dash as separator
Franz Glasner <fzglas.hg@dom66.de>
parents: 17
diff changeset
1027 max_value = schema.get("max-value", None)
c3a0fe8d4587 Consistent casing of schema items: all lowercase with dash as separator
Franz Glasner <fzglas.hg@dom66.de>
parents: 17
diff changeset
1028 if max_value is not None and obj > max_value:
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
1029 yield ValidationProblem(code=ERRORS.E10022, hint=obj, context=context)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1030 enumvalues = schema.get("enum", None)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1031 if enumvalues is not None:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1032 for ev in enumvalues:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1033 if ev == obj:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1034 break
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1035 else:
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
1036 yield ValidationProblem(code=ERRORS.E10048, hint=obj, context=context)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1037 value = schema.get("value", None)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1038 if value is not None:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1039 if callable(value):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1040 yield from value(obj, schema, context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1041 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1042 raise SchemaError("unknown value validator (only a callable allowed)")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1043
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1044
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1045 def validate_float(obj, schema, context):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1046 if _is_null_allowed_for_object(obj, schema, context):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1047 return
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1048 if not isinstance(obj, float):
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
1049 yield ValidationProblem(code=ERRORS.E10023, hint=obj, context=context)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1050 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1051 yield from _validate_index_constraint(obj, schema, context)
19
c3a0fe8d4587 Consistent casing of schema items: all lowercase with dash as separator
Franz Glasner <fzglas.hg@dom66.de>
parents: 17
diff changeset
1052 min_value = schema.get("min-value", None)
c3a0fe8d4587 Consistent casing of schema items: all lowercase with dash as separator
Franz Glasner <fzglas.hg@dom66.de>
parents: 17
diff changeset
1053 if min_value is not None and obj < min_value:
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
1054 yield ValidationProblem(code=ERRORS.E10024, hint=obj, context=context)
19
c3a0fe8d4587 Consistent casing of schema items: all lowercase with dash as separator
Franz Glasner <fzglas.hg@dom66.de>
parents: 17
diff changeset
1055 max_value = schema.get("max-value", None)
c3a0fe8d4587 Consistent casing of schema items: all lowercase with dash as separator
Franz Glasner <fzglas.hg@dom66.de>
parents: 17
diff changeset
1056 if max_value is not None and obj > max_value:
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
1057 yield ValidationProblem(code=ERRORS.E10025, hint=obj, context=context)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1058 value = schema.get("value", None)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1059 if value is not None:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1060 if callable(value):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1061 yield from value(obj, schema, context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1062 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1063 raise SchemaError("unknown value validator (only a callable allowed)")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1064
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1065
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1066 def validate_number(obj, schema, context):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1067 if _is_null_allowed_for_object(obj, schema, context):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1068 return
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1069 if not isinstance(obj, (int, float)):
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
1070 yield ValidationProblem(code=ERRORS.E10030, hint=obj, context=context)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1071 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1072 yield from _validate_index_constraint(obj, schema, context)
19
c3a0fe8d4587 Consistent casing of schema items: all lowercase with dash as separator
Franz Glasner <fzglas.hg@dom66.de>
parents: 17
diff changeset
1073 min_value = schema.get("min-value", None)
c3a0fe8d4587 Consistent casing of schema items: all lowercase with dash as separator
Franz Glasner <fzglas.hg@dom66.de>
parents: 17
diff changeset
1074 if min_value is not None and isinstance(obj, float):
c3a0fe8d4587 Consistent casing of schema items: all lowercase with dash as separator
Franz Glasner <fzglas.hg@dom66.de>
parents: 17
diff changeset
1075 min_value *= 1.0
c3a0fe8d4587 Consistent casing of schema items: all lowercase with dash as separator
Franz Glasner <fzglas.hg@dom66.de>
parents: 17
diff changeset
1076 if min_value is not None and obj < min_value:
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
1077 yield ValidationProblem(code=ERRORS.E10031, hint=obj, context=context)
19
c3a0fe8d4587 Consistent casing of schema items: all lowercase with dash as separator
Franz Glasner <fzglas.hg@dom66.de>
parents: 17
diff changeset
1078 max_value = schema.get("max-value", None)
c3a0fe8d4587 Consistent casing of schema items: all lowercase with dash as separator
Franz Glasner <fzglas.hg@dom66.de>
parents: 17
diff changeset
1079 if max_value is not None and isinstance(obj, float):
c3a0fe8d4587 Consistent casing of schema items: all lowercase with dash as separator
Franz Glasner <fzglas.hg@dom66.de>
parents: 17
diff changeset
1080 max_value *= 1.0
c3a0fe8d4587 Consistent casing of schema items: all lowercase with dash as separator
Franz Glasner <fzglas.hg@dom66.de>
parents: 17
diff changeset
1081 if max_value is not None and obj > max_value:
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
1082 yield ValidationProblem(code=ERRORS.E10032, hint=obj, context=context)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1083 enumvalues = schema.get("enum", None)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1084 if enumvalues is not None:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1085 for ev in enumvalues:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1086 if ev == obj:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1087 break
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1088 else:
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
1089 yield ValidationProblem(code=ERRORS.E10049, hint=obj, context=context)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1090 value = schema.get("value", None)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1091 if value is not None:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1092 if callable(value):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1093 yield from value(obj, schema, context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1094 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1095 raise SchemaError("unknown value validator (only a callable allowed)")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1096
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1097
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1098 def validate_scalar(obj, schema, context):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1099 if _is_null_allowed_for_object(obj, schema, context):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1100 return
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1101 yield from _validate_index_constraint(obj, schema, context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1102 if obj is None:
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
1103 yield ValidationProblem(code=ERRORS.E10033, hint=obj, context=context)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1104 if isinstance(obj, (dict, list, tuple, set, frozenset)):
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
1105 yield ValidationProblem(code=ERRORS.E10033, hint=obj, context=context)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1106
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1107
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1108 def validate_deny(obj, schema, context):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1109 yield from _validate_index_constraint(obj, schema, context)
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
1110 yield ValidationProblem(code=ERRORS.E10010, context=context)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1111
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1112
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1113 def validate_accept(obj, schema, context):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1114 yield from _validate_index_constraint(obj, schema, context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1115
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1116
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1117 def validate_null(obj, schema, context):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1118 yield from _validate_index_constraint(obj, schema, context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1119 if obj is not None:
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
1120 yield ValidationProblem(code=ERRORS.E10011, context=context)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1121
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1122
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1123 def validate_empty(obj, schema, context):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1124 yield from _validate_index_constraint(obj, schema, context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1125 if obj is None:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1126 return
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1127 if isinstance(obj, (dict, list, tuple, set, frozenset)) and not obj:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1128 return
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
1129 yield ValidationProblem(ERRORS.E10018, context=context)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1130
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1131
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1132 def validate_bool(obj, schema, context):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1133 if _is_null_allowed_for_object(obj, schema, context):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1134 return
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1135 if not isinstance(obj, bool):
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
1136 yield ValidationProblem(code=ERRORS.E10026, hint=obj, context=context)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1137 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1138 yield from _validate_index_constraint(obj, schema, context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1139 value = schema.get("value", None)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1140 if value is not None:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1141 if callable(value):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1142 yield from value(obj, schema, context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1143 elif value and not obj:
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
1144 yield ValidationProblem(code=ERRORS.E10027, hint=obj, context=context)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1145 elif not value and obj:
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
1146 yield ValidationProblem(code=ERRORS.E10028, hint=obj, context=context)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1147
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1148
23
413b344be2d1 Consistent naming of validation methods: all lowercase
Franz Glasner <fzglas.hg@dom66.de>
parents: 19
diff changeset
1149 def validate_all_of(obj, schema, context):
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1150 if not isinstance(schema, (list, tuple)):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1151 raise SchemaError("require a list of schematas for `all-of'")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1152 res = []
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1153 for idx, s in enumerate(schema):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1154 assert isinstance(s, _Schema)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1155 tr = list(_validate(obj, s, context))
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1156 if tr:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1157 res.append((idx, tr, ))
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1158 if res:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1159 yield ValidationProblem(
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
1160 code=ERRORS.E10057,
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1161 context=context,
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1162 cause=[
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1163 ValidationProblem(
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
1164 code=ERRORS.E10058,
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1165 context=context,
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1166 cause=tr,
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1167 index=idx) for (idx, tr) in res])
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1168
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1169
23
413b344be2d1 Consistent naming of validation methods: all lowercase
Franz Glasner <fzglas.hg@dom66.de>
parents: 19
diff changeset
1170 def validate_any_of(obj, schema, context):
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1171 if not isinstance(schema, (list, tuple)):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1172 raise SchemaError("require a list of schematas for `any-of'")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1173 res = []
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1174 for s in schema:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1175 assert isinstance(s, _Schema)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1176 tr = list(_validate(obj, s, context))
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1177 if tr:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1178 res.append(tr)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1179 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1180 # Erfolg: gleich positiv zurueck ohne Meldungen
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1181 return
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1182 # Ansonsten: alle Fehlschlaege protokollieren
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1183 if res:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1184 yield ValidationProblem(
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
1185 code=ERRORS.E10055,
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1186 context=context,
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1187 cause=[
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1188 ValidationProblem(
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
1189 code=ERRORS.E10056,
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1190 context=context,
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1191 cause=tr) for tr in res])
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1192
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1193
23
413b344be2d1 Consistent naming of validation methods: all lowercase
Franz Glasner <fzglas.hg@dom66.de>
parents: 19
diff changeset
1194 def validate_one_of(obj, schema, context):
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1195 if not isinstance(schema, (list, tuple)):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1196 raise SchemaError("require a list of schematas for `one-of'")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1197 success_res = []
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1198 failed_res = []
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1199 for idx, s in enumerate(schema):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1200 assert isinstance(s, _Schema)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1201 tr = list(_validate(obj, s, context))
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1202 if tr:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1203 failed_res.append((idx, tr, ))
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1204 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1205 success_res.append(idx)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1206 if len(success_res) == 1:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1207 return
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1208 elif len(success_res) == 0:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1209 # Ansonsten: alle Fehlschlaege protokollieren
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1210 if failed_res:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1211 yield ValidationProblem(
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
1212 code=ERRORS.E10053,
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1213 context=context,
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1214 cause=[
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1215 ValidationProblem(
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
1216 code=ERRORS.E10054,
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1217 context=context,
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1218 cause=tr,
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1219 index=idx) for (idx, tr) in failed_res])
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1220 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1221 # Die Indizes der "zuvielen" in "hint" anzeigen
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
1222 yield ValidationProblem(code=ERRORS.E10019, hint=",".join([str(k) for k in success_res]))
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1223
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1224
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1225 def validate_not(obj, schema, context):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1226 assert isinstance(schema, _Schema)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1227 res = list(_validate(obj, schema, context))
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1228 if not res:
13
940676a0de84 ERRORS and WARNINGS are now enums
Franz Glasner <fzglas.hg@dom66.de>
parents: 10
diff changeset
1229 yield ValidationProblem(code=ERRORS.E10029, hint=obj, context=context,
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1230 cause=res)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1231
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1232
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1233 def process_schema_references(schema, context, check_single_ref_key=True):
40
2376224a9717 FIX: when processing schema references and the resolved schema contains conditionals they must be evaluated
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 39
diff changeset
1234 """
2376224a9717 FIX: when processing schema references and the resolved schema contains conditionals they must be evaluated
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 39
diff changeset
1235
2376224a9717 FIX: when processing schema references and the resolved schema contains conditionals they must be evaluated
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 39
diff changeset
1236 .. note:: If a new dereferenced schema is found schema conditionals are
2376224a9717 FIX: when processing schema references and the resolved schema contains conditionals they must be evaluated
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 39
diff changeset
1237 evaluated also. So the resolved schema containing conditionals
2376224a9717 FIX: when processing schema references and the resolved schema contains conditionals they must be evaluated
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 39
diff changeset
1238 behaves according to the given conditions.
2376224a9717 FIX: when processing schema references and the resolved schema contains conditionals they must be evaluated
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 39
diff changeset
1239
2376224a9717 FIX: when processing schema references and the resolved schema contains conditionals they must be evaluated
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 39
diff changeset
1240 """
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1241 try:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1242 ref = schema[SCHEMA_REF_KEY]
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1243 except (KeyError, TypeError):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1244 return schema
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1245 # if `$ref' is found it MUST be the only key
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1246 if check_single_ref_key and len(schema) != 1:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1247 raise SchemaError("`{}' must be the single key if it exists")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1248 schema = try_get_reference(ref, context, schema)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1249 if not isinstance(schema, _Schema):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1250 raise SchemaError(
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1251 "dereferenced schema is not a `_Schema': {}".format(ref))
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1252 schema = copy.deepcopy(schema)
40
2376224a9717 FIX: when processing schema references and the resolved schema contains conditionals they must be evaluated
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 39
diff changeset
1253 # process schema conditionals "cond" and "match" again
2376224a9717 FIX: when processing schema references and the resolved schema contains conditionals they must be evaluated
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 39
diff changeset
1254 schema = process_schema_conditionals(schema, context)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1255 return process_schema_references(schema, context, check_single_ref_key=True)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1256
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1257
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1258 def process_schema_conditionals(schema, context):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1259 """Lisp-like `cond` to provide schema modifications
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1260
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1261 :param schema: the input schema
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1262 :param context: the validation context with a valid
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1263 `context.root.root_object`
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1264 :returns: the processed schema: the schema itself if it is unchanged and
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1265 a copy of the schema if has been changed
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1266
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1267 """
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1268 what, conds = _get_one_of(schema, "cond", "match", default=None)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1269 if what is None or conds is None:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1270 return schema
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1271 if not isinstance(conds, (list, tuple)):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1272 raise SchemaError("the conditions of a cond must be a sequence")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1273 if what == "cond":
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1274 return _process_schema_conditionals_cond(schema, conds, context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1275 elif what == "match":
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1276 return _process_schema_conditionals_match(schema, conds, context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1277 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1278 assert False, "unreachable"
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1279
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1280
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1281 def _process_schema_conditionals_cond(schema, conds, context):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1282 for cond in conds:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1283 if not isinstance(cond, dict):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1284 raise SchemaError("a single condition must be a dict")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1285 if eval_condition(cond, context, schema):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1286 rep_type, rep_schema = _get_one_of(
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1287 cond, "then", "then-replace", "then-merge")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1288 rep_schema = schema.ensure_child_schema(rep_schema)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1289 if rep_type in ("then", "then-replace"):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1290 do_merge = False
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1291 elif rep_type == "then-merge":
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1292 do_merge = True
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1293 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1294 raise SchemaError("unknown then type: {}".format(rep_type))
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1295 break
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1296 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1297 #
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1298 # No condition was true: just remove the "cond" to get the
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1299 # effective schema.
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1300 #
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1301 rep_schema = None
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1302 do_merge = False
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1303
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1304 new_schema = schema.copy()
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1305 del new_schema["cond"]
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1306 if rep_schema:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1307 rep_schema = process_schema_references(rep_schema, context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1308 # this could insert a new nested "cond" or "match" again
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1309 if do_merge:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1310 rep_schema = copy.deepcopy(rep_schema)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1311 new_schema = _merge(rep_schema, new_schema)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1312 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1313 new_schema.update(rep_schema)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1314 # Recursively apply "cond/match" evaluation to the resulting schema
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1315 return process_schema_conditionals(new_schema, context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1316
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1317
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1318 def _process_schema_conditionals_match(schema, conds, context):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1319 rep_schemata = []
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1320 for cond in conds:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1321 if not isinstance(cond, dict):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1322 raise SchemaError("a single condition must be a dict")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1323 if eval_condition(cond, context, schema):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1324 rep_type, rep_schema = _get_one_of(
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1325 cond, "then", "then-replace", "then-merge")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1326 rep_schema = schema.ensure_child_schema(rep_schema)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1327 if rep_type in ("then", "then-replace"):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1328 rep_schemata.append((False, rep_schema))
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1329 elif rep_type == "then-merge":
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1330 rep_schemata.append((True, rep_schema))
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1331 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1332 raise SchemaError("unknown then type: {}".format(rep_type))
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1333
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1334 new_schema = schema.copy()
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1335 del new_schema["match"]
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1336 for do_merge, rep_schema in rep_schemata:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1337 rep_schema = process_schema_references(rep_schema, context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1338 # this could insert a new nested "cond" or "match" again
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1339 if do_merge:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1340 rep_schema = copy.deepcopy(rep_schema)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1341 new_schema = _merge(rep_schema, new_schema)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1342 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1343 new_schema.update(rep_schema)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1344 # Recursively apply "cond/match" evaluation to the resulting schema
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1345 return process_schema_conditionals(new_schema, context)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1346
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1347
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1348 def eval_condition(cond, context, schema):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1349 """Eval the condition in `cond` and return a tuple `(hit, predval)`
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1350
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1351 """
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1352 pred, predval = _get_one_of(
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1353 cond,
39
78f5ef0ee087 Implement "ref-false", "ref-not-exists", "when-ref-false" and "when-ref-not-exists" in addition to the existing conditions
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 38
diff changeset
1354 "when-ref-true", "when-ref-false",
78f5ef0ee087 Implement "ref-false", "ref-not-exists", "when-ref-false" and "when-ref-not-exists" in addition to the existing conditions
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 38
diff changeset
1355 "when-ref-exists", "when-ref-not-exists", "when",
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1356 default=_SENTINEL)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1357
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1358 if pred == "when":
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1359 # rekursive evaluation of `predval` as the real predicate
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1360 return eval_pred(predval, context, schema)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1361 elif pred == "when-ref-true":
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1362 refobj = try_get_reference(predval, context, schema, default=None)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1363 return bool(refobj)
39
78f5ef0ee087 Implement "ref-false", "ref-not-exists", "when-ref-false" and "when-ref-not-exists" in addition to the existing conditions
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 38
diff changeset
1364 elif pred == "when-ref-false":
78f5ef0ee087 Implement "ref-false", "ref-not-exists", "when-ref-false" and "when-ref-not-exists" in addition to the existing conditions
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 38
diff changeset
1365 refobj = try_get_reference(predval, context, schema, default=None)
78f5ef0ee087 Implement "ref-false", "ref-not-exists", "when-ref-false" and "when-ref-not-exists" in addition to the existing conditions
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 38
diff changeset
1366 return not bool(refobj)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1367 elif pred == "when-ref-exists":
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1368 refobj = try_get_reference(predval, context, schema, default=_SENTINEL)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1369 return refobj is not _SENTINEL
39
78f5ef0ee087 Implement "ref-false", "ref-not-exists", "when-ref-false" and "when-ref-not-exists" in addition to the existing conditions
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 38
diff changeset
1370 elif pred == "when-ref-not-exists":
78f5ef0ee087 Implement "ref-false", "ref-not-exists", "when-ref-false" and "when-ref-not-exists" in addition to the existing conditions
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 38
diff changeset
1371 refobj = try_get_reference(predval, context, schema, default=_SENTINEL)
78f5ef0ee087 Implement "ref-false", "ref-not-exists", "when-ref-false" and "when-ref-not-exists" in addition to the existing conditions
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 38
diff changeset
1372 return refobj is _SENTINEL
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1373 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1374 raise SchemaError("unknown condition type: {}".format(pred))
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1375
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1376
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1377 def eval_pred(pred, context, schema):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1378 if isinstance(pred, dict):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1379 combinator, combinator_val = _get_one_of(
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1380 pred,
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1381 "not", "all-of", "any-of", "one-of",
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1382 default=None)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1383 if combinator:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1384 if combinator == "not":
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1385 return not eval_pred(combinator_val, context, schema)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1386 elif combinator == "all-of":
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1387 if not isinstance(combinator_val, (list, tuple)):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1388 raise SchemaError("`all-of' requires a list of childs")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1389 for cv in combinator_val:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1390 if not eval_pred(cv, context, schema):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1391 return False
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1392 return True
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1393 elif combinator == "any-of":
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1394 if not isinstance(combinator_val, (list, tuple)):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1395 raise SchemaError("`any-of' requires a list of childs")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1396 for cv in combinator_val:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1397 if eval_pred(cv, context, schema):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1398 return True
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1399 return False
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1400 elif combinator == "one-of":
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1401 if not isinstance(combinator_val, (list, tuple)):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1402 raise SchemaError("`one-of' requires a list of childs")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1403 num_true = 0
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1404 for cv in combinator_val:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1405 if eval_pred(cv, context, schema):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1406 num_true += 1
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1407 # shortcut
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1408 if num_true > 1:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1409 return False
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1410 if num_true == 1:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1411 return True
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1412 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1413 return False
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1414 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1415 raise SchemaError(
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1416 "unknown logical operator: {}".format(combinator))
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1417 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1418 pred_key, pred_val = _get_one_of(
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1419 pred,
39
78f5ef0ee087 Implement "ref-false", "ref-not-exists", "when-ref-false" and "when-ref-not-exists" in addition to the existing conditions
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 38
diff changeset
1420 "ref-true", "ref-false", "ref-exists", "ref-not-exists",
78f5ef0ee087 Implement "ref-false", "ref-not-exists", "when-ref-false" and "when-ref-not-exists" in addition to the existing conditions
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 38
diff changeset
1421 "equals",
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1422 default=None)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1423 if pred_key == "ref-true":
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1424 refobj = try_get_reference(
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1425 pred_val, context, schema, default=None)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1426 return bool(refobj)
39
78f5ef0ee087 Implement "ref-false", "ref-not-exists", "when-ref-false" and "when-ref-not-exists" in addition to the existing conditions
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 38
diff changeset
1427 elif pred_key == "ref-false":
78f5ef0ee087 Implement "ref-false", "ref-not-exists", "when-ref-false" and "when-ref-not-exists" in addition to the existing conditions
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 38
diff changeset
1428 refobj = try_get_reference(
78f5ef0ee087 Implement "ref-false", "ref-not-exists", "when-ref-false" and "when-ref-not-exists" in addition to the existing conditions
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 38
diff changeset
1429 pred_val, context, schema, default=None)
78f5ef0ee087 Implement "ref-false", "ref-not-exists", "when-ref-false" and "when-ref-not-exists" in addition to the existing conditions
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 38
diff changeset
1430 return not bool(refobj)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1431 elif pred_key == "ref-exists":
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1432 refobj = try_get_reference(
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1433 pred_val, context, schema, default=_SENTINEL)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1434 return refobj is not _SENTINEL
39
78f5ef0ee087 Implement "ref-false", "ref-not-exists", "when-ref-false" and "when-ref-not-exists" in addition to the existing conditions
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 38
diff changeset
1435 elif pred_key == "ref-not-exists":
78f5ef0ee087 Implement "ref-false", "ref-not-exists", "when-ref-false" and "when-ref-not-exists" in addition to the existing conditions
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 38
diff changeset
1436 refobj = try_get_reference(
78f5ef0ee087 Implement "ref-false", "ref-not-exists", "when-ref-false" and "when-ref-not-exists" in addition to the existing conditions
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 38
diff changeset
1437 pred_val, context, schema, default=_SENTINEL)
78f5ef0ee087 Implement "ref-false", "ref-not-exists", "when-ref-false" and "when-ref-not-exists" in addition to the existing conditions
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 38
diff changeset
1438 return refobj is _SENTINEL
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1439 elif pred_key == "equals":
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1440 if not isinstance(pred_val, (list, tuple)):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1441 raise SchemaError("`equals' requires a list as childs")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1442 if len(pred_val) != 2:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1443 raise SchemaError("`equals' requires a list of len 2")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1444 op1 = eval_comparison_operator_operand(
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1445 pred_val[0], context, schema)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1446 op2 = eval_comparison_operator_operand(
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1447 pred_val[1], context, schema)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1448 return op1 == op2
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1449 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1450 raise SchemaError("unknown predicate: {}".format(pred))
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1451 elif isinstance(pred, list):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1452 # implicit all-of (aka AND)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1453 for cv in pred:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1454 if not eval_pred(cv, context, schema):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1455 return False
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1456 return True
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1457 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1458 return pred
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1459
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1460
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1461 def eval_comparison_operator_operand(op, context, schema):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1462 if not isinstance(op, dict):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1463 raise SchemaError("an operand must be a dict")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1464 opkey, opval = _get_one_of(op, "ref", "val", "value")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1465 if opkey is None:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1466 raise SchemaError("no operant given in {!r}".format(op))
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1467 if opkey == "ref":
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1468 return try_get_reference(opval, context, schema)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1469 elif opkey in ("val", "value"):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1470 return opval
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1471 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1472 assert False
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1473
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1474
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1475 def try_get_reference(ref, context, schema, default=None):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1476 """Get the object referenced in `ref`
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1477
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1478 Use `context` as data/object context and `schema` as the current schema
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1479 context.
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1480
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1481 """
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1482 uri = rfc3986.URIReference.from_string(ref).normalize()
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1483 if not uri.scheme:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1484 uri = uri.copy_with(scheme="object")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1485 if uri.scheme == "object":
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1486 if ref.startswith("object#"):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1487 for attr in ("authority", "path", "query"):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1488 if getattr(uri, attr, None) is not None:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1489 raise SchemaError(
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1490 "bogus {} in URI reference `{}'".format(attr, ref))
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1491 if uri.fragment is None:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1492 raise SchemaError("fragment required in reference")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1493 if not uri.fragment:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1494 return context.root.root_object
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1495 elif uri.fragment == '.':
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1496 return context.current_object
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1497 parts = uri.fragment.split('.') # use '.' separator as in configmix
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1498 if parts[0]:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1499 # absolute
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1500 d = context.root.root_object
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1501 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1502 # relative
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1503 d = context.current_object
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1504 parts = parts[1:]
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1505 c = context # needed to determine relative object references
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1506 relative_refs_allowed = True
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1507 for part in [urllib.parse.unquote(p) for p in parts]:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1508 if part:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1509 relative_refs_allowed = False
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1510 try:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1511 d = d[part]
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1512 except (KeyError, IndexError, TypeError):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1513 return default
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1514 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1515 if not relative_refs_allowed:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1516 raise SchemaError(
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1517 "empty part in path to object reference not allowed")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1518 c = c.safe_parent
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1519 d = c.current_object
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1520 return d
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1521 elif uri.scheme == "schema":
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1522 if not uri.path or (uri.path == SCHEMA_PATH_SELF):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1523 s = schema.SELF
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1524 elif uri.path == SCHEMA_PATH_ROOT:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1525 s = schema.ROOT
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1526 else:
28
db3491e1b590 Allow to customize the loading of the schema dict
Franz Glasner <fzglas.hg@dom66.de>
parents: 25
diff changeset
1527 s = schema.get_cached_schema(
db3491e1b590 Allow to customize the loading of the schema dict
Franz Glasner <fzglas.hg@dom66.de>
parents: 25
diff changeset
1528 uri.path,
db3491e1b590 Allow to customize the loading of the schema dict
Franz Glasner <fzglas.hg@dom66.de>
parents: 25
diff changeset
1529 load_if_needed=True,
29
68286d27f27d FIX: Allow customization of the data stream loader (get_data_stream())
Franz Glasner <fzglas.hg@dom66.de>
parents: 28
diff changeset
1530 data_stream_loader=context.settings.data_stream_loader,
28
db3491e1b590 Allow to customize the loading of the schema dict
Franz Glasner <fzglas.hg@dom66.de>
parents: 25
diff changeset
1531 schema_loader=context.settings.schema_loader)
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1532 if uri.fragment is None:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1533 raise SchemaError("fragment required in reference")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1534
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1535 if not uri.fragment.startswith('/'):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1536 raise SchemaError("references to parts of a schema must be absolute (begin with `/')")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1537 if uri.fragment == '/':
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1538 return s
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1539 parts = uri.fragment.split('/')
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1540 parent_for_subschema = s
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1541 for part in [urllib.parse.unquote(p) for p in parts[1:]]:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1542 try:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1543 v = s[part]
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1544 except (KeyError, IndexError, TypeError):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1545 return default
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1546 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1547 if isinstance(v, _Schema):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1548 pass
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1549 elif isinstance(v, dict):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1550 s = _Schema(parent_for_subschema, False, v)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1551 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1552 # need not try further
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1553 return default
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1554 return s
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1555 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1556 raise SchemaError("Unknown schema reference scheme: {}".format(uri.scheme))
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1557
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1558
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1559 _DEL_VALUE = '{{::DEL::}}'
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1560 """Sigil to mark keys to be deleted in the target when merging"""
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1561
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1562
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1563 def _merge(user, default):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1564 """Logically merge the configuration in `user` into `default`.
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1565
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1566 :param dict user:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1567 the new configuration that will be logically merged
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1568 into `default`
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1569 :param dict default:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1570 the base configuration where `user` is logically merged into
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1571 :returns: `user` with the necessary amendments from `default`.
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1572 If `user` is ``None`` then `default` is returned.
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1573
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1574 .. note:: Implementation: The configuration in `user` is
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1575 augmented/changed **inplace**.
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1576
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1577 If a value in `user` is equal to :data:`._DEL_VALUE`
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1578 (``{{::DEL::}}``) the corresponding key will be deleted from the
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1579 merged output.
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1580
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1581 From http://stackoverflow.com/questions/823196/yaml-merge-in-python
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1582
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1583 """
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1584 if user is None:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1585 _filter_deletions(default)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1586 return default
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1587 if isinstance(user, dict) and isinstance(default, dict):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1588 for k, v in default.items():
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1589 if k in user:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1590 if user[k] == _DEL_VALUE:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1591 # do not copy and delete the marker
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1592 del user[k]
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1593 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1594 user[k] = _merge_item(user[k], v)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1595 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1596 user[k] = v
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1597 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1598 raise SchemaError("can only merge two dicts on top-level")
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1599 _filter_deletions(user)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1600 return user
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1601
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1602
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1603 def _merge_item(user, default):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1604 """Recursion helper for :func:`._merge`
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1605
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1606 """
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1607 if isinstance(user, dict) and isinstance(default, dict):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1608 for k, v in default.items():
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1609 if k in user:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1610 if user[k] == _DEL_VALUE:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1611 # do not copy and delete the marker
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1612 del user[k]
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1613 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1614 user[k] = _merge_item(user[k], v)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1615 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1616 user[k] = v
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1617 elif isinstance(user, (list, tuple)) and isinstance(default, (list, tuple)):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1618 for idx, v in enumerate(default):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1619 user.insert(idx, v)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1620 return user
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1621
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1622
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1623 def _filter_deletions(d):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1624 """Recursively filter deletions in the dict `d`.
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1625
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1626 Deletions have values that equal :data:`._DEL_VALUE`.
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1627
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1628 """
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1629 if not isinstance(d, dict):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1630 return
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1631 # use a copy of the items because we change `d` while iterating
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1632 for k, v in list(d.items()):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1633 if v == _DEL_VALUE:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1634 del d[k]
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1635 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1636 if isinstance(d[k], dict):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1637 _filter_deletions(d[k])
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1638
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1639
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1640 def _log_problem_cause_all(logger, loglevel, level, problems):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1641 if not problems:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1642 return
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1643 for pr in problems:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1644 logger.log(loglevel, "%s> %r", "-"*((level*2)+2), pr)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1645 _log_problem_cause_all(logger, loglevel, level+1, pr.cause)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1646
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1647
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1648 def _build_problems_by_level_and_depth(by_level, by_depth, level, problems):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1649 for pr in problems:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1650 if not pr.cause:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1651 continue
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1652 try:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1653 prl = by_level[level]
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1654 except LookupError:
9
Franz Glasner <fzglas.hg@dom66.de>
parents: 5
diff changeset
1655 prl = []
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1656 by_level[level] = prl
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1657 prl.append(pr)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1658
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1659 depth = pr.context.depth
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1660 try:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1661 prd = by_depth[depth]
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1662 except LookupError:
9
Franz Glasner <fzglas.hg@dom66.de>
parents: 5
diff changeset
1663 prd = []
5
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1664 by_depth[depth] = prd
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1665 prd.append(pr)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1666 _build_problems_by_level_and_depth(
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1667 by_level, by_depth, level+1, pr.cause)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1668
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1669
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1670 def _log_problem_cause(logger, loglevel, max_level, max_depth, level, problems):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1671 for pr in problems:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1672 #
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1673 # Check whether we will start logging from this level downwards
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1674 # all problems
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1675 #
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1676 if max_level is None or level == max_level:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1677 new_max_level = None # trigger logging
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1678 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1679 new_max_level = max_level
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1680 if max_depth is None or max_depth == pr.context.depth:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1681 new_max_depth = None # trigger logging
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1682 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1683 new_max_depth = max_depth
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1684 if new_max_level is None or new_max_depth is None:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1685 logger.log(loglevel, "%s> %r", "-"*((level*2)+2), pr)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1686 if pr.cause:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1687 _log_problem_cause(
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1688 logger, loglevel,
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1689 new_max_level, new_max_depth,
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1690 level+1, pr.cause)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1691
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1692
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1693 def log_problem_cause(logger, loglevel, debug, level, problems):
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1694 if not problems:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1695 return
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1696 if debug:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1697 _log_problem_cause_all(logger, loglevel, level, problems)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1698 else:
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1699 by_level = {} # to determine maximum problem nesting level
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1700 by_depth = {} # to determine maximum context nexting level
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1701 _build_problems_by_level_and_depth(by_level, by_depth, level, problems)
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1702
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1703 max_level = max(by_level.keys())
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1704 max_depth = max(by_depth.keys())
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1705
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1706 _log_problem_cause(
84dfd1a94926 Add the existing implementation.
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1707 logger, loglevel, max_level, max_depth, level, problems)