From e968462893be2c73a38ebc73ca4722d699ee2bc2 Mon Sep 17 00:00:00 2001 From: Rahul Date: Wed, 23 Oct 2024 14:28:23 +0200 Subject: [PATCH 1/2] update blazegraph sparql endpoint --- nanopub/definitions.py | 2 ++ nanopub/nanopub.py | 4 ++-- nanopub/nanopub_conf.py | 5 ++++- nanopub/sign_utils.py | 14 ++++++++++++-- nanopub/utils.py | 11 ++++++++++- 5 files changed, 30 insertions(+), 6 deletions(-) diff --git a/nanopub/definitions.py b/nanopub/definitions.py index 6d73034d..209f42c0 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 26d17506..ce0a677e 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 3977f3df..336f2ab0 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 9d4bccb0..d2856f07 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 6f2ea82e..ad41ef6a 100644 --- a/nanopub/utils.py +++ b/nanopub/utils.py @@ -7,7 +7,16 @@ from nanopub.definitions import DUMMY_NAMESPACE, DUMMY_URI -log = logging.getLogger() +logging.basicConfig( + filename='app.log', + filemode='a', + level=logging.INFO, + format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' +) + +log = logging.getLogger(__name__) + +log.info("Logger initialized. Starting the application.") class MalformedNanopubError(ValueError): From eef405a990c950b554e612bb42433c6633d4de34 Mon Sep 17 00:00:00 2001 From: Rahul Date: Wed, 23 Oct 2024 14:45:17 +0200 Subject: [PATCH 2/2] unmounted the logger in ./utils.py --- nanopub/utils.py | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/nanopub/utils.py b/nanopub/utils.py index ad41ef6a..3090901e 100644 --- a/nanopub/utils.py +++ b/nanopub/utils.py @@ -7,16 +7,7 @@ from nanopub.definitions import DUMMY_NAMESPACE, DUMMY_URI -logging.basicConfig( - filename='app.log', - filemode='a', - level=logging.INFO, - format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' -) - -log = logging.getLogger(__name__) - -log.info("Logger initialized. Starting the application.") +log = logging.getLogger() class MalformedNanopubError(ValueError): @@ -108,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