bridge.pipelines.utils.conversions module

bridge.pipelines.utils.conversions module#

Utility functions for converting object types and preparing them for serialization.

This module provides small, focused helpers that are useful when generating artifacts such as YAML, JSON, or Markdown documents from richer Python objects.

bridge.pipelines.utils.conversions.find_matching_enum_member(value, enum_cls)[source]#

Resolve a free-text string to a member of a given Enum via case-insensitive matching on both member .value and .name.

The input is matched against:
  1. str(member.value) (primary match target)

  2. member.name (fallback match target)

Matching is performed in a case-insensitive manner. No fuzzy matching, partial matching, or alias expansion is applied.

Parameters:
  • value (str) – Free-text input to be normalized.

  • enum_cls (type[E]) – The Enum class to match against.

Returns:

The matching Enum member if an exact case-insensitive match is found against either .value or .name; otherwise None.

Return type:

E | None

Notes

  • This function assumes Enum values are string-like or safely castable to str.

  • If multiple Enum members share the same normalized value, the first match in definition order is returned.

bridge.pipelines.utils.conversions.object_to_primitive(obj)[source]#

Recursively convert complex objects into plain Python types.

This function walks an arbitrary Python object and produces a structure composed only of “primitive” container-friendly types:

  • dict with primitive values

  • list of primitive values

  • str, int, float, bool, or None

It is useful before serializing data to JSON, YAML, or other text-based formats where custom classes (e.g. Pydantic models, enums) would otherwise introduce unwanted artifacts or non-serializable types.

Conversion rules#

  • Pydantic models: - For v2 models, model_dump(mode="python", exclude_none=True) is used. - For v1 models, dict(exclude_none=True) is used. - The resulting dict is then processed recursively.

  • Enum instances: - Replaced with their .value.

  • Mappings / dicts: - Keys are left as-is, values are passed through object_to_primitive

    recursively.

  • Iterables (list, tuple, set): - Converted to a list with each element converted recursively.

  • Anything else: - Returned unchanged, under the assumption that it is already a primitive

    type or is otherwise safely serializable.

type obj:

Any

param obj:

The object (or nested structure of objects) to convert.

type obj:

Any

returns:

A recursively converted object that only contains primitive types and containers thereof.

rtype:

Any

Parameters:

obj (Any)

Return type:

Any

bridge.pipelines.utils.conversions.svg_to_base64(svg_path)[source]#

Convert an SVG file into a cleaned, base64-encoded string.

This helper is intended for scenarios where an SVG needs to be embedded directly into another format (e.g. HTML img tags with data URIs, Markdown, or JSON/YAML configuration files) rather than referenced by filesystem path.

Parameters:

svg_path (str) – Path to the SVG file on disk.

Returns:

A base64-encoded string representing the cleaned SVG content. The resulting string contains only ASCII characters and no newlines, and can be safely used in data URIs such as:

f"data:image/svg+xml;base64,{svg_to_base64('icon.svg')}"

Return type:

str

Raises:

FileNotFoundError – If the SVG file does not exist at the given path.