diff options
author | Joshua Harlow <harlowja@yahoo-inc.com> | 2012-06-15 17:36:07 -0700 |
---|---|---|
committer | Joshua Harlow <harlowja@yahoo-inc.com> | 2012-06-15 17:36:07 -0700 |
commit | aa69b001e0624ca297b199984b28e9d5401a439f (patch) | |
tree | 86808341f8a93730b6d3b20ae7a0d8bbf81fd55e | |
parent | 76a7160d4a4eb70e946cd29317ace1fe93ffe5d5 (diff) | |
download | vyos-cloud-init-aa69b001e0624ca297b199984b28e9d5401a439f.tar.gz vyos-cloud-init-aa69b001e0624ca297b199984b28e9d5401a439f.zip |
This provides a nice little utility class that avoids the python config parser throwing when options + values are being added to unknown sections since it handles the
creation and checking that those sections exist before the option is added. Also it adds a little helper that can turn that config into a string.
-rw-r--r-- | cloudinit/cfg.py | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/cloudinit/cfg.py b/cloudinit/cfg.py new file mode 100644 index 00000000..dd8f7baf --- /dev/null +++ b/cloudinit/cfg.py @@ -0,0 +1,72 @@ +# vi: ts=4 expandtab +# +# Copyright (C) 2012 Yahoo! Inc. +# +# Author: Joshua Harlow <harlowja@yahoo-inc.com> +# +# 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 <http://www.gnu.org/licenses/>. + +import io + +from ConfigParser import (NoSectionError, NoOptionError, RawConfigParser) + + +class DefaultingConfigParser(RawConfigParser): + DEF_INT = 0 + DEF_FLOAT = 0.0 + DEF_BOOLEAN = False + DEF_BASE = None + + def get(self, section, option): + value = self.DEF_BASE + try: + value = RawConfigParser.get(self, section, option) + except NoSectionError: + pass + except NoOptionError: + pass + return value + + def set(self, section, option, value): + if not self.has_section(section) and section.lower() != 'default': + self.add_section(section) + RawConfigParser.set(self, section, option, value) + + def remove_option(self, section, option): + if self.has_option(section, option): + RawConfigParser.remove_option(self, section, option) + + def getboolean(self, section, option): + if not self.has_option(section, option): + return self.DEF_BOOLEAN + return RawConfigParser.getboolean(self, section, option) + + def getfloat(self, section, option): + if not self.has_option(section, option): + return self.DEF_FLOAT + return RawConfigParser.getfloat(self, section, option) + + def getint(self, section, option): + if not self.has_option(section, option): + return self.DEF_INT + return RawConfigParser.getint(self, section, option) + + def stringify(self, header=None): + contents = '' + with io.BytesIO() as outputstream: + self.write(outputstream) + outputstream.flush() + contents = outputstream.getvalue() + if header: + contents = "\n".join([header, contents]) + return contents |