Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate Local Blazegraph SPARQL Endpoint #167

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions nanopub/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
USER_CONFIG_DIR = Path.home() / ".nanopub"
DEFAULT_PROFILE_PATH = USER_CONFIG_DIR / "profile.yml"

BLAZEGRAPH_SERVER = 'http://localhost:9999/blazegraph/namespace/kb/sparql'

NANOPUB_TEST_SERVER = 'https://np.test.knowledgepixels.com/'
# List of servers: https://monitor.petapico.org/.csv
NANOPUB_SERVER_LIST = [
Expand Down
4 changes: 2 additions & 2 deletions nanopub/nanopub.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,11 @@ def sign(self) -> None:


def publish(self) -> None:
"""Publish a Nanopub object"""
"""Publish a Nanopub object & optionally update the blazegraph sparql endpoint"""
if not self.source_uri:
self.sign()

publish_graph(self.rdf, use_server=self._conf.use_server)
publish_graph(self.rdf, use_server=self._conf.use_server, publish_to_blazegraph=self._conf.publish_to_blazegraph, blazegraph_server=self._conf.blazegraph_server)
log.info(f'Published {self.source_uri} to {self._conf.use_server}')
self.published = True

Expand Down
5 changes: 4 additions & 1 deletion nanopub/nanopub_conf.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from dataclasses import asdict, dataclass
from typing import Optional

from nanopub.definitions import NANOPUB_SERVER_LIST
from nanopub.definitions import NANOPUB_SERVER_LIST, BLAZEGRAPH_SERVER
from nanopub.profile import Profile


Expand All @@ -24,6 +24,9 @@ class NanopubConf:

profile: Optional[Profile] = None

publish_to_blazegraph: bool = False
blazegraph_server: str = BLAZEGRAPH_SERVER

use_test_server: bool = False
use_server: str = NANOPUB_SERVER_LIST[0]

Expand Down
14 changes: 12 additions & 2 deletions nanopub/sign_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from Crypto.Signature import PKCS1_v1_5
from rdflib import BNode, ConjunctiveGraph, Graph, Literal, Namespace, URIRef

from nanopub.definitions import NANOPUB_SERVER_LIST, NP_PURL, NP_TEMP_PREFIX
from nanopub.definitions import NANOPUB_SERVER_LIST, NP_PURL, NP_TEMP_PREFIX, BLAZEGRAPH_SERVER
from nanopub.namespaces import NPX
from nanopub.profile import Profile
from nanopub.trustyuri.rdf import RdfHasher, RdfUtils
Expand Down Expand Up @@ -107,14 +107,24 @@ def replace_trusty_in_graph(trusty_artefact: str, dummy_ns: str, graph: Conjunct
return graph


def publish_graph(g: ConjunctiveGraph, use_server: str = NANOPUB_SERVER_LIST[0]) -> bool:
def publish_graph(g: ConjunctiveGraph, use_server: str = NANOPUB_SERVER_LIST[0], publish_to_blazegraph: bool = False, blazegraph_server: str = BLAZEGRAPH_SERVER) -> bool:
"""Publish a signed nanopub to the given nanopub server.
"""
log.info(f"Publishing to the nanopub server {use_server}")
headers = {'Content-Type': 'application/trig'}
# NOTE: nanopub-java uses {'Content-Type': 'application/x-www-form-urlencoded'}
data = g.serialize(format="trig")
r = requests.post(use_server, headers=headers, data=data.encode('utf-8'))
"""
Update your blazegraph sparql endpoint with the same signed nanopub.
"""
# NOTE: by-default, it updates the locally-run blazegraph sparql endpoint at port 9999
if publish_to_blazegraph:
headers = {'Content-Type': 'application/x-trig'}
r = requests.post(blazegraph_server, headers=headers, data=data)
log.info(f"Publishing to the blazegraph server {blazegraph_server}")
log.info(f"Status code: {r.status_code}")
log.info(f"Response Content: {r.text}")
r.raise_for_status()
return True

Expand Down
2 changes: 1 addition & 1 deletion nanopub/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,4 @@ def extract_np_metadata(g: ConjunctiveGraph) -> NanopubMetadata:
# TODO: improve as the signed np namespace might be using / or # or .
np_meta.namespace = Namespace(np_meta.np_uri + '#')

return np_meta
return np_meta