madam.vector module

class madam.vector.SVGContext(processor: SVGProcessor, tree: ET.ElementTree[ET.Element])[source]

Bases: ProcessingContext

Deferred in-memory state for an SVG processing run.

Holds a live xml.etree.ElementTree.ElementTree so that consecutive SVG operators can transform the document without intermediate serialise/parse cycles. Call materialize() to produce the final encoded Asset.

Instances are created by SVGProcessor and passed to execute_run(). Custom operator implementations can traverse or mutate tree in place before the result is serialised.

Variables:

tree (xml.etree.ElementTree.ElementTree) – The live element tree being transformed. Operators may modify its nodes in place or replace child elements. The root element must remain a valid <svg> element.

Added in version 1.0.

__init__(processor: SVGProcessor, tree: ET.ElementTree[ET.Element]) None[source]
materialize() Asset[source]

Encode and return the final Asset.

property processor: SVGProcessor

The Processor that owns this context.

class madam.vector.SVGMetadataProcessor(config: Mapping[str, Any] | None = None)[source]

Bases: MetadataProcessor

Represents a metadata processor that handles Scalable Vector Graphics (SVG) data.

It is assumed that the SVG XML uses UTF-8 encoding.

__init__(config: Mapping[str, Any] | None = None) None[source]

Initializes a new SVGMetadataProcessor.

Parameters:

config – Mapping with settings.

combine(file: IO, metadata: Mapping[str, Mapping]) IO[source]

Returns a byte stream whose contents represent the specified file where the specified metadata was added.

Parameters:
  • metadata (Mapping) – Mapping of the metadata format to the metadata dict

  • file (IO) – Container file

Returns:

file-like object with combined content

Return type:

IO

property formats: Iterable[str]

The metadata formats which are supported.

Returns:

supported metadata formats

Return type:

set[str]

read(file: IO) Mapping[str, Mapping][source]

Reads the file and returns the metadata.

The metadata that is returned is grouped by type. The keys are specified by format.

Parameters:

file (IO) – File-like object to be read

Returns:

Metadata contained in the file

Return type:

Mapping

Raises:

UnsupportedFormatError – if the data is corrupt or its format is not supported

strip(file: IO) IO[source]

Removes all metadata of the supported type from the specified file.

Parameters:

file (IO) – file-like that should get stripped of the metadata

Returns:

file-like object without metadata

Return type:

IO

class madam.vector.SVGProcessor(config: Mapping[str, Any] | None = None)[source]

Bases: Processor

Represents a processor that handles Scalable Vector Graphics (SVG) data.

__init__(config: Mapping[str, Any] | None = None) None[source]

Initializes a new SVGProcessor.

Parameters:

config – Mapping with settings.

can_read(file: IO) bool[source]

Returns whether the specified MIME type is supported by this processor.

Parameters:

file (IO) – file-like object to be tested

Returns:

whether the data format of the specified file is supported or not

Return type:

bool

execute_run(steps: list[Callable], asset_or_context: Asset | SVGContext) Asset | SVGContext[source]

Apply a group of consecutive SVG operators in a single parse/serialise cycle.

The input Asset (or incoming SVGContext from a prior run) is parsed once. Each step’s XML transform is applied in memory via a _transform_* method. The result is returned as an SVGContext for the pipeline to serialise at the processor boundary or pipeline end.

read(file: IO) Asset[source]

Returns an Asset object whose essence is identical to the contents of the specified file.

Parameters:

file (IO) – file-like object to be read

Returns:

Asset with essence

Return type:

Asset

Raises:

UnsupportedFormatError – if the specified data format is not supported

shrink(asset: Asset) Asset[source]

Shrinks the size of an SVG asset.

Parameters:

asset (Asset) – Media asset to be shrunk

Returns:

Shrunk vector asset

Return type:

Asset

property supported_mime_types: frozenset

MIME types this processor can handle (used to build the Madam index).

Added in version 0.24.