diff --git a/nanopub/definitions.py b/nanopub/definitions.py index 6d73034..209f42c 100644 --- a/nanopub/definitions.py +++ b/nanopub/definitions.py @@ -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 = [ diff --git a/nanopub/nanopub.py b/nanopub/nanopub.py index 26d1750..ce0a677 100644 --- a/nanopub/nanopub.py +++ b/nanopub/nanopub.py @@ -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 diff --git a/nanopub/nanopub_conf.py b/nanopub/nanopub_conf.py index 3977f3d..336f2ab 100644 --- a/nanopub/nanopub_conf.py +++ b/nanopub/nanopub_conf.py @@ -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 @@ -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] diff --git a/nanopub/sign_utils.py b/nanopub/sign_utils.py index 9d4bccb..d2856f0 100644 --- a/nanopub/sign_utils.py +++ b/nanopub/sign_utils.py @@ -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 @@ -107,7 +107,7 @@ 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}") @@ -115,6 +115,16 @@ def publish_graph(g: ConjunctiveGraph, use_server: str = NANOPUB_SERVER_LIST[0]) # 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 diff --git a/nanopub/utils.py b/nanopub/utils.py index 6f2ea82..3090901 100644 --- a/nanopub/utils.py +++ b/nanopub/utils.py @@ -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 \ No newline at end of file