prov.serializers¶
prov.serializers defines the pluggable serializer interface used by
ProvDocument.serialize() and
ProvDocument.deserialize(), and the registry
that looks up the serializer class for a given format string ("json", "xml", "rdf",
"provn"). For how to use each format, see the Work with PROV-JSON,
Work with PROV-XML, Work with PROV-O (RDF), and Work with PROV-N how-to guides;
this page documents the underlying interface and registry only.
- class prov.serializers.Serializer(document: ProvDocument | None = None)[source]¶
Bases:
ABCSerializer for PROV documents.
- document = None¶
PROV document to serialise.
- abstractmethod serialize(stream: IOBase, **args: Any) None[source]¶
Serialize
self.documentand write it to a stream.Subclasses implement this to encode the bound document and write the result to
stream.- Parameters:
stream – Stream to write the serialized document into.
**args – Format-specific serialization options, passed through by subclasses.
- abstractmethod deserialize(stream: io.IOBase, **args: Any) ProvDocument[source]¶
Read and parse a document from a stream.
Subclasses implement this to parse
streamand build a newProvDocumentfrom it.- Parameters:
stream – Stream to deserialize the document from.
**args – Format-specific deserialization options, passed through by subclasses.
- Returns:
The deserialized
ProvDocument.
- class prov.serializers.Registry[source]¶
Registry of serializers.
- serializers: ClassVar[dict[str, type[Serializer]] | None] = None¶
Property caching all available serializers in a dict.
- static load_serializers() None[source]¶
Load all serializers whose optional dependencies are installed.
jsonandprovnhave no optional dependencies and are always registered.rdf(needsrdflib) andxml(needslxml) are registered only if their extra is installed, so thatimport provand JSON/PROV-N work in a minimal install; requesting an unavailable format then raises an informativeDoNotExist(seeget()) rather than a bareModuleNotFoundError.The insertion order is kept as
json, rdf, provn, xml(the historic order when all extras are present) becauseprov.read()’s format auto-detection iteratesRegistry.serializersin order and several tests (test_read_auto_detects_rdf,test_read_auto_detect_of_xml_hits_uncaught_rdf_syntax_error,test_read_on_unparseable_content_raises_bad_syntax) pin the exact candidate tried second.
- prov.serializers.get(format_name: str) type[Serializer][source]¶
Return the serializer class registered for a format.
- Parameters:
format_name – Registry key, e.g.
"json","xml","rdf","provn".- Returns:
The
Serializersubclass for the format.- Raises:
DoNotExist – If no serializer is registered under
format_name.
- class prov.serializers.DoNotExist[source]¶
Bases:
ErrorException for the case a serializer is not available.
Reading documents¶
prov.read() auto-detects the format of a source by trying each registered
deserializer in turn; see Work with PROV-JSON for its caveats around error reporting.
- prov.read(source: io.IOBase | IO[Any] | str | bytes | os.PathLike[str], format: str | None = None) ProvDocument | None[source]¶
Read a
ProvDocumentfrom a file, path, or string.A
str/bytessource naming an existing file is read from that file; any otherstr/bytesis parsed as raw document content.If
formatis not given, the format is detected by trying each registered deserializer in turn and returning the first that both succeeds and produces a non-empty document (a parse yielding no records or bundles – e.g. rdflib parsing empty/foreign input to an empty graph – is treated as “not this format”; registered namespaces are not used as a signal here since the rdf deserializer always registers rdflib’s own default-bound namespace prefixes on every successful parse, empty or not). Auto-detection swallows all deserializer errors; passformatexplicitly to get the actual traceback from the matching deserializer.A seekable stream
source(e.g. an open file object,io.StringIO,io.BytesIO) is rewound to its starting position before each auto-detection attempt, so every candidate format sees the full content. A non-seekable stream is consumed by the first candidate; passformatexplicitly for those.- Parameters:
source – File-like stream, path to an existing file, or raw content.
format – Serialization format to use (e.g.
"json","xml","rdf","provn"). IfNone, every registered format is tried in turn.
- Returns:
The deserialized
ProvDocument.- Raises:
TypeError – If
formatisNoneand no registered serializer produced a non-empty document fromsource.