c56bef871b
Sync docs with Docusaurus / sync (push) Waiting to run
Tests / Check if changed (push) Waiting to run
Tests / format (push) Blocked by required conditions
Tests / check-imports (push) Blocked by required conditions
Tests / Unit / macos-latest (push) Blocked by required conditions
Tests / Unit / ubuntu-latest (push) Blocked by required conditions
Tests / Unit / windows-latest (push) Blocked by required conditions
Tests / mypy (push) Blocked by required conditions
Tests / Integration / ubuntu-latest (push) Blocked by required conditions
Tests / Integration / macos-latest (push) Blocked by required conditions
Tests / Integration / windows-latest (push) Blocked by required conditions
Tests / notify-slack-on-failure (push) Blocked by required conditions
Tests / Mark tests as completed (push) Blocked by required conditions
Docker image release / Build base image (push) Waiting to run
CodeQL / Analyze (python) (push) Has been cancelled
Update Platform Components Table / update (push) Has been cancelled
46 lines
1.7 KiB
Python
46 lines
1.7 KiB
Python
# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
|
|
#
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
from typing import Any
|
|
|
|
import yaml
|
|
|
|
|
|
# Custom YAML safe loader that supports loading Python tuples
|
|
class YamlLoader(yaml.SafeLoader):
|
|
def construct_python_tuple(self, node: yaml.SequenceNode) -> tuple:
|
|
"""Construct a Python tuple from the sequence."""
|
|
return tuple(self.construct_sequence(node))
|
|
|
|
|
|
class YamlDumper(yaml.SafeDumper):
|
|
def represent_tuple(self, data: tuple) -> yaml.SequenceNode:
|
|
"""Represent a Python tuple."""
|
|
return self.represent_sequence("tag:yaml.org,2002:python/tuple", data)
|
|
|
|
|
|
YamlDumper.add_representer(tuple, YamlDumper.represent_tuple)
|
|
YamlLoader.add_constructor("tag:yaml.org,2002:python/tuple", YamlLoader.construct_python_tuple)
|
|
|
|
|
|
class YamlMarshaller:
|
|
def marshal(self, dict_: dict[str, Any]) -> str:
|
|
"""Return a YAML representation of the given dictionary."""
|
|
try:
|
|
return yaml.dump(dict_, Dumper=YamlDumper)
|
|
except yaml.representer.RepresenterError as e:
|
|
raise TypeError(
|
|
"Error dumping pipeline to YAML - Ensure that all pipeline components only serialize basic Python types"
|
|
) from e
|
|
|
|
def unmarshal(self, data_: str | bytes | bytearray) -> dict[str, Any]:
|
|
"""Return a dictionary from the given YAML data."""
|
|
try:
|
|
return yaml.load(data_, Loader=YamlLoader)
|
|
except yaml.constructor.ConstructorError as e:
|
|
raise TypeError(
|
|
"Error loading pipeline from YAML - Ensure that all pipeline "
|
|
"components only serialize basic Python types"
|
|
) from e
|