summaryrefslogtreecommitdiff
path: root/cloudinit/safeyaml.py
diff options
context:
space:
mode:
authorScott Moser <smoser@brickies.net>2019-10-24 20:02:15 +0000
committerServer Team CI Bot <josh.powers+server-team-bot@canonical.com>2019-10-24 20:02:15 +0000
commit5bec6b0e2a2ce5fd03bb04f441536fc130e67997 (patch)
treed4fbc5ae8d7360ff87c283c2550f4661ef007dff /cloudinit/safeyaml.py
parent3e2482e8aa6630ca9bc115dc1f82d44d3fde1681 (diff)
downloadvyos-cloud-init-5bec6b0e2a2ce5fd03bb04f441536fc130e67997.tar.gz
vyos-cloud-init-5bec6b0e2a2ce5fd03bb04f441536fc130e67997.zip
Fix usages of yaml, and move yaml_dump to safeyaml.dumps.
Here we replace uses of the pyyaml module directly with functions provided by cloudinit.safeyaml. Also, change/move cloudinit.util.yaml_dumps to cloudinit.safeyaml.dumps LP: #1849640
Diffstat (limited to 'cloudinit/safeyaml.py')
-rw-r--r--cloudinit/safeyaml.py15
1 files changed, 15 insertions, 0 deletions
diff --git a/cloudinit/safeyaml.py b/cloudinit/safeyaml.py
index 3bd5e03d..d6f5f95b 100644
--- a/cloudinit/safeyaml.py
+++ b/cloudinit/safeyaml.py
@@ -6,6 +6,8 @@
import yaml
+YAMLError = yaml.YAMLError
+
class _CustomSafeLoader(yaml.SafeLoader):
def construct_python_unicode(self, node):
@@ -27,4 +29,17 @@ class NoAliasSafeDumper(yaml.dumper.SafeDumper):
def load(blob):
return(yaml.load(blob, Loader=_CustomSafeLoader))
+
+def dumps(obj, explicit_start=True, explicit_end=True, noalias=False):
+ """Return data in nicely formatted yaml."""
+
+ return yaml.dump(obj,
+ line_break="\n",
+ indent=4,
+ explicit_start=explicit_start,
+ explicit_end=explicit_end,
+ default_flow_style=False,
+ Dumper=(NoAliasSafeDumper
+ if noalias else yaml.dumper.Dumper))
+
# vi: ts=4 expandtab