From 4c8ba4688ec455c326501a64c827c627db9fd9c4 Mon Sep 17 00:00:00 2001 From: Eliot Berriot <contact@eliotberriot.com> Date: Thu, 18 Apr 2019 21:06:38 +0200 Subject: [PATCH] Added CLI for validation --- retribute_me/__init__.py | 0 retribute_me/cli.py | 31 +++++++++++++++++++++++++++++++ retribute_me/schemas.py | 13 +++++++++++++ setup.cfg | 5 +++++ 4 files changed, 49 insertions(+) create mode 100644 retribute_me/__init__.py create mode 100644 retribute_me/cli.py create mode 100644 retribute_me/schemas.py diff --git a/retribute_me/__init__.py b/retribute_me/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/retribute_me/cli.py b/retribute_me/cli.py new file mode 100644 index 0000000..06cbdb5 --- /dev/null +++ b/retribute_me/cli.py @@ -0,0 +1,31 @@ +import click +import json +import jsonschema +from . import schemas + + +@click.group() +def cli(): + pass + + +@cli.command() +@click.argument("document", type=click.File("r")) +@click.option("--version", "-v") +def validate(document, version): + content = document.read() + + payload = json.loads(content) + if not version: + try: + version = payload["version"] + except KeyError: + version = schemas.latest_schema_version + + schema = schemas.get(version) + jsonschema.validate(instance=payload, schema=schema) + click.echo("Schema is valid!") + + +if __name__ == "__main__": + cli() diff --git a/retribute_me/schemas.py b/retribute_me/schemas.py new file mode 100644 index 0000000..b439b72 --- /dev/null +++ b/retribute_me/schemas.py @@ -0,0 +1,13 @@ +import json +import os + +SCHEMAS_DIR = os.path.join( + os.path.dirname(os.path.abspath(os.path.dirname(__file__))), "schemas" +) + +latest_schema_version = "0.1" + + +def get(version): + with open(os.path.join(SCHEMAS_DIR, version, "schema.json")) as f: + return json.loads(f.read()) diff --git a/setup.cfg b/setup.cfg index 6ec92ed..6fee70c 100644 --- a/setup.cfg +++ b/setup.cfg @@ -20,6 +20,7 @@ include_package_data = True packages = find: install_requires = jsonschema + click [options.extras_require] @@ -27,6 +28,10 @@ dev = ipdb pytest +[options.entry_points] +console_scripts = + retribute-me = retribute_me.cli:cli + [options.packages.find] exclude = -- GitLab