dkg 8.0.12__py3-none-any.whl → 8.0.14__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,7 +5,8 @@ from dkg.exceptions import DatasetInputFormatNotSupported, InvalidDataset
5
5
  from dkg.types import JSONLD, NQuads
6
6
  from pyld import jsonld
7
7
  from dkg.constants import DEFAULT_RDF_FORMAT, DEFAULT_CANON_ALGORITHM, ESCAPE_MAP
8
- from rdflib import Graph, BNode, URIRef, Literal as RDFLiteral
8
+ from rdflib import Graph, BNode, URIRef, Dataset
9
+ from rdflib.exceptions import ParserError as RDFParserError
9
10
  from uuid import uuid4
10
11
  from web3 import Web3
11
12
  import math
@@ -154,29 +155,26 @@ def generate_missing_ids_for_blank_nodes(nquads_list: list[str] | None) -> list[
154
155
 
155
156
  return term # Return IRIs or Literals unchanged
156
157
 
157
- # Create a temporary graph for parsing individual quads
158
- result = []
158
+ all_nquads = "\n".join(nquad for nquad in nquads_list if nquad.strip())
159
159
 
160
- # Process each N-Quad string individually to maintain order
161
- for nquad in nquads_list:
162
- if not nquad.strip():
163
- continue
160
+ # Create a single Dataset
161
+ g = Graph()
162
+ try:
163
+ g.parse(data=all_nquads, format="nt")
164
+ except RDFParserError:
165
+ raise UnsupportedJSONLD(nquads_list)
164
166
 
165
- # Parse single N-Quad
166
- g = Graph()
167
- g.parse(data=nquad, format="nquads")
168
-
169
- # Get the triple and replace blank nodes
170
- for s, p, o in g:
171
- updated_quad = (
172
- replace_blank_node(s),
173
- replace_blank_node(p),
174
- replace_blank_node(o),
175
- )
176
- # Format as N-Quad string
177
- result.append(
178
- f"{updated_quad[0].n3()} {updated_quad[1].n3()} {updated_quad[2].n3()} ."
179
- )
167
+ # Process all quads
168
+ result = []
169
+ for s, p, o in g:
170
+ updated_quad = (
171
+ replace_blank_node(s),
172
+ replace_blank_node(p),
173
+ replace_blank_node(o),
174
+ )
175
+ result.append(
176
+ f"{updated_quad[0].n3()} {updated_quad[1].n3()} {updated_quad[2].n3()} ."
177
+ )
180
178
 
181
179
  return result
182
180
 
@@ -184,16 +182,13 @@ def generate_missing_ids_for_blank_nodes(nquads_list: list[str] | None) -> list[
184
182
  def group_nquads_by_subject(nquads_list: list[str], sort: bool = False):
185
183
  grouped = {}
186
184
 
187
- # Process each quad in original order
188
- for nquad in nquads_list:
189
- if not nquad.strip(): # Skip empty lines
190
- continue
185
+ all_nquads = "\n".join(nquad for nquad in nquads_list if nquad.strip())
191
186
 
192
- # Parse single quad
193
- g = Graph()
194
- g.parse(data=nquad, format="nquads")
195
- quad = next(iter(g))
196
- subject, predicate, obj = quad
187
+ d = Dataset()
188
+ d.parse(data=all_nquads, format="nquads")
189
+
190
+ for quad in d:
191
+ subject, predicate, obj, graph = quad
197
192
 
198
193
  # Get subject key
199
194
  subject_key = (
@@ -206,11 +201,8 @@ def group_nquads_by_subject(nquads_list: list[str], sort: bool = False):
206
201
  if subject_key not in grouped:
207
202
  grouped[subject_key] = []
208
203
 
209
- # Format object
210
- object_value = f'"{obj}"' if isinstance(obj, RDFLiteral) else f"<{obj}>"
211
-
212
204
  # Add quad to group
213
- quad_string = f"{subject_key} <{predicate}> {object_value} ."
205
+ quad_string = f"{subject.n3()} {predicate.n3()} {obj.n3()} ."
214
206
  grouped[subject_key].append(quad_string)
215
207
 
216
208
  # Return grouped quads (sorted if requested)
@@ -266,3 +258,44 @@ def escape_literal_dict(obj):
266
258
  return escape_literal_string(s=obj)
267
259
  else:
268
260
  return obj
261
+
262
+
263
+ # Used when JSON-LD parsing fails due to quads being passed instead of triples
264
+ class UnsupportedJSONLD(Exception):
265
+ def __init__(self, nquads_list):
266
+ self.nquads_list = nquads_list
267
+ self.message = f"""
268
+ Unsupported JSON-LD input detected
269
+
270
+ After parsing the JSON-LD input, the parser detected creation of new named graphs.
271
+ The DKG does not support custom named graphs.
272
+
273
+ Problematic Quads:
274
+
275
+ {self.find_problematic_quads()}
276
+
277
+ Full Parsed N-Quads Array:
278
+
279
+ {self.format_nquads_list()}
280
+
281
+ """
282
+ super().__init__(self.message)
283
+
284
+ def __str__(self):
285
+ return f"{self.__class__.__name__}: {self.message}"
286
+
287
+ def format_nquads_list(self):
288
+ return "\n".join(nquad.strip() for nquad in self.nquads_list)
289
+
290
+ def find_problematic_quads(self):
291
+ problematic = []
292
+ g = Graph()
293
+ for quad in self.nquads_list:
294
+ if not quad.strip():
295
+ continue
296
+ try:
297
+ g.parse(data=quad, format="nt")
298
+ except RDFParserError:
299
+ problematic.append(quad)
300
+
301
+ return "\n".join(f"{i + 1}. {quad}" for i, quad in enumerate(problematic))
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: dkg
3
- Version: 8.0.12
3
+ Version: 8.0.14
4
4
  Summary: Python library for interacting with the OriginTrail Decentralized Knowledge Graph
5
5
  License: Apache-2.0
6
6
  Author: Uladzislau Hubar
@@ -68,15 +68,15 @@ dkg/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
68
68
  dkg/utils/blockchain_request.py,sha256=CMz3sNRhfmYirm0-VUK2HTpbRfgc1gtG2SoPIuRYKb0,12446
69
69
  dkg/utils/decorators.py,sha256=uUh0xI9wv1gE6JF44eYEfUPGSFEBLR6Ua5D9Dhc3s10,1566
70
70
  dkg/utils/knowledge_asset_tools.py,sha256=uehOshzaYg03hDU8RSIV_k2mNaASVgqyyErzj6GwBxw,81
71
- dkg/utils/knowledge_collection_tools.py,sha256=hPCFcIdyWDCgfbo5uPQizvfin0hHvEElAYpvKYVDIBs,7951
71
+ dkg/utils/knowledge_collection_tools.py,sha256=qGEZqsAo25J4P0HCwXA6EI37fVsAf7Nne1zEXR0Ot0Y,8776
72
72
  dkg/utils/merkle.py,sha256=924kloBAnCXydteVtWMj_QLP5CRJf2GbHMZ-lbIK0KE,5141
73
73
  dkg/utils/metadata.py,sha256=483OroYwGNfZ_cCXfH3-xUrZgiR4mjjo9iU_Ie5RYRs,1658
74
74
  dkg/utils/node_request.py,sha256=wppF8Xf0RkuAAcURcYgyjGAdMsXm0L5YEem8wFGE2g8,6517
75
75
  dkg/utils/rdf.py,sha256=AvlcxZEeP58UbaGGvPX_ss69O-tgTXOJ9y9COZqVgkw,2973
76
76
  dkg/utils/string_transformations.py,sha256=eR51fVwTF9QKxEqXo9_1Bfw_k8iQajdXD6rKuTvhs70,972
77
77
  dkg/utils/ual.py,sha256=g7PFyS4Sbwjmwkq-eB20uRULEC2wlPGZr31BVQjs5OQ,1569
78
- dkg-8.0.12.dist-info/LICENSE,sha256=Dr70w2zcW8-jrPGlpTTTlJPL8lR4j2zpDD32tdEFgjY,11375
79
- dkg-8.0.12.dist-info/METADATA,sha256=qoW7Du0ik1jwQC-qkG3nBQgZQLMoRtUrh4FMJXLJzcQ,10821
80
- dkg-8.0.12.dist-info/NOTICE,sha256=Rk5toFR2ZqPwVZ3P_P4wE6U1xCnWR9KD3rNBqfPY7h8,368
81
- dkg-8.0.12.dist-info/WHEEL,sha256=fGIA9gx4Qxk2KDKeNJCbOEwSrmLtjWCwzBz351GyrPQ,88
82
- dkg-8.0.12.dist-info/RECORD,,
78
+ dkg-8.0.14.dist-info/LICENSE,sha256=Dr70w2zcW8-jrPGlpTTTlJPL8lR4j2zpDD32tdEFgjY,11375
79
+ dkg-8.0.14.dist-info/METADATA,sha256=KnPQYF84DahhRYTXoH3vd478uHS7KeF829qLuSZDiV0,10821
80
+ dkg-8.0.14.dist-info/NOTICE,sha256=Rk5toFR2ZqPwVZ3P_P4wE6U1xCnWR9KD3rNBqfPY7h8,368
81
+ dkg-8.0.14.dist-info/WHEEL,sha256=fGIA9gx4Qxk2KDKeNJCbOEwSrmLtjWCwzBz351GyrPQ,88
82
+ dkg-8.0.14.dist-info/RECORD,,
File without changes
File without changes
File without changes