File size: 1,374 Bytes
079c32c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from typing import Mapping

from .base import Loader, CAPTURE_EXCEPTIONS, ILoaderClass
from .exception import CompositeStructureError

DICT_ERRORS = Mapping[str, Exception]


class DictError(CompositeStructureError):
    """
    Overview:
        Dict error.
    Interfaces:
        ``__init__``, ``errors``
    Properties:
        ``errors``
    """

    def __init__(self, errors: DICT_ERRORS):
        """
        Overview:
            Initialize the DictError.
        Arguments:
            - errors (:obj:`DICT_ERRORS`): The errors.
        """

        self.__error = errors

    @property
    def errors(self) -> DICT_ERRORS:
        """
        Overview:
            Get the errors.
        """

        return self.__error


def dict_(**kwargs) -> ILoaderClass:
    """
    Overview:
        Create a dict loader.
    Arguments:
        - kwargs (:obj:`Mapping[str, ILoaderClass]`): The loaders.
    """

    kwargs = [(k, Loader(v)) for k, v in kwargs.items()]

    def _load(value):
        _errors = {}
        _results = {}

        for k, vl in kwargs:
            try:
                v = vl(value)
            except CAPTURE_EXCEPTIONS as err:
                _errors[k] = err
            else:
                _results[k] = v

        if not _errors:
            return _results
        else:
            raise DictError(_errors)

    return Loader(_load)