summaryrefslogtreecommitdiff
path: root/cloudinit/helpers.py
diff options
context:
space:
mode:
authorJoshua Harlow <harlowja@yahoo-inc.com>2012-06-20 16:36:02 -0700
committerJoshua Harlow <harlowja@yahoo-inc.com>2012-06-20 16:36:02 -0700
commit6f347387d57049bec80c920757e327223889798a (patch)
tree17a4812476895d006457c59083b077234ffe17e7 /cloudinit/helpers.py
parent00b19244b9e3a88ff6252494e43268ba692815a6 (diff)
downloadvyos-cloud-init-6f347387d57049bec80c920757e327223889798a.tar.gz
vyos-cloud-init-6f347387d57049bec80c920757e327223889798a.zip
Move the configparser helper to here since it being in a cfg.py is
confusing when there is also a directory named 'config'
Diffstat (limited to 'cloudinit/helpers.py')
-rw-r--r--cloudinit/helpers.py63
1 files changed, 63 insertions, 0 deletions
diff --git a/cloudinit/helpers.py b/cloudinit/helpers.py
index 21d203db..45633e0f 100644
--- a/cloudinit/helpers.py
+++ b/cloudinit/helpers.py
@@ -23,8 +23,11 @@
from time import time
import contextlib
+import io
import os
+from ConfigParser import (NoSectionError, NoOptionError, RawConfigParser)
+
from cloudinit.settings import (PER_INSTANCE, PER_ALWAYS, PER_ONCE)
from cloudinit import log as logging
@@ -298,3 +301,63 @@ class Paths(object):
return None
else:
return ipath
+
+
+# This config parser will not throw when sections don't exist
+# and you are setting values on those sections which is useful
+# when writing to new options that may not have corresponding
+# sections. Also it can default other values when doing gets
+# so that if those sections/options do not exist you will
+# get a default instead of an error. Another useful case where
+# you can avoid catching exceptions that you typically don't
+# care about...
+
+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