示例#1
0
    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
示例#2
0
    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
示例#3
0
    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
示例#4
0
    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
示例#5
0
 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
示例#6
0
    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
示例#7
0
    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
示例#8
0
 def read_file(uri):
     with closing(urlopen(uri)) as fp:
         if is_yaml(uri):
             return yaml.load(fp)
         else:
             return json.loads(fp.read().decode("utf-8"))
示例#9
0
 def read_file(uri):
     fp = urlopen(uri)
     if is_yaml(uri):
         return yaml.load(fp)
     else:
         return json.loads(fp.read().decode("utf-8"))
示例#10
0
 def read_file(uri):
     fp = urlopen(uri)
     if is_yaml(uri):
         return yaml.load(fp)
     else:
         return json.loads(fp.read().decode("utf-8"))
示例#11
0
 def read_file(uri):
     with closing(urlopen(uri)) as fp:
         if is_yaml(uri):
             return yaml.load(fp)
         else:
             return json.loads(fp.read().decode("utf-8"))