Example #1
0
def get_thing(id):
    try:
        th_node = get_th_node(id)
        g = R.pull(th_node, cache=True, infer=False, expire=300)

        for prefix, ns in R.fountain.prefixes.items():
            g.bind(prefix, ns)

        if not list(g.objects(th_node, CORE.describedBy)):
            td_node = get_td_node(id)
            g.add((th_node, CORE.describedBy, td_node))

        th_types = list(g.objects(URIRef(th_node), RDF.type))
        th_type = th_types.pop() if th_types else None

        format = TURTLE if request_wants_turtle() else JSONLD
        ttl = serialize_graph(g, format, frame=th_type)

        own_base = unicode(request.url_root)
        ttl = ttl.decode('utf-8').replace(REPOSITORY_BASE + u'/', own_base)
        response = make_response(ttl)
        response.headers['Content-Type'] = 'text/turtle'
        return response
    except IndexError:
        pass

    response = make_response()
    response.status_code = 404

    return response
Example #2
0
def show_ted(ted, format):
    g = ted.to_graph()
    ttl = serialize_graph(g,
                          format,
                          frame=CORE.ThingEcosystemDescription,
                          skolem=False)
    click.echo(ttl)
Example #3
0
    def _get_ted():
        fountain = gw.repository.fountain
        try:
            local_node = URIRef(url_for('_get_ted', _external=True))
            ted = gw.get_ted(ted_uri=local_node, fountain=fountain, lazy=True)
            g = ted.to_graph(node=local_node, fetch=False, abstract=True)

            format = TURTLE if request_wants_turtle() else JSONLD

            ted_str = serialize_graph(g, format, frame=CORE.ThingEcosystemDescription)
            own_base = unicode(request.url_root)
            ted_str = ted_str.decode('utf-8')
            ted_str = ted_str.replace(gw.repository.base.rstrip('/') + u'/', own_base)

            response = make_response(ted_str)
            response.headers['Content-Type'] = format
            return response
        except (EnvironmentError, IndexError, AttributeError):
            traceback.print_exc()
            pass

        response = make_response()
        response.status_code = 404

        return response
Example #4
0
    def _get_enrichments():
        all_enrichments = gw.enrichments
        g = prefixed_graph(gw)
        for e in all_enrichments:
            e.to_graph(graph=g)

        own_base = unicode(request.url_root)
        format = TURTLE if request_wants_turtle() else JSONLD
        ttl = serialize_graph(g, format, frame=MAP.Enrichment)
        ttl = ttl.decode('utf-8').replace(gw.repository.base.rstrip('/') + u'/', own_base)
        response = make_response(ttl)
        response.headers['Content-Type'] = format
        return response
Example #5
0
    def _get_resources():
        all_resources = gw.resources
        g = Graph()
        for r in all_resources:
            g.__iadd__(r.to_graph(graph=prefixed_graph(gw)))

        own_base = unicode(request.url_root)
        format = TURTLE if request_wants_turtle() else JSONLD
        ttl = serialize_graph(g, format)
        ttl = ttl.decode('utf-8').replace(gw.repository.base.rstrip('/') + u'/', own_base)
        response = make_response(ttl)
        response.headers['Content-Type'] = format
        return response
Example #6
0
    def _get_descriptions():
        all_descriptions = gw.descriptions
        g = prefixed_graph(gw)
        for td in all_descriptions:
            g.add((td.node, RDF.type, CORE.ThingDescription))
            g.add((td.node, CORE.describes, td.resource.node))

        own_base = unicode(request.url_root)
        format = TURTLE if request_wants_turtle() else JSONLD
        ttl = serialize_graph(g, format, frame=CORE.ThingDescription)
        ttl = ttl.decode('utf-8').replace(gw.repository.base.rstrip('/') + u'/', own_base)
        response = make_response(ttl)
        response.headers['Content-Type'] = format
        return response
Example #7
0
    def _get_enrichment(id):
        try:
            g = gw.get_enrichment(id).to_graph(graph=prefixed_graph(gw))
            format = TURTLE if request_wants_turtle() else JSONLD
            ttl = serialize_graph(g, format, frame=MAP.Enrichment)

            own_base = unicode(request.url_root)
            ttl = ttl.decode('utf-8').replace(gw.repository.base.rstrip('/') + u'/', own_base)
            response = make_response(ttl)
            response.headers['Content-Type'] = format
            return response
        except (IndexError, AttributeError):
            traceback.print_exc()
            pass

        response = make_response()
        response.status_code = 404

        return response
