Mercurial > hgrepos > Python > libs > data-schema
comparison data_schema/__init__.py @ 16:2a9e7c4b717e
Problem severity now is also an enum
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Fri, 07 Jul 2023 02:33:28 +0200 |
| parents | 696b83f29363 |
| children | 65f937fb8de7 |
comparison
equal
deleted
inserted
replaced
| 15:696b83f29363 | 16:2a9e7c4b717e |
|---|---|
| 18 | 18 |
| 19 __revision__ = "|VCSRevision|" | 19 __revision__ = "|VCSRevision|" |
| 20 | 20 |
| 21 __date__ = "|VCSJustDate|" | 21 __date__ = "|VCSJustDate|" |
| 22 | 22 |
| 23 __all__ = ["ERROR", "WARNING", "INFO", "ERRORS", "WARNINGS", | 23 __all__ = ["SEVERITY", "ERRORS", "WARNINGS", |
| 24 "level_name", "problem_message", | 24 "problem_message", |
| 25 "ValidationProblem", "SchemaError", | 25 "ValidationProblem", "SchemaError", |
| 26 "validate", | 26 "validate", |
| 27 "log_problem_cause"] | 27 "log_problem_cause"] |
| 28 | 28 |
| 29 | 29 |
| 45 def NC_(ctx, msg): | 45 def NC_(ctx, msg): |
| 46 """Mimimum dummy translation support""" | 46 """Mimimum dummy translation support""" |
| 47 return msg | 47 return msg |
| 48 | 48 |
| 49 | 49 |
| 50 ERROR = 40 | 50 @enum.unique |
| 51 WARNING = 30 | 51 class SEVERITY(enum.IntEnum): |
| 52 INFO = 20 | 52 INFO = 20 |
| 53 | 53 WARNING = 30 |
| 54 _level_to_name = { | 54 ERROR = 40 |
| 55 ERROR: "ERROR", | |
| 56 WARNING: "WARNING", | |
| 57 INFO: "INFO", | |
| 58 } | |
| 59 _name_to_level = {name: level for (level, name) in _level_to_name.items()} | |
| 60 | 55 |
| 61 | 56 |
| 62 @enum.unique | 57 @enum.unique |
| 63 class ERRORS(enum.Enum): | 58 class ERRORS(enum.Enum): |
| 64 E10000 = NC_("schema-msg", "dict expected") | 59 E10000 = NC_("schema-msg", "dict expected") |
| 148 | 143 |
| 149 SCHEMA_PATH_SELF = "$self" | 144 SCHEMA_PATH_SELF = "$self" |
| 150 """URI path to the current schema""" | 145 """URI path to the current schema""" |
| 151 | 146 |
| 152 | 147 |
| 153 def level_name(level): | |
| 154 name = _level_to_name.get(level) | |
| 155 if name is None: | |
| 156 name = "Level {}".format(level) | |
| 157 return name | |
| 158 | |
| 159 | |
| 160 def problem_message(pr): | 148 def problem_message(pr): |
| 161 if isinstance(pr, ValidationProblem): | 149 if isinstance(pr, ValidationProblem): |
| 162 code = getattr(pr, "code", None) | 150 code = getattr(pr, "code", None) |
| 163 elif isinstance(pr, (ERRORS, WARNINGS)): | 151 elif isinstance(pr, (ERRORS, WARNINGS)): |
| 164 code = pr | 152 code = pr |
| 189 "unknown validation error code: {}".format(code)) | 177 "unknown validation error code: {}".format(code)) |
| 190 self.code = code | 178 self.code = code |
| 191 if severity is None: | 179 if severity is None: |
| 192 # autodetermine | 180 # autodetermine |
| 193 if code in ERRORS: | 181 if code in ERRORS: |
| 194 self.severity = ERROR | 182 self.severity = SEVERITY.ERROR |
| 195 elif code in WARNINGS: | 183 elif code in WARNINGS: |
| 196 self.severity = WARNING | 184 self.severity = SEVERITY.WARNING |
| 197 else: | 185 else: |
| 198 assert False | 186 assert False |
| 199 else: | 187 else: |
| 188 if not isinstance(severity, SEVERITY): | |
| 189 raise TypeError("invalid type for `severity'") | |
| 200 self.severity = severity | 190 self.severity = severity |
| 201 self.hint = hint | 191 self.hint = hint |
| 202 self.context = context | 192 self.context = context |
| 203 if cause: | 193 if cause: |
| 204 if not isinstance(cause, (list, tuple, set, frozenset)): | 194 if not isinstance(cause, (list, tuple, set, frozenset)): |
| 214 try: | 204 try: |
| 215 msg = " (" + problem_message(self) + ")" | 205 msg = " (" + problem_message(self) + ")" |
| 216 except LookupError: | 206 except LookupError: |
| 217 msg = "" | 207 msg = "" |
| 218 if self.index is None: | 208 if self.index is None: |
| 219 return "ValidationProblem(code={}{}, severity={!r}, hint={}, context=[depth={}]{})".format( | 209 return "ValidationProblem(code={}{}, severity={}, hint={}, context=[depth={}]{})".format( |
| 220 self.code.name, msg, self.severity, self.hint, self.context.depth, self.context) | 210 self.code.name, msg, self.severity.name, self.hint, self.context.depth, self.context) |
| 221 else: | 211 else: |
| 222 return "ValidationProblem(code={}{}, severity={!r}, hint={}, context=[depth={}]{}, index={})".format( | 212 return "ValidationProblem(code={}{}, severity={}, hint={}, context=[depth={}]{}, index={})".format( |
| 223 self.code.name, msg, self.severity, self.hint, self.context.depth, self.context, self.index) | 213 self.code.name, msg, self.severity.name, self.hint, self.context.depth, self.context, self.index) |
| 224 | 214 |
| 225 | 215 |
| 226 class SchemaError(Exception): | 216 class SchemaError(Exception): |
| 227 """An error within the schema itself""" | 217 """An error within the schema itself""" |
| 228 pass | 218 pass |
