src/rdflib/plugins/sparql/results/tsvresults.py
1"""
2This implements the Tab Separated SPARQL Result Format
3
4It is implemented with pyparsing, reusing the elements from the SPARQL Parser
5"""
6
7from __future__ import annotations
8
9import codecs
10import typing
11from typing import IO, Union
12
13from pyparsing import (
14 FollowedBy,
15 LineEnd,
16 Literal,
17 Optional,
18 ParserElement,
19 Suppress,
20 ZeroOrMore,
21)
22
23from rdflib.plugins.sparql.parser import (
24 BLANK_NODE_LABEL,
25 IRIREF,
26 LANGTAG,
27 STRING_LITERAL1,
28 STRING_LITERAL2,
29 BooleanLiteral,
30 NumericLiteral,
31 Var,
32)
33from rdflib.plugins.sparql.parserutils import Comp, CompValue, Param
34from rdflib.query import Result, ResultParser
35from rdflib.term import BNode, Identifier, URIRef
36from rdflib.term import Literal as RDFLiteral
37
38ParserElement.set_default_whitespace_chars(" \n")
39
40
41String = STRING_LITERAL1 | STRING_LITERAL2
42
43RDFLITERAL = Comp(
44 "literal",
45 Param("string", String)
46 + Optional(
47 Param("lang", LANGTAG.leave_whitespace())
48 | Literal("^^").leave_whitespace()
49 + Param("datatype", IRIREF).leave_whitespace()
50 ),
51)
52
53NONE_VALUE = object()
54
55EMPTY = FollowedBy(LineEnd()) | FollowedBy("\t")
56EMPTY.set_parse_action(lambda x: NONE_VALUE)
57
58TERM = RDFLITERAL | IRIREF | BLANK_NODE_LABEL | NumericLiteral | BooleanLiteral
59
60ROW = (EMPTY | TERM) + ZeroOrMore(Suppress("\t") + (EMPTY | TERM))
61ROW.parse_with_tabs()
62
63HEADER = Var + ZeroOrMore(Suppress("\t") + Var)
64HEADER.parse_with_tabs()
65
66
67class TSVResultParser(ResultParser):
68 """Parses SPARQL TSV results into a Result object."""
69
70 # type error: Signature of "parse" incompatible with supertype "ResultParser" [override]
71 def parse(self, source: IO, content_type: typing.Optional[str] = None) -> Result: # type: ignore[override]
72 if isinstance(source.read(0), bytes):
73 # if reading from source returns bytes do utf-8 decoding
74 # type error: Incompatible types in assignment (expression has type "StreamReader", variable has type "IO[Any]")
75 source = codecs.getreader("utf-8")(source) # type: ignore[assignment]
76
77 r = Result("SELECT")
78
79 header = source.readline()
80
81 r.vars = list(HEADER.parse_string(header.strip(), parse_all=True))
82 r.bindings = []
83 while True:
84 line = source.readline()
85 if not line:
86 break
87 line = line.strip("\n")
88 if line == "":
89 continue
90
91 row = ROW.parse_string(line, parse_all=True)
92 this_row_dict = {}
93 for var, val_read in zip(r.vars, row):
94 val = self.convertTerm(val_read)
95 if val is None:
96 # Skip unbound vars
97 continue
98 this_row_dict[var] = val
99 # Preserve solution row cardinality, including fully-unbound rows.
100 r.bindings.append(this_row_dict)
101 return r
102
103 def convertTerm(
104 self, t: Union[object, RDFLiteral, BNode, CompValue, URIRef]
105 ) -> typing.Optional[Identifier]:
106 if t is NONE_VALUE:
107 return None
108 if isinstance(t, CompValue):
109 if t.name == "literal":
110 return RDFLiteral(t.string, lang=t.lang, datatype=t.datatype)
111 else:
112 raise Exception("I dont know how to handle this: %s" % (t,))
113 elif isinstance(t, (RDFLiteral, BNode, URIRef)):
114 return t
115 else:
116 raise ValueError(f"Unexpected type {type(t)} found in TSV result")