From 0bb9deaf191f8d331482bd828c387b6b4edb4df9 Mon Sep 17 00:00:00 2001 From: James Date: Fri, 30 Jul 2021 09:23:19 +0100 Subject: [PATCH 1/3] feature: Can pass schema, not just filename --- CHANGELOG.md | 4 ++++ compiletojsonschema/compiletojsonschema.py | 25 +++++++++++++------ tests/test_error.py | 9 +++++++ tests/test_simple.py | 28 ++++++++++++++++++++++ 4 files changed, 59 insertions(+), 7 deletions(-) create mode 100644 tests/test_error.py diff --git a/CHANGELOG.md b/CHANGELOG.md index a965c52..78dd02e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +### Added + +- Can pass schema to Python class as well as filename + ## [0.3.0] - 2021-01-27 ### Added diff --git a/compiletojsonschema/compiletojsonschema.py b/compiletojsonschema/compiletojsonschema.py index d08b0af..7d53eb4 100644 --- a/compiletojsonschema/compiletojsonschema.py +++ b/compiletojsonschema/compiletojsonschema.py @@ -11,11 +11,15 @@ class CompileToJsonSchema: def __init__( self, - input_filename, + input_filename=None, set_additional_properties_false_everywhere=False, codelist_base_directory=None, + input_schema=None, ): + if not isinstance(input_schema, dict) and not input_filename: + raise Exception("Must pass input_filename or input_schema") self.input_filename = input_filename + self.input_schema = input_schema self.set_additional_properties_false_everywhere = ( set_additional_properties_false_everywhere ) @@ -25,12 +29,19 @@ def __init__( self.codelist_base_directory = os.getcwd() def get(self): - with open(self.input_filename) as fp: - resolved = jsonref.load( - fp, - object_pairs_hook=OrderedDict, - base_uri=pathlib.Path(os.path.realpath(self.input_filename)).as_uri(), - ) + if self.input_filename: + with open(self.input_filename) as fp: + resolved = jsonref.load( + fp, + object_pairs_hook=OrderedDict, + base_uri=pathlib.Path( + os.path.realpath(self.input_filename) + ).as_uri(), + ) + elif isinstance(self.input_schema, dict): + resolved = jsonref.JsonRef.replace_refs(self.input_schema) + else: + raise Exception("Must pass input_filename or input_schema") resolved = self.__process(resolved) diff --git a/tests/test_error.py b/tests/test_error.py new file mode 100644 index 0000000..3168d96 --- /dev/null +++ b/tests/test_error.py @@ -0,0 +1,9 @@ +import pytest + +from compiletojsonschema.compiletojsonschema import CompileToJsonSchema + + +def test_no_pass(): + """If you do not pass any inputs (eg input_filename, input_schema) it should raise an exception""" + with pytest.raises(Exception): + CompileToJsonSchema() diff --git a/tests/test_simple.py b/tests/test_simple.py index 90a3642..321bd25 100644 --- a/tests/test_simple.py +++ b/tests/test_simple.py @@ -1,5 +1,6 @@ import json import os +from collections import OrderedDict from compiletojsonschema.compiletojsonschema import CompileToJsonSchema @@ -23,6 +24,28 @@ def test_in_file(): assert out["properties"]["home_address"]["description"] == "Where the person lives" +def test_in_file_pass_as_schema(): + + input_filename = os.path.join( + os.path.dirname(os.path.realpath(__file__)), + "fixtures", + "simple", + "in_file.json", + ) + + with open(input_filename) as fp: + input_schema = json.load(fp, object_pairs_hook=OrderedDict,) + + ctjs = CompileToJsonSchema(input_schema=input_schema) + out_string = ctjs.get_as_string() + out = json.loads(out_string) + + assert out["properties"]["work_address"]["title"] == "Work Address" + assert out["properties"]["work_address"]["description"] == "Where the person works" + assert out["properties"]["home_address"]["title"] == "Home Address" + assert out["properties"]["home_address"]["description"] == "Where the person lives" + + def test_file_main(): input_filename = os.path.join( @@ -65,3 +88,8 @@ def test_file_list_anyof(): out["items"]["oneOf"][1]["properties"]["address"]["description"] == "Where the person works" ) + + +def test_passing_empty_schema_is_ok(): + ctjs = CompileToJsonSchema(input_schema={}) + assert "{}" == ctjs.get_as_string() From 84699a5e3a5644dc98da9c287787bb4746cc785c Mon Sep 17 00:00:00 2001 From: James Date: Fri, 30 Jul 2021 09:37:45 +0100 Subject: [PATCH 2/3] docs: Add pypi install --- docs/installing.rst | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/docs/installing.rst b/docs/installing.rst index 18b64db..bf6493b 100755 --- a/docs/installing.rst +++ b/docs/installing.rst @@ -1,6 +1,18 @@ Installing ========== +Via pip +------- + +It is available at https://pypi.org/project/compiletojsonschema/ + +.. code-block:: shell-session + + pip install compiletojsonschema + +Directly +-------- + To install, 1) Check out the git repository ( https://github.com/OpenDataServices/compile-to-json-schema ) onto your machine. From cad63c3af9e05c92d4852e8459c7eec5e8205d0c Mon Sep 17 00:00:00 2001 From: James Date: Fri, 30 Jul 2021 09:38:44 +0100 Subject: [PATCH 3/3] release: Release 0.4.0 --- CHANGELOG.md | 2 ++ setup.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 78dd02e..330ebf2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +## [0.4.0] - 2021-07-30 + ### Added - Can pass schema to Python class as well as filename diff --git a/setup.py b/setup.py index a762436..ddce9a9 100644 --- a/setup.py +++ b/setup.py @@ -9,7 +9,7 @@ setup( name="compiletojsonschema", - version="0.3.0", + version="0.4.0", author="Open Data Services", author_email="code@opendataservices.coop", url="https://github.com/OpenDataServices/compile-to-json-schema",