changed yaml loaders

This commit is contained in:
ari committed 2022-05-20 10:53:53 -04:00
1 parent cbbc099384
commit 298a233a5f
6 files changed
+83 -30

No files matched your search

+1 -1
View File
@@ -4,7 +4,7 @@ import setuptools # type: ignore
setuptools.setup(
python_requires=">=3.9",
install_requires=[
"ruamel.yaml==0.17.17",
"PyYAML==6.0",
"fastapi==0.70.0",
"pydantic==1.8.2",
"uvicorn[standard]==0.15.0",
+12 -3
View File
@@ -1,7 +1,9 @@
import yaml
import io
from typing import Any, TextIO, Union
from src.yaml_serializer.base_yaml import yaml
from src.yaml_serializer.loader import YamlSafeLoader
from src.yaml_serializer.dumper import YamlSafeDumper
def dump_yaml(obj: Any) -> str:
@@ -14,7 +16,14 @@ def dump_yaml(obj: Any) -> str:
str
"""
stream = io.StringIO()
yaml.dump(obj, stream)
yaml.dump(
obj,
stream,
default_flow_style=False,
allow_unicode=True,
sort_keys=False,
Dumper=YamlSafeDumper,
)
stream.seek(0)
return stream.read()
@@ -28,4 +37,4 @@ def load_yaml(yaml_stream: TextIO) -> Union[dict, list]:
Returns:
List[dict, list]
"""
return yaml.load(yaml_stream)
return yaml.load(yaml_stream, Loader=YamlSafeLoader)
-19
View File
@@ -1,19 +0,0 @@
from ruamel.yaml import YAML
from src.yaml_serializer.types import (
IncludedYaml,
IncludedYamlDir,
SecretValue,
)
yaml = YAML()
yaml.register_class(IncludedYaml)
yaml.register_class(SecretValue)
yaml.register_class(IncludedYamlDir)
yaml.constructor.add_constructor("!include", IncludedYaml.from_yaml)
yaml.constructor.add_constructor("!secret", SecretValue.from_yaml)
yaml.constructor.add_constructor("!env_var", IncludedYamlDir.from_yaml)
yaml.constructor.add_constructor("!include_dir_list", IncludedYamlDir.from_yaml)
yaml.constructor.add_constructor("!include_dir_merge_list", IncludedYamlDir.from_yaml)
yaml.constructor.add_constructor("!include_dir_named", IncludedYamlDir.from_yaml)
yaml.constructor.add_constructor("!include_dir_merge_named", IncludedYamlDir.from_yaml)
+16
View File
@@ -0,0 +1,16 @@
import yaml
from src.yaml_serializer.types import (
IncludedYaml,
IncludedYamlDir,
SecretValue,
)
class YamlSafeDumper(yaml.SafeDumper):
pass
YamlSafeDumper.add_representer(IncludedYaml, IncludedYaml.to_yaml)
YamlSafeDumper.add_representer(SecretValue, SecretValue.to_yaml)
YamlSafeDumper.add_representer(IncludedYamlDir, IncludedYamlDir.to_yaml)
+26
View File
@@ -0,0 +1,26 @@
import yaml
from src.yaml_serializer.types import (
IncludedYaml,
IncludedYamlDir,
SecretValue,
)
class YamlSafeLoader(yaml.SafeLoader):
pass
# def compose_node(self, parent: yaml.nodes.Node, index: int) -> yaml.nodes.Node: # type: ignore[override]
# """Annotate a node with the first line it was seen."""
# last_line: int = self.line
# node: yaml.nodes.Node = super().compose_node(parent, index) # type: ignore[assignment]
# node.__line__ = last_line + 1 # type: ignore[attr-defined]
# return node
YamlSafeLoader.add_constructor("!include", IncludedYaml.from_yaml)
YamlSafeLoader.add_constructor("!secret", SecretValue.from_yaml)
YamlSafeLoader.add_constructor("!env_var", IncludedYamlDir.from_yaml)
YamlSafeLoader.add_constructor("!include_dir_list", IncludedYamlDir.from_yaml)
YamlSafeLoader.add_constructor("!include_dir_merge_list", IncludedYamlDir.from_yaml)
YamlSafeLoader.add_constructor("!include_dir_named", IncludedYamlDir.from_yaml)
YamlSafeLoader.add_constructor("!include_dir_merge_named", IncludedYamlDir.from_yaml)
+28 -7
View File
@@ -19,21 +19,29 @@ class dumping_yamls_tests(TestCase):
)
}
)
self.assertEqual(yaml, "automations: !include automations.yaml\n")
self.assertEqual(yaml, "automations: !include 'automations.yaml'\n")
def test_dumping_secrets(self):
yaml = dump_yaml({"google_password": SecretValue("gpass", "supersec")})
self.assertEqual(yaml, "google_password: !secret gpass\n")
self.assertEqual(yaml, "google_password: !secret 'gpass'\n")
def test_dumping_stub(self):
yaml = dump_yaml({"sensor": IncludedYamlDir("include_dir_list", "sensors/*yaml")})
self.assertEqual(yaml, "sensor: !include_dir_list sensors/*yaml\n")
self.assertEqual(yaml, "sensor: !include_dir_list 'sensors/*yaml'\n")
def test_load_stub(self):
yaml_obj = load_yaml(
io.StringIO(
"""
name: cool
date: "10:00"
bools:
- "yes"
- "no"
- true
- false
bobIsNull:
bobIsNull2: null
google_password: !secret gpass
sensor: !include_dir_list sensors/*yaml
automations: !include automations.yaml
@@ -44,6 +52,10 @@ class dumping_yamls_tests(TestCase):
dict(yaml_obj),
{
"name": "cool",
"date": "10:00",
"bools": ["yes", "no", True, False],
"bobIsNull": None,
"bobIsNull2": None,
"google_password": SecretValue("gpass", "n/a"),
"sensor": IncludedYamlDir("include_dir_list", "sensors/*yaml"),
"automations": IncludedYaml("automations.yaml"),
@@ -51,8 +63,17 @@ class dumping_yamls_tests(TestCase):
)
self.assertEqual(
dump_yaml(yaml_obj),
"name: cool\n"
"google_password: !secret gpass\n"
"sensor: !include_dir_list sensors/*yaml\n"
f"automations: !include automations.yaml\n",
"""name: cool
date: '10:00'
bools:
- 'yes'
- 'no'
- true
- false
bobIsNull: null
bobIsNull2: null
google_password: !secret 'gpass'
sensor: !include_dir_list 'sensors/*yaml'
automations: !include 'automations.yaml'
""",
)