Skip to content

Commit

Permalink
manage: Extract traverse method
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmckinney committed Aug 10, 2021
1 parent f242bf2 commit 3dea17c
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,20 @@ def coerce_to_list(data, key):
return item


def traverse(block):
def method(schema, pointer='', **kwargs):
if isinstance(schema, list):
for index, item in enumerate(schema):
method(item, pointer=f'{pointer}/{index}', **kwargs)
elif isinstance(schema, dict):
block(schema, pointer=pointer, **kwargs)

for key, value in schema.items():
method(value, pointer=f'{pointer}/{key}', **kwargs)

return method


def get_metaschema():
"""
Patches and returns the JSON Schema Draft 4 metaschema.
Expand Down Expand Up @@ -291,14 +305,11 @@ def update_refs_to_unversioned_definitions(schema):
update_refs_to_unversioned_definitions(value)


def get_unversioned_pointers(schema, fields, pointer=''):
def get_unversioned_pointers(schema, fields):
"""
Returns the JSON Pointers to ``id`` fields that must not be versioned if the object is in an array.
"""
if isinstance(schema, list):
for index, item in enumerate(schema):
get_unversioned_pointers(item, fields, pointer=f'{pointer}/{index}')
elif isinstance(schema, dict):
def block(schema, pointer='', fields=()):
# Follows the logic of _get_merge_rules in merge.py from ocds-merge.
types = coerce_to_list(schema, 'type')

Expand All @@ -318,50 +329,45 @@ def get_unversioned_pointers(schema, fields, pointer=''):
reference = pointer
fields.add(f'{reference}/properties/id')

for key, value in schema.items():
get_unversioned_pointers(value, fields, pointer=f'{pointer}/{key}')
traverse(block)(schema, fields=fields)


def remove_omit_when_merged(schema):
"""
Removes properties that set ``omitWhenMerged``.
"""
if isinstance(schema, list):
for item in schema:
remove_omit_when_merged(item)
elif isinstance(schema, dict):
def block(schema, **kwargs):
for key, value in schema.items():
if key == 'properties':
for prop in list(value):
if value[prop].get('omitWhenMerged'):
del value[prop]
if prop in schema['required']:
schema['required'].remove(prop)
remove_omit_when_merged(value)

traverse(block)(schema)


def remove_metadata_and_extended_keywords(schema):
"""
Removes metadata and extended keywords from properties and definitions.
"""
if isinstance(schema, list):
for item in schema:
remove_metadata_and_extended_keywords(item)
elif isinstance(schema, dict):
def block(schema, **kwargs):
for key, value in schema.items():
if key in ('definitions', 'properties'):
for subschema in value.values():
for keyword in keywords_to_remove:
subschema.pop(keyword, None)
remove_metadata_and_extended_keywords(value)

traverse(block)(schema)


def get_dereferenced_release_schema(schema, output=None):
"""
Returns the dereferenced release schema.
"""
# Without a deepcopy, changes to referenced objects are copied across referring objects. However, the deepcopy does
# not retain the `__reference__` property.
# Without a deepcopy, all referring objects will share the same referenced objects. However, the deepcopy does not
# retain the `__reference__` property. So, we need to pass both when recursing.
if not output:
output = deepcopy(schema)

Expand Down Expand Up @@ -473,10 +479,10 @@ def pre_commit():
Update meta-schema.json, dereferenced-release-schema.json and versioned-release-validation-schema.json.
"""
release_schema = json_load('release-schema.json')
jsonref_release_schema = json_load('release-schema.json', jsonref)
jsonref_schema = json_load('release-schema.json', jsonref)

json_dump('meta-schema.json', get_metaschema())
json_dump('dereferenced-release-schema.json', get_dereferenced_release_schema(jsonref_release_schema))
json_dump('dereferenced-release-schema.json', get_dereferenced_release_schema(jsonref_schema))
json_dump('versioned-release-validation-schema.json', get_versioned_release_schema(release_schema))


Expand Down

0 comments on commit 3dea17c

Please sign in to comment.