Exemplo n.º 1
0
def create_document_loader(test):
    httpBase = 'http://json-ld.org/test-suite'
    httpsBase = 'https://json-ld.org/test-suite'
    loader = jsonld.get_document_loader()

    def is_test_suite_url(url):
        return url.startswith(httpBase) or url.startswith(httpsBase)

    def strip_base(url):
        if url.startswith(httpBase):
            return url[len(httpBase):]
        elif url.startswith(httpsBase):
            return url[len(httpsBase):]
        else:
            raise Exception('unkonwn base')

    def load_locally(url):
        doc = {'contextUrl': None, 'documentUrl': url, 'document': None}
        options = test.data.get('option')
        if options and url == test.base:
            if ('redirectTo' in options and options.get('httpStatus') >= 300):
                doc['documentUrl'] = (
                        test.manifest.data['baseIri'] + options['redirectTo'])
            elif 'httpLink' in options:
                content_type = options.get('contentType')
                if not content_type and url.endswith('.jsonld'):
                    content_type = 'application/ld+json'
                link_header = options.get('httpLink', '')
                if isinstance(link_header, list):
                    link_header = ','.join(link_header)
                link_header = jsonld.parse_link_header(
                    link_header).get('http://www.w3.org/ns/json-ld#context')
                if link_header and content_type != 'application/ld+json':
                    if isinstance(link_header, list):
                        raise Exception('multiple context link headers')
                    doc['contextUrl'] = link_header['target']
        global ROOT_MANIFEST_DIR
        if doc['documentUrl'].find(':') == -1:
            filename = os.path.join(ROOT_MANIFEST_DIR, doc['documentUrl'])
            doc['documentUrl'] = 'file://' + filename
        else:
            #filename = os.path.join(
            #    ROOT_MANIFEST_DIR, doc['documentUrl'][len(base):])
            filename = ROOT_MANIFEST_DIR + strip_base(doc['documentUrl'])
        try:
            doc['document'] = read_json(filename)
        except:
            raise Exception('loading document failed')
        return doc

    def local_loader(url):
        # always load remote-doc and non-base tests remotely
        if ((not is_test_suite_url(url) and url.find(':') != -1) or
                test.manifest.data.get('name') == 'Remote document'):
            return loader(url)

        # attempt to load locally
        return load_locally(url)

    return local_loader
Exemplo n.º 2
0
    def load_document(uri):
        doc = jsonld.get_document_loader()(uri)

        if doc['document'].get('status', 200) != 200:
            raise falcon.HTTPBadRequest(
                'Proxy Error',
                'An error has ocurred while requesting this resource.')

        return doc
Exemplo n.º 3
0
def create_document_loader(test):
    base = 'http://json-ld.org/test-suite'
    loader = jsonld.get_document_loader()

    def load_locally(url):
        doc = {'contextUrl': None, 'documentUrl': url, 'document': None}
        options = test.data.get('option')
        if options and url == test.base:
            if ('redirectTo' in options and
                options.get('httpStatus') >= 300):
                doc['documentUrl'] = (test.manifest.data['baseIri'] +
                    options['redirectTo'])
            elif 'httpLink' in options:
                content_type = options.get('contentType')
                if not content_type and url.endswith('.jsonld'):
                    content_type = 'application/ld+json'
                link_header = options.get('httpLink', '')
                if isinstance(link_header, list):
                    link_header = ','.join(link_header)
                link_header = jsonld.parse_link_header(
                    link_header).get('http://www.w3.org/ns/json-ld#context')
                if link_header and content_type != 'application/ld+json':
                    if isinstance(link_header, list):
                        raise Exception('multiple context link headers')
                    doc['contextUrl'] = link_header['target']
        global ROOT_MANIFEST_DIR
        if doc['documentUrl'].find(':') == -1:
            filename = os.path.join(ROOT_MANIFEST_DIR, doc['documentUrl'])
            doc['documentUrl'] = 'file://' + filename
        else:
            #filename = os.path.join(
            #    ROOT_MANIFEST_DIR, doc['documentUrl'][len(base):])
            filename = ROOT_MANIFEST_DIR + doc['documentUrl'][len(base):]
        try:
            doc['document'] = read_json(filename)
        except:
            raise Exception('loading document failed')
        return doc

    def local_loader(url):
        # always load remote-doc and non-base tests remotely
        if ((not url.startswith(base) and url.find(':') != -1) or
            test.manifest.data.get('name') == 'Remote document'):
            return loader(url)

        # attempt to load locally
        return load_locally(url)

    return local_loader
Exemplo n.º 4
0
def compute_calls(uri, numCalls):

    start = datetime.datetime.now()
    for i in range(1, numCalls):
        doc = jsonld.get_document_loader()(uri)
        #doc = CommunityModel.load_document(uri)
        #adding some minimal processing
        if doc['document'] is None:
            print("Error loading the URL: " + uri)

    end = datetime.datetime.now()
    diff = end - start
    total_ms = diff.total_seconds() * 1000

    return total_ms
