mageml-singer-python 5.13.0.post3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mageml_singer_python-5.13.0.post3.dist-info/METADATA +14 -0
- mageml_singer_python-5.13.0.post3.dist-info/RECORD +19 -0
- mageml_singer_python-5.13.0.post3.dist-info/WHEEL +5 -0
- mageml_singer_python-5.13.0.post3.dist-info/licenses/LICENSE +177 -0
- mageml_singer_python-5.13.0.post3.dist-info/top_level.txt +1 -0
- singer/__init__.py +86 -0
- singer/bookmarks.py +46 -0
- singer/catalog.py +156 -0
- singer/exceptions.py +30 -0
- singer/logger.py +54 -0
- singer/logging.conf +24 -0
- singer/messages.py +287 -0
- singer/metadata.py +46 -0
- singer/metrics.py +248 -0
- singer/requests.py +6 -0
- singer/schema.py +111 -0
- singer/statediff.py +70 -0
- singer/transform.py +422 -0
- singer/utils.py +311 -0
singer/statediff.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import collections
|
|
2
|
+
|
|
3
|
+
# Named tuples for holding add, change, and remove operations
|
|
4
|
+
Add = collections.namedtuple('Add', ['path', 'newval'])
|
|
5
|
+
Change = collections.namedtuple('Change', ['path', 'oldval', 'newval'])
|
|
6
|
+
Remove = collections.namedtuple('Remove', ['path', 'oldval'])
|
|
7
|
+
|
|
8
|
+
def paths(data, base=None):
|
|
9
|
+
'''Walk a data structure and return a list of (path, value) tuples, where
|
|
10
|
+
each path is the path to a leaf node in the data structure and the
|
|
11
|
+
value is the value it points to. Each path will be a tuple.
|
|
12
|
+
|
|
13
|
+
'''
|
|
14
|
+
if base is None:
|
|
15
|
+
base = ()
|
|
16
|
+
|
|
17
|
+
result = []
|
|
18
|
+
if isinstance(data, dict):
|
|
19
|
+
for key, val in sorted(data.items()):
|
|
20
|
+
result.extend(paths(val, base + (key,)))
|
|
21
|
+
|
|
22
|
+
elif isinstance(data, list):
|
|
23
|
+
for i, val in enumerate(data):
|
|
24
|
+
result.extend(paths(val, base + (i,)))
|
|
25
|
+
|
|
26
|
+
elif base:
|
|
27
|
+
result.append((base, data))
|
|
28
|
+
|
|
29
|
+
return result
|
|
30
|
+
|
|
31
|
+
def diff(oldstate, newstate):
|
|
32
|
+
'''Compare two states, returning a list of Add, Change, and Remove
|
|
33
|
+
objects.
|
|
34
|
+
|
|
35
|
+
Add(path, newval) means path exists in newstate but not oldstate and
|
|
36
|
+
its value in newstate is newval.
|
|
37
|
+
|
|
38
|
+
Change(path, oldval, newval) means that path exists in both oldstate
|
|
39
|
+
and newstate but has different values. oldval is the val in oldstate
|
|
40
|
+
and newval is the value in newstate.
|
|
41
|
+
|
|
42
|
+
Remove(path, oldval) means the path exists in oldstate but not in
|
|
43
|
+
newstate, and the value in oldstate is oldval.
|
|
44
|
+
|
|
45
|
+
'''
|
|
46
|
+
|
|
47
|
+
# Convert oldstate and newstate from a deeply nested dict into a
|
|
48
|
+
# single-level dict, mapping a path to a value.
|
|
49
|
+
olddict = dict(paths(oldstate))
|
|
50
|
+
newdict = dict(paths(newstate))
|
|
51
|
+
|
|
52
|
+
# Build the list of all paths in both oldstate and newstate to iterate
|
|
53
|
+
# over.
|
|
54
|
+
all_paths = set()
|
|
55
|
+
all_paths.update(set(olddict.keys()))
|
|
56
|
+
all_paths.update(set(newdict.keys()))
|
|
57
|
+
|
|
58
|
+
result = []
|
|
59
|
+
for path in sorted(all_paths):
|
|
60
|
+
if path in olddict:
|
|
61
|
+
if path in newdict:
|
|
62
|
+
if olddict[path] == newdict[path]:
|
|
63
|
+
pass # Don't emit anything if values are the same
|
|
64
|
+
else:
|
|
65
|
+
result.append(Change(path, olddict[path], newdict[path]))
|
|
66
|
+
else:
|
|
67
|
+
result.append(Remove(path, olddict[path]))
|
|
68
|
+
else:
|
|
69
|
+
result.append(Add(path, newdict[path]))
|
|
70
|
+
return result
|
singer/transform.py
ADDED
|
@@ -0,0 +1,422 @@
|
|
|
1
|
+
import datetime
|
|
2
|
+
import decimal
|
|
3
|
+
import logging
|
|
4
|
+
import re
|
|
5
|
+
from referencing import Registry, Resource
|
|
6
|
+
from referencing.jsonschema import DRAFT4
|
|
7
|
+
|
|
8
|
+
import singer.metadata
|
|
9
|
+
from singer.logger import get_logger
|
|
10
|
+
from singer.utils import (strftime, strptime_to_utc)
|
|
11
|
+
|
|
12
|
+
LOGGER = get_logger()
|
|
13
|
+
|
|
14
|
+
NO_INTEGER_DATETIME_PARSING = "no-integer-datetime-parsing"
|
|
15
|
+
UNIX_SECONDS_INTEGER_DATETIME_PARSING = "unix-seconds-integer-datetime-parsing"
|
|
16
|
+
UNIX_MILLISECONDS_INTEGER_DATETIME_PARSING = "unix-milliseconds-integer-datetime-parsing"
|
|
17
|
+
|
|
18
|
+
VALID_DATETIME_FORMATS = [
|
|
19
|
+
NO_INTEGER_DATETIME_PARSING,
|
|
20
|
+
UNIX_SECONDS_INTEGER_DATETIME_PARSING,
|
|
21
|
+
UNIX_MILLISECONDS_INTEGER_DATETIME_PARSING,
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def string_to_datetime(value):
|
|
26
|
+
try:
|
|
27
|
+
return strftime(strptime_to_utc(value))
|
|
28
|
+
except Exception as ex:
|
|
29
|
+
LOGGER.warning("%s, (%s)", ex, value)
|
|
30
|
+
return None
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def unix_milliseconds_to_datetime(value):
|
|
34
|
+
return strftime(datetime.datetime.fromtimestamp(float(value) / 1000.0, datetime.timezone.utc))
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def unix_seconds_to_datetime(value):
|
|
38
|
+
return strftime(datetime.datetime.fromtimestamp(int(value), datetime.timezone.utc))
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def breadcrumb_path(breadcrumb):
|
|
42
|
+
"""
|
|
43
|
+
Transform breadcrumb into familiar object dot-notation
|
|
44
|
+
"""
|
|
45
|
+
name = ".".join(breadcrumb)
|
|
46
|
+
name = name.replace('properties.', '')
|
|
47
|
+
name = name.replace('.items', '[]')
|
|
48
|
+
return name
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class SchemaMismatch(Exception):
|
|
52
|
+
def __init__(self, errors):
|
|
53
|
+
if not errors:
|
|
54
|
+
msg = "An error occured during transform that was not a schema mismatch"
|
|
55
|
+
|
|
56
|
+
else:
|
|
57
|
+
estrs = [e.tostr() for e in errors]
|
|
58
|
+
msg = "Errors during transform\n\t{}".format("\n\t".join(estrs))
|
|
59
|
+
msg += "\n\n\nErrors during transform: [{}]".format(", ".join(estrs))
|
|
60
|
+
|
|
61
|
+
super().__init__(msg)
|
|
62
|
+
|
|
63
|
+
class SchemaKey:
|
|
64
|
+
ref = "$ref"
|
|
65
|
+
items = "items"
|
|
66
|
+
properties = "properties"
|
|
67
|
+
pattern_properties = "patternProperties"
|
|
68
|
+
any_of = 'anyOf'
|
|
69
|
+
|
|
70
|
+
class Error:
|
|
71
|
+
def __init__(self, path, data, schema=None, logging_level=logging.INFO):
|
|
72
|
+
self.path = path
|
|
73
|
+
self.data = data
|
|
74
|
+
self.schema = schema
|
|
75
|
+
self.logging_level = logging_level
|
|
76
|
+
|
|
77
|
+
def tostr(self):
|
|
78
|
+
path = ".".join(map(str, self.path))
|
|
79
|
+
if self.schema:
|
|
80
|
+
if self.logging_level >= logging.INFO:
|
|
81
|
+
msg = "data does not match {}".format(self.schema)
|
|
82
|
+
else:
|
|
83
|
+
msg = "does not match {}".format(self.schema)
|
|
84
|
+
else:
|
|
85
|
+
msg = "not in schema"
|
|
86
|
+
|
|
87
|
+
if self.logging_level >= logging.INFO:
|
|
88
|
+
output = "{}: {}".format(path, msg)
|
|
89
|
+
else:
|
|
90
|
+
output = "{}: {} {}".format(path, self.data, msg)
|
|
91
|
+
return output
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
class Transformer:
|
|
95
|
+
def __init__(self, integer_datetime_fmt=NO_INTEGER_DATETIME_PARSING, pre_hook=None):
|
|
96
|
+
self.integer_datetime_fmt = integer_datetime_fmt
|
|
97
|
+
self.pre_hook = pre_hook
|
|
98
|
+
self.removed = set()
|
|
99
|
+
self.filtered = set()
|
|
100
|
+
self.errors = []
|
|
101
|
+
|
|
102
|
+
def log_warning(self):
|
|
103
|
+
if self.filtered:
|
|
104
|
+
LOGGER.debug("Filtered %s paths during transforms "
|
|
105
|
+
"as they were unsupported or not selected:\n\t%s",
|
|
106
|
+
len(self.filtered),
|
|
107
|
+
"\n\t".join(sorted(self.filtered)))
|
|
108
|
+
# Output list format to parse for reporting
|
|
109
|
+
LOGGER.debug("Filtered paths list: %s",
|
|
110
|
+
sorted(self.filtered))
|
|
111
|
+
|
|
112
|
+
if self.removed:
|
|
113
|
+
LOGGER.debug("Removed %s paths during transforms:\n\t%s",
|
|
114
|
+
len(self.removed),
|
|
115
|
+
"\n\t".join(sorted(self.removed)))
|
|
116
|
+
# Output list format to parse for reporting
|
|
117
|
+
LOGGER.debug("Removed paths list: %s", sorted(self.removed))
|
|
118
|
+
|
|
119
|
+
def __enter__(self):
|
|
120
|
+
return self
|
|
121
|
+
|
|
122
|
+
def __exit__(self, *args):
|
|
123
|
+
self.log_warning()
|
|
124
|
+
|
|
125
|
+
def filter_data_by_metadata(self, data, metadata, parent=()):
|
|
126
|
+
if isinstance(data, dict) and metadata:
|
|
127
|
+
for field_name in list(data.keys()):
|
|
128
|
+
breadcrumb = parent + ('properties', field_name)
|
|
129
|
+
selected = singer.metadata.get(metadata, breadcrumb, 'selected')
|
|
130
|
+
inclusion = singer.metadata.get(metadata, breadcrumb, 'inclusion')
|
|
131
|
+
if inclusion == 'automatic':
|
|
132
|
+
continue
|
|
133
|
+
|
|
134
|
+
if (selected is False) or (inclusion == 'unsupported'):
|
|
135
|
+
data.pop(field_name, None)
|
|
136
|
+
# Track that a field was filtered because the customer
|
|
137
|
+
# didn't select it or the tap declared it as unsupported.
|
|
138
|
+
self.filtered.add(breadcrumb_path(breadcrumb))
|
|
139
|
+
else:
|
|
140
|
+
data[field_name] = self.filter_data_by_metadata(
|
|
141
|
+
data[field_name], metadata, breadcrumb)
|
|
142
|
+
|
|
143
|
+
if isinstance(data, list) and metadata:
|
|
144
|
+
breadcrumb = parent + ('items',)
|
|
145
|
+
data = [self.filter_data_by_metadata(d, metadata, breadcrumb) for d in data]
|
|
146
|
+
|
|
147
|
+
return data
|
|
148
|
+
|
|
149
|
+
def transform(self, data, schema, metadata=None):
|
|
150
|
+
data = self.filter_data_by_metadata(data, metadata)
|
|
151
|
+
|
|
152
|
+
success, transformed_data = self.transform_recur(data, schema, [])
|
|
153
|
+
if not success:
|
|
154
|
+
raise SchemaMismatch(self.errors)
|
|
155
|
+
|
|
156
|
+
return transformed_data
|
|
157
|
+
|
|
158
|
+
def transform_recur(self, data, schema, path):
|
|
159
|
+
if "anyOf" in schema:
|
|
160
|
+
return self._transform_anyof(data, schema, path)
|
|
161
|
+
|
|
162
|
+
if "type" not in schema:
|
|
163
|
+
# indicates no typing information so don't bother transforming it
|
|
164
|
+
return True, data
|
|
165
|
+
|
|
166
|
+
types = schema["type"]
|
|
167
|
+
if not isinstance(types, list):
|
|
168
|
+
types = [types]
|
|
169
|
+
|
|
170
|
+
if "null" in types:
|
|
171
|
+
types.remove("null")
|
|
172
|
+
types.append("null")
|
|
173
|
+
|
|
174
|
+
for typ in types:
|
|
175
|
+
success, transformed_data = self._transform(data, typ, schema, path)
|
|
176
|
+
if success:
|
|
177
|
+
return success, transformed_data
|
|
178
|
+
else: # pylint: disable=useless-else-on-loop
|
|
179
|
+
# exhaused all types and didn't return, so we failed :-(
|
|
180
|
+
self.errors.append(Error(path, data, schema, logging_level=LOGGER.level))
|
|
181
|
+
return False, None
|
|
182
|
+
|
|
183
|
+
def _transform_anyof(self, data, schema, path):
|
|
184
|
+
subschemas = schema['anyOf']
|
|
185
|
+
for subschema in subschemas:
|
|
186
|
+
success, transformed_data = self.transform_recur(data, subschema, path)
|
|
187
|
+
if success:
|
|
188
|
+
return success, transformed_data
|
|
189
|
+
else: # pylint: disable=useless-else-on-loop
|
|
190
|
+
# exhaused all schemas and didn't return, so we failed :-(
|
|
191
|
+
self.errors.append(Error(path, data, schema, logging_level=LOGGER.level))
|
|
192
|
+
return False, None
|
|
193
|
+
|
|
194
|
+
def _transform_object(self, data, schema, path, pattern_properties):
|
|
195
|
+
# We do not necessarily have a dict to transform here. The schema's
|
|
196
|
+
# type could contain multiple possible values. Eg:
|
|
197
|
+
# ["null", "object", "string"]
|
|
198
|
+
if not isinstance(data, dict):
|
|
199
|
+
return False, data
|
|
200
|
+
|
|
201
|
+
# Don't touch an empty schema
|
|
202
|
+
if schema == {} and not pattern_properties:
|
|
203
|
+
return True, data
|
|
204
|
+
|
|
205
|
+
result = {}
|
|
206
|
+
successes = []
|
|
207
|
+
for key, value in data.items():
|
|
208
|
+
# patternProperties are a map of {"pattern": { schema...}}
|
|
209
|
+
pattern_schemas = [schema for pattern, schema
|
|
210
|
+
in (pattern_properties or {}).items()
|
|
211
|
+
if re.match(pattern, key)]
|
|
212
|
+
if key in schema or pattern_schemas:
|
|
213
|
+
sub_schema = schema.get(key, {'anyOf': pattern_schemas})
|
|
214
|
+
success, subdata = self.transform_recur(value, sub_schema, path + [key])
|
|
215
|
+
successes.append(success)
|
|
216
|
+
result[key] = subdata
|
|
217
|
+
else:
|
|
218
|
+
# track that field has been removed because it wasn't
|
|
219
|
+
# found in the schema. This likely indicates some problem
|
|
220
|
+
# with discovery but rather than failing the run because
|
|
221
|
+
# new data was added we'd rather continue the sync and
|
|
222
|
+
# allow customers to indicate that they want the new data.
|
|
223
|
+
self.removed.add(".".join(map(str, path + [key])))
|
|
224
|
+
|
|
225
|
+
return all(successes), result
|
|
226
|
+
|
|
227
|
+
def _transform_array(self, data, schema, path):
|
|
228
|
+
# We do not necessarily have a list to transform here. The schema's
|
|
229
|
+
# type could contain multiple possible values. Eg:
|
|
230
|
+
# ["null", "array", "integer"]
|
|
231
|
+
if not isinstance(data, list):
|
|
232
|
+
return False, data
|
|
233
|
+
result = []
|
|
234
|
+
successes = []
|
|
235
|
+
for i, row in enumerate(data):
|
|
236
|
+
success, subdata = self.transform_recur(row, schema, path + [i])
|
|
237
|
+
successes.append(success)
|
|
238
|
+
result.append(subdata)
|
|
239
|
+
|
|
240
|
+
return all(successes), result
|
|
241
|
+
|
|
242
|
+
def _transform_datetime(self, value):
|
|
243
|
+
if value is None or value == "":
|
|
244
|
+
return None # Short circuit in the case of null or empty string
|
|
245
|
+
|
|
246
|
+
if self.integer_datetime_fmt not in VALID_DATETIME_FORMATS:
|
|
247
|
+
raise Exception("Invalid integer datetime parsing option")
|
|
248
|
+
|
|
249
|
+
if self.integer_datetime_fmt == NO_INTEGER_DATETIME_PARSING:
|
|
250
|
+
return string_to_datetime(value)
|
|
251
|
+
else:
|
|
252
|
+
try:
|
|
253
|
+
if self.integer_datetime_fmt == UNIX_SECONDS_INTEGER_DATETIME_PARSING:
|
|
254
|
+
return unix_seconds_to_datetime(value)
|
|
255
|
+
else:
|
|
256
|
+
return unix_milliseconds_to_datetime(value)
|
|
257
|
+
except:
|
|
258
|
+
return string_to_datetime(value)
|
|
259
|
+
|
|
260
|
+
def _transform(self, data, typ, schema, path):
|
|
261
|
+
if self.pre_hook:
|
|
262
|
+
data = self.pre_hook(data, typ, schema)
|
|
263
|
+
|
|
264
|
+
if typ == "null":
|
|
265
|
+
if data is None or data == "":
|
|
266
|
+
return True, None
|
|
267
|
+
else:
|
|
268
|
+
return False, None
|
|
269
|
+
|
|
270
|
+
elif schema.get("format") == "date-time":
|
|
271
|
+
data = self._transform_datetime(data)
|
|
272
|
+
if data is None:
|
|
273
|
+
return False, None
|
|
274
|
+
|
|
275
|
+
return True, data
|
|
276
|
+
elif schema.get("format") == "singer.decimal":
|
|
277
|
+
if data is None:
|
|
278
|
+
return False, None
|
|
279
|
+
|
|
280
|
+
if isinstance(data, (str, float, int)):
|
|
281
|
+
try:
|
|
282
|
+
return True, str(decimal.Decimal(str(data)))
|
|
283
|
+
except:
|
|
284
|
+
return False, None
|
|
285
|
+
elif isinstance(data, decimal.Decimal):
|
|
286
|
+
try:
|
|
287
|
+
if data.is_snan():
|
|
288
|
+
return True, 'NaN'
|
|
289
|
+
else:
|
|
290
|
+
return True, str(data)
|
|
291
|
+
except:
|
|
292
|
+
return False, None
|
|
293
|
+
|
|
294
|
+
return False, None
|
|
295
|
+
elif typ == "object":
|
|
296
|
+
# Objects do not necessarily specify properties
|
|
297
|
+
return self._transform_object(data,
|
|
298
|
+
schema.get("properties", {}),
|
|
299
|
+
path,
|
|
300
|
+
schema.get(SchemaKey.pattern_properties))
|
|
301
|
+
|
|
302
|
+
elif typ == "array":
|
|
303
|
+
return self._transform_array(data, schema["items"], path)
|
|
304
|
+
|
|
305
|
+
elif typ == "string":
|
|
306
|
+
if data is not None:
|
|
307
|
+
try:
|
|
308
|
+
return True, str(data)
|
|
309
|
+
except:
|
|
310
|
+
return False, None
|
|
311
|
+
else:
|
|
312
|
+
return False, None
|
|
313
|
+
|
|
314
|
+
elif typ == "integer":
|
|
315
|
+
if isinstance(data, str):
|
|
316
|
+
data = data.replace(",", "")
|
|
317
|
+
|
|
318
|
+
try:
|
|
319
|
+
return True, int(data)
|
|
320
|
+
except:
|
|
321
|
+
return False, None
|
|
322
|
+
|
|
323
|
+
elif typ == "number":
|
|
324
|
+
if isinstance(data, str):
|
|
325
|
+
data = data.replace(",", "")
|
|
326
|
+
|
|
327
|
+
try:
|
|
328
|
+
return True, float(data)
|
|
329
|
+
except:
|
|
330
|
+
return False, None
|
|
331
|
+
|
|
332
|
+
elif typ == "boolean":
|
|
333
|
+
if isinstance(data, str) and data.lower() == "false":
|
|
334
|
+
return True, False
|
|
335
|
+
|
|
336
|
+
try:
|
|
337
|
+
return True, bool(data)
|
|
338
|
+
except:
|
|
339
|
+
return False, None
|
|
340
|
+
|
|
341
|
+
else:
|
|
342
|
+
return False, None
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
def transform(data, schema, integer_datetime_fmt=NO_INTEGER_DATETIME_PARSING,
|
|
346
|
+
pre_hook=None, metadata=None):
|
|
347
|
+
"""
|
|
348
|
+
Applies schema (and integer_datetime_fmt, if supplied) to data, transforming
|
|
349
|
+
each field in data to the type specified in schema. If no type matches a
|
|
350
|
+
data field, this throws an Exception.
|
|
351
|
+
|
|
352
|
+
This applies types in order with the exception of 'null', which is always
|
|
353
|
+
applied last.
|
|
354
|
+
|
|
355
|
+
The valid types are: integer, number, boolean, array, object, null, string,
|
|
356
|
+
and string with date-time format.
|
|
357
|
+
|
|
358
|
+
If an integer_datetime_fmt is supplied, integer values in fields with date-
|
|
359
|
+
time formats are appropriately parsed as unix seconds or unix milliseconds.
|
|
360
|
+
|
|
361
|
+
The pre_hook should be a callable that takes data, type, and schema and
|
|
362
|
+
returns the transformed data to be fed into the _transform function.
|
|
363
|
+
"""
|
|
364
|
+
transformer = Transformer(integer_datetime_fmt, pre_hook)
|
|
365
|
+
return transformer.transform(data, schema, metadata=metadata)
|
|
366
|
+
|
|
367
|
+
def _transform_datetime(value, integer_datetime_fmt=NO_INTEGER_DATETIME_PARSING):
|
|
368
|
+
transformer = Transformer(integer_datetime_fmt)
|
|
369
|
+
return transformer._transform_datetime(value)
|
|
370
|
+
|
|
371
|
+
def resolve_schema_references(schema, refs=None):
|
|
372
|
+
'''Resolves and replaces json-schema $refs with the appropriate dict.
|
|
373
|
+
|
|
374
|
+
Recursively walks the given schema dict, converting every instance
|
|
375
|
+
of $ref in a 'properties' structure with a resolved dict.
|
|
376
|
+
|
|
377
|
+
This modifies the input schema and also returns it.
|
|
378
|
+
|
|
379
|
+
Arguments:
|
|
380
|
+
schema:
|
|
381
|
+
the schema dict
|
|
382
|
+
refs:
|
|
383
|
+
a dict of <string, dict> which forms a store of referenced schemata
|
|
384
|
+
|
|
385
|
+
Returns:
|
|
386
|
+
schema
|
|
387
|
+
'''
|
|
388
|
+
refs = refs or {}
|
|
389
|
+
# jsonschema.RefResolver is deprecated in favor of the referencing library. References
|
|
390
|
+
# resolve against the root schema, at the empty URI, and the schemata in refs.
|
|
391
|
+
registry = Registry().with_resources(
|
|
392
|
+
[('', Resource.from_contents(schema, default_specification=DRAFT4))]
|
|
393
|
+
+ [
|
|
394
|
+
(uri, Resource.from_contents(contents, default_specification=DRAFT4))
|
|
395
|
+
for uri, contents in refs.items()
|
|
396
|
+
],
|
|
397
|
+
)
|
|
398
|
+
return _resolve_schema_references(schema, registry.resolver(base_uri=''))
|
|
399
|
+
|
|
400
|
+
def _resolve_schema_references(schema, resolver):
|
|
401
|
+
if SchemaKey.ref in schema:
|
|
402
|
+
reference_path = schema.pop(SchemaKey.ref, None)
|
|
403
|
+
resolved = resolver.lookup(reference_path).contents
|
|
404
|
+
schema.update(resolved)
|
|
405
|
+
return _resolve_schema_references(schema, resolver)
|
|
406
|
+
|
|
407
|
+
if SchemaKey.properties in schema:
|
|
408
|
+
for k, val in schema[SchemaKey.properties].items():
|
|
409
|
+
schema[SchemaKey.properties][k] = _resolve_schema_references(val, resolver)
|
|
410
|
+
|
|
411
|
+
if SchemaKey.pattern_properties in schema:
|
|
412
|
+
for k, val in schema[SchemaKey.pattern_properties].items():
|
|
413
|
+
schema[SchemaKey.pattern_properties][k] = _resolve_schema_references(val, resolver)
|
|
414
|
+
|
|
415
|
+
if SchemaKey.items in schema:
|
|
416
|
+
schema[SchemaKey.items] = _resolve_schema_references(schema[SchemaKey.items], resolver)
|
|
417
|
+
|
|
418
|
+
if SchemaKey.any_of in schema:
|
|
419
|
+
for i, element in enumerate(schema[SchemaKey.any_of]):
|
|
420
|
+
schema[SchemaKey.any_of][i] = _resolve_schema_references(element, resolver)
|
|
421
|
+
|
|
422
|
+
return schema
|