Skip to content

Commit

Permalink
buildchain/targets: support list of objects for YAML serializer
Browse files Browse the repository at this point in the history
Refs: #2070
Signed-off-by: Sylvain Laperche <[email protected]>
  • Loading branch information
slaperche-scality authored and ChengYanJin committed Dec 17, 2019
1 parent e5f7e29 commit 371aa01
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
4 changes: 2 additions & 2 deletions buildchain/buildchain/salt_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def _get_parts(self) -> Iterator[str]:
data=targets.SaltState(
shebang='#!jinja | metalk8s_kubernetes',
imports=[],
content={
content=[{
'apiVersion': 'v1',
'kind': 'ConfigMap',
'metadata': {
Expand All @@ -259,7 +259,7 @@ def _get_parts(self) -> Iterator[str]:
SCALITY_LOGO.read_bytes()
)
},
},
}],
),
file_dep=[SCALITY_LOGO, SCALITY_FAVICON, LOGIN_STYLE],
renderer=targets.Renderer.SLS,
Expand Down
13 changes: 7 additions & 6 deletions buildchain/buildchain/targets/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import enum
import json
from pathlib import Path
from typing import Any, Callable, Dict, IO, Mapping
from typing import Any, Callable, Dict, IO, Mapping, Sequence

import yaml

Expand All @@ -33,7 +33,7 @@ def render_envfile(variables: Mapping[str, str], filepath: Path) -> None:
fp.write('\n')


def render_yaml(data: Any, filepath: Path) -> None:
def render_yaml(data: Sequence[Any], filepath: Path) -> None:
"""Serialize an object as YAML to a given file path."""
with filepath.open('w', encoding='utf-8') as fp:
_yaml_dump(data, fp)
Expand Down Expand Up @@ -164,7 +164,7 @@ def _bytestring_representer(dumper: yaml.BaseDumper, data: Any) -> Any:
)


def _yaml_dump(data: Any, fp: IO[Any]) -> None:
def _yaml_dump(data: Sequence[Any], fp: IO[Any]) -> None:
dumper = yaml.SafeDumper(fp, sort_keys=False) # type: ignore
dumper.add_representer( # type: ignore
YAMLDocument.Literal, _literal_representer
Expand All @@ -173,9 +173,10 @@ def _yaml_dump(data: Any, fp: IO[Any]) -> None:
YAMLDocument.ByteString, _bytestring_representer
)
try:
dumper.open() # type: ignore
dumper.represent(data) # type: ignore
dumper.close() # type: ignore
dumper.open() # type: ignore
for document in data:
dumper.represent(document) # type: ignore
dumper.close() # type: ignore
finally:
dumper.dispose() # type: ignore

Expand Down

0 comments on commit 371aa01

Please sign in to comment.