Skip to content

Commit 39aaa8c

Browse files
Documentation rework that plugins usage and modules are crosslinked (#3320)
* Reworked description for all modules, that are linked as plugin description. Added crosslink for plugin related things. linked: Graph.serialize - rdflib.plugins.serializers; Graph.parse - rdflib.plugins.parsers; Result.serialize - rdflib.plugins.sparql.results; Graph.__init__ - rdflib.plugins.stores; * add apostrophe --------- Co-authored-by: Nicholas Car <[email protected]> Co-authored-by: Nicholas Car <[email protected]>
1 parent a1b5226 commit 39aaa8c

File tree

7 files changed

+121
-48
lines changed

7 files changed

+121
-48
lines changed

rdflib/graph.py

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -476,30 +476,40 @@ class Graph(Node):
476476
477477
```
478478
479-
!!! info "Graph stores"
480-
The constructor accepts one argument, the "store" that will be used to store the
481-
graph data with the default being the [`Memory`][rdflib.plugins.stores.memory.Memory]
482-
(in memory) Store. Other Stores that persist content to disk using various file
483-
databases or Stores that use remote servers (SPARQL systems) are supported. See
484-
the `rdflib.plugins.stores` package for Stores currently shipped with RDFLib.
485-
Other Stores not shipped with RDFLib can be added, such as
486-
[HDT](https://github.com/rdflib/rdflib-hdt/).
487-
488-
Stores can be context-aware or unaware. Unaware stores take up
489-
(some) less space but cannot support features that require
490-
context, such as true merging/demerging of sub-graphs and
491-
provenance.
492-
493-
Even if used with a context-aware store, Graph will only expose the quads which
494-
belong to the default graph. To access the rest of the data the
495-
`Dataset` class can be used instead.
496-
497-
The Graph constructor can take an identifier which identifies the Graph
498-
by name. If none is given, the graph is assigned a BNode for its
499-
identifier.
500-
501-
For more on Named Graphs, see the RDFLib `Dataset` class and the TriG Specification,
502-
<https://www.w3.org/TR/trig/>.
479+
480+
Args:
481+
store: The constructor accepts one argument, the "store" that will be used to store the
482+
graph data with the default being the [`Memory`][rdflib.plugins.stores.memory.Memory]
483+
(in memory) Store. Other Stores that persist content to disk using various file
484+
databases or Stores that use remote servers (SPARQL systems) are supported.
485+
All [builtin storetypes][rdflib.plugins.stores] can be accessed via
486+
their registered names.
487+
Other Stores not shipped with RDFLib can be added as plugins, such as
488+
[HDT](https://github.com/rdflib/rdflib-hdt/).
489+
Registration of external plugins
490+
is described in [`rdflib.plugin`][rdflib.plugin].
491+
492+
Stores can be context-aware or unaware. Unaware stores take up
493+
(some) less space but cannot support features that require
494+
context, such as true merging/demerging of sub-graphs and
495+
provenance.
496+
497+
Even if used with a context-aware store, Graph will only expose the quads which
498+
belong to the default graph. To access the rest of the data the
499+
`Dataset` class can be used instead.
500+
501+
The Graph constructor can take an identifier which identifies the Graph
502+
by name. If none is given, the graph is assigned a BNode for its
503+
identifier.
504+
505+
For more on Named Graphs, see the RDFLib `Dataset` class and the TriG Specification,
506+
<https://www.w3.org/TR/trig/>.
507+
identifier: identifier of the graph itself
508+
namespace_manager: Used namespace manager.
509+
Create with bind_namespaces if `None`.
510+
base: Base used for [URIs][rdflib.term.URIRef]
511+
bind_namespaces: Used bind_namespaces for namespace_manager
512+
Is only used, when no namespace_manager is provided.
503513
"""
504514

505515
context_aware: bool
@@ -1450,9 +1460,11 @@ def serialize(
14501460
string or pathlib.PurePath object, or it can be an IO[bytes] like object.
14511461
If this parameter is not supplied the serialized graph will be returned.
14521462
format: The format that the output should be written in. This value
1453-
references a Serializer plugin. Format support can be extended with plugins,
1454-
but "xml", "n3", "turtle", "nt", "pretty-xml", "trix", "trig", "nquads",
1455-
"json-ld" and "hext" are built in. Defaults to "turtle".
1463+
references a Serializer plugin.
1464+
Format support is managed with [plugins][rdflib.plugin].
1465+
Defaults to "turtle" and some other formats are builtin,
1466+
like "xml" and "trix".
1467+
See also [serialize module][rdflib.plugins.parsers].
14561468
base: The base IRI for formats that support it. For the turtle format this
14571469
will be used as the @base directive.
14581470
encoding: Encoding of output.
@@ -1539,9 +1551,10 @@ def parse(
15391551
RFC 3986 <https://datatracker.ietf.org/doc/html/rfc3986#section-5.1.4>`_,
15401552
given the source document does not define a base URI.
15411553
format: Used if format can not be determined from source, e.g.
1542-
file extension or Media Type. Defaults to text/turtle. Format
1543-
support can be extended with plugins, but "xml", "n3" (use for
1544-
turtle), "nt" & "trix" are built in.
1554+
file extension or Media Type. Format support is managed
1555+
with [plugins][rdflib.plugin].
1556+
Available formats are e.g. "turle", "xml", "n3", "nt" and "trix"
1557+
or see [parser module][rdflib.plugins.parsers].
15451558
location: A string indicating the relative or absolute URL of the
15461559
source. `Graph`'s absolutize method is used if a relative location
15471560
is specified.

rdflib/plugins/parsers/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1-
"""
1+
"""Module for builtin RDF parser
2+
3+
All builtin [RDF parser][rdflib.parser.Parser] available
4+
in [RDFLib Graphs method `parse`][rdflib.graph.Graph.parse] are listed here.
25
6+
Registration of builtin and external plugins
7+
is described in [`rdflib.plugin`][rdflib.plugin].
38
"""
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""Module for builtin RDF Serializer.
2+
3+
All builtin [RDF serializer][rdflib.serializer.Serializer] available
4+
in [RDFLib Graphs method `serialize`][rdflib.graph.Graph.serialize] are listed here.
5+
6+
Registration of builtin and external plugins
7+
is described in [`rdflib.plugin`][rdflib.plugin].
8+
"""
Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1-
"""
2-
Parsers and serializers for SPARQL Result formats
1+
"""Parsers and serializers for SPARQL Result formats
2+
3+
All builtin [SPARQL result serializers][rdflib.query.ResultSerializer] available
4+
after [a query][rdflib.graph.Graph.query] are listed here.
5+
The serializer can be chosen during [serialization of a result][rdflib.query.Result.serialize].
6+
7+
```
8+
import rdflib
9+
10+
result : rdflib.query.Result
11+
g = rdflib.Graph().parse(format="ttl", data=\"\"\"
12+
@base <http://example.org/> .
13+
<MyObject> a <MyClass>.
14+
\"\"\")
15+
result = g.query("SELECT ?x ?cls WHERE {?x a ?cls}")
16+
print(result.serialize(format="txt").decode("utf8"))
17+
```
18+
19+
Registration of builtin and external plugins
20+
is described in [`rdflib.plugin`][rdflib.plugin].
321
"""

rdflib/plugins/stores/__init__.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1-
"""
2-
This package contains modules for additional RDFLib stores
1+
"""Module for builtin RDF stores.
2+
3+
All builtin [RDF Stores][rdflib.store.Store] available
4+
during [RDFLib Graphs creation][rdflib.graph.Graph] are listed here.
5+
6+
Registration of builtin and external plugins
7+
is described in [`rdflib.plugin`][rdflib.plugin].
38
"""

rdflib/query.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -283,20 +283,25 @@ def serialize(
283283
"""
284284
Serialize the query result.
285285
286-
The `format` argument determines the Serializer class to use.
287-
288-
- csv: [`CSVResultSerializer`][rdflib.plugins.sparql.results.csvresults.CSVResultSerializer]
289-
- json: [`JSONResultSerializer`][rdflib.plugins.sparql.results.jsonresults.JSONResultSerializer]
290-
- txt: [`TXTResultSerializer`][rdflib.plugins.sparql.results.txtresults.TXTResultSerializer]
291-
- xml: [`XMLResultSerializer`][rdflib.plugins.sparql.results.xmlresults.XMLResultSerializer]
292286
293287
Args:
294-
destination: Path of file output or BufferedIOBase object to write the output to.
288+
destination: Path of file output or BufferedIOBase object
289+
to write the output to. If `None` this function
290+
will return the output as `bytes`
295291
encoding: Encoding of output.
296-
format: One of ['csv', 'json', 'txt', xml']
292+
format: The format used for serialization.
293+
See [sparql.results module][rdflib.plugins.sparql.results]
294+
for all builtin SPARQL result serialization.
295+
Further serializer can be loaded [as plugin][rdflib.plugin].
296+
Some example formats are
297+
[csv][rdflib.plugins.sparql.results.csvresults.CSVResultSerializer],
298+
[json][rdflib.plugins.sparql.results.jsonresults.JSONResultSerializer],
299+
[txt][rdflib.plugins.sparql.results.txtresults.TXTResultSerializer]
300+
or
301+
[xml][rdflib.plugins.sparql.results.xmlresults.XMLResultSerializer]
297302
298303
Returns:
299-
bytes
304+
Serialized result, when destination is not given.
300305
"""
301306
if self.type in ("CONSTRUCT", "DESCRIBE"):
302307
# type error: Item "None" of "Optional[Graph]" has no attribute "serialize"

rdflib/serializer.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
"""Serializer plugin interface.
22
3-
This module is useful for those wanting to write a serializer that can
4-
plugin to rdflib. If you are wanting to invoke a serializer you likely
5-
want to do so through the Graph class serialize method.
3+
The RDF serialization plugins used during
4+
[RDFlib's common serialization of a graph][rdflib.graph.Graph.serialize]
5+
is described in this module.
66
77
TODO: info for how to write a serializer that can plugin to rdflib.
88
See also [`rdflib.plugin`][rdflib.plugin]
9+
10+
All builtin RDF Serializer are listed
11+
under [`rdflib.plugins.serializers`][rdflib.plugins.serializers].
912
"""
1013

1114
from __future__ import annotations
@@ -24,6 +27,12 @@
2427

2528

2629
class Serializer:
30+
"""Base class for RDF Serializer.
31+
32+
New Serializer can be registered as plugin as described
33+
in [`rdflib.plugin`][rdflib.plugin].
34+
"""
35+
2736
def __init__(self, store: Graph):
2837
self.store: Graph = store
2938
self.encoding: str = "utf-8"
@@ -36,7 +45,17 @@ def serialize(
3645
encoding: Optional[str] = None,
3746
**args: Any,
3847
) -> None:
39-
"""Abstract method"""
48+
"""Abstract method. Print [Graph][rdflib.graph.Graph].
49+
Used in [Graph.serialize][rdflib.graph.Graph.serialize]
50+
51+
Serialize Graph
52+
53+
Args:
54+
stream: The destination to serialize the graph to.
55+
base: The base IRI for formats that support it.
56+
encoding: Encoding of output.
57+
args: Additional arguments to pass to the Serializer that will be used.
58+
"""
4059

4160
def relativize(self, uri: _StrT) -> Union[_StrT, URIRef]:
4261
base = self.base

0 commit comments

Comments
 (0)