Peter Surda 8cb0953401
Moved writeKeysFile into configparser
- shared.writeKeysFile -> BMConfigParser().save
- makes it easier to get rid of circular imports
2017-01-15 10:50:02 +01:00

68 lines
2.4 KiB

import ConfigParser
import datetime
import shutil
import os
from singleton import Singleton
import state
class BMConfigParser(ConfigParser.SafeConfigParser):
def set(self, section, option, value=None):
if self._optcre is self.OPTCRE or value:
if not isinstance(value, basestring):
raise TypeError("option values must be strings")
return ConfigParser.ConfigParser.set(self, section, option, value)
def get(self, section, option, raw=False, vars=None):
if section == "bitmessagesettings" and option == "timeformat":
return ConfigParser.ConfigParser.get(self, section, option, raw, vars)
except ConfigParser.InterpolationError:
return ConfigParser.ConfigParser.get(self, section, option, True, vars)
return ConfigParser.ConfigParser.get(self, section, option, True, vars)
def safeGetBoolean(self, section, field):
if self.has_option(section, field):
return self.getboolean(section, field)
except ValueError:
return False
return False
def safeGetInt(self, section, field):
if self.has_option(section, field):
return self.getint(section, field)
except ValueError:
return 0
return 0
def safeGet(self, section, option, default = None):
if self.has_option(section, option):
return self.get(section, option)
return default
def items(self, section, raw=False, vars=None):
return ConfigParser.ConfigParser.items(self, section, True, vars)
def save(self):
fileName = os.path.join(state.appdata, 'keys.dat')
fileNameBak = fileName + "." +"%Y%j%H%M%S%f") + '.bak'
# create a backup copy to prevent the accidental loss due to the disk write failure
shutil.copyfile(fileName, fileNameBak)
# The backup succeeded.
fileNameExisted = True
# The backup failed. This can happen if the file didn't exist before.
fileNameExisted = False
# write the file
with open(fileName, 'wb') as configfile:
# delete the backup
if fileNameExisted: