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: str | bytes | os.PathLike[str], format: str | None = None) ProvDocument | None[source]¶
Read a
ProvDocumentfrom a file, path, or string.If
formatis not given, the format is detected lazily by trying each registered serializer’sdeserialize()in turn and returning the first one that succeeds. The deserializers should fail fairly early when data of the wrong type is passed to them thus the try/except is likely cheap. One could of course also do some more advanced format auto-detection but I am not sure that is necessary.The downside of auto-detection is that no proper error messages will be produced; pass the
formatparameter explicitly to get the actual traceback from the matching deserializer.- Parameters:
source – File-like stream, path, or raw content to deserialize.
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 none of the registered serializers could deserializesource.