1
1
# Licensed under a 3-clause BSD style license - see LICENSE.rst
2
2
3
3
import re
4
- from collections import OrderedDict
5
4
6
5
7
6
# return_result included for backward compatibility
@@ -169,11 +168,17 @@ def merge_property_trees(schema):
169
168
can be represented in individual files and then referenced elsewhere. They
170
169
are then combined by this function into a single schema data structure.
171
170
"""
172
- newschema = OrderedDict ()
171
+ # track the "combined" and "top" items separately
172
+ # this allows the top level "id", "$schema", etc to overwrite
173
+ # combined items (so that the schema["id"] doesn't change).
174
+ combined_items = {}
175
+ top_items = {}
173
176
174
177
def add_entry (path , schema , combiner ):
178
+ if not top_items :
179
+ top_items .update (schema )
175
180
# TODO: Simplify?
176
- cursor = newschema
181
+ cursor = combined_items
177
182
for i in range (len (path )):
178
183
part = path [i ]
179
184
if part == combiner :
@@ -185,23 +190,23 @@ def add_entry(path, schema, combiner):
185
190
cursor .append ({})
186
191
cursor = cursor [part ]
187
192
elif part == 'items' :
188
- cursor = cursor .setdefault ('items' , OrderedDict () )
193
+ cursor = cursor .setdefault ('items' , {} )
189
194
else :
190
- cursor = cursor .setdefault ('properties' , OrderedDict () )
195
+ cursor = cursor .setdefault ('properties' , {} )
191
196
if i < len (path ) - 1 and isinstance (path [i + 1 ], int ):
192
197
cursor = cursor .setdefault (part , [])
193
198
else :
194
- cursor = cursor .setdefault (part , OrderedDict () )
199
+ cursor = cursor .setdefault (part , {} )
195
200
196
201
cursor .update (schema )
197
202
198
203
def callback (schema , path , combiner , ctx , recurse ):
199
- type = schema .get ('type' )
200
- schema = OrderedDict (schema )
201
- if type == 'object' :
204
+ schema_type = schema .get ('type' )
205
+ schema = dict (schema ) # shallow copy
206
+ if schema_type == 'object' :
202
207
if 'properties' in schema :
203
208
del schema ['properties' ]
204
- elif type == 'array' :
209
+ elif schema_type == 'array' :
205
210
del schema ['items' ]
206
211
if 'allOf' in schema :
207
212
del schema ['allOf' ]
@@ -210,7 +215,7 @@ def callback(schema, path, combiner, ctx, recurse):
210
215
211
216
walk_schema (schema , callback )
212
217
213
- return newschema
218
+ return combined_items | top_items
214
219
215
220
216
221
def build_docstring (klass , template = "{fits_hdu} {title}" ):
0 commit comments