Skip to content
Snippets Groups Projects
Commit 4db8b35b authored by Christoph Alt's avatar Christoph Alt
Browse files

added the yaml utility for the gitlab ci

parent 8a6396ab
Branches
No related tags found
No related merge requests found
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)
......@@ -18,6 +18,7 @@ dependencies = [
"numpy",
"pandas",
"importlib_resources ; python_version<'3.7'",
"pyyaml",
]
[project.optional-dependencies]
......
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", "")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment