Mercurial > hgrepos > Python > libs > data-schema
changeset 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 |
| files | data_schema/__init__.py tests/test_schema.py |
| diffstat | 2 files changed, 22 insertions(+), 31 deletions(-) [+] |
line wrap: on
line diff
--- a/data_schema/__init__.py Fri Jul 07 02:17:45 2023 +0200 +++ b/data_schema/__init__.py Fri Jul 07 02:33:28 2023 +0200 @@ -20,8 +20,8 @@ __date__ = "|VCSJustDate|" -__all__ = ["ERROR", "WARNING", "INFO", "ERRORS", "WARNINGS", - "level_name", "problem_message", +__all__ = ["SEVERITY", "ERRORS", "WARNINGS", + "problem_message", "ValidationProblem", "SchemaError", "validate", "log_problem_cause"] @@ -47,16 +47,11 @@ return msg -ERROR = 40 -WARNING = 30 -INFO = 20 - -_level_to_name = { - ERROR: "ERROR", - WARNING: "WARNING", - INFO: "INFO", -} -_name_to_level = {name: level for (level, name) in _level_to_name.items()} +@enum.unique +class SEVERITY(enum.IntEnum): + INFO = 20 + WARNING = 30 + ERROR = 40 @enum.unique @@ -150,13 +145,6 @@ """URI path to the current schema""" -def level_name(level): - name = _level_to_name.get(level) - if name is None: - name = "Level {}".format(level) - return name - - def problem_message(pr): if isinstance(pr, ValidationProblem): code = getattr(pr, "code", None) @@ -191,12 +179,14 @@ if severity is None: # autodetermine if code in ERRORS: - self.severity = ERROR + self.severity = SEVERITY.ERROR elif code in WARNINGS: - self.severity = WARNING + self.severity = SEVERITY.WARNING else: assert False else: + if not isinstance(severity, SEVERITY): + raise TypeError("invalid type for `severity'") self.severity = severity self.hint = hint self.context = context @@ -216,11 +206,11 @@ except LookupError: msg = "" if self.index is None: - return "ValidationProblem(code={}{}, severity={!r}, hint={}, context=[depth={}]{})".format( - self.code.name, msg, self.severity, self.hint, self.context.depth, self.context) + return "ValidationProblem(code={}{}, severity={}, hint={}, context=[depth={}]{})".format( + self.code.name, msg, self.severity.name, self.hint, self.context.depth, self.context) else: - return "ValidationProblem(code={}{}, severity={!r}, hint={}, context=[depth={}]{}, index={})".format( - self.code.name, msg, self.severity, self.hint, self.context.depth, self.context, self.index) + return "ValidationProblem(code={}{}, severity={}, hint={}, context=[depth={}]{}, index={})".format( + self.code.name, msg, self.severity.name, self.hint, self.context.depth, self.context, self.index) class SchemaError(Exception):
--- a/tests/test_schema.py Fri Jul 07 02:17:45 2023 +0200 +++ b/tests/test_schema.py Fri Jul 07 02:33:28 2023 +0200 @@ -10,7 +10,7 @@ import data_schema import data_schema.util -from data_schema import ERRORS, WARNINGS +from data_schema import SEVERITY, ERRORS, WARNINGS TYPE_RE = type(re.compile(r"\A.+\Z")) @@ -1334,6 +1334,7 @@ code = ERRORS.E10001 self.assertEqual("ERRORS.E10001", str(code)) self.assertEqual("E10001", code.name) + self.assertTrue(isinstance(code, ERRORS)) def test_schema_must_be_a_dict_alike(self): try: @@ -1349,11 +1350,11 @@ def test_error_ctor(self): v = data_schema.ValidationProblem(code=ERRORS.E10000) - self.assertEqual(data_schema.ERROR, v.severity) + self.assertEqual(SEVERITY.ERROR, v.severity) def test_warning_ctor(self): v = data_schema.ValidationProblem(code=WARNINGS.W80000) - self.assertEqual(data_schema.WARNING, v.severity) + self.assertEqual(SEVERITY.WARNING, v.severity) def test_d1(self): x = list(data_schema.validate({}, {"type": "dict"})) @@ -1372,7 +1373,7 @@ def test_d2(self): x = list(data_schema.validate([], {"type": "map"})) self.assertEqual(1, len(x)) - self.assertEqual(data_schema.ERROR, x[0].severity) + self.assertEqual(SEVERITY.ERROR, x[0].severity) self.assertEqual(ERRORS.E10000, x[0].code) def test_d3(self): @@ -1381,10 +1382,10 @@ {"type": "dict", "required": ["key2"]})) self.assertEqual(2, len(x)) - self.assertEqual(data_schema.ERROR, x[0].severity) + self.assertEqual(SEVERITY.ERROR, x[0].severity) self.assertEqual(ERRORS.E10004, x[0].code) self.assertEqual("key", x[0].hint) - self.assertEqual(data_schema.ERROR, x[1].severity) + self.assertEqual(SEVERITY.ERROR, x[1].severity) self.assertEqual(ERRORS.E10005, x[1].code) self.assertEqual(["key2"], x[1].hint)
