From a7a9de1a079a70f5c8290ab5158661d3a33e5552 Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Fri, 28 Sep 2012 16:31:50 -0400 Subject: add 'safeyaml' to cloudinit In 0.7.0 we started using yaml.safe_load to load data rather than yaml.load. Some producers (namely, ubuntu MAAS created) have produced cloud-config data in the past that included python unicode types. This creates a specialized safe_loader that is basically safe_load + support for python unicode. --- cloudinit/safeyaml.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 cloudinit/safeyaml.py (limited to 'cloudinit/safeyaml.py') diff --git a/cloudinit/safeyaml.py b/cloudinit/safeyaml.py new file mode 100644 index 00000000..8b4da1fa --- /dev/null +++ b/cloudinit/safeyaml.py @@ -0,0 +1,31 @@ +# vi: ts=4 expandtab +# +# Copyright (C) 2012 Canonical Ltd. +# +# Author: Scott Moser +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 3, as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +import yaml + + +class _CustomSafeLoader(yaml.SafeLoader): + def construct_python_unicode(self, node): + return self.construct_scalar(node) + +_CustomSafeLoader.add_constructor( + u'tag:yaml.org,2002:python/unicode', + _CustomSafeLoader.construct_python_unicode) + +def load(blob): + return(yaml.load(blob, Loader=_CustomSafeLoader)) -- cgit v1.2.3 From f1e3ae3c49b9424a7e3cdbf835651720cc60e143 Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Fri, 28 Sep 2012 16:35:53 -0400 Subject: fix pep8 and pylint --- cloudinit/config/cc_ssh_authkey_fingerprints.py | 3 ++- cloudinit/config/cc_users_groups.py | 3 +-- cloudinit/safeyaml.py | 1 + 3 files changed, 4 insertions(+), 3 deletions(-) (limited to 'cloudinit/safeyaml.py') diff --git a/cloudinit/config/cc_ssh_authkey_fingerprints.py b/cloudinit/config/cc_ssh_authkey_fingerprints.py index 2b9a6e0e..f9bdf5fc 100644 --- a/cloudinit/config/cc_ssh_authkey_fingerprints.py +++ b/cloudinit/config/cc_ssh_authkey_fingerprints.py @@ -95,4 +95,5 @@ def handle(name, cfg, cloud, log, _args): (users, _groups) = distros.normalize_users_groups(cfg, cloud.distro) for (user_name, _cfg) in users.items(): (auth_key_fn, auth_key_entries) = extract_func(user_name, cloud.paths) - _pprint_key_entries(user_name, auth_key_fn, auth_key_entries, hash_meth) + _pprint_key_entries(user_name, auth_key_fn, auth_key_entries, + hash_meth) diff --git a/cloudinit/config/cc_users_groups.py b/cloudinit/config/cc_users_groups.py index 464f55c3..aa6e0579 100644 --- a/cloudinit/config/cc_users_groups.py +++ b/cloudinit/config/cc_users_groups.py @@ -17,14 +17,13 @@ # along with this program. If not, see . from cloudinit import distros -from cloudinit import util from cloudinit.settings import PER_INSTANCE frequency = PER_INSTANCE -def handle(name, cfg, cloud, log, _args): +def handle(name, cfg, cloud, _log, _args): (users, groups) = distros.normalize_users_groups(cfg, cloud.distro) for (name, members) in groups.items(): cloud.distro.create_group(name, members) diff --git a/cloudinit/safeyaml.py b/cloudinit/safeyaml.py index 8b4da1fa..eba5d056 100644 --- a/cloudinit/safeyaml.py +++ b/cloudinit/safeyaml.py @@ -27,5 +27,6 @@ _CustomSafeLoader.add_constructor( u'tag:yaml.org,2002:python/unicode', _CustomSafeLoader.construct_python_unicode) + def load(blob): return(yaml.load(blob, Loader=_CustomSafeLoader)) -- cgit v1.2.3