Source code

Download source archive ↓

src/rdflib/plugins/sparql/results/csvresults.py

1"""
2This module implements a parser and serializer for the CSV SPARQL result
3formats
4
5http://www.w3.org/TR/sparql11-results-csv-tsv/
6"""
7
8from __future__ import annotations
9
10import codecs
11import csv
12from io import BufferedIOBase, TextIOBase
13from typing import IO, Dict, List, Optional, Union, cast
14
15from rdflib.plugins.sparql.processor import SPARQLResult
16from rdflib.query import Result, ResultParser, ResultSerializer
17from rdflib.term import BNode, Identifier, Literal, URIRef, Variable
18
19
20class CSVResultParser(ResultParser):
21    """Parses SPARQL CSV results into a Result object."""
22
23    def __init__(self):
24        self.delim = ","
25
26    # type error: Signature of "parse" incompatible with supertype "ResultParser"
27    def parse(self, source: IO, content_type: Optional[str] = None) -> Result:  # type: ignore[override]
28        r = Result("SELECT")
29
30        # type error: Incompatible types in assignment (expression has type "StreamReader", variable has type "IO[Any]")
31        if isinstance(source.read(0), bytes):
32            # if reading from source returns bytes do utf-8 decoding
33            # type error: Incompatible types in assignment (expression has type "StreamReader", variable has type "IO[Any]")
34            source = codecs.getreader("utf-8")(source)  # type: ignore[assignment]
35
36        reader = csv.reader(source, delimiter=self.delim)
37        r.vars = [Variable(x) for x in next(reader)]
38        r.bindings = []
39
40        for row in reader:
41            r.bindings.append(self.parseRow(row, r.vars))
42
43        return r
44
45    def parseRow(
46        self, row: List[str], v: List[Variable]
47    ) -> Dict[Variable, Union[BNode, URIRef, Literal]]:
48        return dict(
49            (var, val)
50            for var, val in zip(v, [self.convertTerm(t) for t in row])
51            if val is not None
52        )
53
54    def convertTerm(self, t: str) -> Optional[Union[BNode, URIRef, Literal]]:
55        if t == "":
56            return None
57        if t.startswith("_:"):
58            return BNode(t)  # or generate new IDs?
59        if t.startswith("http://") or t.startswith("https://"):  # TODO: more?
60            return URIRef(t)
61        return Literal(t)
62
63
64class CSVResultSerializer(ResultSerializer):
65    """Serializes SPARQL results into CSV format."""
66
67    def __init__(self, result: SPARQLResult):
68        ResultSerializer.__init__(self, result)
69
70        self.delim = ","
71        if result.type != "SELECT":
72            raise Exception("CSVSerializer can only serialize select query results")
73
74    def serialize(self, stream: IO, encoding: str = "utf-8", **kwargs) -> None:
75        # the serialiser writes bytes in the given encoding
76        # in py3 csv.writer is unicode aware and writes STRINGS,
77        # so we encode afterward
78
79        import codecs
80
81        # TODO: Find a better solution for all this casting
82        writable_stream = cast(Union[TextIOBase, BufferedIOBase], stream)
83        if isinstance(writable_stream, TextIOBase):
84            string_stream: TextIOBase = writable_stream
85        else:
86            byte_stream = cast(BufferedIOBase, writable_stream)
87            string_stream = cast(TextIOBase, codecs.getwriter(encoding)(byte_stream))
88
89        out = csv.writer(string_stream, delimiter=self.delim)
90
91        vs = [self.serializeTerm(v, encoding) for v in self.result.vars]  # type: ignore[union-attr]
92        out.writerow(vs)
93        for row in self.result.bindings:
94            out.writerow(
95                [self.serializeTerm(row.get(v), encoding) for v in self.result.vars]  # type: ignore[union-attr]
96            )
97
98    def serializeTerm(
99        self, term: Optional[Identifier], encoding: str
100    ) -> Union[str, Identifier]:
101        if term is None:
102            return ""
103        elif isinstance(term, BNode):
104            return f"_:{term}"
105        else:
106            return term