def from_model(graph, query): s = query rv = [] while s: elem = URIRef(s) class_of = None class_of_objects = graph.objects(elem, RDF.type) if class_of_objects: for class_of_value in graph.objects(elem, RDF.type): class_of = from_node(graph, class_of_value, [], False, query) break triples = list(triple_frags(elem, graph, [], class_of, query)) graph.remove((elem, None, None)) model = { 'display': DISPLAYER.apply(elem), 'literal_encode': quote_plus(str(elem)), 'uri': query, 'uri_encode': quote_plus(query), 'triples': triples, 'has_triples': len(triples) > 0, 'classOf': class_of, 'context': CONTEXT, 'inverses': list(inverse_triple_frags(elem, graph, query)) } graph.remove((None, None, elem)) rv.append(model) s = next_subject(graph, class_of) return { 'head': rv[0], 'tail': rv[1:] }
def from_node(graph, node, stack, recurse=True, query=None): if type(node) == URIRef: fragment = None if '#' in str(node): fragment = str(node)[str(node).index('#') + 1:] if recurse and str(node).startswith(str(query)): triples = list(triple_frags(node, graph, stack, None, query)) graph.remove((node, None, None)) return { 'display': DISPLAYER.apply(node), 'uri': str(node), 'uri_encode': quote_plus(str(node)), 'triples': triples, 'has_triples': len(triples) > 0, 'context': CONTEXT, 'fragment': fragment } else: return { 'display': DISPLAYER.apply(node), 'uri': str(node), 'uri_encode': quote_plus(str(node)), 'triples': [], 'has_triples': False, 'context': CONTEXT, 'fragment': fragment } elif type(node) == BNode: triples = list(triple_frags(node, graph, stack, None, query)) graph.remove((node, None, None)) return { 'display': DISPLAYER.apply(node), 'bnode': True, 'triples': triples, 'has_triples': len(triples) > 0, 'context': CONTEXT } elif type(node) == Literal: return { 'display': str(node), 'literal': True, 'literal_encode': quote_plus(str(node)), 'lang': node.language, 'datatype': from_dt(node.datatype), 'has_triples': False, 'context': CONTEXT }
def from_dt(dt): if dt: return { 'display': DISPLAYER.apply(dt), 'uri': str(dt) } else: return None
def sparql_results_to_dict(result): if result.findall( "{http://www.w3.org/2005/sparql-results#}boolean"): r = (result.findall( "{http://www.w3.org/2005/sparql-results#}boolean")[0].text == "true") return {"boolean": r} variables = [] head = result.findall( "{http://www.w3.org/2005/sparql-results#}head")[0] r = {"variables": [], "results": [], "context": CONTEXT} for variable in head: variables.append(variable.get("name")) r["variables"].append({"name": variable.get("name")}) body = result.findall( "{http://www.w3.org/2005/sparql-results#}results")[0] results = body.findall( "{http://www.w3.org/2005/sparql-results#}result") n = 0 for result in results: r["results"].append({"result": []}) for v in variables: r["results"][n]["result"].append(dict()) bindings = result.findall( "{http://www.w3.org/2005/sparql-results#}binding") for binding in bindings: name = binding.get("name") target = r["results"][n]["result"][variables.index(name)] = {} if (binding[0].tag == '{http://www.w3.org/2005/sparql-results#}uri'): target['uri'] = binding[0].text target['display'] = DISPLAYER.apply(binding[0].text) if (binding[0].tag == '{http://www.w3.org/2005/sparql-results#}bnode'): target['bnode'] = binding[0].text if (binding[0].tag == '{http://www.w3.org/2005/sparql-results#}literal'): target['value'] = binding[0].text if binding[0].get( "{http://www.w3.org/XML/1998/namespace}lang"): target['lang'] = binding[0].get( "{http://www.w3.org/XML/1998/namespace}lang") elif binding[0].get("datatype"): target['datatype'] = binding[0].get("datatype") n += 1 return r