diff options
-rwxr-xr-x | docs/api/schemas/generate-schemas.py | 56 |
1 files changed, 54 insertions, 2 deletions
diff --git a/docs/api/schemas/generate-schemas.py b/docs/api/schemas/generate-schemas.py index 09e6c44..a0c1e45 100755 --- a/docs/api/schemas/generate-schemas.py +++ b/docs/api/schemas/generate-schemas.py @@ -1,15 +1,24 @@ #!/usr/bin/env python3 +import argparse import os +import sys import jinja2 +try: + import openapi_spec_validator + import yaml +except ImportError: + openapi_spec_validator = None + yaml = None + ROOT_DIR = os.path.dirname(os.path.realpath(__file__)) VERSIONS = [(1, 0), (1, 1), (1, 2), None] LATEST_VERSION = (1, 2) -def generate_schema(): +def generate_schemas(): env = jinja2.Environment( loader=jinja2.FileSystemLoader(ROOT_DIR), trim_blocks=True, @@ -32,6 +41,49 @@ def generate_schema(): version_url=version_url).dump(fh, encoding='utf-8') fh.write(b'\n') + print(f'Schemas written to {ROOT_DIR}.') + + +def validate_schemas(): + for version in VERSIONS: + schema = os.path.join( + ROOT_DIR, + 'v%d.%d' % version if version else 'latest', + 'patchwork.yaml', + ) + + with open(schema) as fh: + spec = yaml.safe_load(fh.read()) + openapi_spec_validator.validate_spec(spec) + + print('Validation successful.') + + +def main(): + parser = argparse.ArgumentParser( + description='Generate schemas from the schema template.', + ) + parser.add_argument( + '--validate', + action='store_true', + help='validate the generated schemas. Requires the openapi-validator ' + 'package.', + ) + args = parser.parse_args() + + if args.validate and openapi_spec_validator is None: + print( + '\033[1m\033[91mERROR:\033[0m Validation requires the ' + 'openapi-validator and yaml packages', + file=sys.stderr, + ) + sys.exit(1) + + generate_schemas() + + if args.validate: + validate_schemas() + if __name__ == '__main__': - generate_schema() + main() |