From cbe50e488e69704546da31bf5a88e93d5c3ffba8 Mon Sep 17 00:00:00 2001 From: Andrew Bonney Date: Thu, 8 Nov 2018 13:37:43 +0000 Subject: [PATCH] ramlfications: patch to support Windows. Resolves #5 --- Patches.py | 35 +++++++++++++++++++++++++++++++++++ Specification.py | 10 ++++++++++ 2 files changed, 45 insertions(+) create mode 100644 Patches.py diff --git a/Patches.py b/Patches.py new file mode 100644 index 00000000..a98957db --- /dev/null +++ b/Patches.py @@ -0,0 +1,35 @@ +# Copyright (C) 2018 British Broadcasting Corporation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os +import jsonref + + +# Work around ramlfications Windows compatibility issues +def _parse_json(self, jsonfile, base_path): + """ + Parses JSON as well as resolves any `$ref`s, including references to + local files and remote (HTTP/S) files. + """ + base_path = os.path.abspath(base_path) + if not base_path.endswith("/"): + base_path = base_path + "/" + if os.name == "nt": + base_path = "file:///" + base_path.replace('\\', '/') + else: + base_path = "file://" + base_path + + with open(jsonfile, "r") as f: + schema = jsonref.load(f, base_uri=base_path, jsonschema=True) + return schema diff --git a/Specification.py b/Specification.py index c3f792d0..06d1c0e5 100644 --- a/Specification.py +++ b/Specification.py @@ -16,6 +16,16 @@ import json import ramlfications +from Patches import _parse_json + + +try: + # Patch ramlfications for Windows support + ramlfications.loader.RAMLLoader._parse_json = _parse_json +except AttributeError: + pass + + class Specification(object): def __init__(self, file_path): self.data = {}