def publish(obj, model, oml, rdfstore): gr = Graph() # import pdb; pdb.set_trace() # ns_mgr = NamespaceManager(Graph()) # gr.namespace_manager = ns_mgr try: gr = build_rdf(gr, obj, oml, False) except Exception as e: return HttpResponse("Error during serialisation: " + str(e), status=500) for ns in _nslist.keys(): gr.namespace_manager.bind(str(ns), namespace.Namespace(str(_nslist[ns])), override=False) # curl -X POST -H "Content-Type: text/turtle" -d @- http://192.168.56.151:8080/marmotta/import/upload?context=http://mapstory.org/def/featuretypes/gazetteer resttgt = "".join( (rdfstore['server'], _resolveTemplate(rdfstore['target'], model, obj))) if rdfstore['server_api'] == "RDF4JREST": return _rdf4j_push(rdfstore, resttgt, model, obj, gr) elif rdfstore['server_api'] == "LDP": return _ldp_push(rdfstore, resttgt, model, obj, gr) else: return HttpResponse("Unknown server API", status=500)
def _tordf(request,model,id,key): if request.GET.get('pdb') : import pdb; pdb.set_trace() format = request.GET.get('_format') if format not in ('turtle','json-ld'): if format == 'json': format = "json-ld" else: format = "turtle" # find the model type referenced try: (app,model) = model.split('.') ct = ContentType.objects.get(app_label=app,model=model) except: ct = ContentType.objects.get(model=model) if not ct : raise Http404("No such model found") oml = ObjectMapping.objects.filter(content_type=ct) if not oml : return HttpResponse("Model not serialisable to RDF", status=410 ) if id : obj = get_object_or_404(ct.model_class(), pk=id) else : try: obj = ct.model_class().objects.get_by_natural_key(key) except Exception as e: try: (prefix,term) = key.split(':') ns = Namespace.objects.get(prefix=prefix) urikey = "".join((ns.uri,term)) obj = ct.model_class().objects.get_by_natural_key(urikey) except Exception as e2: raise e # ok so object exists and is mappable, better get down to it.. includemembers = False gr = Graph() # import pdb; pdb.set_trace() # ns_mgr = NamespaceManager(Graph()) # gr.namespace_manager = ns_mgr try: gr = build_rdf(gr, obj, oml, includemembers) except Exception as e: raise Http404("Error during serialisation: " + str(e) ) for ns in _nslist.keys() : gr.namespace_manager.bind( str(ns), namespace.Namespace(str(_nslist[ns])), override=False) return HttpResponse(content_type="text/turtle", content=gr.serialize(format=format))
def publish(obj, model, oml, rdfstore ): gr = Graph() # import pdb; pdb.set_trace() # ns_mgr = NamespaceManager(Graph()) # gr.namespace_manager = ns_mgr try: gr = build_rdf(gr, obj, oml, False) except Exception as e: raise Exception("Error during serialisation: " + str(e) ) for ns in _nslist.keys() : gr.namespace_manager.bind( str(ns), namespace.Namespace(str(_nslist[ns])), override=False) # curl -X POST -H "Content-Type: text/turtle" -d @- http://192.168.56.151:8080/marmotta/import/upload?context=http://mapstory.org/def/featuretypes/gazetteer return push_to_store( None, model, obj, gr )
def as_resource(gr,curie) : cleaned = dequote(curie) if cleaned[0:4] == 'http' : return URIRef(cleaned) # this will raise error if not valid curie format try: (ns,value) = cleaned.split(":",2) except: return URIRef(cleaned) # just have to assume its not a problem - URNs are valid uri.s # raise ValueError("value not value HTTP or CURIE format %s" % curie) try : nsuri = Namespace.getNamespace(ns) if nsuri : gr.namespace_manager.bind( str(ns), namespace.Namespace(nsuri.uri), override=False) return URIRef("".join((nsuri.uri,value))) else : return URIRef(cleaned) except: raise ValueError("prefix " + ns + "not recognised")
# You should have received a copy of the GNU General Public License # along with Kochief. If not, see <http://www.gnu.org/licenses/>. import rdflib.graph as rg import rdflib.namespace as ns import rdflib.term as rt import django.conf as conf DB_MAP = { 'mysql': 'MySQL', 'postgresql': 'PostgreSQL', 'postgresql_psycopg2': 'PostgreSQL', 'sqlite3': 'SQLite', } LOCALNS = ns.Namespace(conf.settings.LOCALNS) STORE_GRAPH = rg.ConjunctiveGraph(DB_MAP[conf.settings.DATABASE_ENGINE], identifier=LOCALNS) # TODO: graph configuration for mysql/postgresql STORE_GRAPH.open(conf.settings.DATABASE_NAME, create=True) class ResourceManager(object): """Manager for resource objects.""" def all(self): return STORE_GRAPH def filter(self, sparql_query): return STORE_GRAPH.query(sparql_query) def get(self, id):