]> jfr.im git - erebus.git/blobdiff - config.py
update nitter to replace instance
[erebus.git] / config.py
index 0d4106c7e0b51d0897074e56e83ad12be742d2e5..5538dc78c9f68db4b27d6d9c2c1995fa2bb26e4c 100644 (file)
--- a/config.py
+++ b/config.py
@@ -1,8 +1,9 @@
 # Erebus IRC bot - Author: John Runyon
+# vim: fileencoding=utf-8
 # "Config" class (reading/providing access to bot.config)
 
 from __future__ import print_function
-import sys
+import sys, os
 
 if sys.version_info.major < 3:
        import ConfigParser
@@ -39,20 +40,35 @@ class Config(object):
                        return self.config.get(section, key)
                except:
                        return default
-       def getboolean(self, section, key):
-               val = self.get(section, key, False)
-               if val == False or val == "0" or val.lower() == "false":
+       def getint(self, section, key, default=0):
+               try:
+                       return int(self.config.get(section, key))
+               except:
+                       return default
+       def getboolean(self, section, key, default=False):
+               val = self.get(section, key, default)
+               if type(val) is bool:
+                       return val
+               elif val == "0" or val.lower() == "false":
                        return False
                else:
                        return True
 
        def set(self, section, key, value):
-               self.config.set(section, key, value)
+               if not self.config.has_section(section):
+                       self.config.add_section(section)
+               self.config.set(section, key, str(value))
+               if self.writeout: self.write()
+
+       def delete(self, section, key):
+               if self.config.has_section(section):
+                       self.config.remove_option(section, key)
                if self.writeout: self.write()
 
        def write(self):
-               with open(self.filename, 'wb') as configfile:
+               with open(self.filename+'.tmp', 'w') as configfile:
                        self.config.write(configfile)
+                       os.rename(configfile.name, self.filename)
 
        def __del__(self):
                if self.writeout: self.write()