def test_ref_path(): from jsonschema.compat import urlsplit url = "file:////home/user/mappyfile/mappyfile/schemas/" scheme, netloc, path, query, fragment = urlsplit(url) print(scheme, netloc, path, query, fragment) assert(scheme == "file") url = "file:///D:/GitHub/mappyfile/mappyfile/schemas/" scheme, netloc, path, query, fragment = urlsplit(url) print(scheme, netloc, path, query, fragment) assert(scheme == "file")
def test_ref_path(): from jsonschema.compat import urlsplit url = "file:////home/user/mappyfile/mappyfile/schemas/" scheme, netloc, path, query, fragment = urlsplit(url) print(scheme, netloc, path, query, fragment) assert (scheme == "file") url = "file:///D:/GitHub/mappyfile/mappyfile/schemas/" scheme, netloc, path, query, fragment = urlsplit(url) print(scheme, netloc, path, query, fragment) assert (scheme == "file")
def resolve_remote(self, uri): scheme = urlsplit(uri).scheme # if there's no scheme, it's a local file, so prefix it with "file://" if scheme == '': uri = 'file://' + uri if scheme in self.handlers: result = self.handlers[scheme](uri) elif scheme in [u"http", u"https"]: # Requests has support for detecting the correct encoding of # json over http result = requests.get(uri).json() else: # Otherwise, pass off to urllib and assume utf-8 with urlopen(uri) as url: content = url.read().decode("utf-8") if uri.endswith('.yaml') or uri.endswith('.yml'): result = yaml.safe_load(content) else: result = json.loads(content) if self.cache_remote: self.store[uri] = result return result
def resolve_remote(self, uri): """ Resolve a remote ``uri``. If called directly, does not check the store first, but after retrieving the document at the specified URI it will be saved in the store if :attr:`cache_remote` is True. .. note:: If the requests_ library is present, ``jsonschema`` will use it to request the remote ``uri``, so that the correct encoding is detected and used. If it isn't, or if the scheme of the ``uri`` is not ``http`` or ``https``, UTF-8 is assumed. Arguments: uri (str): The URI to resolve Returns: The retrieved document .. _requests: http://pypi.python.org/pypi/requests/ """ try: import requests except ImportError: requests = None scheme = urlsplit(uri).scheme if scheme in self.handlers: result = self.handlers[scheme](uri) elif ( scheme in [u"http", u"https"] and requests and getattr(requests.Response, "json", None) is not None ): # Requests has support for detecting the correct encoding of # json over http if callable(requests.Response.json): result = requests.get(uri).json() else: result = requests.get(uri).json else: # Otherwise, pass off to urllib and assume utf-8 with urlopen(uri) as url: result = json.loads(url.read().decode("utf-8")) if self.cache_remote: self.store[uri] = result return result
def resolve_remote(self, uri): """ Resolve a remote ``uri``. If called directly, does not check the store first, but after retrieving the document at the specified URI it will be saved in the store if :attr:`cache_remote` is True. .. note:: If the requests_ library is present, ``jsonschema`` will use it to request the remote ``uri``, so that the correct encoding is detected and used. If it isn't, or if the scheme of the ``uri`` is not ``http`` or ``https``, UTF-8 is assumed. Arguments: uri (str): The URI to resolve Returns: The retrieved document .. _requests: http://pypi.python.org/pypi/requests/ """ scheme = urlsplit(uri).scheme if scheme in self.handlers: result = self.handlers[scheme](uri) elif ( scheme in [u"http", u"https"] and requests and getattr(requests.Response, "json", None) is not None ): # Requests has support for detecting the correct encoding of # json over http if callable(requests.Response.json): result = requests.get(uri).json() else: result = requests.get(uri).json else: # Otherwise, pass off to urllib and assume utf-8 result = json.loads(urlopen(uri).read().decode("utf-8")) if self.cache_remote: self.store[uri] = result return result
def package_name_for(self, schema_id): """ Choose a package name for a given schema id. """ path = urlsplit(schema_id).path uri_parts = path.split("/") if self.keep_uri_parts: uri_parts = uri_parts[-self.keep_uri_parts:] parts = [underscore(part) for part in uri_parts[:-1] if part] if not all(self.is_legal_package_name(part) for part in parts): raise ValueError("Unable to construct legal package name from: {}".format(path)) return ".".join([self.basename] + parts)
def resolve_from_url(self, uri): uri, fragment = urldefrag(uri) split_url = urlsplit(uri) if split_url.scheme in self.handlers: return self.handlers[split_url.scheme](uri) full_path = find_schema_path(uri.split('/')[-1]) if full_path: with open(full_path) as fp: json_schema = json.load(fp) if not json_schema: # TODO: fix exception method raise RefResolutionError("Unresolvable JSON schema: %r" % uri) return self.resolve_fragment(json_schema, fragment) else: # if a schema can't be found locally, just default to using the standard resolver return super(DjangoSchemaResolver, self).resolve_from_url(uri)
def resolve_remote(self, uri): """ Resolve a remote ``uri``. Does not check the store first, but stores the retrieved document in the store if :attr:`RefResolver.cache_remote` is True. .. note:: If the requests_ library is present, ``jsonschema`` will use it to request the remote ``uri``, so that the correct encoding is detected and used. If it isn't, or if the scheme of the ``uri`` is not ``http`` or ``https``, UTF-8 is assumed. :argument str uri: the URI to resolve :returns: the retrieved document .. _requests: http://pypi.python.org/pypi/requests/ """ scheme = urlsplit(uri).scheme if scheme in self.handlers: result = self.handlers[scheme](uri) elif ( scheme in [u"http", u"https"] and requests and getattr(requests.Response, "json", None) is not None ): # Requests has support for detecting the correct encoding of # json over http if callable(requests.Response.json): result = requests.get(uri).json() else: result = requests.get(uri).json else: # Otherwise, pass off to urllib and assume utf-8 result = json.loads(urlopen(uri).read().decode("utf-8")) if self.cache_remote: self.store[uri] = result return result
def resolve_remote(self, uri): ''' Basically the same code provided by RefResolver, but we use OrderedDict as base class to load json documents. ''' scheme = urlsplit(uri).scheme if scheme in self.handlers: result = self.handlers[scheme](uri) elif (scheme in [u"http", u"https"] and requests and getattr(requests.Response, "json", None) is not None): if callable(requests.Response.json): result = requests.get(uri).json() else: result = requests.get(uri).json else: result = json.loads(urlopen(uri).read().decode("utf-8"), object_pairs_hook=OrderedDict) if self.cache_remote: self.store[uri] = result return result
def resolve_remote(self, uri): """override default resolve_remote to allow testing when there is no ssl certificate """ scheme = urlsplit(uri).scheme if scheme in self.handlers: result = self.handlers[scheme](uri) elif (scheme in [u"http", u"https"] and requests and getattr(requests.Response, "json", None) is not None): # Requests has support for detecting the correct encoding of # json over http if callable(requests.Response.json): result = requests.get(uri, verify=False).json() else: result = requests.get(uri, verify=False).json else: # Otherwise, pass off to urllib and assume utf-8 result = json.loads(urlopen(uri).read().decode("utf-8")) if self.cache_remote: self.store[uri] = result return result
def resolve_remote(self, uri): schema_name = uri.split("/")[-1] if self.schema_file and self.file_schema_name == schema_name: uri = self.schema_file else: uri = urljoin(self.schema_url, schema_name) document = self.store.get(uri) if document: return document if uri.startswith("http"): # This branch of the if-statement in-lines `RefResolver.resolve_remote()`, but using `get_request()`. scheme = urlsplit(uri).scheme if scheme in self.handlers: result = self.handlers[scheme](uri) elif scheme in [u"http", u"https"]: # Requests has support for detecting the correct encoding of # json over http result = get_request(uri, config=self.config).json() else: # Otherwise, pass off to urllib and assume utf-8 with urlopen(uri) as url: result = json.loads(url.read().decode("utf-8")) if self.cache_remote: self.store[uri] = result return result else: with open(uri) as schema_file: result = json.load(schema_file) add_is_codelist(result) self.store[uri] = result return result
def normalize(self, uri): return urlsplit(uri).geturl()
def normalize(self, uri): parts = urlsplit(uri) if 'file' == parts.scheme: parts = parts._replace(path=os.path.abspath(parts.path)) return parts.geturl()