Exemplo n.º 5
0
def create_document_loader(test):
    base = "http://json-ld.org/test-suite"
    loader = jsonld.get_document_loader()

    def load_locally(url):
        doc = {"contextUrl": None, "documentUrl": url, "document": None}
        options = test.data.get("option")
        if options and url == test.base:
            if "redirectTo" in options and options.get("httpStatus") >= 300:
                doc["documentUrl"] = test.manifest.data["baseIri"] + options["redirectTo"]
            elif "httpLink" in options:
                content_type = options.get("contentType")
                if not content_type and url.endswith(".jsonld"):
                    content_type = "application/ld+json"
                link_header = options.get("httpLink", "")
                if isinstance(link_header, list):
                    link_header = ",".join(link_header)
                link_header = jsonld.parse_link_header(link_header).get("http://www.w3.org/ns/json-ld#context")
                if link_header and content_type != "application/ld+json":
                    if isinstance(link_header, list):
                        raise Exception("multiple context link headers")
                    doc["contextUrl"] = link_header["target"]
        global ROOT_MANIFEST_DIR
        if doc["documentUrl"].find(":") == -1:
            filename = os.path.join(ROOT_MANIFEST_DIR, doc["documentUrl"])
            doc["documentUrl"] = "file://" + filename
        else:
            # filename = os.path.join(
            #    ROOT_MANIFEST_DIR, doc['documentUrl'][len(base):])
            filename = ROOT_MANIFEST_DIR + doc["documentUrl"][len(base) :]
        try:
            doc["document"] = read_json(filename)
        except:
            raise Exception("loading document failed")
        return doc

    def local_loader(url):
        # always load remote-doc and non-base tests remotely
        if (not url.startswith(base) and url.find(":") != -1) or test.manifest.data.get("name") == "Remote document":
            return loader(url)

        # attempt to load locally
        return load_locally(url)

    return local_loader
Exemplo n.º 6
0
 def get(cls, _urlComplement):
     url = CONST_BASE_URL + _urlComplement
     doc = jsonld.get_document_loader()(url)
     return cls.from_dict(doc['document'])
Exemplo n.º 7
0
def create_document_loader(test):
    loader = jsonld.get_document_loader()

    def is_test_suite_url(url):
        return any(url.startswith(base) for base in LOCAL_BASES)

    def strip_base(url):
        for base in LOCAL_BASES:
            if url.startswith(base):
                return url[len(base):]
        raise Exception('unkonwn base')

    def strip_fragment(url):
        if '#' in url:
            return url[:url.index('#')]
        else:
            return url

    def load_locally(url):
        options = test.data.get('option', {})
        content_type = options.get('contentType')

        url_no_frag = strip_fragment(url)
        if not content_type and url_no_frag.endswith('.jsonld'):
            content_type = 'application/ld+json'
        if not content_type and url_no_frag.endswith('.json'):
            content_type = 'application/json'
        if not content_type and url_no_frag.endswith('.html'):
            content_type = 'text/html'
        if not content_type:
            content_type = 'application/octet-stream'
        doc = {
            'contentType': content_type,
            'contextUrl': None,
            'documentUrl': url,
            'document': None
        }
        if options and url == test.base:
            if ('redirectTo' in options and options.get('httpStatus') >= 300):
                doc['documentUrl'] = (test.manifest.data['baseIri'] +
                                      options['redirectTo'])
            elif 'httpLink' in options:
                link_header = options.get('httpLink', '')
                if isinstance(link_header, list):
                    link_header = ','.join(link_header)
                linked_context = jsonld.parse_link_header(link_header).get(
                    'http://www.w3.org/ns/json-ld#context')
                if linked_context and content_type != 'application/ld+json':
                    if isinstance(linked_context, list):
                        raise Exception('multiple context link headers')
                    doc['contextUrl'] = linked_context['target']
                linked_alternate = jsonld.parse_link_header(link_header).get(
                    'alternate')
                # if not JSON-LD, alternate may point there
                if (linked_alternate and linked_alternate.get('type')
                        == 'application/ld+json' and not re.match(
                            r'^application\/(\w*\+)?json$', content_type)):
                    doc['contentType'] = 'application/ld+json'
                    doc['documentUrl'] = jsonld.prepend_base(
                        url, linked_alternate['target'])
        global ROOT_MANIFEST_DIR
        if doc['documentUrl'].find(':') == -1:
            filename = os.path.join(ROOT_MANIFEST_DIR, doc['documentUrl'])
            doc['documentUrl'] = 'file://' + filename
        else:
            filename = test.dirname + strip_fragment(
                strip_base(doc['documentUrl']))
        try:
            doc['document'] = read_file(filename)
        except:
            raise Exception('loading document failed')
        return doc

    def local_loader(url, headers):
        # always load remote-doc tests remotely
        # (some skipped due to lack of reasonable HTTP header support)
        if (test.manifest.data.get('name') == 'Remote document'
                and not test.data.get('runLocal')):
            return loader(url)

        # always load non-base tests remotely
        if not is_test_suite_url(url) and url.find(':') != -1:
            return loader(url)

        # attempt to load locally
        return load_locally(url)

    return local_loader