-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
types.py
397 lines (301 loc) · 11.2 KB
/
types.py
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# This file is part of Buildbot. Buildbot is free software: you can
# redistribute it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation, version 2.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright Buildbot Team Members
from __future__ import absolute_import
from __future__ import print_function
# See "Type Validation" in master/docs/developer/tests.rst
from future.utils import integer_types
from future.utils import iteritems
from future.utils import text_type
import datetime
import json
import re
from buildbot import util
from buildbot.util import bytes2NativeString
class Type(object):
name = None
doc = None
@property
def ramlname(self):
return self.name
def valueFromString(self, arg):
# convert a urldecoded bytestring as given in a URL to a value, or
# raise an exception trying. This parent method raises an exception,
# so if the method is missing in a subclass, it cannot be created from
# a string.
raise TypeError
def cmp(self, val, arg):
argVal = self.valueFromString(arg)
if val < argVal:
return -1
elif val == argVal:
return 0
return 1
def validate(self, name, object):
raise NotImplementedError
def getSpec(self):
r = dict(name=self.name)
if self.doc is not None:
r["doc"] = self.doc
return r
class NoneOk(Type):
def __init__(self, nestedType):
assert isinstance(nestedType, Type)
self.nestedType = nestedType
self.name = self.nestedType.name + " or None"
@property
def ramlname(self):
return self.nestedType.ramlname
def valueFromString(self, arg):
return self.nestedType.valueFromString(arg)
def cmp(self, val, arg):
return self.nestedType.cmp(val, arg)
def validate(self, name, object):
if object is None:
return
for msg in self.nestedType.validate(name, object):
yield msg
def getSpec(self):
r = self.nestedType.getSpec()
r["can_be_null"] = True
return r
def toRaml(self):
return self.nestedType.toRaml()
class Instance(Type):
types = ()
ramlType = "unknown"
@property
def ramlname(self):
return self.ramlType
def validate(self, name, object):
if not isinstance(object, self.types):
yield "%s (%r) is not a %s" % (
name, object, self.name or repr(self.types))
def toRaml(self):
return self.ramlType
class Integer(Instance):
name = "integer"
types = integer_types
ramlType = "integer"
def valueFromString(self, arg):
return int(arg)
class DateTime(Instance):
name = "datetime"
types = (datetime.datetime)
ramlType = "date"
class String(Instance):
name = "string"
types = (text_type,)
ramlType = "string"
def valueFromString(self, arg):
val = util.bytes2unicode(arg)
return val
class Binary(Instance):
name = "binary"
types = (bytes,)
ramlType = "string"
def valueFromString(self, arg):
return arg
class Boolean(Instance):
name = "boolean"
types = (bool,)
ramlType = "boolean"
def valueFromString(self, arg):
return util.string2boolean(arg)
class Identifier(Type):
name = "identifier"
identRe = re.compile('^[a-zA-Z_-][a-zA-Z0-9_-]*$')
ramlType = "string"
def __init__(self, len=None, **kwargs):
Type.__init__(self, **kwargs)
self.len = len
def valueFromString(self, arg):
val = util.bytes2unicode(arg)
if not self.identRe.match(val) or len(val) > self.len or not val:
raise TypeError
return val
def validate(self, name, object):
if not isinstance(object, text_type):
yield "%s - %r - is not a unicode string" % (name, object)
elif not self.identRe.match(object):
yield "%s - %r - is not an identifier" % (name, object)
elif len(object) < 1:
yield "%s - identifiers cannot be an empty string" % (name,)
elif len(object) > self.len:
yield "%s - %r - is longer than %d characters" % (name, object,
self.len)
def toRaml(self):
return {'type': self.ramlType,
'pattern': self.identRe.pattern}
class List(Type):
name = "list"
ramlType = "list"
@property
def ramlname(self):
return self.of.ramlname
def __init__(self, of=None, **kwargs):
Type.__init__(self, **kwargs)
self.of = of
def validate(self, name, object):
if not isinstance(object, list): # we want a list, and NOT a subclass
yield "%s (%r) is not a %s" % (name, object, self.name)
return
for idx, elt in enumerate(object):
for msg in self.of.validate("%s[%d]" % (name, idx), elt):
yield msg
def valueFromString(self, arg):
# valueFromString is used to process URL args, which come one at
# a time, so we defer to the `of`
return self.of.valueFromString(arg)
def getSpec(self):
return dict(type=self.name,
of=self.of.getSpec())
def toRaml(self):
return {'type': 'array', 'items': self.of.name}
def maybeNoneOrList(k, v):
if isinstance(v, NoneOk):
return k + "?"
if isinstance(v, List):
return k + "[]"
return k
class SourcedProperties(Type):
name = "sourcedproperties"
def validate(self, name, object):
if not isinstance(object, dict): # we want a dict, and NOT a subclass
yield "%s is not sourced properties (not a dict)" % (name,)
return
for k, v in iteritems(object):
if not isinstance(k, text_type):
yield "%s property name %r is not unicode" % (name, k)
if not isinstance(v, tuple) or len(v) != 2:
yield "%s property value for '%s' is not a 2-tuple" % (name, k)
return
propval, propsrc = v
if not isinstance(propsrc, text_type):
yield "%s[%s] source %r is not unicode" % (name, k, propsrc)
try:
json.loads(bytes2NativeString(propval))
except ValueError:
yield "%s[%r] value is not JSON-able" % (name, k)
def toRaml(self):
return {'type': "object",
'properties':
{'[]': {'type': 'object',
'properties': {
1: 'string',
2: 'integer | string | object | array | boolean'
}
}}}
class Dict(Type):
name = "dict"
@property
def ramlname(self):
return self.toRaml()
def __init__(self, **contents):
self.contents = contents
self.keys = set(contents)
def validate(self, name, object):
if not isinstance(object, dict):
yield "%s (%r) is not a dictionary (got type %s)" \
% (name, object, type(object))
return
gotNames = set(object.keys())
unexpected = gotNames - self.keys
if unexpected:
yield "%s has unexpected keys %s" % (name,
", ".join([repr(n) for n in unexpected]))
missing = self.keys - gotNames
if missing:
yield "%s is missing keys %s" % (name,
", ".join([repr(n) for n in missing]))
for k in gotNames & self.keys:
f = self.contents[k]
for msg in f.validate("%s[%r]" % (name, k), object[k]):
yield msg
def getSpec(self):
return dict(type=self.name,
fields=[dict(name=k,
type=v.name,
type_spec=v.getSpec())
for k, v in iteritems(self.contents)
])
def toRaml(self):
return {'type': "object",
'properties': dict([(maybeNoneOrList(k, v), v.ramlname) for k, v in self.contents.items()])}
class JsonObject(Type):
name = "jsonobject"
ramlname = 'object'
def validate(self, name, object):
if not isinstance(object, dict):
yield "%s (%r) is not a dictionary (got type %s)" \
% (name, object, type(object))
return
# make sure JSON can represent it
try:
json.dumps(object)
except Exception as e:
yield "%s is not JSON-able: %s" % (name, e)
return
def toRaml(self):
return "object"
class Entity(Type):
# NOTE: this type is defined by subclassing it in each resource type class.
# Instances are generally accessed at e.g.,
# * buildsets.Buildset.entityType or
# * self.master.data.rtypes.buildsets.entityType
name = None # set in constructor
fields = {}
fieldNames = set([])
def __init__(self, name):
fields = {}
for k, v in iteritems(self.__class__.__dict__):
if isinstance(v, Type):
fields[k] = v
self.fields = fields
self.fieldNames = set(fields)
self.name = name
def validate(self, name, object):
# this uses isinstance, allowing dict subclasses as used by the DB API
if not isinstance(object, dict):
yield "%s (%r) is not a dictionary (got type %s)" \
% (name, object, type(object))
return
gotNames = set(object.keys())
unexpected = gotNames - self.fieldNames
if unexpected:
yield "%s has unexpected keys %s" % (name,
", ".join([repr(n) for n in unexpected]))
missing = self.fieldNames - gotNames
if missing:
yield "%s is missing keys %s" % (name,
", ".join([repr(n) for n in missing]))
for k in gotNames & self.fieldNames:
f = self.fields[k]
for msg in f.validate("%s[%r]" % (name, k), object[k]):
yield msg
def getSpec(self):
return dict(type=self.name,
fields=[dict(name=k,
type=v.name,
type_spec=v.getSpec())
for k, v in iteritems(self.fields)
])
def toRaml(self):
return {'type': "object",
'properties': dict([
(
maybeNoneOrList(k, v),
{'type': v.ramlname, 'description': ''}
)
for k, v in iteritems(self.fields)])}