voluptuous-errors 0.0__tar.gz → 0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- voluptuous_errors-0.1/LICENSE +21 -0
- voluptuous_errors-0.1/PKG-INFO +95 -0
- voluptuous_errors-0.1/README.rst +74 -0
- voluptuous_errors-0.1/pyproject.toml +44 -0
- voluptuous_errors-0.1/voluptuous_errors.py +83 -0
- voluptuous_errors-0.0/PKG-INFO +0 -77
- voluptuous_errors-0.0/README.rst +0 -66
- voluptuous_errors-0.0/pyproject.toml +0 -15
- voluptuous_errors-0.0/voluptuous_errors.py +0 -49
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Ken Kundert
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: voluptuous_errors
|
|
3
|
+
Version: 0.1
|
|
4
|
+
Summary: report voluptuous errors
|
|
5
|
+
Author: Ken Kundert
|
|
6
|
+
Author-email: quantiphy@nurdletech.com
|
|
7
|
+
Requires-Python: >=3.8
|
|
8
|
+
Description-Content-Type: text/x-rst
|
|
9
|
+
Classifier: Intended Audience :: Developers
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Natural Language :: English
|
|
12
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
License-File: LICENSE
|
|
15
|
+
Requires-Dist: inform
|
|
16
|
+
Requires-Dist: nestedtext
|
|
17
|
+
Requires-Dist: voluptuous
|
|
18
|
+
Project-URL: homepage, https://github.com/KenKundert/voluptuous_errors
|
|
19
|
+
Project-URL: repository, https://github.com/KenKundert/voluptuous_errors
|
|
20
|
+
|
|
21
|
+
Voluptuous Errors — Voluptuous Error Reporting when using NestedText and Inform
|
|
22
|
+
===============================================================================
|
|
23
|
+
|
|
24
|
+
.. image:: https://pepy.tech/badge/voluptuous_errors/month
|
|
25
|
+
:target: https://pepy.tech/project/voluptuous_errors
|
|
26
|
+
|
|
27
|
+
.. image:: https://github.com/KenKundert/voluptuous_errors/actions/workflows/build.yaml/badge.svg
|
|
28
|
+
:target: https://github.com/KenKundert/voluptuous_errors/actions/workflows/build.yaml
|
|
29
|
+
|
|
30
|
+
.. image:: https://coveralls.io/repos/github/KenKundert/voluptuous_errors/badge.svg?branch=master
|
|
31
|
+
:target: https://coveralls.io/github/KenKundert/voluptuous_errors?branch=master
|
|
32
|
+
|
|
33
|
+
.. image:: https://img.shields.io/pypi/v/voluptuous_errors.svg
|
|
34
|
+
:target: https://pypi.python.org/pypi/voluptuous_errors
|
|
35
|
+
|
|
36
|
+
.. image:: https://img.shields.io/pypi/pyversions/voluptuous_errors.svg
|
|
37
|
+
:target: https://pypi.python.org/pypi/voluptuous_errors/
|
|
38
|
+
|
|
39
|
+
:Author: Ken Kundert
|
|
40
|
+
:Version: 0.0.0
|
|
41
|
+
:Released: 2024-02-25
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
A convenience function used for reporting voluptuous_ errors from nestedtext_
|
|
45
|
+
with inform_. Here is a typical use of this function::
|
|
46
|
+
|
|
47
|
+
>>> from voluptuous import Schema, Invalid, MultipleInvalid, Required
|
|
48
|
+
>>> from voluptuous_errors import report_voluptuous_errors
|
|
49
|
+
>>> from inform import error, os_error, terminate
|
|
50
|
+
>>> import nestedtext as nt
|
|
51
|
+
|
|
52
|
+
>>> try:
|
|
53
|
+
... settings_path = Path('settings.nt')
|
|
54
|
+
... settings = nt.load(
|
|
55
|
+
... settings_path,
|
|
56
|
+
... keymap = (keymap:={}),
|
|
57
|
+
... )
|
|
58
|
+
... settings = schema(settings)
|
|
59
|
+
>>> except nt.NestedTextError as e:
|
|
60
|
+
... e.report()
|
|
61
|
+
>>> except MultipleInvalid as e:
|
|
62
|
+
... report_voluptuous_errors(e, keymap, settings_path)
|
|
63
|
+
>>> except OSError as e:
|
|
64
|
+
... error(os_error(e))
|
|
65
|
+
>>> terminate()
|
|
66
|
+
|
|
67
|
+
This code reports all errors found by *Voluptuous* when reading the settings
|
|
68
|
+
file. It employs the *NestedText* *keymap* facility to annotate the error
|
|
69
|
+
messages with helpful context such as line numbers.
|
|
70
|
+
|
|
71
|
+
You can map Voluptuous error messages by importing and modifying
|
|
72
|
+
*voluptuous_error_msg_mappings*. For example::
|
|
73
|
+
|
|
74
|
+
voluptuous_error_msg_mappings["expected a table name"] = ("unknown table.", "key")
|
|
75
|
+
|
|
76
|
+
The value consists of two values, the new message, and the location they message
|
|
77
|
+
refers to. This second value may be either "key" or "value".
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
Releases
|
|
82
|
+
--------
|
|
83
|
+
|
|
84
|
+
**Latest development release**:
|
|
85
|
+
| Version: 0.0.0
|
|
86
|
+
| Released: 2024-03-25
|
|
87
|
+
|
|
88
|
+
**0.0 (2024-02-25)**:
|
|
89
|
+
Initial version.
|
|
90
|
+
|
|
91
|
+
.. _voluptuous: https://github.com/alecthomas/voluptuous
|
|
92
|
+
.. _nestedtext: https://nestedtext.org
|
|
93
|
+
.. _inform: https://inform.readthedocs.io
|
|
94
|
+
|
|
95
|
+
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
Voluptuous Errors — Voluptuous Error Reporting when using NestedText and Inform
|
|
2
|
+
===============================================================================
|
|
3
|
+
|
|
4
|
+
.. image:: https://pepy.tech/badge/voluptuous_errors/month
|
|
5
|
+
:target: https://pepy.tech/project/voluptuous_errors
|
|
6
|
+
|
|
7
|
+
.. image:: https://github.com/KenKundert/voluptuous_errors/actions/workflows/build.yaml/badge.svg
|
|
8
|
+
:target: https://github.com/KenKundert/voluptuous_errors/actions/workflows/build.yaml
|
|
9
|
+
|
|
10
|
+
.. image:: https://coveralls.io/repos/github/KenKundert/voluptuous_errors/badge.svg?branch=master
|
|
11
|
+
:target: https://coveralls.io/github/KenKundert/voluptuous_errors?branch=master
|
|
12
|
+
|
|
13
|
+
.. image:: https://img.shields.io/pypi/v/voluptuous_errors.svg
|
|
14
|
+
:target: https://pypi.python.org/pypi/voluptuous_errors
|
|
15
|
+
|
|
16
|
+
.. image:: https://img.shields.io/pypi/pyversions/voluptuous_errors.svg
|
|
17
|
+
:target: https://pypi.python.org/pypi/voluptuous_errors/
|
|
18
|
+
|
|
19
|
+
:Author: Ken Kundert
|
|
20
|
+
:Version: 0.0.0
|
|
21
|
+
:Released: 2024-02-25
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
A convenience function used for reporting voluptuous_ errors from nestedtext_
|
|
25
|
+
with inform_. Here is a typical use of this function::
|
|
26
|
+
|
|
27
|
+
>>> from voluptuous import Schema, Invalid, MultipleInvalid, Required
|
|
28
|
+
>>> from voluptuous_errors import report_voluptuous_errors
|
|
29
|
+
>>> from inform import error, os_error, terminate
|
|
30
|
+
>>> import nestedtext as nt
|
|
31
|
+
|
|
32
|
+
>>> try:
|
|
33
|
+
... settings_path = Path('settings.nt')
|
|
34
|
+
... settings = nt.load(
|
|
35
|
+
... settings_path,
|
|
36
|
+
... keymap = (keymap:={}),
|
|
37
|
+
... )
|
|
38
|
+
... settings = schema(settings)
|
|
39
|
+
>>> except nt.NestedTextError as e:
|
|
40
|
+
... e.report()
|
|
41
|
+
>>> except MultipleInvalid as e:
|
|
42
|
+
... report_voluptuous_errors(e, keymap, settings_path)
|
|
43
|
+
>>> except OSError as e:
|
|
44
|
+
... error(os_error(e))
|
|
45
|
+
>>> terminate()
|
|
46
|
+
|
|
47
|
+
This code reports all errors found by *Voluptuous* when reading the settings
|
|
48
|
+
file. It employs the *NestedText* *keymap* facility to annotate the error
|
|
49
|
+
messages with helpful context such as line numbers.
|
|
50
|
+
|
|
51
|
+
You can map Voluptuous error messages by importing and modifying
|
|
52
|
+
*voluptuous_error_msg_mappings*. For example::
|
|
53
|
+
|
|
54
|
+
voluptuous_error_msg_mappings["expected a table name"] = ("unknown table.", "key")
|
|
55
|
+
|
|
56
|
+
The value consists of two values, the new message, and the location they message
|
|
57
|
+
refers to. This second value may be either "key" or "value".
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
Releases
|
|
62
|
+
--------
|
|
63
|
+
|
|
64
|
+
**Latest development release**:
|
|
65
|
+
| Version: 0.0.0
|
|
66
|
+
| Released: 2024-03-25
|
|
67
|
+
|
|
68
|
+
**0.0 (2024-02-25)**:
|
|
69
|
+
Initial version.
|
|
70
|
+
|
|
71
|
+
.. _voluptuous: https://github.com/alecthomas/voluptuous
|
|
72
|
+
.. _nestedtext: https://nestedtext.org
|
|
73
|
+
.. _inform: https://inform.readthedocs.io
|
|
74
|
+
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "voluptuous_errors"
|
|
3
|
+
version = "0.1"
|
|
4
|
+
description = "report voluptuous errors"
|
|
5
|
+
readme = "README.rst"
|
|
6
|
+
requires-python = ">=3.8"
|
|
7
|
+
dependencies = [
|
|
8
|
+
"inform",
|
|
9
|
+
"nestedtext",
|
|
10
|
+
"voluptuous",
|
|
11
|
+
]
|
|
12
|
+
authors = [
|
|
13
|
+
{name = "Ken Kundert"},
|
|
14
|
+
{email = "quantiphy@nurdletech.com"}
|
|
15
|
+
]
|
|
16
|
+
classifiers = [
|
|
17
|
+
# "Development Status :: 5 - Production/Stable",
|
|
18
|
+
"Intended Audience :: Developers",
|
|
19
|
+
"License :: OSI Approved :: MIT License",
|
|
20
|
+
"Natural Language :: English",
|
|
21
|
+
"Operating System :: POSIX :: Linux",
|
|
22
|
+
"Programming Language :: Python :: 3",
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
[project.urls]
|
|
26
|
+
homepage = "https://github.com/KenKundert/voluptuous_errors"
|
|
27
|
+
repository = "https://github.com/KenKundert/voluptuous_errors"
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
[build-system]
|
|
31
|
+
requires = ["flit_core >=2,<4"]
|
|
32
|
+
build-backend = "flit_core.buildapi"
|
|
33
|
+
|
|
34
|
+
[tool.pytest.ini_options]
|
|
35
|
+
addopts = "--tb=short"
|
|
36
|
+
|
|
37
|
+
[tool.ruff]
|
|
38
|
+
exclude = [".tox", "doc", "Diffs"]
|
|
39
|
+
|
|
40
|
+
[tool.ruff.lint]
|
|
41
|
+
select = ["F"]
|
|
42
|
+
ignore = []
|
|
43
|
+
|
|
44
|
+
[tool.ruff.lint.per-file-ignores]
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# DESCRIPTION {{{1
|
|
2
|
+
# Provides a convenience function used for reporting voluptuous errors when
|
|
3
|
+
# using with NestedText and inform. Uses Inform's error() function when
|
|
4
|
+
# reporting the errors as it allows for multiple errors to be reported.
|
|
5
|
+
|
|
6
|
+
# IMPORTS {{{1
|
|
7
|
+
from inform import cull, error, full_stop, is_array, is_mapping
|
|
8
|
+
import nestedtext as nt
|
|
9
|
+
|
|
10
|
+
# GLOBALS {{{1
|
|
11
|
+
voluptuous_error_msg_mappings = {
|
|
12
|
+
"extra keys not allowed": ("unknown key", "key"),
|
|
13
|
+
"expected a dict": ("expected a key-value pair", "value"),
|
|
14
|
+
"expected a dictionary": ("expected a key-value pair", "value"),
|
|
15
|
+
"required key not provided": ("required key is missing", "value"),
|
|
16
|
+
}
|
|
17
|
+
__version__ = '0.1'
|
|
18
|
+
__released__ = '2026-06-19'
|
|
19
|
+
|
|
20
|
+
# _nested_getvalue() {{{1
|
|
21
|
+
def _nested_getvalue(data, path):
|
|
22
|
+
for item_index in path:
|
|
23
|
+
try:
|
|
24
|
+
data = data[item_index]
|
|
25
|
+
except (KeyError, IndexError, TypeError):
|
|
26
|
+
# The index is not present in the dictionary, list or other
|
|
27
|
+
# indexable or data is not subscriptable
|
|
28
|
+
return None
|
|
29
|
+
return data
|
|
30
|
+
|
|
31
|
+
# _summarize_value() {{{1
|
|
32
|
+
def _summarize_value(value):
|
|
33
|
+
if is_array(value):
|
|
34
|
+
return 'list'
|
|
35
|
+
if is_mapping(value):
|
|
36
|
+
return 'key-value pair'
|
|
37
|
+
value = repr(value)
|
|
38
|
+
if len(value) > 20:
|
|
39
|
+
return value[:20] + ' ...'
|
|
40
|
+
return value
|
|
41
|
+
|
|
42
|
+
# report_voluptuous_errors() {{{1
|
|
43
|
+
def report_voluptuous_errors(multiple_invalid, data=None, *, keymap=None, source=None, sep="›", path_fmt="{path}@{lines}"):
|
|
44
|
+
source = str(source) if source else ""
|
|
45
|
+
|
|
46
|
+
for err in multiple_invalid.errors:
|
|
47
|
+
|
|
48
|
+
# convert message to something easier for non-savvy user to understand
|
|
49
|
+
msg, kind = voluptuous_error_msg_mappings.get(
|
|
50
|
+
err.msg, (err.msg, 'value')
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
# get metadata about error
|
|
54
|
+
codicil = ()
|
|
55
|
+
if keymap:
|
|
56
|
+
culprit = nt.get_keys(err.path, keymap=keymap, strict="found", sep=sep)
|
|
57
|
+
line_nums = nt.get_line_numbers(err.path, keymap, kind=kind, sep="-", strict=False)
|
|
58
|
+
loc = nt.get_location(err.path, keymap)
|
|
59
|
+
if data and kind == 'value':
|
|
60
|
+
try:
|
|
61
|
+
value = nt.get_value(data, err.path)
|
|
62
|
+
if value is not None:
|
|
63
|
+
codicil = (f"Found {_summarize_value(value)}.",)
|
|
64
|
+
except KeyError:
|
|
65
|
+
pass
|
|
66
|
+
if loc:
|
|
67
|
+
codicil += (loc.as_line(kind),)
|
|
68
|
+
else: # required key is missing
|
|
69
|
+
missing = nt.get_keys(err.path, keymap, strict="missing", sep=sep)
|
|
70
|
+
codicil += (f"‘{missing}’ was not found.",)
|
|
71
|
+
|
|
72
|
+
file_and_lineno = path_fmt.format(path=str(source), lines=line_nums)
|
|
73
|
+
culprit = cull((file_and_lineno, culprit))
|
|
74
|
+
else:
|
|
75
|
+
keys = sep.join(str(c) for c in err.path)
|
|
76
|
+
culprit = cull([source, keys])
|
|
77
|
+
if data and kind == 'value':
|
|
78
|
+
value = _nested_getvalue(data, err.path)
|
|
79
|
+
if value is not None:
|
|
80
|
+
codicil = (f"Found {_summarize_value(value)}.",)
|
|
81
|
+
|
|
82
|
+
# report error
|
|
83
|
+
error(full_stop(msg), culprit=culprit, codicil=codicil)
|
voluptuous_errors-0.0/PKG-INFO
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.1
|
|
2
|
-
Name: voluptuous_errors
|
|
3
|
-
Version: 0.0
|
|
4
|
-
Summary: report voluptuous errors
|
|
5
|
-
Requires-Python: >=3.6
|
|
6
|
-
Description-Content-Type: text/x-rst
|
|
7
|
-
Requires-Dist: inform
|
|
8
|
-
Requires-Dist: nestedtext>=3.7
|
|
9
|
-
Requires-Dist: voluptuous
|
|
10
|
-
|
|
11
|
-
Voluptuous Errors — Error Reporting for Voluptuous with NestedText and Inform
|
|
12
|
-
=============================================================================
|
|
13
|
-
|
|
14
|
-
.. ignore for now:
|
|
15
|
-
|
|
16
|
-
.. image:: https://pepy.tech/badge/voluptuous_errors/month
|
|
17
|
-
:target: https://pepy.tech/project/voluptuous_errors
|
|
18
|
-
|
|
19
|
-
.. image:: https://github.com/KenKundert/voluptuous_errors/actions/workflows/build.yaml/badge.svg
|
|
20
|
-
:target: https://github.com/KenKundert/voluptuous_errors/actions/workflows/build.yaml
|
|
21
|
-
|
|
22
|
-
.. image:: https://coveralls.io/repos/github/KenKundert/voluptuous_errors/badge.svg?branch=master
|
|
23
|
-
:target: https://coveralls.io/github/KenKundert/voluptuous_errors?branch=master
|
|
24
|
-
|
|
25
|
-
.. image:: https://img.shields.io/pypi/v/voluptuous_errors.svg
|
|
26
|
-
:target: https://pypi.python.org/pypi/voluptuous_errors
|
|
27
|
-
|
|
28
|
-
.. image:: https://img.shields.io/pypi/pyversions/voluptuous_errors.svg
|
|
29
|
-
:target: https://pypi.python.org/pypi/voluptuous_errors/
|
|
30
|
-
|
|
31
|
-
:Author: Ken Kundert
|
|
32
|
-
:Version: 0.0.0
|
|
33
|
-
:Released: 2024-02-25
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
A convenience function used for reporting voluptuous_ errors from nestedtext_
|
|
37
|
-
with inform_. Here is a typical use of this function::
|
|
38
|
-
|
|
39
|
-
>>> from voluptuous import Schema, Invalid, MultipleInvalid, Required
|
|
40
|
-
>>> from voluptuous_errors import report_voluptuous_errors
|
|
41
|
-
>>> from inform import error, os_error, terminate
|
|
42
|
-
>>> import nestedtext as nt
|
|
43
|
-
|
|
44
|
-
>>> try:
|
|
45
|
-
... settings_path = Path('settings.nt')
|
|
46
|
-
... settings = nt.load(
|
|
47
|
-
... settings_path,
|
|
48
|
-
... keymap = (keymap:={}),
|
|
49
|
-
... )
|
|
50
|
-
... settings = schema(settings)
|
|
51
|
-
>>> except nt.NestedTextError as e:
|
|
52
|
-
... e.report()
|
|
53
|
-
>>> except MultipleInvalid as e:
|
|
54
|
-
... report_voluptuous_errors(e, keymap, settings_path)
|
|
55
|
-
>>> except OSError as e:
|
|
56
|
-
... error(os_error(e))
|
|
57
|
-
>>> terminate()
|
|
58
|
-
|
|
59
|
-
This code will report all errors found by Voluptuous when reading the settings
|
|
60
|
-
file.
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
Releases
|
|
64
|
-
--------
|
|
65
|
-
|
|
66
|
-
**Latest development release**:
|
|
67
|
-
| Version: 0.5.0
|
|
68
|
-
| Released: 2022-09-02
|
|
69
|
-
|
|
70
|
-
**0.0 (2024-02-25)**:
|
|
71
|
-
Initial version.
|
|
72
|
-
|
|
73
|
-
.. _voluptuous: https://github.com/alecthomas/voluptuous
|
|
74
|
-
.. _nestedtext: https://nestedtext.org
|
|
75
|
-
.. _inform: https://readthedocs.io/inform
|
|
76
|
-
|
|
77
|
-
|
voluptuous_errors-0.0/README.rst
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
Voluptuous Errors — Error Reporting for Voluptuous with NestedText and Inform
|
|
2
|
-
=============================================================================
|
|
3
|
-
|
|
4
|
-
.. ignore for now:
|
|
5
|
-
|
|
6
|
-
.. image:: https://pepy.tech/badge/voluptuous_errors/month
|
|
7
|
-
:target: https://pepy.tech/project/voluptuous_errors
|
|
8
|
-
|
|
9
|
-
.. image:: https://github.com/KenKundert/voluptuous_errors/actions/workflows/build.yaml/badge.svg
|
|
10
|
-
:target: https://github.com/KenKundert/voluptuous_errors/actions/workflows/build.yaml
|
|
11
|
-
|
|
12
|
-
.. image:: https://coveralls.io/repos/github/KenKundert/voluptuous_errors/badge.svg?branch=master
|
|
13
|
-
:target: https://coveralls.io/github/KenKundert/voluptuous_errors?branch=master
|
|
14
|
-
|
|
15
|
-
.. image:: https://img.shields.io/pypi/v/voluptuous_errors.svg
|
|
16
|
-
:target: https://pypi.python.org/pypi/voluptuous_errors
|
|
17
|
-
|
|
18
|
-
.. image:: https://img.shields.io/pypi/pyversions/voluptuous_errors.svg
|
|
19
|
-
:target: https://pypi.python.org/pypi/voluptuous_errors/
|
|
20
|
-
|
|
21
|
-
:Author: Ken Kundert
|
|
22
|
-
:Version: 0.0.0
|
|
23
|
-
:Released: 2024-02-25
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
A convenience function used for reporting voluptuous_ errors from nestedtext_
|
|
27
|
-
with inform_. Here is a typical use of this function::
|
|
28
|
-
|
|
29
|
-
>>> from voluptuous import Schema, Invalid, MultipleInvalid, Required
|
|
30
|
-
>>> from voluptuous_errors import report_voluptuous_errors
|
|
31
|
-
>>> from inform import error, os_error, terminate
|
|
32
|
-
>>> import nestedtext as nt
|
|
33
|
-
|
|
34
|
-
>>> try:
|
|
35
|
-
... settings_path = Path('settings.nt')
|
|
36
|
-
... settings = nt.load(
|
|
37
|
-
... settings_path,
|
|
38
|
-
... keymap = (keymap:={}),
|
|
39
|
-
... )
|
|
40
|
-
... settings = schema(settings)
|
|
41
|
-
>>> except nt.NestedTextError as e:
|
|
42
|
-
... e.report()
|
|
43
|
-
>>> except MultipleInvalid as e:
|
|
44
|
-
... report_voluptuous_errors(e, keymap, settings_path)
|
|
45
|
-
>>> except OSError as e:
|
|
46
|
-
... error(os_error(e))
|
|
47
|
-
>>> terminate()
|
|
48
|
-
|
|
49
|
-
This code will report all errors found by Voluptuous when reading the settings
|
|
50
|
-
file.
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
Releases
|
|
54
|
-
--------
|
|
55
|
-
|
|
56
|
-
**Latest development release**:
|
|
57
|
-
| Version: 0.5.0
|
|
58
|
-
| Released: 2022-09-02
|
|
59
|
-
|
|
60
|
-
**0.0 (2024-02-25)**:
|
|
61
|
-
Initial version.
|
|
62
|
-
|
|
63
|
-
.. _voluptuous: https://github.com/alecthomas/voluptuous
|
|
64
|
-
.. _nestedtext: https://nestedtext.org
|
|
65
|
-
.. _inform: https://readthedocs.io/inform
|
|
66
|
-
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
[project]
|
|
2
|
-
name = "voluptuous_errors"
|
|
3
|
-
version = "0.0"
|
|
4
|
-
description = "report voluptuous errors"
|
|
5
|
-
readme = "README.rst"
|
|
6
|
-
requires-python = ">=3.6"
|
|
7
|
-
dependencies = [
|
|
8
|
-
"inform",
|
|
9
|
-
"nestedtext>=3.7",
|
|
10
|
-
"voluptuous",
|
|
11
|
-
]
|
|
12
|
-
|
|
13
|
-
[build-system]
|
|
14
|
-
requires = ["flit_core >=2,<4"]
|
|
15
|
-
build-backend = "flit_core.buildapi"
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
# DESCRIPTION {{{1
|
|
2
|
-
# Provides a convenience function used for reporting voluptuous errors when
|
|
3
|
-
# using with NestedText and inform. Uses Inform's error() function when
|
|
4
|
-
# reporting the errors as it allows for multiple errors to be reported.
|
|
5
|
-
|
|
6
|
-
# IMPORTS {{{1
|
|
7
|
-
from inform import cull, error, full_stop
|
|
8
|
-
import nestedtext as nt
|
|
9
|
-
|
|
10
|
-
# GLOBALS {{{1
|
|
11
|
-
voluptuous_error_msg_mappings = {
|
|
12
|
-
"extra keys not allowed": ("unknown key", "key"),
|
|
13
|
-
"expected a dictionary": ("expected a key-value pair", "value"),
|
|
14
|
-
"required key not provided": ("required key is missing", "value"),
|
|
15
|
-
}
|
|
16
|
-
__version__ = '0.0.0'
|
|
17
|
-
__released__ = '2024-02-25'
|
|
18
|
-
|
|
19
|
-
# report_voluptuous_errors() {{{1
|
|
20
|
-
def report_voluptuous_errors(multiple_invalid, keymap, source=None, sep="›"):
|
|
21
|
-
source = str(source) if source else ""
|
|
22
|
-
|
|
23
|
-
for err in multiple_invalid.errors:
|
|
24
|
-
|
|
25
|
-
# convert message to something easier for non-savvy user to understand
|
|
26
|
-
msg, kind = voluptuous_error_msg_mappings.get(
|
|
27
|
-
err.msg, (err.msg, 'value')
|
|
28
|
-
)
|
|
29
|
-
|
|
30
|
-
# get metadata about error
|
|
31
|
-
if keymap:
|
|
32
|
-
culprit = nt.get_keys(err.path, keymap=keymap, strict="found", sep=sep)
|
|
33
|
-
line_nums = nt.get_line_numbers(err.path, keymap, kind=kind, sep="-", strict=False)
|
|
34
|
-
loc = nt.get_location(err.path, keymap)
|
|
35
|
-
if loc:
|
|
36
|
-
codicil = loc.as_line(kind)
|
|
37
|
-
else: # required key is missing
|
|
38
|
-
missing = nt.get_keys(err.path, keymap, strict="missing", sep=sep)
|
|
39
|
-
codicil = f"‘{missing}’ was not found."
|
|
40
|
-
|
|
41
|
-
file_and_lineno = f"{source!s}@{line_nums}"
|
|
42
|
-
culprit = cull((file_and_lineno, culprit))
|
|
43
|
-
else:
|
|
44
|
-
keys = sep.join(str(c) for c in err.path)
|
|
45
|
-
culprit=cull([source, keys])
|
|
46
|
-
codicil = None
|
|
47
|
-
|
|
48
|
-
# report error
|
|
49
|
-
error(full_stop(msg), culprit=culprit, codicil=codicil)
|