summaryrefslogtreecommitdiff
path: root/grc/core/schema_checker/utils.py
blob: 2fe0bb089f44182b3058dd0e0f06ebf64b2f06a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import collections

Spec = collections.namedtuple('Spec', 'types required item_scheme')


def expand(**kwargs):
    def expand_spec(spec):
        if not isinstance(spec, Spec):
            types_ = spec if isinstance(spec, tuple) else (spec,)
            spec = Spec(types=types_, required=False, item_scheme=None)
        elif not isinstance(spec.types, tuple):
            spec = Spec(types=(spec.types,), required=spec.required,
                        item_scheme=spec.item_scheme)
        return spec
    return {key: expand_spec(value) for key, value in kwargs.items()}


class Message(collections.namedtuple('Message', 'path type message')):
    fmt = '{path}: {type}: {message}'

    def __str__(self):
        return self.fmt.format(**self._asdict())