From 4db8b35b215b266bdf3feee4693a8024acab1364 Mon Sep 17 00:00:00 2001 From: Christoph Alt <christoph.alt@fau.de> Date: Thu, 2 Nov 2023 13:58:11 +0100 Subject: [PATCH] added the yaml utility for the gitlab ci --- cbutil/yaml_util.py | 45 +++++++++++++++++++++++++++++++++++++++++ pyproject.toml | 1 + tests/test_yaml_util.py | 8 ++++++++ 3 files changed, 54 insertions(+) create mode 100644 cbutil/yaml_util.py create mode 100644 tests/test_yaml_util.py diff --git a/cbutil/yaml_util.py b/cbutil/yaml_util.py new file mode 100644 index 0000000..17f30e5 --- /dev/null +++ b/cbutil/yaml_util.py @@ -0,0 +1,45 @@ +import yaml + + +class Reference: + yaml_tag = '!reference' + + def __init__(self, values): + self._values = values + + +def reference_constructor(loader: yaml.SafeLoader, node: yaml.nodes.CollectionNode) -> Reference: + return Reference(loader.construct_sequence(node)) + + +def reference_representer(dumper: yaml.SafeDumper, ref: Reference) -> yaml.nodes.SequenceNode: + return dumper.represent_sequence(Reference.yaml_tag, ref._values, flow_style=True) + + +def loader(): + loader = yaml.SafeLoader + loader.add_constructor(Reference.yaml_tag, reference_constructor) + return loader + + +def dumper(): + dumper = yaml.SafeDumper + dumper.add_representer(Reference, reference_representer) + dumper.ignore_aliases = lambda *args: True + return dumper + + +def load_script(raw_strings): + if isinstance(raw_strings, str): + raw_strings = [raw_strings] + for raw_string in raw_strings: + yield yaml.load(raw_string, Loader=loader()) + + +def print_yaml_file(config: dict) -> str: + return yaml.dump(config, + default_flow_style=False, + line_break=False, + width=120, + Dumper=dumper(), + sort_keys=False) diff --git a/pyproject.toml b/pyproject.toml index 5bd434f..914429b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -18,6 +18,7 @@ dependencies = [ "numpy", "pandas", "importlib_resources ; python_version<'3.7'", + "pyyaml", ] [project.optional-dependencies] diff --git a/tests/test_yaml_util.py b/tests/test_yaml_util.py new file mode 100644 index 0000000..8e09f17 --- /dev/null +++ b/tests/test_yaml_util.py @@ -0,0 +1,8 @@ +from cbutil.yaml_util import load_script, dumper +import yaml + + +def test_load_script(): + test_string = "before_script:\n -!reference [.load_cbutil, script]\n""" + actual = yaml.dump(next(load_script(test_string)), Dumper=dumper()) + assert actual.replace(" ", "").replace("\n", "") == test_string.replace(" ", "").replace("\n", "") -- GitLab