HextuplesSerializer RDF graph serializer for RDFLib.
See https://github.com/ontola/hextuples for details about the format.
Classes:
__all__
module-attribute
__all__ = ['HextuplesSerializer']
HextuplesSerializer
Bases: Serializer
Serializes RDF graphs to NTriples format.
Methods:
Attributes:
Source code in rdflib/plugins/serializers/hext.py
| def __init__(self, store: Union[Graph, Dataset, ConjunctiveGraph]):
self.default_context: Optional[Union[Graph, IdentifiedNode]]
self.graph_type: Union[Type[Graph], Type[Dataset], Type[ConjunctiveGraph]]
if isinstance(store, (Dataset, ConjunctiveGraph)):
self.graph_type = (
Dataset if isinstance(store, Dataset) else ConjunctiveGraph
)
self.contexts = list(store.contexts())
if store.default_context:
self.default_context = store.default_context
self.contexts.append(store.default_context)
else:
self.default_context = None
else:
self.graph_type = Graph
self.contexts = [store]
self.default_context = None
Serializer.__init__(self, store)
|
contexts
instance-attribute
default_context
instance-attribute
graph_type
instance-attribute
__new__
Source code in rdflib/plugins/serializers/hext.py
| def __new__(cls, store: Union[Graph, Dataset, ConjunctiveGraph]):
if _HAS_ORJSON:
cls.str_local_id: Union[str, Any] = orjson.Fragment(b'"localId"')
cls.str_global_id: Union[str, Any] = orjson.Fragment(b'"globalId"')
cls.empty: Union[str, Any] = orjson.Fragment(b'""')
cls.lang_str: Union[str, Any] = orjson.Fragment(
b'"' + RDF.langString.encode("utf-8") + b'"'
)
cls.xsd_string: Union[str, Any] = orjson.Fragment(
b'"' + XSD.string.encode("utf-8") + b'"'
)
else:
cls.str_local_id = "localId"
cls.str_global_id = "globalId"
cls.empty = ""
cls.lang_str = f"{RDF.langString}"
cls.xsd_string = f"{XSD.string}"
return super(cls, cls).__new__(cls)
|
serialize
serialize(stream: IO[bytes], base: Optional[str] = None, encoding: Optional[str] = 'utf-8', **kwargs: Any) -> None
Source code in rdflib/plugins/serializers/hext.py
| def serialize(
self,
stream: IO[bytes],
base: Optional[str] = None,
encoding: Optional[str] = "utf-8",
**kwargs: Any,
) -> None:
if base is not None:
warnings.warn(
"base has no meaning for Hextuples serialization. "
"I will ignore this value"
)
if encoding not in [None, "utf-8"]:
warnings.warn(
f"Hextuples files are always utf-8 encoded. "
f"I was passed: {encoding}, "
"but I'm still going to use utf-8 anyway!"
)
if self.store.formula_aware is True:
raise Exception(
"Hextuple serialization can't (yet) handle formula-aware stores"
)
context: Union[Graph, IdentifiedNode]
context_str: Union[bytes, str]
for context in self.contexts:
for triple in context:
# Generate context string just once, because it doesn't change
# for every triple in this context
context_str = cast(
Union[str, bytes],
(
self.empty
if self.graph_type is Graph
else (
orjson.Fragment('"' + self._context_str(context) + '"')
if _HAS_ORJSON
else self._context_str(context)
)
),
)
hl = self._hex_line(triple, context_str)
if hl is not None:
stream.write(hl if _HAS_ORJSON else hl.encode())
|