Mercurial > hgrepos > Python > libs > data-schema
comparison data_schema/__init__.py @ 17:65f937fb8de7
Put the computation of the default problem severity into a new public function
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Fri, 07 Jul 2023 09:15:47 +0200 |
| parents | 2a9e7c4b717e |
| children | c3a0fe8d4587 |
comparison
equal
deleted
inserted
replaced
| 16:2a9e7c4b717e | 17:65f937fb8de7 |
|---|---|
| 19 __revision__ = "|VCSRevision|" | 19 __revision__ = "|VCSRevision|" |
| 20 | 20 |
| 21 __date__ = "|VCSJustDate|" | 21 __date__ = "|VCSJustDate|" |
| 22 | 22 |
| 23 __all__ = ["SEVERITY", "ERRORS", "WARNINGS", | 23 __all__ = ["SEVERITY", "ERRORS", "WARNINGS", |
| 24 "problem_message", | 24 "problem_message", "problem_severity", |
| 25 "ValidationProblem", "SchemaError", | 25 "ValidationProblem", "SchemaError", |
| 26 "validate", | 26 "validate", |
| 27 "log_problem_cause"] | 27 "log_problem_cause"] |
| 28 | 28 |
| 29 | 29 |
| 144 SCHEMA_PATH_SELF = "$self" | 144 SCHEMA_PATH_SELF = "$self" |
| 145 """URI path to the current schema""" | 145 """URI path to the current schema""" |
| 146 | 146 |
| 147 | 147 |
| 148 def problem_message(pr): | 148 def problem_message(pr): |
| 149 """ | |
| 150 | |
| 151 :raises KeyError: the code in `pr` does not refer to | |
| 152 :class:`.ERRORS` or `.WARNINGS` | |
| 153 | |
| 154 """ | |
| 149 if isinstance(pr, ValidationProblem): | 155 if isinstance(pr, ValidationProblem): |
| 150 code = getattr(pr, "code", None) | 156 code = getattr(pr, "code", None) |
| 151 elif isinstance(pr, (ERRORS, WARNINGS)): | 157 elif isinstance(pr, (ERRORS, WARNINGS)): |
| 152 code = pr | 158 code = pr |
| 153 else: | 159 else: |
| 154 if pr >= 80000: | 160 if pr >= 80000: |
| 155 code = WARNINGS["W" + str(pr)] | 161 code = WARNINGS["W" + str(pr)] |
| 156 else: | 162 else: |
| 157 code = ERRORS["E" + str(pr)] | 163 code = ERRORS["E" + str(pr)] |
| 158 return code.value | 164 return code.value |
| 165 | |
| 166 | |
| 167 def problem_severity(pr): | |
| 168 """Get the default severity for error or warning code `pr` | |
| 169 | |
| 170 :raises TypeError: if `pr` is not in :class:`.ERRORS` or :class:`.WARNINGS` | |
| 171 | |
| 172 """ | |
| 173 if pr in ERRORS: | |
| 174 return SEVERITY.ERROR | |
| 175 if pr in WARNINGS: | |
| 176 return SEVERITY.WARNING | |
| 177 raise TypeError("invalid error or warning code: %r" % (pr, )) | |
| 159 | 178 |
| 160 | 179 |
| 161 class ValidationProblem(object): | 180 class ValidationProblem(object): |
| 162 | 181 |
| 163 __slots__ = ("code", "severity", "hint", "context", "cause", "index") | 182 __slots__ = ("code", "severity", "hint", "context", "cause", "index") |
| 175 if code not in ERRORS and code not in WARNINGS: | 194 if code not in ERRORS and code not in WARNINGS: |
| 176 raise ValueError( | 195 raise ValueError( |
| 177 "unknown validation error code: {}".format(code)) | 196 "unknown validation error code: {}".format(code)) |
| 178 self.code = code | 197 self.code = code |
| 179 if severity is None: | 198 if severity is None: |
| 180 # autodetermine | 199 self.severity = problem_severity(code) |
| 181 if code in ERRORS: | |
| 182 self.severity = SEVERITY.ERROR | |
| 183 elif code in WARNINGS: | |
| 184 self.severity = SEVERITY.WARNING | |
| 185 else: | |
| 186 assert False | |
| 187 else: | 200 else: |
| 188 if not isinstance(severity, SEVERITY): | 201 if not isinstance(severity, SEVERITY): |
| 189 raise TypeError("invalid type for `severity'") | 202 raise TypeError("invalid type for `severity'") |
| 190 self.severity = severity | 203 self.severity = severity |
| 191 self.hint = hint | 204 self.hint = hint |
