-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate JSON schema from documentation
Using the structured information extracted from the documentation (see previous commit), build and write a JSON schema. This also introduces a new utility library to convert build the schema description from the set of objects and attributes.
- Loading branch information
Showing
4 changed files
with
156 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import json | ||
import re | ||
|
||
# ============================================================================= | ||
class JsonSchema: | ||
# ------------------------------------------------------------------------- | ||
def __init__(self, title, uri): | ||
self.meta = { | ||
'$schema': 'http://json-schema.org/draft-07/schema', | ||
'$id': uri, | ||
'title': title, | ||
} | ||
self.types = {} | ||
self.object_types = {} | ||
self.attributes = {} | ||
|
||
# ------------------------------------------------------------------------- | ||
def add_type(self, typedesc): | ||
if typedesc in self.types: | ||
# Type already defined; nothing to do | ||
return | ||
|
||
if '|' in typedesc: | ||
types = typedesc.split('|') | ||
for t in types: | ||
self.add_type(t) | ||
|
||
self.types[typedesc] = { | ||
'oneOf': [ | ||
{'$ref': f'#/definitions/types/{t}'} for t in types | ||
], | ||
} | ||
|
||
return | ||
|
||
m = re.match(r'^(list|map)[(](.*)[)]$', typedesc) | ||
if m: | ||
outer, inner = m.groups() | ||
self.add_type(inner) | ||
|
||
if outer == 'list': | ||
self.types[typedesc] = { | ||
'type': 'array', | ||
'items': {'$ref': f'#/definitions/types/{inner}'}, | ||
} | ||
|
||
else: | ||
self.types[typedesc] = { | ||
'type': 'object', | ||
'patternProperties': { | ||
'': {'$ref': f'#/definitions/types/{inner}'}, | ||
}, | ||
} | ||
|
||
elif typedesc in {'string'}: | ||
# Handle simple (non-compound) types | ||
self.types[typedesc] = {'type': typedesc} | ||
|
||
else: | ||
# Anything unknown is assumed to be an object type, which must be | ||
# defined via add_object_type | ||
pass | ||
|
||
# ------------------------------------------------------------------------- | ||
def add_attribute(self, name, instance, typedesc, description): | ||
self.attributes[f'{name}@{instance}'] = { | ||
'description': description, | ||
'$ref': f'#/definitions/types/{typedesc}', | ||
} | ||
|
||
self.add_type(typedesc) | ||
|
||
# ------------------------------------------------------------------------- | ||
def add_object_type(self, name, description, attributes, strict=False): | ||
self.object_types[name] = { | ||
'type': 'object', | ||
'description': description, | ||
'properties': { | ||
n: {'$ref': f'#/definitions/attributes/{n}@{i}'} | ||
for n, i, r in attributes | ||
}, | ||
'required': [n for n, i, r in attributes if r], | ||
'additionalProperties': not strict, | ||
} | ||
|
||
# ------------------------------------------------------------------------- | ||
def _build_schema(self, root_object): | ||
all_types = self.types | ||
all_types.update(self.object_types) | ||
|
||
schema = self.meta | ||
schema['definitions'] = { | ||
'types': all_types, | ||
'attributes': self.attributes, | ||
} | ||
schema['$ref'] = f'#/definitions/types/{root_object}' | ||
|
||
return schema | ||
|
||
# ------------------------------------------------------------------------- | ||
def write(self, root_object, path): | ||
json.dump(self._build_schema(root_object), open(path, 'wt')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters