-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolanderutil.py
212 lines (187 loc) · 6.72 KB
/
colanderutil.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
import colander
class SchemaConverter(object):
""" Converts a colander schema to a JSON Schema (expressed
as a data structure consisting of primitive Python types,
suitable for serializing to JSON).
"""
def __init__(self):
self.converters = {
colander.Mapping: self.convert_mapping,
colander.Sequence: self.convert_sequence,
colander.Tuple: self.convert_tuple,
colander.String: self.convert_string,
colander.Integer: self.convert_integer,
colander.Float: self.convert_number,
colander.Decimal: self.convert_number,
colander.Money: self.convert_number,
colander.Boolean: self.convert_boolean,
colander.DateTime: self.convert_datetime,
colander.Date: self.convert_date,
colander.Time: self.convert_time,
}
def to_jsonschema(self, node):
nodetype = type(node.typ)
converter = self.converters.get(nodetype)
if converter is None:
raise ValueError, "Unexpected node type: %r" % nodetype
else:
ret = converter(node)
if node.title:
ret['title'] = node.title
if node.description:
ret['description'] = node.description
if node.default != colander.null:
ret['default'] = node.default
return ret
def convert_mapping(self, node):
ret = {}
if node.required:
ret['type'] = 'object'
ret['required'] = True
else:
ret['type'] = ['null', 'object']
ret['required'] = False
props = {}
ret['properties'] = props
for cnode in node.children:
name = cnode.name
props[name] = self.to_jsonschema(cnode)
return ret
def convert_tuple(self, node):
ret = {}
if node.required:
ret['type'] = 'array'
ret['required'] = True
else:
ret['type'] = ['null', 'array']
ret['required'] = False
items = []
ret['items'] = items
for cnode in node.children:
items.append(self.to_jsonschema(cnode))
return ret
def convert_sequence(self, node):
ret = {}
if node.required:
ret['type'] = 'array'
ret['required'] = True
else:
ret['type'] = ['null', 'array']
ret['required'] = False
ret['items'] = self.to_jsonschema(node.children[0])
for v in self.normalize_node_validators(node):
if type(v) == colander.Length:
if v.min is not None:
ret['minItems'] = v.min
if v.max is not None:
ret['maxItems'] = v.max
return ret
def convert_datetime(self, node, format='date-time'):
ret = {}
if node.required:
ret['type'] = 'string'
ret['minLength'] = 1
ret['required'] = True
else:
ret['type'] = ['null', 'string']
ret['required'] = False
ret['format'] = format
return ret
def convert_date(self, node):
return self.convert_datetime(node, format='date')
def convert_time(self, node):
return self.convert_datetime(node, format='time')
def convert_string(self, node):
ret = {}
if node.required:
ret['type'] = 'string'
ret['minLength'] = 1
ret['required'] = True
else:
ret['type'] = ['null', 'string']
ret['required'] = False
for v in self.normalize_node_validators(node):
if type(v) == colander.Length:
if v.min is not None:
ret['minLength'] = v.min
if v.max is not None:
ret['maxLength'] = v.max
elif type(v) == colander.Email:
ret['format'] = 'email'
elif type(v) == colander.Regex:
ret['pattern'] = v.match_object.pattern
elif type(v) == colander.OneOf:
ret['enum'] = v.choices
return ret
def convert_number(self, node, typename='number'):
ret = {}
if node.required:
ret['type'] = typename
ret['required'] = True
else:
ret['type'] = ['null', typename]
ret['required'] = False
for v in self.normalize_node_validators(node):
if type(v) == colander.Range:
if v.min is not None:
ret['minimum'] = v.min
if v.max is not None:
ret['maximum'] = v.max
elif type(v) == colander.OneOf:
ret['enum'] = v.choices
return ret
def convert_integer(self, node):
return self.convert_number(node, typename='integer')
def convert_boolean(self, node):
ret = {}
if node.required:
ret['type'] = 'boolean'
ret['required'] = True
else:
ret['type'] = ['null', 'boolean']
ret['required'] = False
return ret
def normalize_node_validators(self, node):
# Returns a sequence (possibly empty) of validators.
ret = []
if node.validator is not None:
if type(node.validator) == colander.All:
ret = node.validator.validators
else:
ret.append(node.validator)
return ret
# FIXME: move SchemaConverter into a separate module/package...?
# Seems like it could be useful outside of Audrey.
import audrey.types
OBJECTID_REGEX = '^[0-9a-f]{24}$'
class AudreySchemaConverter(SchemaConverter):
def convert_file(self, node):
ret = {}
if node.required:
ret['type'] = 'object'
ret['required'] = True
else:
ret['type'] = ['null', 'object']
ret['required'] = False
ret['properties'] = dict(
FileId=dict(type='string', required=True, pattern=OBJECTID_REGEX)
)
return ret
def convert_reference(self, node):
ret = {}
if node.required:
ret['type'] = 'object'
ret['required'] = True
else:
ret['type'] = ['null', 'object']
ret['required'] = False
ret['properties'] = dict(
ObjectId=dict(type='string', required=True, pattern=OBJECTID_REGEX),
)
if node.typ.collection is None:
ret['properties']['collection'] = dict(type='string', required=True, minLength=1)
return ret
def __init__(self):
SchemaConverter.__init__(self)
self.converters[audrey.types.File] = self.convert_file
self.converters[audrey.types.Reference] = self.convert_reference