def _render_skos_rdf(self): namespace_manager = NamespaceManager(Graph()) namespace_manager.bind('dct', DCTERMS) namespace_manager.bind('skos', SKOS) concept_g = Graph() concept_g.namespace_manager = namespace_manager for s, p, o in g.triples((URIRef(self.concept.uri), None, None)): concept_g.add((s, p, o)) # serialise in the appropriate RDF format if self.format in ['application/rdf+json', 'application/json']: return Response(concept_g.serialize(format='json-ld'), mimetype=self.format) else: return Response(concept_g.serialize(format=self.format), mimetype=self.format)
def get_narrowers(uri, depth): """ Recursively get all skos:narrower properties as a list. :param uri: URI node :param depth: The current depth :param g: The graph :return: list of tuples(tree_depth, uri, prefLabel) :rtype: list """ depth += 1 # Some RVA sources won't load on first try, so .. # if failed, try load again. g = None max_attempts = 10 for i in range(max_attempts): try: g = Graph().parse(uri + ".ttl", format="turtle") break except: logging.warning( "Failed to load resource at URI {}. Attempt: {}.".format(uri, i + 1) ) if not g: raise Exception( "Failed to load Graph from {}. Maximum attempts exceeded {}.".format( uri, max_attempts ) ) items = [] for s, p, o in g.triples((None, SKOS.broader, URIRef(uri))): items.append((depth, str(s), get_prefLabel_from_uri(s))) items.sort(key=lambda x: x[2]) count = 0 for item in items: count += 1 new_items = get_narrowers(item[1], item[0]) items = items[:count] + new_items + items[count:] count += len(new_items) return items