Example #8
0
    def _get_td(id):
        try:
            td = gw.get_description(id, fetch=False)
            g = td.to_graph(graph=prefixed_graph(gw))
            format = TURTLE if request_wants_turtle() else JSONLD
            ttl = serialize_graph(g, format, frame=CORE.ThingDescription)

            own_base = unicode(request.url_root)
            ttl = ttl.decode('utf-8').replace(gw.repository.base.rstrip('/') + u'/', own_base)
            response = make_response(ttl)
            response.headers['Content-Type'] = format
            return response
        except (IndexError, AttributeError):
            pass

        response = make_response()
        response.status_code = 404

        return response
Example #9
0
def get_ted():
    try:
        local_node = URIRef(url_for('get_ted', _external=True))
        fountain = R.fountain
        known_types = fountain.types
        ns = R.ns()
        ted = TED()
        g = ted.to_graph(node=local_node, abstract=True)
        for root_uri, td_uri in VTED.roots:
            root_uri = URIRef(root_uri)
            types = get_th_types(root_uri, infer=True)
            valid_types = filter(lambda t: t.n3(ns) in known_types, types)
            if valid_types:
                r = Resource(root_uri, types=valid_types)
                if td_uri is None:
                    g.__iadd__(r.to_graph(abstract=True))
                g.add((ted.ecosystem.node, CORE.hasComponent, root_uri))

        format = TURTLE if request_wants_turtle() else JSONLD
        # g = ted.to_graph(node=local_node, abstract=True)
        for prefix, ns in fountain.prefixes.items():
            g.bind(prefix, ns)

        ted_str = serialize_graph(g,
                                  format,
                                  frame=CORE.ThingEcosystemDescription)
        own_base = unicode(request.url_root)
        ted_str = ted_str.decode('utf-8')
        ted_str = ted_str.replace(REPOSITORY_BASE + u'/', own_base)

        response = make_response(ted_str)
        response.headers['Content-Type'] = format
        return response
    except (EnvironmentError, IndexError):
        pass

    response = make_response()
    response.status_code = 404

    return response
Example #10
0
def get_td(id):
    try:
        td_node = get_td_node(id)
        g = R.pull(td_node, cache=True, infer=False, expire=300)
        for ns, uri in R.fountain.prefixes.items():
            g.bind(ns, uri)

        format = TURTLE if request_wants_turtle() else JSONLD
        ttl = serialize_graph(g, format, frame=CORE.ThingDescription)

        own_base = unicode(request.url_root)
        ttl = ttl.decode('utf-8').replace(REPOSITORY_BASE + u'/', own_base)
        response = make_response(ttl)
        response.headers['Content-Type'] = 'text/turtle'
        return response
    except IndexError:
        pass

    response = make_response()
    response.status_code = 404

    return response
Example #11
0
    def _get_resource(uri):
        try:
            g = gw.get_resource(uri).to_graph(graph=prefixed_graph(gw))
            r_types = list(g.objects(URIRef(uri), RDF.type))
            r_type = r_types.pop() if r_types else None

            format = TURTLE if request_wants_turtle() else JSONLD
            ttl = serialize_graph(g, format, frame=r_type)

            own_base = unicode(request.url_root)
            ttl = ttl.decode('utf-8').replace(gw.repository.base.rstrip('/') + u'/', own_base)
            response = make_response(ttl)
            response.headers['Content-Type'] = format
            return response
        except (IndexError, AttributeError):
            traceback.print_exc()
            pass

        response = make_response()
        response.status_code = 404

        return response
Example #12
0
def show_thing(g, format):
    th_node = g.identifier
    th_types = list(g.objects(URIRef(th_node), RDF.type))
    th_type = th_types.pop() if th_types else None
    ttl = serialize_graph(g, format, frame=th_type, skolem=False)
    click.echo(ttl)
Example #13
0
def show_td(td, format):
    g = td.to_graph()
    ttl = serialize_graph(g, format, frame=CORE.ThingDescription, skolem=False)
    click.echo(ttl)