meta.py 6.87 KB
Newer Older
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
1
2
3
4
5
6
7
8
9
10
'''
Meta-programming for the models.
'''
import os
import json
import jsonschema
import inspect
import copy

from abc import ABCMeta
11
from collections import MutableMapping, namedtuple
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35


class BaseMeta(ABCMeta):
    '''
    Metaclass for models. It extracts the default values for the fields in
    the model.

    For instance, instances of the following class wouldn't need to mark
    their version or description on initialization:

    .. code-block:: python

       class MyPlugin(Plugin):
           version=0.3
           description='A dull plugin'


    Note that these operations could be included in the __init__ of the
    class, but it would be very inefficient.
    '''
    _subtypes = {}

    def __new__(mcs, name, bases, attrs, **kwargs):
        register_afterwards = False
36
        defaults = {}
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
37
38
39
40

        attrs = mcs.expand_with_schema(name, attrs)
        if 'schema' in attrs:
            register_afterwards = True
41
42
43
44
45
46
47
48
49
50
51
52
53
54
        for base in bases:
            if hasattr(base, '_defaults'):
                defaults.update(getattr(base, '_defaults'))

        info, rest = mcs.split_attrs(attrs)

        for i in list(info.keys()):
            if isinstance(info[i], _Alias):
                fget, fset, fdel = make_property(info[i].indict)
                rest[i] = property(fget=fget, fset=fset, fdel=fdel)
            else:
                defaults[i] = info[i]

        rest['_defaults'] = defaults
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
55

56
        cls = super(BaseMeta, mcs).__new__(mcs, name, tuple(bases), rest)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
57
58

        if register_afterwards:
59
            mcs.register(cls, defaults['@type'])
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
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
        return cls

    @classmethod
    def register(mcs, rsubclass, rtype=None):
        mcs._subtypes[rtype or rsubclass.__name__] = rsubclass

    @staticmethod
    def expand_with_schema(name, attrs):
        if 'schema' in attrs:  # Schema specified by name
            schema_file = '{}.json'.format(attrs['schema'])
        elif 'schema_file' in attrs:
            schema_file = attrs['schema_file']
            del attrs['schema_file']
        else:
            return attrs

        if '/' not in 'schema_file':
            thisdir = os.path.dirname(os.path.realpath(__file__))
            schema_file = os.path.join(thisdir,
                                       'schemas',
                                       schema_file)

        schema_path = 'file://' + schema_file

        with open(schema_file) as f:
            schema = json.load(f)

        resolver = jsonschema.RefResolver(schema_path, schema)
        attrs['@type'] = "".join((name[0].lower(), name[1:]))
        attrs['_schema_file'] = schema_file
        attrs['schema'] = schema
        attrs['_validator'] = jsonschema.Draft4Validator(schema, resolver=resolver)
92
93
94
95

        schema_defaults = BaseMeta.get_defaults(attrs['schema'])
        attrs.update(schema_defaults)

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
96
97
98
        return attrs

    @staticmethod
99
100
101
102
103
104
105
106
107
108
109
110
111
    def is_func(v):
        return inspect.isroutine(v) or inspect.ismethod(v) or \
            inspect.ismodule(v) or isinstance(v, property)

    @staticmethod
    def is_internal(k):
        return k[0] == '_' or k == 'schema' or k == 'data'

    @staticmethod
    def get_key(key):
        if key[0] != '_':
            key = key.replace("__", ":", 1)
        return key
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
112
113
114
115
116
117
118
119
120
121

    @staticmethod
    def split_attrs(attrs):
        '''
        Extract the attributes of the class.

        This allows adding default values in the class definition.
        e.g.:
        '''
        isattr = {}
122
        rest = {}
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
123
        for key, value in attrs.items():
124
125
            if not (BaseMeta.is_internal(key)) and (not BaseMeta.is_func(value)):
                isattr[key] = value
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
126
            else:
127
128
                rest[key] = value
        return isattr, rest
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
129
130
131
132
133
134
135
136
137

    @staticmethod
    def get_defaults(schema):
        temp = {}
        for obj in [
                schema,
        ] + schema.get('allOf', []):
            for k, v in obj.get('properties', {}).items():
                if 'default' in v and k not in temp:
138
                    temp[k] = v['default']
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
139
        return temp
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257


def make_property(key):

    def fget(self):
        return self[key]

    def fdel(self):
        del self[key]

    def fset(self, value):
        self[key] = value

    return fget, fset, fdel


class CustomDict(MutableMapping, object):
    '''
    A dictionary whose elements can also be accessed as attributes. Since some
    characters are not valid in the dot-notation, the attribute names also
    converted. e.g.:

    > d = CustomDict()
    > d.key = d['ns:name'] = 1
    > d.key == d['key']
    True
    > d.ns__name == d['ns:name']
    '''

    _defaults = {}
    _map_attr_key = {'id': '@id'}

    def __init__(self, *args, **kwargs):
        super(CustomDict, self).__init__()
        for k, v in self._defaults.items():
            self[k] = copy.copy(v)
        for arg in args:
            self.update(arg)
        for k, v in kwargs.items():
            self[self._attr_to_key(k)] = v
        return self

    def serializable(self):
        def ser_or_down(item):
            if hasattr(item, 'serializable'):
                return item.serializable()
            elif isinstance(item, dict):
                temp = dict()
                for kp in item:
                    vp = item[kp]
                    temp[kp] = ser_or_down(vp)
                return temp
            elif isinstance(item, list) or isinstance(item, set):
                return list(ser_or_down(i) for i in item)
            else:
                return item

        return ser_or_down(self.as_dict())

    def __getitem__(self, key):
        key = self._key_to_attr(key)
        return self.__dict__[key]

    def __setitem__(self, key, value):
        '''Do not insert data directly, there might be a property in that key. '''
        key = self._key_to_attr(key)
        return setattr(self, key, value)

    def as_dict(self):
        return {self._attr_to_key(k): v for k, v in self.__dict__.items()
                if not self._internal_key(k)}

    def __iter__(self):
        return (k for k in self.__dict__ if not self._internal_key(k))

    def __len__(self):
        return len(self.__dict__)

    def __delitem__(self, key):
        del self.__dict__[key]

    def update(self, other):
        for k, v in other.items():
            self[k] = v

    def _attr_to_key(self, key):
        key = key.replace("__", ":", 1)
        key = self._map_attr_key.get(key, key)
        return key

    def _key_to_attr(self, key):
        if self._internal_key(key):
            return key
        key = key.replace(":", "__", 1)
        return key

    def __getattr__(self, key):
        try:
            return self.__dict__[self._attr_to_key(key)]
        except KeyError:
            raise AttributeError

    @staticmethod
    def _internal_key(key):
        return key[0] == '_'

    def __str__(self):
        return str(self.serializable())

    def __repr__(self):
        return str(self.serializable())


_Alias = namedtuple('Alias', 'indict')


def alias(key):
    return _Alias(key)