summaryrefslogtreecommitdiff
path: root/warlock/core.py
blob: ad3f3bdd1e3b10ad4a0c1843c5f6972860112fb5 (plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
"""Core Warlock functionality"""

import copy

import jsonschema


class InvalidOperation(RuntimeError):
    pass


class ValidationError(ValueError):
    pass


def model_factory(schema):
    """Generate a model class based on the provided JSON Schema

    :param schema: dict representing valid JSON schema
    """
    schema = copy.deepcopy(schema)

    def validator(obj):
        """Apply a JSON schema to an object"""
        try:
            jsonschema.validate(obj, schema)
        except jsonschema.ValidationError:
            raise ValidationError()

    class Model(dict):
        """Self-validating model for arbitrary objects"""

        def __init__(self, *args, **kwargs):
            d = dict(*args, **kwargs)

            # we overload setattr so set this manually
            self.__dict__['validator'] = validator
            try:
                self.validator(d)
            except ValidationError:
                raise ValueError()
            else:
                dict.__init__(self, d)

            self.__dict__['changes'] = {}

        def __getattr__(self, key):
            try:
                return self.__getitem__(key)
            except KeyError:
                raise AttributeError(key)

        def __setitem__(self, key, value):
            mutation = dict(self.items())
            mutation[key] = value
            try:
                self.validator(mutation)
            except ValidationError:
                raise InvalidOperation()

            dict.__setitem__(self, key, value)

            self.__dict__['changes'][key] = value

        def __setattr__(self, key, value):
            self.__setitem__(key, value)

        def clear(self):
            raise InvalidOperation()

        def pop(self, key, default=None):
            raise InvalidOperation()

        def popitem(self):
            raise InvalidOperation()

        def __delitem__(self, key):
            raise InvalidOperation()

        # NOTE(termie): This is kind of the opposite of what copy usually does
        def copy(self):
            return copy.deepcopy(dict(self))

        def update(self, other):
            mutation = dict(self.items())
            mutation.update(other)
            try:
                self.validator(mutation)
            except ValidationError:
                raise InvalidOperation()
            dict.update(self, other)

        def iteritems(self):
            return copy.deepcopy(dict(self)).iteritems()

        def items(self):
            return copy.deepcopy(dict(self)).items()

        def itervalues(self):
            return copy.deepcopy(dict(self)).itervalues()

        def values(self):
            return copy.deepcopy(dict(self)).values()

        @property
        def changes(self):
            return copy.deepcopy(self.__dict__['changes'])

    Model.__name__ = str(schema['name'])
    return Model